| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * This class generates form components for PCP |
| 38 | * |
| 39 | */ |
| 40 | class CRM_PCP_Form_Event extends CRM_Event_Form_ManageEvent { |
| 41 | |
| 42 | /** |
| 43 | * The type of pcp component. |
| 44 | * |
| 45 | * @var int |
| 46 | */ |
| 47 | public $_component = 'event'; |
| 48 | |
| 49 | |
| 50 | public function preProcess() { |
| 51 | parent::preProcess(); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Set default values for the form. |
| 56 | * |
| 57 | * |
| 58 | * @return void |
| 59 | */ |
| 60 | public function setDefaultValues() { |
| 61 | $defaults = array(); |
| 62 | |
| 63 | $defaults = array(); |
| 64 | if (isset($this->_id)) { |
| 65 | $title = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_id, 'title'); |
| 66 | CRM_Utils_System::setTitle(ts('Personal Campaign Page Settings (%1)', array(1 => $title))); |
| 67 | |
| 68 | $params = array('entity_id' => $this->_id, 'entity_table' => 'civicrm_event'); |
| 69 | CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCPBlock', $params, $defaults); |
| 70 | $defaults['pcp_active'] = CRM_Utils_Array::value('is_active', $defaults); |
| 71 | // Assign contribution page ID to pageId for referencing in PCP.hlp - since $id is overwritten there. dgg |
| 72 | $this->assign('pageId', $this->_id); |
| 73 | } |
| 74 | |
| 75 | if (empty($defaults['id'])) { |
| 76 | $defaults['target_entity_type'] = 'event'; |
| 77 | $defaults['is_approval_needed'] = 1; |
| 78 | $defaults['is_tellfriend_enabled'] = 1; |
| 79 | $defaults['tellfriend_limit'] = 5; |
| 80 | $defaults['link_text'] = ts('Promote this event with a personal campaign page'); |
| 81 | $defaults['owner_notify_id'] = CRM_Core_OptionGroup::getDefaultValue('pcp_owner_notify'); |
| 82 | |
| 83 | if ($this->_id && |
| 84 | $ccReceipt = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id, 'cc_receipt') |
| 85 | ) { |
| 86 | $defaults['notify_email'] = $ccReceipt; |
| 87 | } |
| 88 | } |
| 89 | return $defaults; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Build the form object |
| 94 | * |
| 95 | * @return void |
| 96 | */ |
| 97 | public function buildQuickForm() { |
| 98 | CRM_PCP_BAO_PCP::buildPCPForm($this); |
| 99 | |
| 100 | $this->addElement('checkbox', 'pcp_active', ts('Enable Personal Campaign Pages? (for this event)'), NULL, array('onclick' => "return showHideByValue('pcp_active',true,'pcpFields','table-row','radio',false);")); |
| 101 | |
| 102 | $this->add('select', 'target_entity_type', ts('Campaign Type'), |
| 103 | array('' => ts('- select -'), 'event' => ts('Event'), 'contribute' => ts('Contribution')), |
| 104 | NULL, array('onchange' => "return showHideByValue('target_entity_type','contribute','pcpDetailFields','block','select',false);") |
| 105 | ); |
| 106 | |
| 107 | $this->add('select', 'target_entity_id', |
| 108 | ts('Online Contribution Page'), |
| 109 | array( |
| 110 | '' => ts('- select -'), |
| 111 | ) + |
| 112 | CRM_Contribute_PseudoConstant::contributionPage() |
| 113 | ); |
| 114 | |
| 115 | parent::buildQuickForm(); |
| 116 | |
| 117 | // If at least one PCP has been created, don't allow changing the target |
| 118 | $pcpBlock = new CRM_PCP_DAO_PCPBlock(); |
| 119 | $pcpBlock->entity_table = 'civicrm_event'; |
| 120 | $pcpBlock->entity_id = $this->_id; |
| 121 | $pcpBlock->find(TRUE); |
| 122 | |
| 123 | if (!empty($pcpBlock->id) && CRM_PCP_BAO_PCP::getPcpBlockInUse($pcpBlock->id)) { |
| 124 | foreach (array( |
| 125 | 'target_entity_type', |
| 126 | 'target_entity_id', |
| 127 | ) as $element_name) { |
| 128 | $element = $this->getElement($element_name); |
| 129 | $element->freeze(); |
| 130 | } |
| 131 | } |
| 132 | $this->addFormRule(array('CRM_PCP_Form_Event', 'formRule'), $this); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Validation |
| 137 | * |
| 138 | * @param array $params |
| 139 | * (ref.) an assoc array of name/value pairs. |
| 140 | * |
| 141 | * @param $files |
| 142 | * @param $self |
| 143 | * |
| 144 | * @return bool|array |
| 145 | * mixed true or array of errors |
| 146 | */ |
| 147 | public static function formRule($params, $files, $self) { |
| 148 | $errors = array(); |
| 149 | if (!empty($params['is_active'])) { |
| 150 | |
| 151 | if (!empty($params['is_tellfriend_enabled']) && |
| 152 | (CRM_Utils_Array::value('tellfriend_limit', $params) <= 0) |
| 153 | ) { |
| 154 | $errors['tellfriend_limit'] = ts('if Tell Friend is enable, Maximum recipients limit should be greater than zero.'); |
| 155 | } |
| 156 | if (empty($params['supporter_profile_id'])) { |
| 157 | $errors['supporter_profile_id'] = ts('Supporter profile is a required field.'); |
| 158 | } |
| 159 | else { |
| 160 | if (CRM_PCP_BAO_PCP::checkEmailProfile($params['supporter_profile_id'])) { |
| 161 | $errors['supporter_profile_id'] = ts('Profile is not configured with Email address.'); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | if ($emails = CRM_Utils_Array::value('notify_email', $params)) { |
| 166 | $emailArray = explode(',', $emails); |
| 167 | foreach ($emailArray as $email) { |
| 168 | if ($email && !CRM_Utils_Rule::email(trim($email))) { |
| 169 | $errors['notify_email'] = ts('A valid Notify Email address must be specified'); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | return empty($errors) ? TRUE : $errors; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Process the form submission |
| 179 | * |
| 180 | * |
| 181 | * @return void |
| 182 | */ |
| 183 | public function postProcess() { |
| 184 | // get the submitted form values. |
| 185 | $params = $this->controller->exportValues($this->_name); |
| 186 | |
| 187 | // Source |
| 188 | $params['entity_table'] = 'civicrm_event'; |
| 189 | $params['entity_id'] = $this->_id; |
| 190 | |
| 191 | // Target |
| 192 | $params['target_entity_type'] = CRM_Utils_Array::value('target_entity_type', $params, 'event'); |
| 193 | if ($params['target_entity_type'] == 'event') { |
| 194 | $params['target_entity_id'] = $this->_id; |
| 195 | } |
| 196 | else { |
| 197 | $params['target_entity_id'] = CRM_Utils_Array::value('target_entity_id', $params, $this->_id); |
| 198 | } |
| 199 | |
| 200 | $dao = new CRM_PCP_DAO_PCPBlock(); |
| 201 | $dao->entity_table = $params['entity_table']; |
| 202 | $dao->entity_id = $this->_id; |
| 203 | $dao->find(TRUE); |
| 204 | $params['id'] = $dao->id; |
| 205 | $params['is_active'] = CRM_Utils_Array::value('pcp_active', $params, FALSE); |
| 206 | $params['is_approval_needed'] = CRM_Utils_Array::value('is_approval_needed', $params, FALSE); |
| 207 | $params['is_tellfriend_enabled'] = CRM_Utils_Array::value('is_tellfriend_enabled', $params, FALSE); |
| 208 | |
| 209 | $dao = CRM_PCP_BAO_PCP::add($params); |
| 210 | |
| 211 | // Update tab "disabled" css class |
| 212 | $this->ajaxResponse['tabValid'] = !empty($params['is_active']); |
| 213 | |
| 214 | parent::endPostProcess(); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Return a descriptive name for the page, used in wizard header |
| 219 | * |
| 220 | * @return string |
| 221 | */ |
| 222 | public function getTitle() { |
| 223 | return ts('Enable Personal Campaign Pages'); |
| 224 | } |
| 225 | |
| 226 | } |