public function save(array &$properties)
 {
     if (empty($properties) || !is_array($properties)) {
         throw new InvalidArgumentException('You must provide the values to update.', 400);
     }
     // Use IsPublic to determine whether to set or clear the Published datetime. -- cwells
     if (empty($properties['IsPublic'])) {
         // Not published.
         $properties['Published'] = null;
     } else {
         if ($properties['IsPublic'] === '1' && empty($properties['Published'])) {
             // New publish.
             $properties['Published'] = date(\CWA\DB\DATETIME_PHP_TO_DB);
         }
     }
     unset($properties['IsPublic']);
     // Since it's not a real property on the object.
     // The Summary should be plain text with no double quotes. -- cwells
     $properties['Summary'] = str_replace('"', "'", strip_tags($properties['Summary']));
     // If this is an existing blog post and the slug changed, move associated images. -- cwells
     if (!empty($properties['ID']) && $properties['OldSlug'] !== $properties['Slug']) {
         require_once \CWA\LIB_PATH . 'cwa/io/FileManager.php';
         $fileManager = new \CWA\IO\FileManager("../public/images{$this->pathInURL}");
         $oldPath = $properties['OldSlug'][0] . '/' . $properties['OldSlug'];
         $newPath = $properties['Slug'][0] . '/' . $properties['Slug'];
         if ($fileManager->isDirectory($oldPath)) {
             if (!$fileManager->isDirectory(dirname($newPath))) {
                 $fileManager->mkdir(dirname($newPath));
                 // Error check happens below. -- cwells
             }
             if (!$fileManager->rename($oldPath, $newPath)) {
                 throw new InvalidArgumentException('Failed to rename the image directory.', 500);
             }
             // Update any URLs that refer to the old path in the content. -- cwells
             $properties['Body'] = str_replace("/images{$this->pathInURL}/{$oldPath}/", "/images{$this->pathInURL}/{$newPath}/", $properties['Body']);
         }
     }
     unset($properties['OldSlug']);
     parent::save($properties);
 }
Example #2
0
 public function save(array &$properties)
 {
     if (empty($properties) || !is_array($properties)) {
         throw new InvalidArgumentException('You must provide the values to update.', 400);
     }
     if (!empty($properties['SetPassword']) && $properties['SetPassword'] === 'yes') {
         $errorCode = 400;
         $errorMessage = '';
         if ($properties['Password'] !== $properties['ConfirmPassword']) {
             $errorMessage = 'Confirm Password must match Password.';
         } else {
             if (strlen($properties['Password']) < 10) {
                 $errorMessage = 'Password cannot be less than 10 characters long.';
             } else {
                 if (strlen($properties['Password']) > 100) {
                     $errorMessage = 'Password cannot be more than 100 characters long.';
                 } else {
                     // Create a User object just to generate the password hash. -- cwells
                     $user = new User($properties);
                     if (!$user->setPassword($properties['Password'])) {
                         $errorCode = 500;
                         $errorMessage = 'Failed to set the password. Please try again.';
                     }
                     $properties = $user->toArray();
                 }
             }
         }
         if (!empty($errorMessage)) {
             if (empty($properties[User::getPrimaryKeyName()])) {
                 $this->add($properties);
             } else {
                 $this->edit($properties);
             }
             $this->view->setStatus($errorMessage, $errorCode);
             return;
         }
     }
     // These are not actual properties on the User object. -- cwells
     unset($properties['SetPassword']);
     unset($properties['Password']);
     unset($properties['ConfirmPassword']);
     parent::save($properties);
 }