Ejemplo n.º 1
0
 public function save($data, &$extImgError = false)
 {
     // = = = = = = = = = =
     // Default VALUES FOR Rights in FRONTEND
     // ACCESS -  0: all users can see the category (registered or not registered)
     //             if registered or not registered it will be set in ACCESS LEVEL not here)
     //			   if -1 - user was not selected so every registered or special users can see category
     // UPLOAD - -2: nobody can upload or add images in front (if 0 - every users can do it)
     // DELETE - -2: nobody can upload or add images in front (if 0 - every users can do it)
     if (!isset($data['accessuserid'])) {
         $data['accessuserid'] = array();
     }
     if (!isset($data['uploaduserid'])) {
         $data['uploaduserid'] = array();
     }
     if (!isset($data['deleteuserid'])) {
         $data['deleteuserid'] = array();
     }
     $accessUserIdArray = PhocaGalleryUtils::toArray($data['accessuserid']);
     $uploadUserIdArray = PhocaGalleryUtils::toArray($data['uploaduserid']);
     $deleteUserIdArray = PhocaGalleryUtils::toArray($data['deleteuserid']);
     if (isset($data['access']) && (int) $data['access'] > 0 && (int) $accessUserIdArray[0] == 0) {
         $accessUserId[0] = -1;
     }
     $data['accessuserid'] = implode(',', $accessUserIdArray);
     $data['uploaduserid'] = implode(',', $uploadUserIdArray);
     $data['deleteuserid'] = implode(',', $deleteUserIdArray);
     //TODO - return message
     if (!isset($data['owner_id'])) {
         $data['owner_id'] = 0;
     }
     // Owner can have only one main category - check it
     $errorMsgOwner = '';
     $ownerMainCategory = $this->getOwnerMainCategory($data['owner_id'], $data['id'], $data['parent_id'], $errorMsgOwner);
     if ($errorMsgOwner != '') {
         $data['owner_id'] = 0;
         $this->setError($errorMsgOwner);
         return false;
     }
     // = = = = = = = = = =
     // Initialise variables;
     $dispatcher = JDispatcher::getInstance();
     $table = $this->getTable();
     $pk = !empty($data['id']) ? $data['id'] : (int) $this->getState($this->getName() . '.id');
     $isNew = true;
     // Include the content plugins for the on save events.
     JPluginHelper::importPlugin('content');
     // Load the row if saving an existing record.
     if ($pk > 0) {
         $table->load($pk);
         $isNew = false;
     }
     // Bind the data.
     if (!$table->bind($data)) {
         $this->setError($table->getError());
         return false;
     }
     if (intval($table->date) == 0) {
         $table->date = JFactory::getDate()->toSql();
     }
     // Prepare the row for saving
     $this->prepareTable($table);
     // Check the data.
     if (!$table->check()) {
         $this->setError($table->getError());
         return false;
     }
     // Trigger the onContentBeforeSave event.
     /*$result = $dispatcher->trigger($this->event_before_save, array($this->option.'.'.$this->name, $table, $isNew));
     		if (in_array(false, $result, true)) {
     			$this->setError($table->getError());
     			return false;
     		}*/
     // Store the data.
     if (!$table->store()) {
         $this->setError($table->getError());
         return false;
     }
     // Clean the cache.
     $cache = JFactory::getCache($this->option);
     $cache->clean();
     // Trigger the onContentAfterSave event.
     //$dispatcher->trigger($this->event_after_save, array($this->option.'.'.$this->name, $table, $isNew));
     $pkName = $table->getKeyName();
     if (isset($table->{$pkName})) {
         $this->setState($this->getName() . '.id', $table->{$pkName});
     }
     $this->setState($this->getName() . '.new', $isNew);
     $subTask = JRequest::getVar('subtask');
     // TODO
     if ((string) $subTask == 'loadextimgp') {
         if (isset($table->{$pkName})) {
             $errorMsg = '';
             if (!$this->loadExtImages($table->{$pkName}, $data, $message)) {
                 $this->setError($message, 'error');
                 // Be aware the category is stored yet
                 //return false;
                 $extImgError = true;
                 return true;
                 //but with error message
             }
         }
     }
     if ((string) $subTask == 'loadextimgf') {
         if (isset($table->{$pkName})) {
             $errorMsg = '';
             if (!$this->loadExtImagesFb($table->{$pkName}, $data, $message)) {
                 $this->setError($message, 'error');
                 // Be aware the category is stored yet
                 //return false;
                 $extImgError = true;
                 return true;
                 //but with error message
             }
         }
     }
     // Stored - set Owner
     if (isset($table->{$pkName}) && isset($data['owner_id']) && (int) $data['owner_id'] > 0) {
         if (!$this->setOwnerOfCategory($data)) {
             $this->setError($message);
             return false;
         }
     }
     return true;
 }