/** * Save a connection */ function save() { // Check for request forgeries JRequest::checkToken() or die('Invalid Token'); jimport('joomla.utilities.date'); $session =& JFactory::getSession(); $user =& JFactory::getUser(); $db =& JFactory::getDBO(); $pluginManager =& JModel::getInstance('Pluginmanager', 'FabrikModel'); $task = JRequest::getCmd('task'); $id = JRequest::getInt('id', 0, 'post'); $details = JRequest::getVar('details', array(), 'post', 'array'); $className = $details['plugin']; $elementModel = $pluginManager->getPlugIn($className, 'element'); $elementModel->setId($id); $row =& $elementModel->getElement(); $origRow = clone $row; $this->_setSaveRedirect($task, $row->id); $name = JRequest::getVar('name', '', 'post', 'CMD'); $name = str_replace('-', '_', $name); if (FabrikWorker::isReserved($name)) { return JError::raiseWarning(500, JText::_('SORRY THIS NAME IS RESERVED FOR FABRIK')); } if (JRequest::getInt('id') === 0) { //have to forcefully set group id otherwise tablemodel id is blank $elementModel->getElement()->group_id = $details['group_id']; } $tableModel =& $elementModel->getTableModel(); //are we updating the name of the primary key element? if ($row->name === str_replace('`', '', $tableModel->_shortKey())) { if ($name !== $row->name) { //yes we are so update the table $table =& $tableModel->getTable(); $table->db_primary_key = str_replace($row->name, $name, $table->db_primary_key); $table->store(); } } //test for duplicate names //unlinking produces this error if (!JRequest::getVar('unlink', false)) { $row->group_id = (int) $details['group_id']; $db->setQuery("SELECT t.id, group_id FROM `#__fabrik_joins` AS j " . "\n LEFT JOIN #__fabrik_tables AS t " . "\n ON j.table_join = t.db_table_name " . "\n WHERE group_id = " . (int) $row->group_id . " AND element_id = 0"); $res = $db->loadObject(); if (is_null($res)) { // no join found if ($tableModel->fieldExists(JRequest::getVar('name'), array($id))) { return JError::raiseWarning(500, JText::_('SORRY THIS NAME IS ALREADY IN USE')); } } else { $jointableModel =& JModel::getInstance('table', 'fabrikModel'); $jointableModel->setId((int) $res->id); $joinEls = $jointableModel->getElements(); $ignore = array($id); foreach ($joinEls as $joinEl) { if ($joinEl->getElement()->name == JRequest::getVar('name')) { $ignore[] = $joinEl->getElement()->id; } } if ($jointableModel->fieldExists(JRequest::getVar('name'), $ignore)) { JError::raiseNotice(500, JText::_('SORRY THIS NAME IS ALREADY IN USE')); } } } //end duplicate name test $post = JRequest::get('post', 4); // $$$ hugh allows "safe" HTML. //$$$ rob default etc may require you to have \" or < recored - safe html filter removes these $raws = array('default', 'sub_values'); foreach ($raws as $raw) { $post[$raw] = JRequest::getVar($raw, null, 'default', 'none', 2); } $tableParams =& $tableModel->getParams(); //only update the element name if we can alter existing columns, otherwise the name and //field name become out of sync //if ($tableParams->get('alter_existing_db_cols') == 1 || $id == 0) { // $$$ hugh - check to see if there's actually a table if (empty($tableModel->_id) || ($tableModel->_canAlterFields() || $id == 0)) { $post['name'] = $name; } else { $post['name'] = JRequest::getVar('name_orig', '', 'post', 'cmd'); } $ar = array('state', 'use_in_page_title', 'show_in_table_summary', 'link_to_detail', 'can_order', 'filter_exact_match'); foreach ($ar as $a) { if (!array_key_exists($a, $post)) { $post[$a] = 0; } } // $$$ rob - test for change in element type //(eg if changing from db join to field we need to remove the join //entry from the #__fabrik_joins table $origElementModel =& JModel::getInstance('Element', 'FabrikModel'); $origElementModel->setId($id); $origEl =& $origElementModel->getElement(); $origElementPluginModel =& $pluginManager->getPlugIn($origEl->plugin, 'element'); $origElementPluginModel->beforeSave($row); if (!$row->bind($post)) { return JError::raiseWarning(500, $row->getError()); } //unlink linked elements if (JRequest::getVar('unlink') == 'on') { $row->parent_id = 0; } //merge details params into element table fields if (!array_key_exists('eval', $details)) { $details['eval'] = 0; } if (!array_key_exists('hidden', $details)) { $details['hidden'] = 0; } $row->bind($details); $datenow = new JDate(); if ($row->id != 0) { $row->modified = $datenow->toFormat(); $row->modified_by = $user->get('id'); } else { $row->created = $datenow->toFormat(); $row->created_by = $user->get('id'); $row->created_by_alias = $user->get('username'); } // save params $params = $elementModel->getParams(); $row->attribs = $params->updateAttribsFromParams(JRequest::getVar('params', array(), 'post', 'array')); $cond = 'group_id = ' . (int) $row->group_id; //hack for width option if ($row->width == '') { $row->width = 40; } $new = $row->id == 0 ? true : false; if ($new) { $row->ordering = $row->getNextOrder($cond); } if (!$row->store()) { return JError::raiseWarning(500, $row->getError()); } $row->checkin(); $row->reorder($cond); $elementModel->setId($row->id); $oldParams = $elementModel->_params; //unset and reload the params with newly saved values unset($elementModel->_params); $elementModel->getParams(); $elementModel->updateJavascript(); if (!$elementModel->onSave()) { //revert row back to original data foreach ($origRow as $k => $v) { $row->{$k} = $v; $row->store(); } $this->setRedirect('index.php?option=com_fabrik&c=element&task=edit&cid[]=' . $row->id); return; } //set flags in session to ensure we de/encrypt columns data when the field's structure is updated $session->clear('com_fabrik.admin.element.encryptCol'); $session->clear('com_fabrik.admin.element.decryptCol'); $encryptCol = $oldParams->get('encrypt') == 0 && $elementModel->getParams()->get('encrypt') == 1; $session->set('com_fabrik.admin.element.encryptCol', $encryptCol); $decryptCol = $oldParams->get('encrypt') == 1 && $elementModel->getParams()->get('encrypt') == 0; $session->set('com_fabrik.admin.element.decryptCol', $decryptCol); $this->updateChildIds($row); $this->setMessage(JText::_('ELEMENT SAVED')); $origName = JRequest::getVar('name_orig', '', 'post', 'cmd'); list($update, $q, $oldName, $newdesc, $origDesc, $dropIndex) = $tableModel->shouldUpdateElement($elementModel, $origName); // If new, check if the element's db table is used by other tables and if so add the element // to each of those tables' groups if ($new) { $this->addElementToOtherDbTables($elementModel, $row); } $elementModel->createRepeatElement(); if ($update) { $origplugin = JRequest::getVar('plugin_orig'); $session->set('com_fabrik.admin.element.updatequery', $q); $session->set('com_fabrik.admin.element.oldname', $oldName); $session->set('com_fabrik.admin.element.newdesc', $newdesc); $session->set('com_fabrik.admin.element.origdesc', $origDesc); $session->set('com_fabrik.admin.element.newname', $name); $session->set('com_fabrik.admin.element.dropindex', $dropIndex); $this->setRedirect('index.php?option=com_fabrik&c=element&task=confirmElementUpdate&id=' . (int) $row->id . "&origplugin={$origplugin}&&origtaks={$task}&plugin={$row->plugin}"); } else { $this->_setSaveRedirect($task, $row->id); } $cache =& JFactory::getCache('com_fabrik'); $cache->clean(); if ((int) $tableModel->getTable()->id !== 0) { $this->updateIndexes($elementModel, $tableModel, $row); } // $$$ hugh - adding afterSave(), for things like join element to handle adding // rows to joins table for any children we created (can't use onSave 'cos children // haven't been create at that point). $elementModel->onAfterSave($row); //used for prefab return $elementModel; }
/** * Method to validate the form data. * * @param object $form The form to validate against. * @param array $data The data to validate. * @return mixed Array of filtered data if valid, false otherwise. * @since 1.1 */ function validate($form, $data) { $ok = parent::validate($form, $data); //standard jform validation failed so we shouldn't test further as we can't //be sure of the data if (!$ok) { return false; } $db = FabrikWorker::getDbo(true); // validate name //$data['name'] = str_replace('-', '_', $data['name']); if (FabrikWorker::isReserved($data['name'])) { $this->setError(JText::_('COM_FABRIK_RESEVED_NAME_USED')); } $elementModel = $this->getElementPluginModel($data); $elementModel->getElement()->bind($data); if ($data['id'] === 0) { //have to forcefully set group id otherwise listmodel id is blank $elementModel->getElement()->group_id = $data['group_id']; } $listModel =& $elementModel->getListModel(); //test for duplicate names //unlinking produces this error if (!JRequest::getVar('unlink', false) && (int) $data['id'] === 0) { $row->group_id = (int) $data['group_id']; $query = $db->getQuery(true); $query->select('t.id')->from('#__{package}_joins AS j'); $query->join('INNER', "#__{package}_lists AS t ON j.table_join = t.db_table_name"); $query->where("group_id = {$row->group_id} AND element_id = 0"); $db->setQuery($query); $joinTblId = (int) $db->loadResult(); $ignore = array($data['id']); if ($joinTblId === 0) { if ($listModel->fieldExists($data['name'], $ignore)) { $this->setError(JText::_('COM_FABRIK_ELEMENT_NAME_IN_USE')); } } else { $joinListModel = JModel::getInstance('list', 'FabrikFEModel'); $joinListModel->setId($joinTblId); $joinEls = $joinListModel->getElements(); foreach ($joinEls as $joinEl) { if ($joinEl->getElement()->name == $data['name']) { $ignore[] = $joinEl->getElement()->id; } } if ($joinListModel->fieldExists($data['name'], $ignore)) { $this->setError(JText::_('COM_FABRIK_ELEMENT_NAME_IN_USE')); } } } //end duplicate name test // $$$ rob commented out as on new elemetns db join was creating // join records pointing to an el id of 0 // should consider makeing an $element->onValidate() or similar /*if (!$elementModel->onSave()) { $this->setError(JText::_('COM_FABRIK_ERROR_SAVING_ELEMENT_PLUGIN_OPTIONS')); }*/ return count($this->getErrors()) == 0 ? $data : false; }
/** * Method to validate the form data. * * @param JForm $form The form to validate against. * @param array $data The data to validate. * @param string $group The name of the field group to validate. * * @see JFormRule * @see JFilterInput * * @return mixed Array of filtered data if valid, false otherwise. */ public function validate($form, $data, $group = null) { $ok = parent::validate($form, $data); $input = $this->app->input; // Standard jform validation failed so we shouldn't test further as we can't be sure of the data if (!$ok) { return false; } $db = FabrikWorker::getDbo(true); $elementModel = $this->getElementPluginModel($data); $nameChanged = $data['name'] !== $elementModel->getElement()->name; $elementModel->getElement()->bind($data); $listModel = $elementModel->getListModel(); if ($data['id'] == '') { // Have to forcefully set group id otherwise listmodel id is blank $elementModel->getElement()->group_id = $data['group_id']; if ($listModel->canAddFields() === false && $listModel->noTable() === false) { $this->setError(FText::_('COM_FABRIK_ERR_CANT_ADD_FIELDS')); } if (FabrikWorker::isReserved($data['name'])) { $this->setError(FText::_('COM_FABRIK_RESERVED_NAME_USED')); } } else { if ($listModel->canAlterFields() === false && $nameChanged && $listModel->noTable() === false) { $this->setError(FText::_('COM_FABRIK_ERR_CANT_ALTER_EXISTING_FIELDS')); } if ($nameChanged && FabrikWorker::isReserved($data['name'], false)) { $this->setError(FText::_('COM_FABRIK_RESERVED_NAME_USED')); } } $listModel = $elementModel->getListModel(); /** * Test for duplicate names * unlinking produces this error */ if (!$input->get('unlink', false) && (int) $data['id'] === 0) { $query = $db->getQuery(true); $query->select('t.id')->from('#__{package}_joins AS j'); $query->join('INNER', '#__{package}_lists AS t ON j.table_join = t.db_table_name'); $query->where('group_id = ' . (int) $data['group_id'] . ' AND element_id = 0'); $db->setQuery($query); $joinTblId = (int) $db->loadResult(); $ignore = array($data['id']); if ($joinTblId === 0) { if ($listModel->fieldExists($data['name'], $ignore)) { $this->setError(FText::_('COM_FABRIK_ELEMENT_NAME_IN_USE')); } } else { $joinListModel = JModelLegacy::getInstance('list', 'FabrikFEModel'); $joinListModel->setId($joinTblId); $joinEls = $joinListModel->getElements(); foreach ($joinEls as $joinEl) { if ($joinEl->getElement()->name == $data['name']) { $ignore[] = $joinEl->getElement()->id; } } if ($joinListModel->fieldExists($data['name'], $ignore)) { $this->setError(FText::_('COM_FABRIK_ELEMENT_NAME_IN_USE')); } } } // Strip <p> tag from label $data['label'] = JString::str_ireplace(array('<p>', '</p>'), '', $data['label']); return count($this->getErrors()) == 0 ? $data : false; }
/** * Sets up HTML to be injected into the form's bottom * * @param object $params params * @param object $formModel form model * * @return void */ public function getBottomContent($params, $formModel) { // If we have already processed the form $this->html = ''; if (JRequest::getVar('fabrik_confirmation') == 1) { $session = JFactory::getSession(); // Unset this flag JRequest::setVar('fabrik_confirmation', 2); $post = JRequest::get('post', 4); /** * load in the posted values as hidden fields so that if we * return to the form to edit it it will populate with our data */ // $$$ 24/10/2011 testing removing this as data is retrieved via the session not thorugh posted data foreach ($post as $key => $val) { $noneraw = JString::substr($key, 0, JString::strlen($key) - 4); if ($key == 'join' || $key == 'fabrik_vars') { continue; } if ($formModel->hasElement($key) || $formModel->hasElement($noneraw)) { // Return; } if ($formModel->hasElement($noneraw)) { $key = $formModel->getElement($noneraw)->getHTMLName(0); // $$$ rob include both raw and non-raw keys (non raw for radios etc, _raw for db joins) if (is_array($val)) { foreach ($val as $val2) { if (!FabrikWorker::isReserved($key)) { if (!strstr($key, '[]')) { $key .= '[]'; } // $fields[] = '<input type="hidden" name="'.str_replace('_raw','',$key).'[]" value="'.urlencode($val2).'" />'; // $fields[] = '<input type="hidden" name="'.$key.'" value="'.urlencode($val2).'" />'; $fields[] = '<input type="hidden" name="' . $key . '" value="' . $val2 . '" />'; } } } else { if (!FabrikWorker::isReserved($key)) { // $fields[] = '<input type="hidden" name="'.str_replace('_raw','',$key).'" value="'.urlencode($val).'" />'; // $fields[] = '<input type="hidden" name="'.$key.'" value="'.urlencode($val).'" />'; $fields[] = '<input type="hidden" name="' . $key . '" value="' . $val . '" />'; } } } } // Add in a view field as the form doesn't normally contain one $fields[] = '<input type="hidden" name="view" value="form" />'; $fields[] = '<input type="hidden" name="fabrik_confirmation" value="2" />'; // Add in a button to allow you to go back to the form and edit your data $fields[] = "<input type=\"button\" id=\"fabrik_redoconfirmation\" class=\"button\" value=\"" . JText::_('PLG_FORM_CONFIRMATION_RE_EDIT') . "\" />"; // Unset the task otherwise we will submit the form to be processed. FabrikHelperHTML::addScriptDeclaration("head.ready(function() {" . "\$('fabrik_redoconfirmation').addEvent('click', function(e) {;\n" . " this.form.task.value = '';\n" . " this.form.submit.click();\n" . "\t});\n" . "});"); $this->html = implode("\n", $fields); } }
/** * set up the html to be injected into the bottom of the form * * @param object $params (no repeat counter stuff needed here as the plugin manager * which calls this function has already done the work for you * @param object form model */ function getBottomContent(&$params, $formModel) { //if we have already processed the form $this->html = ''; if (JRequest::getVar('fabrik_confirmation') == 1) { //unset this flag JRequest::setVar('fabrik_confirmation', 2); $post = JRequest::get('post', 4); //load in the posted values as hidden fields so that if we //return to the form to edit it it will populate with our data foreach ($post as $key => $val) { // form data is stored in session $noneraw = substr($key, 0, strlen($key) - 4); if ($key == 'join' || $key == 'fabrik_vars' || $formModel->hasElement($key) || $formModel->hasElement($noneraw)) { continue; } // $$$ rob include both raw and non-raw keys (non raw for radios etc, _raw for db joins) if (is_array($val)) { foreach ($val as $val2) { if (!FabrikWorker::isReserved($key)) { $fields[] = '<input type="hidden" name="' . str_replace('_raw', '', $key) . '[]" value="' . urlencode($val2) . '" />'; $fields[] = '<input type="hidden" name="' . $key . '[]" value="' . urlencode($val2) . '" />'; } } } else { if (!FabrikWorker::isReserved($key)) { $fields[] = '<input type="hidden" name="' . str_replace('_raw', '', $key) . '" value="' . urlencode($val) . '" />'; $fields[] = '<input type="hidden" name="' . $key . '" value="' . urlencode($val) . '" />'; } } } //add in a view field as the form doesn't normally contain one $fields[] = '<input type="hidden" name="view" value="form" />'; //add in a button to allow you to go back to the form and edit your data $fields[] = "<input type=\"button\" id=\"fabrik_redoconfirmation\" class=\"button\" value=\"" . JText::_('PLG_FORM_CONFIRMATION_RE_EDIT') . "\" />"; FabrikHelperHTML::addScriptDeclaration("head.ready(function() {" . "\$('fabrik_redoconfirmation').addEvent('click', function(e) {" . " this.form.task.value = '';" . " this.form.submit();" . "\t});" . "});"); $this->html = implode("\n", $fields); } }
/** * Sets up HTML to be injected into the form's bottom * * @return void */ public function getBottomContent() { $formModel = $this->getModel(); $input = $this->app->input; // If we have already processed the form $this->html = ''; if ($input->getInt('fabrik_confirmation') === 1) { // Unset this flag $input->set('fabrik_confirmation', 2); $safeHtmlFilter = JFilterInput::getInstance(null, null, 1, 1); $post = $safeHtmlFilter->clean($_POST, 'array'); /** * load in the posted values as hidden fields so that if we * return to the form to edit it it will populate with our data */ // $$$ 24/10/2011 testing removing this as data is retrieved via the session not through posted data foreach ($post as $key => $val) { $noneRaw = JString::substr($key, 0, JString::strlen($key) - 4); if ($key == 'fabrik_vars') { continue; } if ($formModel->hasElement($key) || $formModel->hasElement($noneRaw)) { // Return; } if ($formModel->hasElement($noneRaw)) { $key = $formModel->getElement($noneRaw)->getHTMLName(0); // $$$ rob include both raw and non-raw keys (non raw for radios etc., _raw for db joins) if (is_array($val)) { foreach ($val as $val2) { if (!FabrikWorker::isReserved($key)) { if (!strstr($key, '[]')) { $key .= '[]'; } // $fields[] = '<input type="hidden" name="'.str_replace('_raw','',$key).'[]" value="'.urlencode($val2).'" />'; // $fields[] = '<input type="hidden" name="'.$key.'" value="'.urlencode($val2).'" />'; $fields[] = '<input type="hidden" name="' . $key . '" value="' . $val2 . '" />'; } } } else { if (!FabrikWorker::isReserved($key)) { // $fields[] = '<input type="hidden" name="'.str_replace('_raw','',$key).'" value="'.urlencode($val).'" />'; // $fields[] = '<input type="hidden" name="'.$key.'" value="'.urlencode($val).'" />'; $fields[] = '<input type="hidden" name="' . $key . '" value="' . $val . '" />'; } } } } // Add in a view field as the form doesn't normally contain one $fields[] = '<input type="hidden" name="view" value="form" />'; $fields[] = '<input type="hidden" name="fabrik_confirmation" value="2" />'; // Add in a button to allow you to go back to the form and edit your data $fields[] = "<input type=\"button\" id=\"fabrik_redoconfirmation\" class=\"button btn\" value=\"" . FText::_('PLG_FORM_CONFIRMATION_RE_EDIT') . "\" />"; // Unset the task otherwise we will submit the form to be processed. FabrikHelperHTML::addScriptDeclaration("\n\t\t\t\twindow.addEvent('fabrik.loaded', function() {\n\t\t\t\t\t\$('fabrik_redoconfirmation').addEvent('click', function(e) {;\n\t\t\t\t\t\tthis.form.task.value = '';\n\t\t\t\t\t\t// this.form.submit();\n\t\t\t\t\t\tvar thisform = Fabrik.getBlock(this.form.id);\n\t\t\t\t\t\tthisform.doSubmit(new Event.Mock(thisform._getButton('Submit')), thisform._getButton('Submit'));\n\t\t\t\t\t});\n\t\t\t\t});\n\t\t\t"); $this->html = implode("\n", $fields); } }