foldershare-8.x-1.2/src/Entity/FolderShareTraits/GetSetSystemHiddenTrait.php
src/Entity/FolderShareTraits/GetSetSystemHiddenTrait.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | <?php namespace Drupal\foldershare\Entity\FolderShareTraits; use Drupal\Core\Database\Database; use Drupal\foldershare\FolderShareInterface; use Drupal\foldershare\Utilities\FormatUtilities; /** * Get/set FolderShare entity system hidden field. * * This trait includes get and set methods for FolderShare entity * system hidden field. * * <B>Internal trait</B> * This trait is internal to the FolderShare module and used to define * features of the FolderShare entity class. It is a mechanism to group * functionality to improve code management. * * @ingroup foldershare */ trait GetSetSystemHiddenTrait { /*--------------------------------------------------------------------- * * SystemHidden field. * *---------------------------------------------------------------------*/ /** * {@inheritdoc} */ public function isSystemHidden() { $value = $this ->get( 'systemhidden' )->value; // An empty field is also FALSE. if ( empty ( $value ) === TRUE || $value === FALSE) { return FALSE; } return TRUE; } /** * Sets the system hidden flag. * * <B>This method is internal and strictly for use by the FolderShare * module itself.</B> * * The caller must call save() for the change to take effect. * * <B>Process locks</B> * This method does not lock access. The caller should lock around changes * to the entity. * * @param bool $state * The new flag state. * * @see ::isSystemHidden() */ public function setSystemHidden(bool $state ) { $this ->systemhidden->setValue( $state ); } /** * Clears the system hidden flag on all hidden items. * * <B>This method is internal and strictly for use by the FolderShare * module itself.</B> * * This method is intended for use in system debugging and file system * fixes in order to quickly reveal all hidden items. Since hidden items * are usually in the process of being deleted, revealing all hidden items * can cause confusion. * * The caller should clear the entity cache and the render cache to insure * that changed items are visible to the user. * * @return int * Returns the number of items changed. * * @see ::setSystemHidden() */ public static function clearAllSystemHidden() { $connection = Database::getConnection(); // Clear the hidden flag on all hidden items. $query = $connection ->update(self::BASE_TABLE); $query ->condition( 'systemhidden' , TRUE, '=' ); $query ->fields([ 'systemhidden' => (int) FALSE, ]); return $query ->execute(); } /** * Sets the system hidden flag on all descendants. * * <B>This method is internal and strictly for use by the FolderShare * module itself.</B> * * This method is intended for use by delete() in order to quickly mark * descendants hidden, pending actual deletion. * * This method recurses through a folder tree, starting with the given * parent item. All descendants are marked hidden via database updates. * * During recursion, this method skips subfolder trees that are already * marked hidden. It is assumed that if a parent folder is marked, the * children are already marked. This is a necessary assumption so that * repeated calls to this method during a scheduled task will eventually * find nothing more to mark. * * @param int $parentId * The FolderShare entity ID of a parent. The parent is presumed to * already have been marked hidden. * * @see ::setSystemHidden() * @see ::delete() */ private static function setDescendantsSystemHidden(int $parentId ) { $connection = Database::getConnection(); // Find folder children in need of updates. $query = $connection ->select(self::BASE_TABLE, 'fs' ); $query ->addField( 'fs' , 'id' , 'id' ); $query ->condition( 'parentid' , $parentId , '=' ); $query ->condition( 'kind' , FolderShareInterface::FOLDER_KIND, '=' ); // An empty field is also FALSE, so we have to test for !TRUE. $query ->condition( 'systemhidden' , TRUE, '<>' ); $childIds = []; foreach ( $query ->execute() as $result ) { $childIds [] = (int) $result ->id; } // Recurse through all folder children. foreach ( $childIds as $childId ) { self::setDescendantsSystemHidden( $childId ); } // Mark all children hidden. $query = $connection ->update(self::BASE_TABLE); $query ->condition( 'parentid' , $parentId , '=' ); $query ->fields([ 'systemhidden' => (int) TRUE, ]); $query ->execute(); } /*--------------------------------------------------------------------- * * Standard error messages. * *---------------------------------------------------------------------*/ /** * Returns a standard hidden message. * * This method provides a generic message that may be used by * operations that need to report that an item is hidden. * * @param string $itemName * (optional, default = NULL = multiple items) The name of a single item * involved in an operation that cannot be done. If NULL, a multi-item * message is returned instead. * * @return \Drupal\Core\Render\Markup * Returns a markup object containing a formatted standard * exception message. */ public static function getStandardHiddenMessage( string $itemName = NULL) { if ( empty ( $itemName ) === TRUE) { return FormatUtilities::createFormattedMessage( t( 'This item could be found.' )); } return FormatUtilities::createFormattedMessage( t( 'The item "@name" could be found.' , [ '@name' => $itemName , ])); } } |