Ejemplo n.º 1
0
 function __construct($original, $translation, $fieldname, $fields = null)
 {
     $options = array();
     $options['option'] = 'com_content';
     $options['ident'] = 'article_id';
     $options['state_ident'] = 'article.id';
     $options['model_item'] = 'JFContentModelItem';
     $options['trans_params'] = 'JFContentParams';
     $options['params_fieldset'] = 'attribs';
     return $this->setup($original, $translation, $fieldname, $fields, $options);
     /////////////////////////////////////////////////////////////////////////
     parent::__construct($original, $translation, $fieldname, $fields);
     $lang = JFactory::getLanguage();
     $lang->load("com_content", JPATH_ADMINISTRATOR);
     $cid = JRequest::getVar('cid', array(0));
     $oldcid = $cid;
     $translation_id = 0;
     if (strpos($cid[0], '|') !== false) {
         list($translation_id, $contentid, $language_id) = explode('|', $cid[0]);
     }
     JRequest::setVar("cid", array($contentid));
     JRequest::setVar("edit", true);
     // model's populate state method assumes the id is in the request object!
     $oldid = JRequest::getInt("article_id", 0);
     // Take care of the name of the id for the item
     JRequest::setVar("article_id", $contentid);
     JLoader::import('models.JFContentModelItem', JOOMFISH_ADMINPATH);
     $this->orig_contentModelItem = new JFContentModelItem();
     // Get The Original form
     // JRequest does NOT this for us in articles!!
     $this->orig_contentModelItem->setState('article.id', $contentid);
     $jfcontentModelForm = $this->orig_contentModelItem->getForm(array(), true, false);
     if ($original != "") {
         $original = json_decode($original);
     }
     // NOW GET THE TRANSLATION - IF AVAILABLE
     $this->trans_contentModelItem = new JFContentModelItem();
     $this->trans_contentModelItem->setState('article.id', $translation_id);
     if ($translation != "") {
         $translation = json_decode($translation);
     }
     $translationcontentModelForm = $this->trans_contentModelItem->getForm(array(), true, true);
     $transfreeform = JFForm::getInstance($translationcontentModelForm);
     //$xml = $freeform->get('xml');
     foreach ($jfcontentModelForm->getFieldsets('attribs') as $name => $ffff) {
         if ($name == 'basic-limited') {
             continue;
         }
         //$attrfieldset = $jfcontentModelForm->getFieldset($name);
         $fieldset = $jfcontentModelForm->getFieldset($name);
         $result = $transfreeform->xml->xpath('//*/fieldset[@name="' . $name . '"]');
         foreach ($fieldset as $field) {
             $freefield = JFFormField::getInstance($field);
             if ($freefield->type == 'Spacer') {
                 continue;
             }
             //$jfcontentModelForm->setFieldAttribute($freefield->name, 'readonly', 'true');
             //foreach ($xml->fields as $xmlfield) {
             //$result= & $xmlfield->xpath('//*[@name="attribs"]');
             $freefieldname = $freefield->get('fieldname');
             $freefield->set('fieldname', $freefieldname . '_orig');
             $freefield->set('name', $freefield->getName($freefieldname . '_orig'));
             $freefield->set('label', 'Original ' . $freefield->get('label'));
             $freeelement = $freefield->get('element')->asXML();
             $freeelement = new JXMLElement($freeelement);
             //$freeelement['label']		= 'Original'.$freeelement['label'];
             //$freeelement['fieldname'] 	= 'orig_'.$freeelement['fieldname'];
             $newname = $freeelement['name'] . '_orig';
             if (isset($original->{$freeelement}['name'])) {
                 $original->{$newname} = $original->{$freeelement}['name'];
                 unset($original->{$freeelement}['name']);
             }
             $freeelement['name'] = $newname;
             $freeelement['label'] = JText::_('ORIGINAL') . ' ' . JText::_($freeelement['label']);
             $freeelement['readonly'] = 'true';
             $freeelement['disabled'] = 'true';
             $freeelement['filter'] = 'UNSET';
             //$result = $freeform->findField($name);
             if ($result) {
                 $transfreeform->addNode($result[0], $freeelement);
             }
             $transfreeform->setFieldAttribute($freeelement['name'], 'filter', 'unset');
             $transfreeform->setFieldAttribute($freeelement['name'], 'readonly', 'true');
             $transfreeform->setFieldAttribute($freeelement['name'], 'disabled', 'true');
             $transfreeform->setValue($freeelement['name'], $name, $freefield->get('value'));
         }
         $fieldarray = array();
         foreach ($result[0]->field as $fld) {
             $fxml = $fld->asXML();
             $fieldarray[] = new JXMLElement($fxml);
         }
         // we need to unset in reverse as indexes keep changing otherwise
         for ($c = count($result[0]->field); $c >= 0; $c--) {
             /*$dom = dom_import_simplexml($fldr);
             		$dom->parentNode->removeChild($dom);*/
             unset($result[0]->field[$c]);
         }
         $this->_nodesort($fieldarray, SORT_ASC);
         $parent = dom_import_simplexml($result[0]);
         foreach ($fieldarray as $far) {
             //$result[0]->addChild('field',$far);
             $child = dom_import_simplexml($far);
             // Import the <cat> into the dictionary document
             $child = $parent->ownerDocument->importNode($child, TRUE);
             $parent->appendChild($child);
         }
     }
     //$freeform->set('xml', $xml);
     if (isset($translation->jfrequest)) {
         $transfreeform->bind(array("attribs" => $translation, "request" => $translation->jfrequest));
         $transfreeform->bind(array("attribs" => $original));
     } else {
         $transfreeform->bind(array("attribs" => $translation));
         $transfreeform->bind(array("attribs" => $original));
     }
     // reset old values in REQUEST array
     $cid = $oldcid;
     JRequest::setVar('cid', $cid);
     JRequest::setVar("article_id", $oldid);
     //	$this->origparams = new JFContentParams( $jfcontentModelForm);
     $this->transparams = new JFContentParams($transfreeform);
 }
