/** * append the form javascript into the document head * @param int table id */ protected function _addJavascript($tableId) { $app = JFactory::getApplication(); $document = JFactory::getDocument(); $model = $this->getModel(); $aLoadedElementPlugins = array(); $jsActions = array(); $jsControllerKey = $model->_editable ? 'form_' . $model->getId() : 'details_' . $model->getId(); if (!defined('_JOS_FABRIK_FORMJS_INCLUDED')) { define('_JOS_FABRIK_FORMJS_INCLUDED', 1); FabrikHelperHTML::slimbox(); $srcs = array('media/com_fabrik/js/form.js', 'media/com_fabrik/js/element.js'); } $aWYSIWYGNames = array(); // $$$ hugh - yeat another one where if we =&, the $groups array pointer get buggered up and it // skips a group $groups = $model->getGroupsHiarachy(); foreach ($groups as $groupModel) { $elementModels = $groupModel->getPublishedElements(); foreach ($elementModels as $elementModel) { $res = $elementModel->useEditor(); if ($res !== false) { $aWYSIWYGNames[] = $res; } $eparams = $elementModel->getParams(); //load in once the element js class files // $$$ hugh - only needed getParent when we weren't saving changes to parent params to child // which we should now be doing ... and getParent() causes an extra table lookup for every child // element on the form. //$element =& $elementModel->getParent(); $element = $elementModel->getElement(); if (!in_array($element->plugin, $aLoadedElementPlugins)) { $aLoadedElementPlugins[] = $element->plugin; $elementModel->formJavascriptClass($srcs); } $eventMax = $groupModel->_repeatTotal == 0 ? 1 : $groupModel->_repeatTotal; for ($c = 0; $c < $eventMax; $c++) { $jsActions[] = $elementModel->getFormattedJSActions($jsControllerKey, $c); } } } FabrikHelperHTML::script($srcs, true); //new $actions = trim(implode("\n", $jsActions)); //end new $params = $model->getParams(); $listModel = $model->getlistModel(); $table = $listModel->getTable(); $form = $model->getForm(); FabrikHelperHTML::mocha(); $bkey = $model->_editable ? 'form_' . $model->getId() : 'details_' . $model->getId(); FabrikHelperHTML::tips('.hasTip', array(), "\$('{$bkey}')"); $key = FabrikString::safeColNameToArrayKey($table->db_primary_key); $this->get('FormCss'); $this->get('CustomJsAction'); $startJs = "head.ready(function() {\n"; $endJs = "});\n"; $start_page = isset($model->sessionModel->last_page) ? (int) $model->sessionModel->last_page : 0; if ($start_page !== 0) { $app->enqueueMessage(JText::_('COM_FABRIK_RESTARTING_MUTLIPAGE_FORM')); } else { // form submitted but fails validation - needs to go to the last page $start_page = JRequest::getInt('currentPage', 0); } $opts = new stdClass(); $opts->admin = $app->isAdmin(); $opts->ajax = $model->isAjax(); $opts->ajaxValidation = $params->get('ajax_validations'); $opts->primaryKey = $key; $opts->error = @$form->origerror; $opts->pages = $model->getPages(); $opts->plugins = array(); $opts->multipage_save = (bool) $model->saveMultiPage(); $opts->editable = $model->_editable; $opts->start_page = $start_page; $opts->inlineMessage = (bool) $this->isMambot; //$$$rob dont int this as keys may be string $opts->rowid = $model->_rowId; //3.0 needed for ajax requests $opts->listid = (int) $this->get('ListModel')->getId(); $imgs = new stdClass(); $imgs->alert = FabrikHelperHTML::image('alert.png', 'form', $this->tmpl, '', true); $imgs->action_check = FabrikHelperHTML::image('action_check.png', 'form', $this->tmpl, '', true); $imgs->ajax_loader = FabrikHelperHTML::image('ajax-loader.gif', 'form', $this->tmpl, '', true); $opts->images = $imgs; //$$$rob if you are loading a table in a window from a form db join select record option // then we want to know the id of the window so we can set its showSpinner() method $opts->fabrik_window_id = JRequest::getVar('fabrik_window_id', ''); //3.0 changed to fabrik_window_id (automatically appended by Fabrik.Window xhr request to load window data //for editing groups with joined data and an empty joined record (ie no joined records) $hidden = array(); $maxRepeat = array(); foreach ($this->groups as $g) { $hidden[$g->id] = $g->startHidden; $maxRepeat[$g->id] = $g->maxRepeat; } $opts->hiddenGroup = $hidden; $opts->maxRepeat = $maxRepeat; //$$$ rob 26/04/2011 joomfish translations of password validation error messages $opts->lang = FabrikWorker::getJoomfishLang(); $opts = json_encode($opts); $lang = new stdClass(); JText::script('COM_FABRIK_VALIDATING'); JText::script('COM_FABRIK_SUCCESS'); JText::script('COM_FABRIK_NO_REPEAT_GROUP_DATA'); JText::script('COM_FABRIK_VALIDATION_ERROR'); JText::script('COM_FABRIK_FORM_SAVED'); Jtext::script('COM_FABRIK_CONFIRM_DELETE'); //$$$ rob dont declare as var $bkey, but rather assign to window, as if loaded via ajax window the function is wrapped // inside an anoymous function, and therefore $bkey wont be available as a global var in window $str = "head.ready(function() {\r\n\t\t{$bkey} = new FbForm(" . $model->getId() . ", {$opts});\n"; //$str .= "$bkey.addListenTo('list_" . $listModel->getId() . "');\n"; //$str .= "$bkey.addListenTo('form_" . $model->getId() . "');\n"; $str .= "if(typeOf(Fabrik) !== 'null') {\n"; $str .= "Fabrik.addBlock('{$bkey}', {$bkey});\n"; $str .= "}\n\r\n\t\t});"; //instantaite js objects for each element $vstr = "\n"; $str .= "{$startJs}"; $groups =& $model->getGroupsHiarachy(); // $$$ rob in php5.2.6 (and possibly elsewhere) $groups's elements havent been updated // to contain the default value used by the element //foreach ($groups as $groupModel) { //testing this one again as Ive updated getGroupsHiarchy $str .= "{$bkey}.addElements({"; foreach ($groups as $groupModel) { $showGroup = $groupModel->getParams()->get('repeat_group_show_first'); if ($showGroup == -1 || $showGroup == 2 && $model->_editable) { // $$$ rob unpublished group so dont include the element js continue; } $aObjs = array(); $elementModels =& $groupModel->getPublishedElements(); // $$$ rob if _repeatTotal is 0 we still want to add the js objects as the els are only hidden $max = $groupModel->_repeatTotal > 0 ? $groupModel->_repeatTotal : 1; $str .= $groupModel->getGroup()->id . ":["; foreach ($elementModels as $elementModel) { $element =& $elementModel->getElement(); if ($element->published == 0) { continue; } $fullName = $elementModel->getFullName(); $id = $elementModel->getHTMLId(); $elementModel->_editable = $model->_editable; if ($elementModel->canUse() || $elementModel->canView()) { for ($c = 0; $c < $max; $c++) { // $$$ rob ensure that some js code has been returned otherwise dont add empty data to array $ref = trim($elementModel->elementJavascript($c)); if ($ref !== '') { $aObjs[] = $ref; } $validations =& $elementModel->getValidations(); if (!empty($validations) && $elementModel->_editable) { $watchElements = $elementModel->getValidationWatchElements($c); foreach ($watchElements as $watchElement) { $vstr .= "{$bkey}.watchValidation('" . $watchElement['id'] . "', '" . $watchElement['triggerEvent'] . "');\n"; } } } } } $str .= implode(",\n", $aObjs); $str .= "],"; } $str = FabrikString::rtrimword($str, ','); $str .= "});\n"; $str .= $actions; $str .= $vstr; $str .= $endJs; $str .= "function submit_form() {"; if (!empty($aWYSIWYGNames)) { jimport('joomla.html.editor'); $editor = JFactory::getEditor(); $str .= $editor->save('label'); foreach ($aWYSIWYGNames as $parsedName) { $str .= $editor->save($parsedName); } } $str .= "\r\n\t\t\treturn false;\r\n\t\t}\r\n\r\n\t\tfunction submitbutton(button) {\r\n\t\t\tif (button==\"cancel\") {\r\n\t\t\t\tdocument.location = '" . JRoute::_('index.php?option=com_fabrik&task=viewTable&cid=' . $tableId) . "';\r\n\t\t\t}\r\n\t\t\tif (button == \"cancelShowForm\") {\r\n\t\t\t\treturn false;\r\n\t\t\t}\r\n\t\t}\r\n"; FabrikHelperHTML::addScriptDeclaration($str); $pluginManager = $model->getPluginManager(); $pluginManager->runPlugins('onAfterJSLoad', $model); FabrikHelperHTML::mootools(); }
/** * Append the form javascript into the document head * * @param int $tableId table id * * @return void */ protected function _addJavascript($tableId) { $app = JFactory::getApplication(); $document = JFactory::getDocument(); $model = $this->getModel(); $aLoadedElementPlugins = array(); $jsActions = array(); $jsControllerKey = $model->isEditable() ? 'form_' . $model->getId() : 'details_' . $model->getId(); $srcs = FabrikHelperHTML::framework(); if (!defined('_JOS_FABRIK_FORMJS_INCLUDED')) { define('_JOS_FABRIK_FORMJS_INCLUDED', 1); FabrikHelperHTML::slimbox(); $srcs[] = 'media/com_fabrik/js/form.js'; $srcs[] = 'media/com_fabrik/js/element.js'; $srcs[] = 'media/com_fabrik/js/lib/form_placeholder/Form.Placeholder.js'; } $aWYSIWYGNames = array(); // $$$ hugh - yeat another one where if we =, the $groups array pointer get buggered up and it // skips a group $groups = $model->getGroupsHiarachy(); foreach ($groups as $groupModel) { $elementModels = $groupModel->getPublishedElements(); foreach ($elementModels as $elementModel) { $res = $elementModel->useEditor(); if ($res !== false) { $aWYSIWYGNames[] = $res; } $eparams = $elementModel->getParams(); /* load in once the element js class files * $$$ hugh - only needed getParent when we weren't saving changes to parent params to child * which we should now be doing ... and getParent() causes an extra table lookup for every child * element on the form. * $element = $elementModel->getParent(); */ $element = $elementModel->getElement(); if (!in_array($element->plugin, $aLoadedElementPlugins)) { /* $$$ hugh - certain elements, like fileupload, need to load different JS files * on a per-element basis, so as a test fix, I modified the fileupload's formJavaScriptClass to return false, * and test for that here, so as to not add it to aLoadedElementPlugins[]. The existing 'static' tests in * formJavascriptClass() should still prevent scripts being added twice. */ if ($elementModel->formJavascriptClass($srcs) !== false) { $aLoadedElementPlugins[] = $element->plugin; } } $eventMax = $groupModel->repeatTotal == 0 ? 1 : $groupModel->repeatTotal; for ($c = 0; $c < $eventMax; $c++) { $jsActions[] = $elementModel->getFormattedJSActions($jsControllerKey, $c); } } } $actions = trim(implode("\n", $jsActions)); $params = $model->getParams(); $listModel = $model->getlistModel(); $table = $listModel->getTable(); $form = $model->getForm(); FabrikHelperHTML::mocha(); $bkey = $model->isEditable() ? 'form_' . $model->getId() : 'details_' . $model->getId(); FabrikHelperHTML::tips('.hasTip', array(), "\$('{$bkey}')"); $key = FabrikString::safeColNameToArrayKey($table->db_primary_key); $this->get('FormCss'); $start_page = isset($model->sessionModel->last_page) ? (int) $model->sessionModel->last_page : 0; if ($start_page !== 0) { $app->enqueueMessage(JText::_('COM_FABRIK_RESTARTING_MUTLIPAGE_FORM')); } else { // Form submitted but fails validation - needs to go to the last page $start_page = JRequest::getInt('currentPage', 0); } $opts = new stdClass(); $opts->admin = $app->isAdmin(); $opts->ajax = $model->isAjax(); $opts->ajaxValidation = (bool) $params->get('ajax_validations'); $opts->primaryKey = $key; $opts->error = @$form->origerror; $opts->pages = $model->getPages(); $opts->plugins = array(); $opts->multipage_save = (int) $model->saveMultiPage(); $opts->editable = $model->isEditable(); $opts->start_page = $start_page; $opts->inlineMessage = (bool) $this->isMambot; // $$$rob dont int this as keys may be string $opts->rowid = (string) $model->_rowId; // 3.0 needed for ajax requests $opts->listid = (int) $this->get('ListModel')->getId(); $imgs = new stdClass(); $imgs->alert = FabrikHelperHTML::image('alert.png', 'form', $this->tmpl, '', true); $imgs->action_check = FabrikHelperHTML::image('action_check.png', 'form', $this->tmpl, '', true); $imgs->ajax_loader = FabrikHelperHTML::image('ajax-loader.gif', 'form', $this->tmpl, '', true); $opts->images = $imgs; // $$$rob if you are loading a table in a window from a form db join select record option // then we want to know the id of the window so we can set its showSpinner() method // 3.0 changed to fabrik_window_id (automatically appended by Fabrik.Window xhr request to load window data $opts->fabrik_window_id = JRequest::getVar('fabrik_window_id', ''); $opts->submitOnEnter = (bool) $params->get('submit_on_enter', false); // For editing groups with joined data and an empty joined record (ie no joined records) $hidden = array(); $maxRepeat = array(); $showMaxRepeats = array(); foreach ($this->groups as $g) { $hidden[$g->id] = $g->startHidden; $maxRepeat[$g->id] = $g->maxRepeat; $showMaxRepeats[$g->id] = $g->showMaxRepeats; } $opts->hiddenGroup = $hidden; $opts->maxRepeat = $maxRepeat; $opts->showMaxRepeats = $showMaxRepeats; // $$$ rob 26/04/2011 joomfish translations of password validation error messages // $opts->lang = FabrikWorker::getJoomfishLang(); // $$$ hugh adding these so calc element can easily find joined and repeated join groups // when it needs to add observe events ... don't ask ... LOL! $opts->join_group_ids = array(); $opts->group_repeats = array(); $opts->group_joins_ids = array(); $groups = $model->getGroupsHiarachy(); foreach ($groups as $groupModel) { if ($groupModel->getGroup()->is_join) { $joinParams = new JRegistry($groupModel->getJoinModel()->getJoin()->params); $opts->group_pk_ids[$groupModel->getGroup()->id] = FabrikString::safeColNameToArrayKey($joinParams->get('pk')); $opts->join_group_ids[$groupModel->getGroup()->join_id] = (int) $groupModel->getGroup()->id; $opts->group_join_ids[$groupModel->getGroup()->id] = (int) $groupModel->getGroup()->join_id; $opts->group_repeats[$groupModel->getGroup()->id] = $groupModel->canRepeat(); $opts->group_copy_element_values[$groupModel->getGroup()->id] = $groupModel->canCopyElementValues(); } } $opts = json_encode($opts); if (!FabrikHelperHTML::inAjaxLoadedPage()) { JText::script('COM_FABRIK_VALIDATING'); JText::script('COM_FABRIK_SUCCESS'); JText::script('COM_FABRIK_NO_REPEAT_GROUP_DATA'); JText::script('COM_FABRIK_VALIDATION_ERROR'); JText::script('COM_FABRIK_FORM_SAVED'); Jtext::script('COM_FABRIK_CONFIRM_DELETE'); } // $$$ rob dont declare as var $bkey, but rather assign to window, as if loaded via ajax window the function is wrapped // inside an anoymous function, and therefore $bkey wont be available as a global var in window $script = array(); $script[] = "window.{$bkey} = new FbForm(" . $model->getId() . ", {$opts});"; $script[] = "if(typeOf(Fabrik) !== 'null') {"; $script[] = "Fabrik.addBlock('{$bkey}', {$bkey});"; $script[] = "}"; // Instantaite js objects for each element $vstr = "\n"; $groups = $model->getGroupsHiarachy(); $script[] = "{$bkey}.addElements({"; $gs = array(); foreach ($groups as $groupModel) { $showGroup = $groupModel->getParams()->get('repeat_group_show_first'); if ($showGroup == -1 || $showGroup == 2 && $model->isEditable()) { // $$$ rob unpublished group so dont include the element js continue; } $aObjs = array(); $elementModels = $groupModel->getPublishedElements(); // $$$ rob if repeatTotal is 0 we still want to add the js objects as the els are only hidden $max = $groupModel->repeatTotal > 0 ? $groupModel->repeatTotal : 1; foreach ($elementModels as $elementModel) { $element = $elementModel->getElement(); if ($element->published == 0) { continue; } $fullName = $elementModel->getFullName(); $id = $elementModel->getHTMLId(); $elementModel->setEditable($model->isEditable()); // If the view is a form then we should always add the js as long as the element is editable or viewable // if the view is details then we should only add hte js if the element is viewable. if ($elementModel->canUse() && $model->isEditable() || $elementModel->canView()) { for ($c = 0; $c < $max; $c++) { // $$$ rob ensure that some js code has been returned otherwise dont add empty data to array $ref = trim($elementModel->elementJavascript($c)); if ($ref !== '') { $aObjs[] = $ref; } $validations = $elementModel->getValidations(); if (!empty($validations) && $elementModel->isEditable()) { $watchElements = $elementModel->getValidationWatchElements($c); foreach ($watchElements as $watchElement) { $vstr .= "{$bkey}.watchValidation('" . $watchElement['id'] . "', '" . $watchElement['triggerEvent'] . "');\n"; } } } } } $gs[] = $groupModel->getGroup()->id . ':[' . implode(",\n", $aObjs) . ']'; } $script[] = implode(", ", $gs); $script[] = '});'; $script[] = $actions; $script[] = $vstr; // Placholder test $script[] = "new Form.Placeholder('.fabrikForm input');"; $script[] = "function submit_form() {"; if (!empty($aWYSIWYGNames)) { jimport('joomla.html.editor'); $editor = JFactory::getEditor(); $script[] = $editor->save('label'); foreach ($aWYSIWYGNames as $parsedName) { $script[] = $editor->save($parsedName); } } $script[] = "\treturn false;"; $script[] = "}"; $script[] = "function submitbutton(button) {"; $script[] = "\tif (button==\"cancel\") {"; $script[] = "\t\tdocument.location = '" . JRoute::_('index.php?option=com_fabrik&task=viewTable&cid=' . $tableId) . "';"; $script[] = "\t}"; $script[] = "\tif (button == \"cancelShowForm\") {"; $script[] = "\t\treturn false;"; $script[] = "\t}"; $script[] = "}"; if (FabrikHelperHTML::inAjaxLoadedPage()) { $tipOpts = FabrikHelperHTML::tipOpts(); $script[] = "new FloatingTips('#" . $bkey . " .fabrikTip', " . json_encode($tipOpts) . ");"; } $pluginManager = FabrikWorker::getPluginManager(); $res = $pluginManager->runPlugins('onJSReady', $model); if (in_array(false, $res)) { return false; } $str = implode("\n", $script); $model->getCustomJsAction($srcs); $srcs[] = 'media/com_fabrik/js/encoder.js'; FabrikHelperHTML::script($srcs, $str); $pluginManager->runPlugins('onAfterJSLoad', $model); }