Exemplo n.º 1
0
 /**
  * Batch move categories to a other category.
  *
  * @param   integer  $value     The new category ID.
  * @param   array    $pks       An array of row IDs.
  * @param   array    $contexts  An array of item contexts.
  *
  * @return  boolean  True on success.
  *
  */
 protected function batchMove($value, $pks, $contexts)
 {
     global $jlistConfig;
     jimport('joomla.filesystem.folder');
     jimport('joomla.filesystem.file');
     // UTF-8 aware alternative functions
     jimport('joomla.utilities.string');
     $root_folder = $jlistConfig['files.uploaddir'];
     $new_parent_id = (int) $value;
     $table = $this->getTable();
     $db = $this->getDbo();
     $query = $db->getQuery(true);
     // check at first, that it is not always run a other batch job
     if ($jlistConfig['categories.batch.in.progress'] || $jlistConfig['downloads.batch.in.progress']) {
         // generate the warning and return
         JError::raiseWarning(100, JText::_('COM_JDOWNLOADS_BATCH_IS_ALWAYS_STARTED'));
         return false;
     } else {
         // actualize at first the batvh progress setting
         $jlistConfig['categories.batch.in.progress'] = 1;
         $query = $db->getQuery(true);
         $query->update('#__jdownloads_config');
         $query->set('setting_value = 1');
         $query->where('setting_name = \'categories.batch.in.progress\'');
         $db->setQuery($query);
         $result = $db->execute();
         if ($error = $db->getErrorMsg()) {
             $this->setError($error);
             return false;
         }
     }
     // Check that the parent exists.
     if ($new_parent_id) {
         if (!$table->load($new_parent_id)) {
             if ($error = $table->getError()) {
                 // Fatal error
                 $this->setError($error);
                 return false;
             } else {
                 // Non-fatal error
                 $this->setError(JText::_('COM_JDOWNLOADS_BATCH_MOVE_PARENT_NOT_FOUND'));
                 $new_parent_id = 0;
             }
         }
     }
     // We are going to store all the children and just move the category
     $children = array();
     // Parent exists so we let's proceed
     foreach ($pks as $pk) {
         // Check that the row actually exists
         if (!$table->load($pk)) {
             if ($error = $table->getError()) {
                 // Fatal error
                 $this->setError($error);
                 return false;
             } else {
                 // Not fatal error
                 $this->setError(JText::sprintf('COM_JDOWNLOADS_BATCH_MOVE_ROW_NOT_FOUND', $pk));
                 continue;
             }
         }
         //$oldParentId = $table->parent_id;
         $oldParentId = 0;
         // Make a copy of the old category folder path
         $old_cat_dir = $table->cat_dir;
         $old_cat_dir_parent = $table->cat_dir_parent;
         if ($old_cat_dir_parent != '') {
             $old_cat_path = $old_cat_dir_parent . '/' . $old_cat_dir;
         } else {
             $old_cat_path = $old_cat_dir;
         }
         $cat_dir = $table->cat_dir;
         // Set the new location in the tree for the node.
         $table->setLocation($new_parent_id, 'last-child');
         // Check if we are moving to a different parent
         if ($new_parent_id != $table->parent_id) {
             // Add the child node ids to the children array.
             $query->clear();
             $query->select('id');
             $query->from($db->quoteName('#__jdownloads_categories'));
             $query->where($db->quoteName('lft') . ' BETWEEN ' . (int) $table->lft . ' AND ' . (int) $table->rgt);
             $db->setQuery($query);
             $children = array_merge($children, (array) $db->loadColumn());
         }
         if ($new_parent_id > 1 || $oldParentId > 1) {
             $new_parent_cat_path = $table->getParentCategoryPath($new_parent_id);
         } else {
             // root cat
             $new_parent_cat_path = '';
         }
         // build the new parent cat path
         $table->cat_dir_parent = $new_parent_cat_path;
         // build new cat_dir name when it exists allways
         $replace = array('(' => '', ')' => '');
         if ($new_parent_id > 1) {
             while (JFolder::exists($root_folder . '/' . $new_parent_cat_path . '/' . $cat_dir)) {
                 $title = JString::increment($cat_dir);
                 $cat_dir = strtr($title, $replace);
             }
         } else {
             while (JFolder::exists($root_folder . '/' . $cat_dir)) {
                 $title = JString::increment($cat_dir);
                 $cat_dir = strtr($title, $replace);
             }
         }
         $table->cat_dir = $cat_dir;
         // Store the row
         if (!$table->store()) {
             $this->setError($table->getError());
             return false;
         }
         // Rebuild the hierarchy.
         if (!$table->rebuild()) {
             $this->setError($table->getError());
             return false;
         }
         // Rebuild the tree path.
         if (!$table->rebuildPath()) {
             $this->setError($table->getError());
             return false;
         }
         // biuld the source path
         if ($old_cat_dir != '') {
             $source_dir = $root_folder . '/' . $old_cat_path;
         } else {
             $source_dir = $root_folder;
         }
         // build the target path
         if ($new_parent_cat_path != '') {
             $target_dir = $root_folder . '/' . $new_parent_cat_path . '/' . $cat_dir;
         } else {
             $target_dir = $root_folder . '/' . $cat_dir;
         }
         // move now the complete category folder to the new location!
         // the path must have at the end a slash
         $message = '';
         JDownloadsHelper::moveDirs($source_dir . '/', $target_dir . '/', true, $message, true, false, false);
         if ($message == '') {
             // check the really result:
             if (JFolder::exists($target_dir) && !JFolder::exists($source_dir)) {
                 // JError::raiseNotice(100, JText::sprintf('COM_JDOWNLOADS_BATCH_CAT_MOVED_MSG', $source_dir));
             } else {
                 if (JFolder::exists($source_dir)) {
                     $res = JDownloadsHelper::delete_dir_and_allfiles($source_dir);
                     if (JFolder::exists($source_dir)) {
                         JError::raiseWarning(100, JText::sprintf('COM_JDOWNLOADS_BATCH_CAT_NOT_MOVED_MSG', $source_dir));
                     }
                 }
             }
         } else {
             JError::raiseWarning(100, $message);
         }
     }
     // Process the child rows
     if (!empty($children)) {
         // Remove any duplicates and sanitize ids.
         $children = array_unique($children);
         JArrayHelper::toInteger($children);
         // Check for a database error.
         if ($db->getErrorNum()) {
             $this->setError($db->getErrorMsg());
             return false;
         }
     }
     // actualize at last the batch progress setting
     $jlistConfig['categories.batch.in.progress'] = 0;
     $query = $db->getQuery(true);
     $query->update('#__jdownloads_config');
     $query->set('setting_value = 0');
     $query->where('setting_name = \'categories.batch.in.progress\'');
     $db->setQuery($query);
     $result = $db->execute();
     if ($error = $db->getErrorMsg()) {
         $this->setError($error);
     }
     return true;
 }