public function __construct(RokCommon_Dispatcher $dispatcher)
 {
     JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_advancedmodules/tables');
     if (JPluginHelper::isEnabled('system', 'advancedmodules') && $this->isComponentEnabled()) {
         RokCommon_Composite::addPackagePath('roksprocket', dirname(__FILE__) . '/AdvancedModuleManager', 25);
         $dispatcher->connect('roksprocket.module.get', array($this, 'getModule'));
         $dispatcher->connect('roksprocket.module.save', array($this, 'saveModule'));
         $dispatcher->connect('roksprocket.module.duplicate', array($this, 'duplicateModule'));
     }
 }
示例#2
0
 /**
  * Method to save the form data.
  *
  * @param   array  $data  The form data.
  *
  * @return  boolean  True on success.
  *
  * @since   1.6
  */
 public function save($data)
 {
     $original_data = $data;
     // Initialise variables;
     $app = JFactory::getApplication();
     $dispatcher = JDispatcher::getInstance();
     $table = $this->getTable();
     $pk = !empty($data['id']) ? $data['id'] : (int) $this->getState('module.id');
     $isNew = true;
     $input = $app->input;
     $task = $input->get('task', null, 'cmd');
     // Include the content modules for the onSave events.
     JPluginHelper::importPlugin('extension');
     // Load the row if saving an existing record.
     if ($pk > 0) {
         $table->load($pk);
         $isNew = false;
     }
     // Alter the title and published state for Save as Copy
     if ($task == 'save2copy') {
         $orig_data = $input->post->get('jform', array(), 'array');
         $orig_table = clone $this->getTable();
         $orig_table->load((int) $orig_data['id']);
         if ($data['title'] == $orig_table->title) {
             $data['title'] .= ' ' . JText::_('JGLOBAL_COPY');
             $data['published'] = 0;
         }
     }
     // Bind the data.
     if (!$table->bind($data)) {
         $this->setError($table->getError());
         return false;
     }
     // Prepare the row for saving
     $this->prepareTable($table);
     // Check the data.
     if (!$table->check()) {
         $this->setError($table->getError());
         return false;
     }
     // Trigger the onExtensionBeforeSave event.
     $result = $dispatcher->trigger('onExtensionBeforeSave', array('com_modules.module', &$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;
     }
     //
     // Process the menu link mappings.
     //
     $assignment = isset($data['assignment']) ? $data['assignment'] : 0;
     // Delete old module to menu item associations
     // $db->setQuery(
     //	'DELETE FROM #__modules_menu'.
     //	' WHERE moduleid = '.(int) $table->id
     // );
     $db = $this->getDbo();
     $query = $db->getQuery(true);
     $query->delete();
     $query->from('#__modules_menu');
     $query->where('moduleid = ' . (int) $table->id);
     $db->setQuery((string) $query);
     $db->query();
     if (!$db->query()) {
         $this->setError($db->getErrorMsg());
         return false;
     }
     // If the assignment is numeric, then something is selected (otherwise it's none).
     if (is_numeric($assignment)) {
         // Variable is numeric, but could be a string.
         $assignment = (int) $assignment;
         // Logic check: if no module excluded then convert to display on all.
         if ($assignment == -1 && empty($data['assigned'])) {
             $assignment = 0;
         }
         // Check needed to stop a module being assigned to `All`
         // and other menu items resulting in a module being displayed twice.
         if ($assignment === 0) {
             // assign new module to `all` menu item associations
             // $this->_db->setQuery(
             //	'INSERT INTO #__modules_menu'.
             //	' SET moduleid = '.(int) $table->id.', menuid = 0'
             // );
             $query->clear();
             $query->insert('#__modules_menu');
             $query->columns(array($db->quoteName('moduleid'), $db->quoteName('menuid')));
             $query->values((int) $table->id . ', 0');
             $db->setQuery((string) $query);
             if (!$db->query()) {
                 $this->setError($db->getErrorMsg());
                 return false;
             }
         } elseif (!empty($data['assigned'])) {
             // Get the sign of the number.
             $sign = $assignment < 0 ? -1 : +1;
             // Preprocess the assigned array.
             $tuples = array();
             foreach ($data['assigned'] as &$pk) {
                 $tuples[] = '(' . (int) $table->id . ',' . (int) $pk * $sign . ')';
             }
             $this->_db->setQuery('INSERT INTO #__modules_menu (moduleid, menuid) VALUES ' . implode(',', $tuples));
             if (!$db->query()) {
                 $this->setError($db->getErrorMsg());
                 return false;
             }
         }
     }
     // Trigger the onExtensionAfterSave event.
     $dispatcher->trigger('onExtensionAfterSave', array('com_modules.module', &$table, $isNew));
     // Compute the extension id of this module in case the controller wants it.
     $query = $db->getQuery(true);
     $query->select('extension_id');
     $query->from('#__extensions AS e');
     $query->leftJoin('#__modules AS m ON e.element = m.module');
     $query->where('m.id = ' . (int) $table->id);
     $db->setQuery($query);
     $extensionId = $db->loadResult();
     if ($error = $db->getErrorMsg()) {
         JError::raiseWarning(500, $error);
         return;
     }
     $this->setState('module.extension_id', $extensionId);
     $this->setState('module.id', $table->id);
     // Add the per item custom settings
     $items = $input->post->get('items', array(), 'array');
     $old_moduleid = (int) $table->id;
     if ($task == 'save2copy') {
         $old_moduleid = (int) $orig_table->id;
     }
     // get the old set of items for the module
     $query = $db->getQuery(true);
     $item_table = JTable::getInstance('Item', 'RokSprocketTable');
     $query->select("*")->from("#__roksprocket_items")->where('module_id = ' . $old_moduleid);
     $db->setQuery((string) $query);
     $raw_old_items = $db->loadAssocList();
     if ($db->getErrorNum()) {
         $this->setError($db->getErrorMsg());
         return false;
     }
     $old_items = array();
     foreach ($raw_old_items as $raw_old_item) {
         /** @var $old_item_table RokSprocketTableItem */
         $old_item_table = JTable::getInstance('Item', 'RokSprocketTable');
         $old_item_table->bind($raw_old_item);
         if (isset($old_item_table->params)) {
             $old_item_table->params = RokCommon_Utils_ArrayHelper::fromObject(RokCommon_JSON::decode($old_item_table->params));
         } else {
             $old_item_table->params = array();
         }
         $old_items[$old_item_table->provider . '-' . $old_item_table->provider_id] = $old_item_table;
     }
     $all_new_items = $this->getArticles((int) $table->id, $table->params);
     if ($task != 'save2copy') {
         // delete old items per module
         $query = $db->getQuery(true);
         $query->delete();
         $query->from('#__roksprocket_items');
         $query->where('module_id = ' . (int) $table->id);
         $db->setQuery((string) $query);
         if (!$db->query()) {
             $this->setError($db->getErrorMsg());
             return false;
         }
         // Pass to addons to save if needed
         if (empty($original_data['id'])) {
             $original_data['id'] = (int) $table->id;
         }
         $event = new RokCommon_Event($this, 'roksprocket.module.save');
         $this->dispatcher->filter($event, array('data' => $original_data, 'save_result' => null));
         $addonreturn = $event->getReturnValue();
         if ($event->isProcessed()) {
             if (!$addonreturn['save_result']) {
                 return false;
             }
         }
     }
     $per_item_form = $this->getPerItemsForm();
     // Save the Showing Items
     $last_ordernumber = 0;
     foreach ($items as $item_id => $item_settings) {
         $item_settings['module_id'] = (int) $table->id;
         list($item_settings['provider'], $item_settings['provider_id']) = explode('-', $item_id);
         /** @var $item_table RokSprocketTableItem */
         $item_table = JTable::getInstance('Item', 'RokSprocketTable');
         $fields = $per_item_form->getFieldsetWithGroup('peritem');
         foreach ($fields as $field) {
             if (!array_key_exists($field->fieldname, $item_settings['params'])) {
                 if (!empty($old_items) && isset($old_items[$item_id]) && array_key_exists($field->fieldname, $old_items[$item_id]->params)) {
                     $item_settings['params'][$field->fieldname] = $old_items[$item_id]->params[$field->fieldname];
                 } else {
                     $item_settings['params'][$field->fieldname] = $per_item_form->getFieldAttribute($field->fieldname, 'default', null, 'params');
                 }
             }
         }
         // Bind the data.
         if (!$item_table->bind($item_settings)) {
             $this->setError($item_table->getError());
             return false;
         }
         // Trigger the onExtensionBeforeSave event.
         $result = $dispatcher->trigger('onExtensionBeforeSave', array('com_roksprocket.module', &$item_table, true));
         if (in_array(false, $result, true)) {
             $this->setError($item_table->getError());
             return false;
         }
         // Store the data.
         if (!$item_table->store()) {
             $this->setError($item_table->getError());
             return false;
         }
         $last_ordernumber = $item_table->order;
         if (isset($old_items[$item_id])) {
             unset($old_items[$item_id]);
         }
         if (isset($all_new_items[$item_id])) {
             unset($all_new_items[$item_id]);
         }
     }
     //Save the remaining items  (Not Shown)
     foreach ($all_new_items as $item_id => $unseen_item) {
         $item_settings = array();
         $item_settings['module_id'] = (int) $table->id;
         list($item_settings['provider'], $item_settings['provider_id']) = explode('-', $item_id);
         $item_settings['order'] = ++$last_ordernumber;
         if (isset($old_items[$item_id]) && $old_items[$item_id]->params != null) {
             $item_settings['params'] = $old_items[$item_id]->params;
         } elseif (isset($all_new_items[$item_id]) && $all_new_items[$item_id]->getParams() != null) {
             $item_settings['params'] = RokCommon_JSON::encode($all_new_items[$item_id]->getParams());
         }
         /** @var $item_table RokSprocketTableItem */
         $item_table = JTable::getInstance('Item', 'RokSprocketTable');
         // Bind the data.
         if (!$item_table->bind($item_settings)) {
             $this->setError($item_table->getError());
             return false;
         }
         // Trigger the onExtensionBeforeSave event.
         $result = $dispatcher->trigger('onExtensionBeforeSave', array('com_roksprocket.module', &$item_table, true));
         if (in_array(false, $result, true)) {
             $this->setError($item_table->getError());
             return false;
         }
         // Store the data.
         if (!$item_table->store()) {
             $this->setError($item_table->getError());
             return false;
         }
     }
     // Clear modules cache
     $this->cleanCache();
     // Clean module cache
     parent::cleanCache($table->module, $table->client_id);
     $app->setUserState('com_roksprocket.module_id', $table->id);
     return true;
 }