foldershare-8.x-1.2/src/Entity/FolderShareTraits/OperationNewFolderTrait.php
src/Entity/FolderShareTraits/OperationNewFolderTrait.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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | <?php namespace Drupal\foldershare\Entity\FolderShareTraits; use Drupal\foldershare\ManageLog; use Drupal\foldershare\Entity\Exception\LockException; use Drupal\foldershare\Entity\Exception\ValidationException; /** * Create new FolderShare folders. * * This trait includes methods to create root and subfolders. * * <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 OperationNewFolderTrait { /*--------------------------------------------------------------------- * * Create root folder. * *---------------------------------------------------------------------*/ /** * Creates a new root folder with the given name. * * If the name is empty, it is set to a default. * * The name is checked for uniqueness among all root items owned by * the current user. If needed, a sequence number is appended before * the extension(s) to make the name unique (e.g. 'My new root 12'). * * <B>Process locks</B> * This method locks the user's root list for exclusive use during * creation of the folder. This will prevent any other edit operation from * modifying the root list until the creation completes. * * <B>Post-operation hooks</B> * This method calls the "hook_foldershare_post_operation_new_folder" hook. * * <B>Activity log</B> * This method posts a log message after the folder is created. * * @param string $name * (optional, default = '') The name for the new folder. If the name is * empty, it is set to a default name. * @param bool $allowRename * (optional, default = TRUE) When TRUE, the entity will be automatically * renamed, if needed, to insure that it is unique within the folder. * When FALSE, non-unique names cause an exception to be thrown. * * @return \Drupal\foldershare\Entity\FolderShare * Returns the new folder at the root. * * @throws \Drupal\foldershare\Entity\Exception\LockException * Throws an exception if an access lock could not be acquired. * @throws \Drupal\foldershare\Entity\Exception\ValidationException * If the name is already in use or is not legal. * * @see ::createFolder() */ public static function createRootFolder( string $name = '' , bool $allowRename = TRUE) { // // Validate. // --------- // If no name given, use a default. Otherwise insure the name is legal. if ( empty ( $name ) === TRUE) { $name = t( 'New folder' ); } elseif (self::isNameLegal( $name ) === FALSE) { throw new ValidationException( self::getStandardIllegalNameExceptionMessage( $name )); } // // Lock user's root list. // ---------------------- // Lock the current user's root list while we check if the name is // unique among other items at the root level. // // LOCK USER'S ROOT LIST. if (self::acquireUserRootListLock() === FALSE) { throw new LockException( self::getStandardLockExceptionMessage(t( 'created' ), $name )); } // // Check name. // ----------- // If allowed, adjust the name to make it unique. $uid = self::getCurrentUserId()[0]; if ( $allowRename === TRUE) { // Insure name doesn't collide with existing root items. // // Checking for name uniqueness can only be done safely while // the root list is locked so that no other process can add or // change a name. $name = self::createUniqueName( self::findAllRootItemNames( $uid ), $name , '' ); if ( $name === FALSE) { // This is very very unlikely because creating a unique name // tries repeatedly to append a number until it gets to // something unique. // // UNLOCK USER'S ROOT LIST. self::releaseUserRootListLock(); throw new ValidationException( self::getStandardCannotCreateUniqueNameExceptionMessage( 'new folder' )); } } elseif (self::isRootNameUnique( $name ) === FALSE) { // UNLOCK USER'S ROOT LIST. self::releaseUserRootListLock(); throw new ValidationException( self::getStandardNameInUseExceptionMessage( $name )); } // // Create folder. // -------------- // Use the new name and create a new root folder. try { // Give the new root item no parent or root. // - Empty parent ID. // - Empty root ID. // - Automatic id. // - Automatic uuid. // - Automatic creation date. // - Automatic changed date. // - Automatic langcode. // - Empty description. // - Empty size. // - Empty author grants. // - Empty view grants. // - Empty disabled grants. $folder = self::create([ 'name' => $name , 'uid' => $uid , 'kind' => self::FOLDER_KIND, 'mime' => self::FOLDER_MIME, 'size' => 0, ]); // Add default grants to a root item. $folder ->addDefaultAccessGrants(); $folder ->save(); } catch (\Exception $e ) { // Unknown exception. Creation should not throw an exception. // // UNLOCK USER'S ROOT LIST. self::releaseUserRootListLock(); throw $e ; } // // Unlock user's root list. // ------------------------ // The folder is created with a safe name. We're done with the root list. // // UNLOCK USER'S ROOT LIST. self::releaseUserRootListLock(); // // Hook & log. // ----------- // Announce the new folder. self::postOperationHook( 'new_folder' , [ $folder , $uid , ]); ManageLog::activity( "Created top-level folder '@name' (# @id)." , [ '@id' => (int) $folder ->id(), '@name' => $folder ->getName(), 'entity' => $folder , 'uid' => $uid , ]); return $folder ; } /*--------------------------------------------------------------------- * * Create subfolder. * *---------------------------------------------------------------------*/ /** * {@inheritdoc} */ public function createFolder( string $name = '' , bool $allowRename = TRUE) { // // Validate // -------- // If no name given, use a default. Otherwise insure the name is legal. if ( empty ( $name ) === TRUE) { $name = t( 'New folder' ); } elseif (self::isNameLegal( $name ) === FALSE) { throw new ValidationException( self::getStandardIllegalNameExceptionMessage( $name )); } // // Lock root folder tree. // ---------------------- // Lock the parent root's folder tree to prevent other operations that // might interfere with the addition of the new folder. // // LOCK ROOT FOLDER TREE. $rootId = $this ->getRootItemId(); if (self::acquireRootOperationLock( $rootId ) === FALSE) { throw new LockException( self::getStandardLockExceptionMessage(t( 'created' ), $name )); } // // Check name. // ----------- // If allowed, adjust the name to make it unique. $uid = self::getCurrentUserId()[0]; if ( $allowRename === TRUE) { // Insure name doesn't collide with existing files or folders. // // Checking for name uniqueness can only be done safely while // the parent folder is locked so that no other process can add or // change a name. $name = self::createUniqueName( $this ->findChildrenNames(), $name , '' ); if ( $name === FALSE) { // This is very very unlikely because creating a unique name // tries repeatedly to append a number until it gets to // something unique. // // UNLOCK ROOT FOLDER TREE. self::releaseRootOperationLock( $rootId ); throw new ValidationException( self::getStandardCannotCreateUniqueNameExceptionMessage( 'new folder' )); } } elseif ( $this ->isNameUnique( $name ) === FALSE) { // UNLOCK ROOT FOLDER TREE. self::releaseRootOperationLock( $rootId ); throw new ValidationException( self::getStandardNameInUseExceptionMessage( $name )); } // // Create the new folder. // ---------------------- // Use the new name and create a new folder. try { // Create and set the parent ID to this folder, // and the root ID to this folder's root. // - Automatic id. // - Automatic uuid. // - Automatic creation date. // - Automatic changed date. // - Automatic langcode. // - Empty description. // - Empty size. // - Empty author grants. // - Empty view grants. // - Empty disabled grants. $folder = self::create([ 'name' => $name , 'uid' => $uid , 'kind' => self::FOLDER_KIND, 'mime' => self::FOLDER_MIME, 'size' => 0, 'parentid' => $this ->id(), 'rootid' => $this ->getRootItemId(), ]); $folder ->save(); } catch (\Exception $e ) { // Unknown exception. Creation should not throw an exception. // // UNLOCK ROOT FOLDER TREE. self::releaseRootOperationLock( $rootId ); throw $e ; } // // Unlock root folder tree. // ------------------------ // The folder is created with a safe name. We're done with the // folder tree. // // UNLOCK ROOT FOLDER TREE. self::releaseRootOperationLock( $rootId ); // // Hook & log. // ----------- // Announce the new folder. self::postOperationHook( 'new_folder' , [ $folder , $uid , ]); ManageLog::activity( "Created folder '@name' (# @id)." , [ '@id' => (int) $folder ->id(), '@name' => $folder ->getName(), 'entity' => $folder , 'uid' => $uid , ]); return $folder ; } } |