示例#1
0
 /**
  * Append the form javascript into the document head
  *
  * @param   int $listId table id
  *
  * @return  void|boolean
  */
 protected function _addJavascript($listId)
 {
     $pluginManager = FabrikWorker::getPluginManager();
     /** @var FabrikFEModelForm $model */
     $model = $this->getModel();
     $aLoadedElementPlugins = array();
     $jsActions = array();
     $bKey = $model->jsKey();
     $srcs = FabrikHelperHTML::framework();
     $shim = array();
     if (!defined('_JOS_FABRIK_FORMJS_INCLUDED')) {
         define('_JOS_FABRIK_FORMJS_INCLUDED', 1);
         FabrikHelperHTML::slimbox();
         $dep = new stdClass();
         $dep->deps = array('fab/element', 'lib/form_placeholder/Form.Placeholder', 'fab/encoder');
         $shim['fabrik/form'] = $dep;
         $deps = new stdClass();
         $deps->deps = array('fab/fabrik', 'fab/element', 'fab/form-submit');
         $framework['fab/elementlist'] = $deps;
         $srcs[] = 'media/com_fabrik/js/lib/form_placeholder/Form.Placeholder.js';
         FabrikHelperHTML::addToFrameWork($srcs, 'media/com_fabrik/js/form');
         FabrikHelperHTML::addToFrameWork($srcs, 'media/com_fabrik/js/form-submit');
         FabrikHelperHTML::addToFrameWork($srcs, 'media/com_fabrik/js/element');
     }
     $aWYSIWYGNames = array();
     // $$$ hugh - yet 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 && $elementModel->canUse() && $model->isEditable()) {
                 $aWYSIWYGNames[] = $res;
             }
             // Load in once the element js class files
             $element = $elementModel->getElement();
             if (!in_array($element->plugin, $aLoadedElementPlugins)) {
                 /* $$$ hugh - certain elements, like file-upload, need to load different JS files
                  * on a per-element basis, so as a test fix, I modified the file-upload'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, '', $shim) !== false) {
                     $aLoadedElementPlugins[] = $element->plugin;
                 }
             }
             $eventMax = $groupModel->repeatTotal == 0 ? 1 : $groupModel->repeatTotal;
             for ($c = 0; $c < $eventMax; $c++) {
                 $jsAct = $elementModel->getFormattedJSActions($bKey, $c);
                 if (!empty($jsAct)) {
                     $jsActions[] = $jsAct;
                 }
             }
         }
     }
     FabrikHelperHTML::iniRequireJS($shim);
     $actions = trim(implode("\n", $jsActions));
     FabrikHelperHTML::windows('a.fabrikWin');
     FabrikHelperHTML::tips('.hasTip', array(), "\$('{$bKey}')");
     $model->getFormCss();
     $opts = $this->jsOpts();
     $model->jsOpts = $opts;
     $pluginManager->runPlugins('onJSOpts', $model);
     $opts = json_encode($model->jsOpts);
     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_CONFIRM_DELETE_1');
     }
     JText::script('COM_FABRIK_FORM_SAVED');
     // $$$ rob don't declare as var $bKey, but rather assign to window, as if loaded via ajax window the function is wrapped
     // inside an anonymous function, and therefore $bKey wont be available as a global var in window
     $script = array();
     $script[] = "\t\tvar {$bKey} = Fabrik.form('{$bKey}', " . $model->getId() . ", {$opts});";
     // Instantiate js objects for each element
     $vstr = "\n";
     $groups = $model->getGroupsHiarachy();
     $script[] = "\tFabrik.blocks['{$bKey}'].addElements(";
     $groupedJs = new stdClass();
     foreach ($groups as $groupModel) {
         $groupId = $groupModel->getGroup()->id;
         $groupedJs->{$groupId} = array();
         if (!$groupModel->canView('form')) {
             continue;
         }
         $elementJs = 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;
             }
             // 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 the js if the element is viewable.
             if ($elementModel->canUse() && $model->isEditable() || $elementModel->canView()) {
                 for ($c = 0; $c < $max; $c++) {
                     $ref = $elementModel->elementJavascript($c);
                     if (!empty($ref)) {
                         $elementJs[] = $ref;
                     }
                     $validations = $elementModel->validator->findAll();
                     if (!empty($validations) && $elementModel->isEditable()) {
                         $watchElements = $elementModel->getValidationWatchElements($c);
                         foreach ($watchElements as $watchElement) {
                             $vstr .= "\tFabrik.blocks['{$bKey}'].watchValidation('" . $watchElement['id'] . "', '" . $watchElement['triggerEvent'] . "');\n";
                         }
                     }
                 }
             }
         }
         $groupedJs->{$groupId} = $elementJs;
     }
     $script[] = json_encode($groupedJs);
     $script[] = "\t);";
     $script[] = $actions;
     $script[] = $vstr;
     // Placeholder
     $script[] = "\tnew Form.Placeholder('.fabrikForm input');";
     $this->_addJavascriptSumbit($script, $listId, $aWYSIWYGNames);
     if (FabrikHelperHTML::inAjaxLoadedPage()) {
         $tipOpts = FabrikHelperHTML::tipOpts();
         $script[] = "new FloatingTips('#" . $bKey . " .fabrikTip', " . json_encode($tipOpts) . ");";
     }
     $res = $pluginManager->runPlugins('onJSReady', $model);
     if (in_array(false, $res)) {
         return false;
     }
     $str = implode("\n", $script);
     $model->getCustomJsAction($srcs);
     $pluginManager->runPlugins('onAfterJSLoad', $model);
     // 3.1 call form js plugin code within main require method
     $srcs = array_merge($srcs, $model->formPluginShim);
     $str .= $model->formPluginJS;
     FabrikHelperHTML::script($srcs, $str);
 }
示例#2
0
 /**
  * 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);
 }