_groupID = CRM_Utils_Request::retrieve('gid', 'Integer', $this, FALSE, NULL, 'REQUEST' ); // ensure that there is a destination, if not set the destination to the // referrer string if (!$this->controller->getDestination()) { $this->controller->setDestination(NULL, TRUE); } if ($this->_groupID) { $groupTypeCondition = CRM_Contact_BAO_Group::groupTypeCondition('Mailing'); // make sure requested qroup is accessible and exists $query = " SELECT title, description FROM civicrm_group WHERE id={$this->_groupID} AND visibility != 'User and User Admin Only' AND $groupTypeCondition"; $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); if ($dao->fetch()) { $this->assign('groupName', $dao->title); CRM_Utils_System::setTitle(ts('Subscribe to Mailing List - %1', array(1 => $dao->title))); } else { CRM_Core_Error::statusBounce("The specified group is not configured for this action OR The group doesn't exist."); } $this->assign('single', TRUE); } else { $this->assign('single', FALSE); CRM_Utils_System::setTitle(ts('Mailing List Subscription')); } } /** * Function to actually build the form * * @return None * @access public */ public function buildQuickForm() { // add the email address $this->add('text', 'email', ts('Email'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_Email', 'email' ), TRUE ); $this->addRule('email', ts("Please enter a valid email address (e.g. 'yourname@example.com')."), 'email'); if (!$this->_groupID) { // create a selector box of all public groups $groupTypeCondition = CRM_Contact_BAO_Group::groupTypeCondition('Mailing'); $query = " SELECT id, title, description FROM civicrm_group WHERE ( saved_search_id = 0 OR saved_search_id IS NULL ) AND visibility != 'User and User Admin Only' AND $groupTypeCondition ORDER BY title"; $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); $rows = array(); while ($dao->fetch()) { $row = array(); $row['id'] = $dao->id; $row['title'] = $dao->title; $row['description'] = $dao->description; $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $row['id']; $this->addElement('checkbox', $row['checkbox'], NULL, NULL ); $rows[] = $row; } if (empty($rows)) { CRM_Core_Error::fatal(ts('There are no public mailing list groups to display.')); } $this->assign('rows', $rows); $this->addFormRule(array('CRM_Mailing_Form_Subscribe', 'formRule')); } $addCaptcha = TRUE; // if recaptcha is not set, then dont add it $config = CRM_Core_Config::singleton(); if (empty($config->recaptchaPublicKey) || empty($config->recaptchaPrivateKey) ) { $addCaptcha = FALSE; } else { // if this is POST request and came from a block, // lets add recaptcha only if already present // gross hack for now if (!empty($_POST) && !array_key_exists('recaptcha_challenge_field', $_POST) ) { $addCaptcha = FALSE; } } if ($addCaptcha) { // add captcha $captcha = CRM_Utils_ReCAPTCHA::singleton(); $captcha->add($this); } $this->addButtons(array( array( 'type' => 'next', 'name' => ts('Subscribe'), 'isDefault' => TRUE, ), array( 'type' => 'cancel', 'name' => ts('Cancel'), ), ) ); } static function formRule($fields) { foreach ($fields as $name => $dontCare) { if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) { return TRUE; } } return array('_qf_default' => 'Please select one or more mailing lists.'); } /** * * @access public * * @return None */ public function postProcess() { $params = $this->controller->exportValues($this->_name); $groups = array(); if ($this->_groupID) { $groups[] = $this->_groupID; } else { foreach ($params as $name => $dontCare) { if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) { $groups[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN); } } } CRM_Mailing_Event_BAO_Subscribe::commonSubscribe($groups, $params); } }