function onExtensionBeforeSave($context, $table, $isNew) { // ******************************* // TODO: add support for com_menus // ******************************* // Check for com_modules context if ($context == 'com_modules.module' || $context == 'com_advancedmodules.module' || substr($context, 0, 10) === "com_falang") { // Check for non-empty layout parameter $layout = $_POST['jform']['params']['layout']; if (empty($layout)) { return; } // Check for currently supported cases, !!! TODO add case of MENUS if (empty($table->module)) { return; } // Check if layout XML parameter file exists $client = JApplicationHelper::getClientInfo($table->client_id); $layoutpath = JPath::clean($client->path . '/modules/' . $table->module . '/tmpl/' . $layout . '.xml'); if (!file_exists($layoutpath)) { $layoutpath = JPath::clean($client->path . '/modules/' . $table->module . '/tmpl/_fallback/_fallback.xml'); if (!file_exists($layoutpath)) { return; } } // Load XML file if (FLEXI_J30GE) { $xml = simplexml_load_file($layoutpath); $xmldoc =& $xml; } else { $xml = JFactory::getXMLParser('Simple'); $xml->loadFile($layoutpath); $xmldoc =& $xml->document; } //echo "<pre>"; print_r($xmldoc); echo "</pre>"; // Create form object loading the , (form name seems not to cause any problem) $jform = new JForm('com_flexicontent.template.item', array('control' => 'jform', 'load_data' => true)); $tmpl_params = FLEXI_J30GE ? $xmldoc->asXML() : $xmldoc->toString(); $jform->load($tmpl_params); // Set cleared layout parameters $_post =& $_POST['jform']['params']; //echo "<pre>"; print_r($_post); echo "</pre>"; $params = new JRegistry($table->params); $grpname = 'params'; $isValid = !$jform->validate($_post, $grpname); if ($isValid) { JFactory::getApplication()->enqueueMessage('Error validating layout posted parameters. Layout parameters were not saved', 'error'); return; } foreach ($jform->getGroup($grpname) as $field) { $fieldname = $field->__get('fieldname'); if (substr($fieldname, 0, 2) == "__") { continue; } $value = $_post[$fieldname]; $params->set($fieldname, $value); } // Set parameters back to module's DB table object $table->params = $params->toString(); //echo "<pre>"; print_r($table->params); echo "</pre>"; //die('onExtensionBeforeSave: '. $layoutpath); } }
/** * Helper method to bind form posted item parameters and and metadata to the item * * @return object * @since 1.5 */ function mergeAttributes(&$item, &$params, &$metadata) { // Build item parameters INI string if (is_array($params)) { $item->attribs = FLEXI_J16GE ? new JRegistry($item->attribs) : new JParameter($item->attribs); $new_ilayout = isset($params['ilayout']) ? $params['ilayout'] : null; // a non-set will return null, but let's make this cleaner $old_ilayout = $item->attribs->get('ilayout'); //echo "new_ilayout: $new_ilayout, old_ilayout: $old_ilayout <br/>"; //echo "<pre>"; print_r($params); exit; if ($new_ilayout !== null && $new_ilayout == '' || $new_ilayout != '' && $new_ilayout != $old_ilayout) { $themes = flexicontent_tmpl::getTemplates(); foreach ($themes->items as $tmpl_name => $tmpl) { //if ( $tmpl_name == @$params['ilayout'] ) continue; $tmpl_params = $tmpl->params; if (FLEXI_J16GE) { $jform = new JForm('com_flexicontent.template.item', array('control' => 'jform', 'load_data' => true)); $jform->load($tmpl_params); foreach ($jform->getGroup('attribs') as $p) { if (!empty($p->fieldname)) { $item->attribs->set($p->fieldname, null); } } } else { if (!empty($tmpl_params->_xml['_default'])) { foreach ($tmpl_params->_xml['_default']->children() as $p) { if (!empty($p->_attributes['name'])) { $item->attribs->set($p->_attributes['name'], null); } } } } } } if (isset($params['layouts'])) { if (isset($params['layouts'][$new_ilayout])) { foreach ($params['layouts'][$new_ilayout] as $k => $v) { //echo "$k: $v <br/>"; $item->attribs->set($k, $v); } } unset($params['layouts']); } foreach ($params as $k => $v) { //$v = is_array($v) ? implode('|', $v) : $v; $item->attribs->set($k, $v); } //echo "<pre>"; print_r($params); print_r($item->attribs); exit; $item->attribs = $item->attribs->toString(); } // Build item metadata INI string if (is_array($metadata)) { $item->metadata = FLEXI_J16GE ? new JRegistry($item->metadata) : new JParameter($item->metadata); foreach ($metadata as $k => $v) { if ($k == 'description' && !FLEXI_J16GE) { // is jform field in J1.6+ $item->metadesc = $v; } elseif ($k == 'keywords' && !FLEXI_J16GE) { // is jform field in J1.6+ $item->metakey = $v; } else { $item->metadata->set($k, $v); } } $item->metadata = $item->metadata->toString(); } }
/** * Logic to render an XML file as form parameters * NOTE: this does not work with Request Data validation in J2.5+. The validation * must be skipped or the parameters must be re-added after the validation * * @access public * @return void * @since 1.5 */ function getlayoutparams() { jimport('joomla.filesystem.file'); $mainframe = JFactory::getApplication(); $user = JFactory::getUser(); //get vars $ext_view = JRequest::getVar('ext_view', ''); $ext_id = JRequest::getInt('ext_id', 0); $layout_name = JRequest::getVar('layout_name', 0); $directory = JRequest::getVar('directory', 0); $path = (!is_dir($directory) ? JPATH_ROOT : '') . $directory; $db = JFactory::getDBO(); if ($ext_view == 'module') { $query = 'SELECT params FROM #__modules WHERE id = ' . $ext_id; } else { if ($ext_view == 'field') { $query = 'SELECT attribs FROM #__flexicontent_fields WHERE id = ' . $ext_id; } else { echo "not supported extension/view: " . $ext_view; return; } } $db->setQuery($query); $ext_params_str = $db->loadResult(); $layoutpath = $path . DS . $layout_name . '.xml'; if (!file_exists($layoutpath)) { echo !FLEXI_J16GE ? '<div style="font-size: 11px; color: gray; background-color: lightyellow; border: 1px solid lightgray; width: auto; padding: 4px 2%; margin: 1px 8px; height: auto;">' : '<p class="tip">'; echo ' Currently selected layout: <b>"' . $layout_name . '"</b> does not have layout specific parameters'; echo !FLEXI_J16GE ? '</div>' : '</p>'; exit; } //Get data from the model if (FLEXI_J16GE) { $grpname = 'params'; // this name of <fields> container if (FLEXI_J30GE) { $xml = simplexml_load_file($layoutpath); $xmldoc =& $xml; } else { $xml = JFactory::getXMLParser('Simple'); $xml->loadFile($layoutpath); $xmldoc =& $xml->document; } $tmpl_params = FLEXI_J30GE ? $xmldoc->asXML() : $xmldoc->toString(); // Create form object, (form name seems not to cause any problem) $jform = new JForm('com_flexicontent.template.item', array('control' => 'jform', 'load_data' => true)); $jform->load($tmpl_params); // Load existing layout values into the object (that we got from DB) $ext_params = new JRegistry($ext_params_str); // and for J1.5: new JParameter($ext_params_str); foreach ($jform->getGroup($grpname) as $field) { $fieldname = $field->__get('fieldname'); $value = $ext_params->get($fieldname); if (strlen($value)) { $jform->setValue($fieldname, $grpname, $value); } } } else { // Create a parameters object $form = new JParameter('', $layoutpath); // Load existing layout values into the object (that we got from DB) $form->loadINI($ext_params_str); } if ($layout_name) { if (!FLEXI_J16GE) { echo $form->render('params', 'layout'); } else { ?> <fieldset class="panelform"><ul class="adminformlist"> <?php foreach ($jform->getGroup($grpname) as $field) { echo '<li>' . $field->label . $field->input . '</li>'; } ?> </ul></fieldset> <?php } } else { echo "<br /><span style=\"padding-left:25px;\"'>" . JText::_('FLEXI_APPLY_TO_SEE_THE_PARAMETERS') . "</span><br /><br />"; } //parent::display($tpl); }
/** * Logic to render an XML file as form parameters * NOTE: this does not work with Request Data validation in J2.5+. The validation * must be skipped or the parameters must be re-added after the validation * * @access public * @return void * @since 1.5 */ function getlayoutparams() { jimport('joomla.filesystem.file'); $app = JFactory::getApplication(); $user = JFactory::getUser(); //get vars $ext_option = JRequest::getVar('ext_option', ''); $ext_view = JRequest::getVar('ext_view', ''); $ext_id = JRequest::getInt('ext_id', 0); $layout_name = JRequest::getVar('layout_name', 0); $directory = JRequest::getVar('directory', 0); $path = (!is_dir($directory) ? JPATH_ROOT : '') . $directory; $db = JFactory::getDBO(); if ($ext_view == 'module') { $query = 'SELECT params FROM #__modules WHERE id = ' . $ext_id; // load english language file for 'mod_flexicontent' module then override with current language file $module_name = basename(dirname($directory)); JFactory::getLanguage()->load($module_name, JPATH_SITE, 'en-GB', true); JFactory::getLanguage()->load($module_name, JPATH_SITE, null, true); } else { if ($ext_view == 'field') { $query = 'SELECT attribs FROM #__flexicontent_fields WHERE id = ' . $ext_id; } else { echo "not supported extension/view: " . $ext_view; return; } } if ($ext_option != 'com_flexicontent' && $ext_option != 'com_modules' && $ext_option != 'com_advancedmodules' && $ext_option != 'com_menus') { echo '<div class="alert fcpadded fcinlineblock" style="">You are editing module via extension: <span class="label label-warning">' . $ext_option . '</span><br/> - If extension does not call Joomla event <span class="label label-warning">onExtensionBeforeSave</span> then custom layout parameters may not be saved</div>'; } $db->setQuery($query); $ext_params_str = $db->loadResult(); $layout_names = explode(':', $layout_name); if (count($layout_names) > 1) { $layout_name = $layout_names[1]; $layoutpath = JPATH_ROOT . DS . 'templates' . DS . $layout_names[0] . DS . 'html' . DS . 'mod_flexicontent/' . $layout_name . '.xml'; } else { $layoutpath = $path . DS . $layout_name . '.xml'; } if (!file_exists($layoutpath)) { if (file_exists($path . DS . '_fallback' . DS . '_fallback.xml')) { $layoutpath = $path . DS . '_fallback' . DS . '_fallback.xml'; echo '<div class="alert fcpadded fcinlineblock">Currently selected layout: <b>"' . $layout_name . '"</b> does not have a parameters XML file, using general defaults. if this is an old template then these parameters will allow to continue using it, but we recommend that you create parameter file: ' . $layout_name . '.xml</div><div class="clear"></div>'; } else { echo !FLEXI_J16GE ? '<div style="font-size: 11px; color: gray; background-color: lightyellow; border: 1px solid lightgray; width: auto; padding: 4px 2%; margin: 1px 8px; height: auto;">' : '<p class="tip">'; echo ' Currently selected layout: <b>"' . $layout_name . '"</b> does not have layout specific parameters'; echo !FLEXI_J16GE ? '</div>' : '</p>'; exit; } } //Get data from the model if (FLEXI_J16GE) { // Load XML file if (FLEXI_J30GE) { $xml = simplexml_load_file($layoutpath); $xmldoc =& $xml; } else { $xml = JFactory::getXMLParser('Simple'); $xml->loadFile($layoutpath); $xmldoc =& $xml->document; } // Create form object, (form name seems not to cause any problem) $jform = new JForm('com_flexicontent.template.item', array('control' => 'jform', 'load_data' => true)); $tmpl_params = FLEXI_J30GE ? $xmldoc->asXML() : $xmldoc->toString(); $jform->load($tmpl_params); // Load existing layout values into the object (that we got from DB) $ext_params = new JRegistry($ext_params_str); // and for J1.5: new JParameter($ext_params_str); $grpname = 'params'; // this is the name of <fields> container foreach ($jform->getGroup($grpname) as $field) { $fieldname = $field->__get('fieldname'); $value = $ext_params->get($fieldname); if (strlen($value)) { $jform->setValue($fieldname, $grpname, $value); } } } else { // Create a parameters object $form = new JParameter('', $layoutpath); // Load existing layout values into the object (that we got from DB) $form->loadINI($ext_params_str); } if ($layout_name) { if (!FLEXI_J16GE) { echo $form->render('params', 'layout'); } else { ?> <fieldset class="panelform"><ul class="adminformlist"> <?php $grpname = 'params'; // this is the name of <fields> container foreach ($jform->getGroup($grpname) as $field) { echo '<li>' . $field->label . $field->input . '</li>'; } ?> </ul></fieldset> <?php } } else { echo "<br /><span style=\"padding-left:25px;\"'>" . JText::_('FLEXI_APPLY_TO_SEE_THE_PARAMETERS') . "</span><br /><br />"; } //parent::display($tpl); }
protected function preprocessForm(JForm $form, $data, $group = 'content') { $app = JFactory::getApplication(); $params = JComponentHelper::getParams('com_djcatalog2'); $user = JFactory::getUser(); $allowed_categories = $params->get('fed_allowed_categories', array()); JArrayHelper::toInteger($allowed_categories); if (!empty($allowed_categories)) { $form->setFieldAttribute('cat_id', 'allowed_categories', implode(',', $allowed_categories)); } if ($params->get('fed_multiple_categories', '0') == '0') { $form->removeField('categories'); } else { if (!empty($allowed_categories)) { $form->setFieldAttribute('categories', 'allowed_categories', implode(',', $allowed_categories)); } if ((int) $params->get('fed_multiple_categories_limit', 3) > 0) { $form->setFieldAttribute('categories', 'limit', (int) $params->get('fed_multiple_categories_limit', 3)); if (!empty($data->cat_id)) { $form->setFieldAttribute('categories', 'ignored_values', $data->cat_id); } } else { if ($params->get('fed_multiple_categories_limit', 3) == '0') { $form->removeField('categories'); } } } if ($params->get('fed_multiple_groups', '0') == '0') { $form->setFieldAttribute('group_id', 'multiple', 'false'); } if ($params->get('fed_producer', '0') == '0') { $form->removeField('producer_id'); } else { if ($params->get('fed_producer', '0') == '2') { $form->setFieldAttribute('producer_id', 'required', 'required'); $form->setFieldAttribute('producer_id', 'class', $form->getFieldAttribute('producer_id', 'class') . ' required'); } if ($params->get('fed_producer_restrict', 0) == '1') { $form->setFieldAttribute('producer_id', 'validate', 'djcproducer'); if (!empty($data->created_by) && $data->created_by > 0) { $form->setFieldAttribute('producer_id', 'validate_user', $data->created_by); } } } if ($params->get('fed_price', '0') == '0') { $form->removeField('price'); $form->removeField('special_price'); } else { if ($params->get('fed_price', '0') == '2') { $form->setFieldAttribute('price', 'required', 'required'); $form->setFieldAttribute('price', 'class', $form->getFieldAttribute('price', 'class') . ' required'); } } if ($params->get('fed_featured', '0') == '0') { $form->removeField('featured'); } else { if ($params->get('fed_featured', '0') == '2') { $form->setFieldAttribute('featured', 'required', 'required'); $form->setFieldAttribute('featured', 'class', $form->getFieldAttribute('featured', 'class') . ' required'); } } if ($params->get('fed_available', '0') == '0') { $form->removeField('available'); } else { if ($params->get('fed_available', '0') == '2') { $form->setFieldAttribute('available', 'required', 'required'); $form->setFieldAttribute('available', 'class', $form->getFieldAttribute('available', 'class') . ' required'); } } if ($params->get('fed_group', '0') == '0') { $form->removeField('group_id'); } else { if ($params->get('fed_group', '0') == '2') { $form->setFieldAttribute('group_id', 'required', 'true'); //$form->setFieldAttribute('group_id', 'class', $form->getFieldAttribute('group_id', 'class').' required'); } } if ($params->get('fed_meta', '0') == '0') { $form->removeField('metatitle'); $form->removeField('metakey'); $form->removeField('metadesc'); } if ($params->get('fed_intro_description_editor', null)) { $form->setFieldAttribute('intro_desc', 'editor', $params->get('fed_intro_description_editor')); } if ($params->get('fed_intro_description', '0') == '0') { $form->removeField('intro_desc'); } else { if ($params->get('fed_intro_description', '0') == '2') { $form->setFieldAttribute('intro_desc', 'required', 'required'); $form->setFieldAttribute('intro_desc', 'class', $form->getFieldAttribute('intro_desc', 'class') . ' required'); } } if ($params->get('fed_description_editor', null)) { $form->setFieldAttribute('description', 'editor', $params->get('fed_description_editor')); } if ($params->get('fed_description', '0') == '0') { $form->removeField('description'); } else { if ($params->get('fed_description', '0') == '2') { $form->setFieldAttribute('description', 'required', 'required'); $form->setFieldAttribute('description', 'class', $form->getFieldAttribute('description', 'class') . ' required'); } } $location_fields = $form->getGroup('location'); $show_location = (bool) ($params->get('fed_location_details', '1') == '1'); foreach ($location_fields as $k => $field) { $field_name = $field->__get('fieldname'); if ($show_location) { $show_location_field = (int) $params->get('fed_location_' . $field_name, '1'); if ($show_location_field == 0) { $form->removeField($field_name, 'location'); } else { if ($show_location_field == 2) { $form->setFieldAttribute($field_name, 'required', 'required', 'location'); $form->setFieldAttribute($field_name, 'class', $form->getFieldAttribute($field_name, 'class', '', 'location') . ' required', 'location'); } } } else { $form->removeField($field_name, 'location'); } } $default_state = $params->get('fed_default_state', '0'); $form->setFieldAttribute('published', 'default', $default_state); $is_owner = empty($data->created_by) && !empty($data) || isset($data->created_by) && (int) JFactory::getUser()->id === (int) $data->created_by ? true : false; if (!(JFactory::getUser()->authorise('core.edit.state', 'com_djcatalog2') || JFactory::getUser()->authorise('core.edit.state.own', 'com_djcatalog2') && $is_owner)) { if (isset($data->published)) { $form->removeField('published'); } } }