Ejemplo n.º 1
0
 /**
  * Method to get a form object.
  *
  * @param	string		$xml		The form data. Can be XML string if file flag is set to false.
  * @param	array		$options	Optional array of parameters.
  * @param	boolean		$clear		Optional argument to force load a new form.
  * @return	mixed		JForm object on success, False on error.
  */
 public function &getForm($xml = null, $name = 'form', $options = array(), $clear = false)
 {
     if ($xml === null) {
         $xml = strtolower($this->getName());
     }
     // Handle the optional arguments.
     $options['array'] = array_key_exists('array', $options) ? $options['array'] : 'jform';
     $options['file'] = array_key_exists('file', $options) ? $options['file'] : true;
     $options['event'] = array_key_exists('event', $options) ? $options['event'] : null;
     $options['group'] = array_key_exists('group', $options) ? $options['group'] : null;
     // Create a signature hash.
     $hash = md5($xml . serialize($options));
     // Check if we can use a previously loaded form.
     if (isset($this->_forms[$hash]) && !$clear) {
         return $this->_forms[$hash];
     }
     // Get the form.
     EForm::addFormPath(JPATH_COMPONENT_ADMINISTRATOR . DS . 'models' . DS . 'forms');
     EForm::addFieldPath(JPATH_COMPONENT_ADMINISTRATOR . DS . 'models' . DS . 'fields');
     EFormValidator::addRulePath(JPATH_COMPONENT_ADMINISTRATOR . DS . 'models' . DS . 'rules');
     $app = JFactory::getApplication();
     if ($app->isSite()) {
         EForm::addFormPath(JPATH_COMPONENT_SITE . DS . 'models' . DS . 'forms');
         EForm::addFieldPath(JPATH_COMPONENT_SITE . DS . 'models' . DS . 'fields');
         EFormValidator::addRulePath(JPATH_COMPONENT_SITE . DS . 'models' . DS . 'rules');
     }
     $form =& EForm::getInstance($xml, $name, $options['file'], $options);
     // Check for an error.
     if (JError::isError($form)) {
         $this->setError($form->getMessage());
         $false = false;
         return $form;
     }
     // Look for an event to fire.
     if ($options['event'] !== null) {
         // Get the dispatcher.
         $dispatcher =& JDispatcher::getInstance();
         // Load an optional plugin group.
         if ($options['group'] !== null) {
             JPluginHelper::importPlugin($options['group']);
         }
         // Trigger the form preparation event.
         $results = $dispatcher->trigger($options['event'], array($form->getName(), $form));
         // Check for errors encountered while preparing the form.
         if (count($results) && in_array(false, $results, true)) {
             // Get the last error.
             $error = $dispatcher->getError();
             // Convert to a JException if necessary.
             if (!JError::isError($error)) {
                 $error = new JException($error, 500);
             }
             return $error;
         }
     }
     // Store the form for later.
     $this->_forms[$hash] = $form;
     return $form;
 }
Ejemplo n.º 2
0
 /**
  * Method to load a form field object.
  *
  * @param	string		$type		The field type.
  * @param	boolean		$new		Flag to toggle whether we should get a new instance of the object.
  * @return	mixed		Field object on success, false otherwise.
  * @since	1.6
  */
 public function &loadFieldType($type, $new = true)
 {
     $false = false;
     $key = md5($type);
     $class = 'JFormField' . ucfirst($type);
     // Return the field object if it already exists and we don't need a new one.
     if (isset($this->_fieldTypes[$key]) && $new === false) {
         return $this->_fieldTypes[$key];
     }
     if (!class_exists('JFormFieldList')) {
         require_once dirname(__FILE__) . DS . 'fields' . DS . 'list.php';
     }
     if (!class_exists('JFormFieldText')) {
         require_once dirname(__FILE__) . DS . 'fields' . DS . 'text.php';
     }
     if (!class_exists($class)) {
         $paths = EForm::addFieldPath();
         // If the type is complex, add the base type to the paths.
         if ($pos = strpos($type, '_')) {
             // Add the complex type prefix to the paths.
             for ($i = 0, $n = count($paths); $i < $n; $i++) {
                 // Derive the new path.
                 $path = $paths[$i] . DS . strtolower(substr($type, 0, $pos));
                 // If the path does not exist, add it.
                 if (!in_array($path, $paths)) {
                     array_unshift($paths, $path);
                 }
             }
             // Break off the end of the complex type.
             $type = substr($type, $pos + 1);
         }
         // Try to find the field file.
         jimport('joomla.filesystem.path');
         if ($file = JPath::find($paths, strtolower($type) . '.php')) {
             require_once $file;
         } else {
             return $false;
         }
         // Check once and for all if the class exists.
         if (!class_exists($class)) {
             return $false;
         }
     }
     // Instantiate a new field object.
     $this->_fieldTypes[$key] = new $class($this);
     return $this->_fieldTypes[$key];
 }