/** * Batch copy items to a new default. * * @param integer $value The new diet. * @param array $pks An array of row IDs. * @param array $contexts An array of item contexts. * * @return mixed An array of new IDs on success, boolean false on failure. */ protected function batchCopy($value, $pks, $contexts) { $defaultId = (int) $value; $table = $this->getTable(); $i = 0; // Check that the diet exists if ($defaultId) { $defaultTable = JTable::getInstance('default', 'SibdietTable'); if (!$defaultTable->load($defaultId)) { if ($error = $defaultTable->getError()) { // Fatal error $this->setError($error); return false; } else { $this->setError(JText::_('COM_SIBDIET_ERROR_BATCH_COPY_DEFAULT_NOT_FOUND')); return false; } } } foreach ($pks as $pk) { $defaultExistSQL = "SELECT id FROM #__sibdiet_defaults_weeklys WHERE defaults_id = " . $defaultId . " AND title = (SELECT title FROM #__sibdiet_defaults_weeklys WHERE id = " . $pk . ")"; $db = JFactory::getDBO(); $db->setQuery($defaultExistSQL); $db->query(); if ($db->getNumRows()) { unset($pks[array_search($pk, $pks, true)]); } } if (empty($defaultId)) { $this->setError(JText::_('COM_SIBDIET_ERROR_BATCH_COPY_DEFAULT_NOT_FOUND')); return false; } // Check that the user has create permission for the component $extension = JFactory::getApplication()->input->get('option', ''); $user = JFactory::getUser(); if (!$user->authorise('core.create', $extension)) { $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_CREATE')); return false; } // Parent exists so we let's proceed while (!empty($pks)) { // Pop the first ID off the stack $pk = array_shift($pks); $table->reset(); // 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('JLIB_APPLICATION_ERROR_BATCH_MOVE_ROW_NOT_FOUND', $pk)); continue; } } // Reset the ID because we are making a copy $table->id = 0; // New default ID $table->defaults_id = $defaultId; // Check the row. if (!$table->check()) { $this->setError($table->getError()); return false; } require_once JPATH_COMPONENT . '/helpers/sibdiet.php'; $table->foods = SibdietHelper::convertFoods($table->foods, $table->defaults_id); // Store the row. if (!$table->store()) { $this->setError($table->getError()); return false; } // Get the new item ID $newId = $table->get('id'); // Add the new ID to the array $newIds[$i] = $newId; $i++; } // Clean the cache $this->cleanCache(); return $newIds; }