Esempio n. 1
0
 /**
  * Method to get a form object.
  *
  * @param   string          $name       The name of the form.
  * @param   string          $source     The form filename (e.g. form.browse)
  * @param   array           $options    Optional array of options for the form creation.
  * @param   boolean         $clear      Optional argument to force load a new form.
  * @param   bool|string     $xpath      An optional xpath to search for the fields.
  *
  * @return  mixed  FOFForm object on success, False on error.
  *
  * @see     FOFForm
  * @since   2.0
  */
 protected function loadForm($name, $source, $options = array(), $clear = false, $xpath = false)
 {
     // Handle the optional arguments.
     $options['control'] = isset($options['control']) ? $options['control'] : false;
     // Create a signature hash.
     $hash = md5($source . serialize($options));
     // Check if we can use a previously loaded form.
     if (isset($this->_forms[$hash]) && !$clear) {
         return $this->_forms[$hash];
     }
     // Try to find the name and path of the form to load
     $formFilename = $this->findFormFilename($source);
     // No form found? Quit!
     if ($formFilename === false) {
         return false;
     }
     // Set up the form name and path
     $source = basename($formFilename, '.xml');
     FOFForm::addFormPath(dirname($formFilename));
     // Set up field paths
     $option = $this->input->getCmd('option', 'com_foobar');
     $componentPaths = FOFPlatform::getInstance()->getComponentBaseDirs($option);
     $view = $this->name;
     $file_root = $componentPaths['main'];
     $alt_file_root = $componentPaths['alt'];
     FOFForm::addFieldPath($file_root . '/fields');
     FOFForm::addFieldPath($file_root . '/models/fields');
     FOFForm::addFieldPath($alt_file_root . '/fields');
     FOFForm::addFieldPath($alt_file_root . '/models/fields');
     FOFForm::addHeaderPath($file_root . '/fields/header');
     FOFForm::addHeaderPath($file_root . '/models/fields/header');
     FOFForm::addHeaderPath($alt_file_root . '/fields/header');
     FOFForm::addHeaderPath($alt_file_root . '/models/fields/header');
     // Get the form.
     try {
         $form = FOFForm::getInstance($name, $source, $options, false, $xpath);
         if (isset($options['load_data']) && $options['load_data']) {
             // Get the data for the form.
             $data = $this->loadFormData();
         } else {
             $data = array();
         }
         // Allows data and form manipulation before preprocessing the form
         $this->onBeforePreprocessForm($form, $data);
         // Allow for additional modification of the form, and events to be triggered.
         // We pass the data because plugins may require it.
         $this->preprocessForm($form, $data);
         // Allows data and form manipulation After preprocessing the form
         $this->onAfterPreprocessForm($form, $data);
         // Load the data into the form after the plugins have operated.
         $form->bind($data);
     } catch (Exception $e) {
         // The above try-catch statement will catch EVERYTHING, even PhpUnit exceptions while testing
         if (stripos(get_class($e), 'phpunit') !== false) {
             throw $e;
         } else {
             $this->setError($e->getMessage());
             return false;
         }
     }
     // Store the form for later.
     $this->_forms[$hash] = $form;
     return $form;
 }
Esempio n. 2
0
 /**
  * Method to get a form object.
  *
  * @param   string   $name     The name of the form.
  * @param   string   $source   The form source. Can be XML string if file flag is set to false.
  * @param   array    $options  Optional array of options for the form creation.
  * @param   boolean  $clear    Optional argument to force load a new form.
  * @param   string   $xpath    An optional xpath to search for the fields.
  *
  * @return  mixed  FOFForm object on success, False on error.
  *
  * @see     FOFForm
  * @since   2.0
  */
 protected function loadForm($name, $source = null, $options = array(), $clear = false, $xpath = false)
 {
     // Handle the optional arguments.
     $options['control'] = JArrayHelper::getValue($options, 'control', false);
     // Create a signature hash.
     $hash = md5($source . serialize($options));
     // Check if we can use a previously loaded form.
     if (isset($this->_forms[$hash]) && !$clear) {
         return $this->_forms[$hash];
     }
     // Try to find the name and path of the form to load
     $formFilename = $this->findFormFilename($source);
     // No form found? Quit!
     if ($formFilename === false) {
         return false;
     }
     // Set up the form name and path
     $source = basename($formFilename, '.xml');
     FOFForm::addFormPath(dirname($formFilename));
     // Set up field paths
     list($isCli, $isAdmin) = FOFDispatcher::isCliAdmin();
     $option = $this->input->getCmd('option', 'com_foobar');
     $view = $this->input->getCmd('view', 'cpanels');
     $file_root = $isAdmin ? JPATH_ADMINISTRATOR : JPATH_SITE;
     $file_root .= '/components/' . $option;
     $alt_file_root = $isAdmin ? JPATH_SITE : JPATH_ADMINISTRATOR;
     $alt_file_root .= '/components/' . $option;
     FOFForm::addFieldPath($file_root . '/fields');
     FOFForm::addFieldPath($file_root . '/models/fields');
     FOFForm::addFieldPath($alt_file_root . '/fields');
     FOFForm::addFieldPath($alt_file_root . '/models/fields');
     FOFForm::addHeaderPath($file_root . '/fields/header');
     FOFForm::addHeaderPath($file_root . '/models/fields/header');
     FOFForm::addHeaderPath($alt_file_root . '/fields/header');
     FOFForm::addHeaderPath($alt_file_root . '/models/fields/header');
     // Get the form.
     try {
         $form = FOFForm::getInstance($name, $source, $options, false, $xpath);
         if (isset($options['load_data']) && $options['load_data']) {
             // Get the data for the form.
             $data = $this->loadFormData();
         } else {
             $data = array();
         }
         // Allow for additional modification of the form, and events to be triggered.
         // We pass the data because plugins may require it.
         $this->preprocessForm($form, $data);
         // Load the data into the form after the plugins have operated.
         $form->bind($data);
     } catch (Exception $e) {
         $this->setError($e->getMessage());
         return false;
     }
     // Store the form for later.
     $this->_forms[$hash] = $form;
     return $form;
 }