/** * Draws the html form element * * @param array $data to pre-populate element with * @param int $repeatCounter repeat group counter * * @return string elements html */ public function render($data, $repeatCounter = 0) { $element = $this->getElement(); $name = $this->getHTMLName($repeatCounter); $htmlId = $this->getHTMLId($repeatCounter); $id = $htmlId; $params = $this->getParams(); /** * $$$ rob - if embedding a form inside a details view then rowid is true (for the detailed view) but we are still showing a new form * so take a look at the element form's rowId and not app input */ $rowId = $this->getFormModel()->rowId; /** * @TODO when editing a form with joined repeat group the rowid will be set but * the record is in fact new */ //if ($params->get('update_on_edit') || !$rowId || ($this->inRepeatGroup && $this->_inJoin && $this->_repeatGroupTotal == $repeatCounter)) if ($params->get('update_on_edit') || !$rowId) { // Set user to logged in user if ($this->isEditable()) { $user = $this->user; } else { $userId = (int) $this->getValue($data, $repeatCounter, array('raw' => 1)); // On failed validation value is 1 - user ids are always more than that so don't load userid=1 otherwise an error is generated $user = $userId <= 1 ? $this->user : JFactory::getUser($userId); } } else { /** * $$$ hugh - this is blowing away the userid, as $element->default is empty at this point * so for now I changed it to the $data value * keep previous user * $user = JFactory::getUser((int) $element->default); */ // $$$ hugh ... what a mess ... of course if it's a new form, $data doesn't exist ... if (empty($data)) { $user = $this->user; } else { if ($this->inDetailedView) { $id = preg_replace('#_ro$#', '_raw', $id); } else { /** * $$$ rob 31/07/2011 not sure this is right - causes js error when field is hidden in form * $$$ hugh 10/31/2011 - but if we don't do it, $id is the label not the value (like 'username') * so wrong uid is written to form, and wipes out real ID when form is submitted. * OK, problem was we were using $id further on as the html ID, so if we added _raw, element * on form had wrong ID. Added $htmlId above, to use as (duh) html ID instead of $id. */ if (!strstr($id, '_raw') && array_key_exists($id . '_raw', $data)) { $id .= '_raw'; } } $id = FArrayHelper::getValue($data, $id, ''); if ($id === '') { $id = $this->getValue($data, $repeatCounter, array('raw' => 1)); } /* * After a failed validation, it may be JSON, and urlencoded, like ["94"] * Or it may be an array with JSON and or urlencode and or ... yada yada ... who the f*ck knows * So let's just cover all the bases, shall we? */ $id = is_array($id) ? $id[0] : $id; $id = html_entity_decode($id); if (FabrikWorker::isJSON($id)) { $id = FabrikWorker::JSONtoData($id, true); } $id = is_array($id) ? $id[0] : $id; /* $$$ hugh - hmmm, might not necessarily be a new row. So corner case check for * editing a row, where user element is not set yet, and 'update on edit' is No. */ if ($rowId && empty($id) && !$params->get('update_on_edit')) { $user = JFactory::getUser(0); } else { $user = $id === '' ? $this->user : JFactory::getUser((int) $id); } } } $displayParam = $this->getLabelOrConcatVal(); $layout = $this->getLayout('form'); $layoutData = new stdClass(); $layoutData->inJDb = $this->inJDb(); $layoutData->name = $name; $layoutData->id = $htmlId; $layoutData->isEditable = $this->isEditable(); $layoutData->hidden = $element->hidden; $layoutData->input = parent::render($data, $repeatCounter); $layoutData->readOnly = is_a($user, 'JUser') ? $user->get($displayParam) : ''; $layoutData->value = is_a($user, 'JUser') ? $user->get('id') : ''; return $layout->render($layoutData); }
/** * Draws the html form element * * @param array $data to pre-populate element with * @param int $repeatCounter repeat group counter * * @return string elements html */ public function render($data, $repeatCounter = 0) { $element = $this->getElement(); $name = $this->getHTMLName($repeatCounter); $html_id = $this->getHTMLId($repeatCounter); $id = $html_id; $params = $this->getParams(); /** * $$$ rob - if embedding a form inside a details view then rowid is true (for the detailed view) but we are still showing a new form * so take a look at the element form's rowId and not app input */ $rowid = $this->getForm()->rowId; /** * @TODO when editing a form with joined repeat group the rowid will be set but * the record is in fact new */ if ($params->get('update_on_edit') || !$rowid || $this->inRepeatGroup && $this->_inJoin && $this->_repeatGroupTotal == $repeatCounter) { // Set user to logged in user if ($this->isEditable()) { $user = JFactory::getUser(); } else { $userid = (int) $this->getValue($data, $repeatCounter); // On failed validation value is 1 - user ids are always more than that so don't load userid=1 otherwise an error is generated $user = $userid <= 1 ? JFactory::getUser() : JFactory::getUser($userid); } } else { /** * $$$ hugh - this is blowing away the userid, as $element->default is empty at this point * so for now I changed it to the $data value * keep previous user * $user = JFactory::getUser((int) $element->default); */ // $$$ hugh ... what a mess ... of course if it's a new form, $data doesn't exist ... if (empty($data)) { $user = JFactory::getUser(); } else { if ($this->inDetailedView) { $id = preg_replace('#_ro$#', '_raw', $id); } else { /** * $$$ rob 31/07/2011 not sure this is right - causes js error when field is hidden in form * $$$ hugh 10/31/2011 - but if we don't do it, $id is the label not the value (like 'username') * so wrong uid is written to form, and wipes out real ID when form is submitted. * OK, problem was we were using $id further on as the html ID, so if we added _raw, element * on form had wrong ID. Added $html_id above, to use as (duh) html ID instead of $id. */ if (!strstr($id, '_raw') && array_key_exists($id . '_raw', $data)) { $id .= '_raw'; } } $id = JArrayHelper::getValue($data, $id, ''); if ($id === '') { $id = $this->getValue($data, $repeatCounter); } $id = is_array($id) ? $id[0] : $id; /* $$$ hugh - hmmm, might not necessarily be a new row. So corner case check for * editing a row, where user element is not set yet, and 'update on edit' is No. */ if ($rowid && empty($id) && !$params->get('update_on_edit')) { $user = JFactory::getUser(0); } else { $user = $id === '' ? JFactory::getUser() : JFactory::getUser((int) $id); } } } /** * If the table database is not the same as the joomla database then * we should simply return a hidden field with the user id in it. */ if (!$this->inJDb()) { return $this->getHiddenField($name, $user->get('id'), $html_id); } $str = ''; if ($this->isEditable()) { $value = is_object($user) ? $user->get('id') : ''; if ($element->hidden) { $str = $this->getHiddenField($name, $value, $html_id); } else { $str = '<div class="input-append">'; $str .= parent::render($data, $repeatCounter); $str .= '<span class="add-on"><span class="icon-user"></span></span>'; $str .= '</div>'; } } else { $displayParam = $this->getLabelOrConcatVal(); if (is_a($user, 'JUser')) { $str = $user->get($displayParam); } else { JError::raiseWarning(E_NOTICE, "Didn't load for {$element->default}"); } } return $str; }