Ejemplo n.º 2
0
 public function setup($original, $translation, $fieldname, $fields = null, $options = array())
 {
     parent::__construct($original, $translation, $fieldname, $fields);
     $lang = JFactory::getLanguage();
     $lang->load($options['option'], JPATH_ADMINISTRATOR);
     $cid = JRequest::getVar('cid', array(0));
     $oldcid = $cid;
     $translation_id = 0;
     if (strpos($cid[0], '|') !== false) {
         list($translation_id, $contentid, $language_id) = explode('|', $cid[0]);
     }
     JRequest::setVar("cid", array($contentid));
     JRequest::setVar("edit", true);
     // model's populate state method assumes the id is in the request object!
     $oldid = JRequest::getInt($options['ident'], 0);
     // Take care of the name of the id for the item
     JRequest::setVar($options['ident'], $contentid);
     JLoader::import('models.' . $options['model_item'], JOOMFISH_ADMINPATH);
     //$this->orig_ModelItem = new $options['model_item']();
     // Get The Original form
     // JRequest does NOT this for us in articles!!
     //$this->orig_ModelItem->setState($options['state_ident'],$contentid);
     //$jfcontentModelForm = $this->orig_ModelItem->getForm();
     if ($original != "" && is_string($original)) {
         $original = json_decode($original);
     }
     // NOW GET THE TRANSLATION - IF AVAILABLE
     $this->trans_ModelItem = new $options['model_item']();
     $this->trans_ModelItem->setState($options['state_ident'], $translation_id);
     if ($translation != "" && is_string($translation)) {
         $translation = json_decode($translation);
     }
     $translationcontentModelForm = $this->trans_ModelItem->getForm();
     $transfreeform = JFForm::getInstance($translationcontentModelForm);
     // Create second instance of the same form to avoid foreach loop problems
     $xml = $transfreeform->xml->asXML();
     $name = $transfreeform->getName();
     $originalModelForm = JForm::getInstance($name . '.original', $xml, $transfreeform->options);
     // Loop trough original form, change original form names and add each field from parameters fieldsets to the translated form with _orig suffix
     // another option here would be to keep 2 forms separated and do double rendering later
     foreach ($originalModelForm->getFieldsets($options['params_fieldset']) as $name => $ffff) {
         if ($name == 'basic-limited') {
             continue;
         }
         $fieldset = $originalModelForm->getFieldset($name);
         $result = $transfreeform->xml->xpath('//*/fieldset[@name="' . $name . '"]');
         foreach ($fieldset as $field) {
             $freefield = JFFormField::getInstance($field);
             //if($freefield->type == 'Spacer') continue;
             $freefieldname = $freefield->get('fieldname');
             $freefield->set('fieldname', $freefieldname . '_orig');
             $freefield->set('name', $freefield->getName($freefieldname . '_orig'));
             $freefield->set('label', 'Original ' . $freefield->get('label'));
             // copy field as XML to break reference, then change its properties
             // @todo check whether this is still necessary as we are now breaking reference when we copy form
             $freeelement = $freefield->get('element')->asXML();
             $freeelement = new JXMLElement($freeelement);
             $newname = $freeelement['name'] . '_orig';
             // also change $original objects property names to match changed names in the form
             if (isset($original->{$freeelement}['name'])) {
                 $original->{$newname} = $original->{$freeelement}['name'];
                 unset($original->{$freeelement}['name']);
             }
             $freeelement['name'] = $newname;
             //$freeelement['label']		= JText::_('ORIGINAL').' '. JText::_($freeelement['label']);
             $freeelement['readonly'] = 'true';
             $freeelement['disabled'] = 'true';
             $freeelement['filter'] = 'UNSET';
             if ($result) {
                 $transfreeform->addNode($result[0], $freeelement);
             }
             $transfreeform->setFieldAttribute($freeelement['name'], 'filter', 'unset');
             $transfreeform->setFieldAttribute($freeelement['name'], 'readonly', 'true');
             $transfreeform->setFieldAttribute($freeelement['name'], 'disabled', 'true');
             $transfreeform->setValue($freeelement['name'], $name, $freefield->get('value'));
         }
         // sorts fields to keep togehter originals and transaltions, not necessary in 2 column seting
         /*$fieldarray = array ();
         	
         			foreach ($result[0]->field AS $fld) {
         				$fxml = $fld->asXML();
         				$fieldarray[] = new JXMLElement($fxml);
         					
         			}
         	
         			// we need to unset in reverse as indexes keep changing otherwise
         			for ($c = count($result[0]->field); $c >=0; $c--) {
         				unset($result[0]->field[$c]);
         			}
         	
         	
         			$this->_nodesort($fieldarray, SORT_ASC);
         			
         			$parent = dom_import_simplexml($result[0]);
         			foreach ($fieldarray AS $far) {
         				$child = dom_import_simplexml($far);
         				$child  = $parent->ownerDocument->importNode($child, TRUE);
         				$parent->appendChild($child);
         			}*/
     }
     if (isset($translation->jfrequest)) {
         $transfreeform->bind(array($options['params_fieldset'] => $translation, "request" => $translation->jfrequest));
         $transfreeform->bind(array($options['params_fieldset'] => $original));
     } else {
         $transfreeform->bind(array($options['params_fieldset'] => $translation));
         $transfreeform->bind(array($options['params_fieldset'] => $original));
     }
     // reset old values in REQUEST array
     $cid = $oldcid;
     JRequest::setVar('cid', $cid);
     JRequest::setVar($options['ident'], $oldid);
     //	$this->origparams = new JFContentParams( $jfcontentModelForm);
     $this->transparams = new $options['trans_params']($transfreeform);
 }