/** * append the form javascript into the document head * @param int table id */ function _addJavascript($tableId) { // $$$ rob ALWAYS load the calendar (so its avaible in ajax forms) FabrikHelperHTML::loadcalendar(); $app =& JFactory::getApplication(); $document =& JFactory::getDocument(); $model =& $this->getModel(); $aLoadedElementPlugins = array(); $jsActions = array(); $jsControllerKey = $model->_editable ? 'form_' . $model->getId() : 'details_' . $model->getId(); $allJsActions = $model->getJsActions(); FabrikHelperHTML::packageJS(); if (!defined('_JOS_FABRIK_FORMJS_INCLUDED')) { define('_JOS_FABRIK_FORMJS_INCLUDED', 1); FabrikHelperHTML::slimbox(); FabrikHelperHTML::script('form.js', 'media/com_fabrik/js/', true); FabrikHelperHTML::script('element.js', 'media/com_fabrik/js/', true); } $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(); } $eventMax = $groupModel->_repeatTotal == 0 ? 1 : $groupModel->_repeatTotal; for ($c = 0; $c < $eventMax; $c++) { $jsActions[] = $elementModel->getFormattedJSActions($allJsActions, $jsControllerKey, $c); } } } //new $actions = trim(implode("\n", $jsActions)); //end new $params =& $model->getParams(); $tableModel =& $model->getTableModel(); $table =& $tableModel->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 = "window.addEvent('domready', function() {\n"; $startJs = "window.addEvent('" . FabrikHelperHTML::useLoadEvent() . "', function() {\n"; $endJs = "});\n"; $start_page = isset($model->sessionModel->last_page) ? (int) $model->sessionModel->last_page : 0; if ($start_page !== 0) { $app->enqueueMessage(JText::_('RESTARTINGMUTLIPAGEFORM')); } 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->postMethod = $this->get('PostMethod'); $opts->ajaxValidation = $params->get('ajax_validations'); $opts->primaryKey = $key; $opts->liveSite = COM_FABRIK_LIVESITE; $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->mooversion = FabrikWorker::getMooVersion() == 1 ? 1.2 : 1.1; //$$$rob dont int this as keys may be string $opts->rowid = $model->_rowId; //$$$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->winid = JRequest::getVar('winid', ''); //for editing groups with joined data and an empty joined record (ie no joined records) $hidden = array(); $maxRepeat = array(); $showMaxRepeats = array(); $postedRepeatGroupCounts = (array) JRequest::getVar('fabrik_repeat_group'); foreach ($this->groups as $g) { $hidden[$g->id] = $g->startHidden; if (JArrayHelper::getValue($postedRepeatGroupCounts, $g->id, 1) == 0) { $hidden[$g->id] = true; } $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) { $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 = json_encode($opts); $lang = new stdClass(); $lang->validation_altered_content = JText::_('VALIDATION_HAS_ALTERED_CONTENT'); $lang->validating = JText::_('VALIDATING'); $lang->success = JText::_('SUCCESS'); $lang->nodata = JText::_('NOREPEATGROUPDATA'); $lang->validation_error = JText::_('VALIDATION_ERROR'); $lang->form_saved = JText::_('FORM_SAVED'); $lang->confirmDelete = JText::_('CONFIRMDELETE'); $lang = json_encode($lang); //$$$ 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 = "window.{$bkey} = new fabrikForm(" . $model->getId() . ", {$opts}, {$lang});\n"; $str .= "{$bkey}.addListenTo('table_" . $tableModel->getId() . "');\n"; $str .= "{$bkey}.addListenTo('form_" . $model->getId() . "');\n"; $str .= "if(\$type(window.oPackage) !== false) {\n"; $str .= "oPackage.addBlock('{$bkey}', {$bkey});\n"; $str .= "}\n"; //instantaite js objects for each element $groupstr = ''; $vstr = "\n"; $str .= "{$startJs}"; // $$$ 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->state == 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 .= "],"; $groupParams =& $groupModel->getParams(); $addJs = str_replace('"', "'", $groupParams->get('repeat_group_js_add')); $addJs = str_replace(array("\n", "\r"), "", $addJs); $delJs = str_replace('"', "'", $groupParams->get('repeat_group_js_delete')); $gdelJs = str_replace(array("\n", "\r"), "", $delJs); if ($delJs !== '') { $groupstr .= "{$bkey}.addGroupJS(" . $groupModel->getId() . ", 'delete', \"{$delJs}\");\n"; } if ($addJs !== '') { $groupstr .= "{$bkey}.addGroupJS(" . $groupModel->getId() . ", 'add', \"{$addJs}\");\n"; } } $str = FabrikString::rtrimword($str, ','); $str .= "});\n"; $str .= $groupstr; $str .= $actions; $str .= $vstr; $str .= $endJs; $str .= "function submit_form() {"; if (!empty($aWYSIWYGNames)) { jimport('joomla.html.editor'); $editor =& FabrikHelperHTML::getEditor(); $str .= $editor->save('label'); foreach ($aWYSIWYGNames as $parsedName) { $str .= $editor->save($parsedName); } } $str .= "\n\t\t\treturn false;\n\t\t}\n\n\t\tfunction submitbutton(button) {\n\t\t\tif (button==\"cancel\") {\n\t\t\t\tdocument.location = '" . JRoute::_('index.php?option=com_fabrik&task=viewTable&cid=' . $tableId) . "';\n\t\t\t}\n\t\t\tif (button == \"cancelShowForm\") {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n"; FabrikHelperHTML::addScriptDeclaration($str); $pluginManager =& $model->getPluginManager(); $pluginManager->runPlugins('onAfterJSLoad', $model); FabrikHelperHTML::mootools(); }