Exemplo n.º 1
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)
 {
     $advancedparams = JFactory::getApplication()->input->get('advancedparams', array(), 'array');
     $dispatcher = JEventDispatcher::getInstance();
     $input = JFactory::getApplication()->input;
     $table = $this->getTable();
     $pk = !empty($data['id']) ? $data['id'] : (int) $this->getState('module.id');
     $isNew = true;
     $context = $this->option . '.' . $this->name;
     // Include the plugins for the save event.
     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 ($input->get('task') == 'save2copy') {
         $orig_table = clone $this->getTable();
         $orig_table->load((int) $input->getInt('id'));
         $data['published'] = 0;
         if ($data['title'] == $orig_table->title) {
             $data['title'] .= ' ' . JText::_('JGLOBAL_COPY');
         }
     }
     require_once JPATH_PLUGINS . '/system/nnframework/helpers/text.php';
     // correct the publish date details
     if (isset($advancedparams['assignto_date_publish_up'])) {
         NNText::fixDateOffset($advancedparams['assignto_date_publish_up']);
     }
     if (isset($advancedparams['assignto_date_publish_down'])) {
         NNText::fixDateOffset($advancedparams['assignto_date_publish_down']);
     }
     if (isset($advancedparams['assignto_date'])) {
         $publish_up = 0;
         $publish_down = 0;
         if ($advancedparams['assignto_date'] == 2) {
             $publish_up = $advancedparams['assignto_date_publish_down'];
         } else {
             if ($advancedparams['assignto_date'] == 1) {
                 $publish_up = $advancedparams['assignto_date_publish_up'];
                 $publish_down = $advancedparams['assignto_date_publish_down'];
             }
         }
         $data['publish_up'] = $publish_up;
         $data['publish_down'] = $publish_down;
     }
     $lang = '*';
     if (isset($advancedparams['assignto_languages']) && $advancedparams['assignto_languages'] == 1 && count($advancedparams['assignto_languages_selection']) === 1) {
         $lang = (string) $advancedparams['assignto_languages_selection']['0'];
     }
     $data['language'] = $lang;
     // 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 before save event.
     $result = $dispatcher->trigger($this->event_before_save, array($context, &$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;
     }
     $table_adv = JTable::getInstance('AdvancedModules', 'AdvancedModulesTable');
     $table_adv->moduleid = $table->id;
     if ($table_adv->moduleid && !$table_adv->load($table_adv->moduleid)) {
         $db = $table_adv->getDbo();
         $db->insertObject($table_adv->getTableName(), $table_adv, $table_adv->getKeyName());
     }
     if (isset($data['rules'])) {
         $table_adv->_title = $data['title'];
         $table_adv->setRules($data['rules']);
     }
     if (!empty($advancedparams['mirror_module'])) {
         $table_adv->mirror_id = $advancedparams['mirror_module'] == 2 ? $advancedparams['mirror_moduleid'] * -1 : $advancedparams['mirror_moduleid'];
         unset($advancedparams['mirror_module']);
         unset($advancedparams['mirror_moduleid']);
     }
     $table_adv->params = json_encode($advancedparams);
     // Check the row
     $table_adv->check();
     // Store the row
     if (!$table_adv->store()) {
         $this->setError($table_adv->getError());
     }
     //
     // Process the menu link mappings.
     //
     $data['assignment'] = '0';
     $data['assigned'] = array();
     if (isset($advancedparams['assignto_menuitems'])) {
         $empty = 0;
         if (isset($advancedparams['assignto_menuitems_selection'])) {
             $data['assigned'] = $advancedparams['assignto_menuitems_selection'];
             $empty = empty($advancedparams['assignto_menuitems_selection']);
         } else {
             $empty = 1;
         }
         if ($advancedparams['assignto_menuitems'] == 1 && $empty) {
             $data['assignment'] = '-';
         } else {
             if ($advancedparams['assignto_menuitems'] == 1) {
                 $data['assignment'] = '1';
             } else {
                 if ($advancedparams['assignto_menuitems'] == 2 && $empty) {
                     $data['assignment'] = '0';
                 } else {
                     if ($advancedparams['assignto_menuitems'] == 2) {
                         $data['assignment'] = '-1';
                     }
                 }
             }
         }
     }
     $assignment = $data['assignment'];
     $db = $this->getDbo();
     $query = $db->getQuery(true)->delete('#__modules_menu')->where('moduleid = ' . (int) $table->id);
     $db->setQuery($query);
     $db->execute();
     // 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.
             $query->clear()->insert('#__modules_menu')->columns(array($db->quoteName('moduleid'), $db->quoteName('menuid')))->values((int) $table->id . ', 0');
             $db->setQuery($query);
             try {
                 $db->execute();
             } catch (RuntimeException $e) {
                 $this->setError($e->getMessage());
                 return false;
             }
         } elseif (!empty($data['assigned'])) {
             // Get the sign of the number.
             $sign = $assignment < 0 ? -1 : 1;
             // Preprocess the assigned array.
             $inserts = array();
             if (!is_array($data['assigned'])) {
                 $data['assigned'] = explode(',', $data['assigned']);
             }
             foreach ($data['assigned'] as &$pk) {
                 if (is_numeric($pk)) {
                     $menuid = (int) $pk * $sign;
                     $inserts[(int) $table->id . '-' . $menuid] = (int) $table->id . ',' . $menuid;
                 }
             }
             if (!empty($inserts)) {
                 $query->clear()->insert('#__modules_menu')->columns(array($db->quoteName('moduleid'), $db->quoteName('menuid')));
                 foreach ($inserts as $insert) {
                     $query->values($insert);
                 }
                 $db->setQuery($query);
                 try {
                     $db->execute();
                 } catch (RuntimeException $e) {
                     $this->setError($e->getMessage());
                     return false;
                 }
             }
         }
     }
     // Trigger the after save event.
     $dispatcher->trigger($this->event_after_save, array($context, &$table, $isNew));
     // Compute the extension id of this module in case the controller wants it.
     $query->clear()->select('extension_id')->from('#__extensions AS e')->join('LEFT', '#__modules AS m ON e.element = m.module')->where('m.id = ' . (int) $table->id);
     $db->setQuery($query);
     try {
         $extensionId = $db->loadResult();
     } catch (RuntimeException $e) {
         JError::raiseWarning(500, $e->getMessage());
         return false;
     }
     $this->setState('module.extension_id', $extensionId);
     $this->setState('module.id', $table->id);
     // Clear modules cache
     $this->cleanCache();
     // Clean module cache
     parent::cleanCache($table->module, $table->client_id);
     return true;
 }