/** * This function displays the newsletter form on the user profile * * @param JForm $form - the user form * @param array $data - the user data * * @return bool */ public function edit($form, $data) { $appl = JFactory::getApplication(); $subscriptionData = CmcHelperUsers::getSubscription($data->email, $this->params->get('listid')); $renderer = CmcHelperXmlbuilder::getInstance($this->params); // Render Content $html = $renderer->build(); if ($appl->isSite()) { CompojoomHtmlBehavior::jquery(); JHtml::script('media/plg_user_cmc/js/cmc.js'); } // Inject fields into the form $form->load($html, false); if ($subscriptionData) { $form->setFieldAttribute('newsletter', 'checked', 'checked', 'cmc'); $form->bind(CmcHelperSubscription::convertMergesToFormData($subscriptionData->merges)); } }
/** * Method to get the record form. * * @param array $data An optional array of data for the form to interogate. * @param boolean $loadData True if the form is to load its own data (default case), false if not. * * @return JForm A JForm object on success, false on failure */ public function getForm($data = array(), $loadData = true) { // Get the form. $form = $this->loadForm('com_cmc.user', 'user', array('control' => 'jform', 'load_data' => $loadData)); $userData = $this->getItem(); // If the user data is stored in the session we are dealing with arrays, if we are loading from DB with object if (is_array($userData)) { $old = $userData; $userData = new JRegistry(); $userData->set('list_id', $old['list_id']); $userData->set('email', $old['cmc_groups']['email']); } elseif (is_object($userData) && !$userData->get('id')) { $userData = new JRegistry(); $userData->set('list_id', isset($data['list_id']) ? $data['list_id'] : ''); $userData->set('email', isset($data['cmc_groups']['EMAIL']) ? $data['cmc_groups']['EMAIL'] : ''); } $listId = $userData->get('list_id', JFactory::getApplication()->input->get('filter_list')); // Get the merge fields and create a new form if ($listId) { $params = new JRegistry(); $params->set('listid', $listId); $fields = array_map(function ($value) { return $value['tag']; }, CmcHelperList::getMergeFields($listId)); $interests = array_map(function ($value) { return $value['id']; }, CmcHelperList::getInterestsFields($listId)); $params->set('fields', $fields); $params->set('interests', $interests); $renderer = CmcHelperXmlbuilder::getInstance($params); // Generate the xml for the form $xml = $renderer->build(); $form->load($xml, true); $subscriptionData = CmcHelperUsers::getSubscription($userData->get('email'), $userData->get('list_id')); // Bind the data to the form if ($subscriptionData) { $form->bind(CmcHelperSubscription::convertMergesToFormData($subscriptionData->merges)); } } if (empty($form)) { return false; } return $form; }
/** * Creates a JForm * * @param int $id - the module id. We use it to create unique jform instance * @param object $params - the module params * * @return object */ public static function getForm($id, $params) { $renderer = CmcHelperXmlbuilder::getInstance($params); $user = JFactory::getUser(); // Generate the xml for the form $xml = $renderer->build(); $mapping = self::getMapping($params->get('mapfields')); try { JForm::addFieldPath(JPATH_ADMINISTRATOR . '/components/com_cmc/models/fields'); $form = JForm::getInstance('mod_cmc_' . $id, $xml, array('control' => 'jform')); $form->bind($mapping); // If we are not dealing with a guest, try to load the already existing profile merges and add them to the form if (!$user->guest) { $subscriptionData = CmcHelperUsers::getSubscription($user->email, $params->get('listid')); if ($subscriptionData) { $form->bind(CmcHelperSubscription::convertMergesToFormData($subscriptionData->merges)); } } } catch (Exception $e) { return false; } return $form; }