Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
34cd78e1 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
34cd78e1 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | |
34cd78e1 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | require_once 'CRM/Core/Form.php'; | |
36 | ||
37 | /** | |
38 | * This class generates form components for adding an account if not already exists | |
39 | * | |
40 | */ | |
41 | class CRM_Auction_Form_ItemAccount extends CRM_Core_Form { | |
42 | ||
43 | /** | |
44 | * the id of the auction for which item needs to be updated/added. | |
45 | * | |
46 | * @var int | |
47 | * @protected | |
48 | */ | |
49 | public $_aid = NULL; | |
50 | ||
51 | /** | |
52 | * the id of the item we are processing | |
53 | * | |
54 | * @var int | |
55 | * @protected | |
56 | */ | |
57 | public $_id = NULL; | |
58 | ||
59 | public function preProcess() { | |
60 | $session = CRM_Core_Session::singleton(); | |
61 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add'); | |
62 | $this->_aid = CRM_Utils_Request::retrieve('aid', 'Positive', $this); | |
63 | ||
64 | if ($session->get('userID')) { | |
65 | $this->_donorID = $session->get('userID'); | |
66 | } | |
67 | ||
68 | if (!$this->_aid) { | |
69 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE); | |
70 | $this->_aid = CRM_Core_DAO::getFieldValue('CRM_Auction_DAO_Item', $this->_id, 'auction_id'); | |
71 | } | |
72 | ||
73 | // we do not want to display recently viewed items, so turn off | |
74 | $this->assign('displayRecent', FALSE); | |
75 | } | |
76 | ||
a1a55b61 EM |
77 | /** |
78 | * This virtual function is used to set the default values of | |
79 | * various form elements | |
80 | * | |
81 | * access public | |
82 | * | |
83 | * @return array reference to the array of default values | |
84 | * | |
85 | */ | |
6a488035 TO |
86 | function setDefaultValues() { |
87 | if (!$this->_donorID) { | |
88 | return; | |
89 | } | |
90 | ||
91 | foreach ($this->_fields as $name => $dontcare) { | |
92 | $fields[$name] = 1; | |
93 | } | |
94 | ||
95 | require_once "CRM/Core/BAO/UFGroup.php"; | |
96 | CRM_Core_BAO_UFGroup::setProfileDefaults($this->_donorID, $fields, $this->_defaults); | |
97 | ||
98 | //set custom field defaults | |
99 | require_once "CRM/Core/BAO/CustomField.php"; | |
100 | foreach ($this->_fields as $name => $field) { | |
101 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) { | |
102 | if (!isset($this->_defaults[$name])) { | |
103 | CRM_Core_BAO_CustomField::setProfileDefaults($customFieldID, $name, $this->_defaults, | |
104 | NULL, CRM_Profile_Form::MODE_REGISTER | |
105 | ); | |
106 | } | |
107 | } | |
108 | } | |
109 | ||
110 | return $this->_defaults; | |
111 | } | |
112 | ||
113 | /** | |
114 | * Function to build the form | |
115 | * | |
116 | * @return None | |
117 | * @access public | |
118 | */ | |
119 | public function buildQuickForm() { | |
120 | $profileID = CRM_Core_DAO::getFieldValue('CRM_Auction_DAO_Auction', | |
121 | $this->_aid, 'donor_profile_id' | |
122 | ); | |
123 | if (!$profileID) { | |
124 | CRM_Core_Error::fatal('Profile not configured for this auction.'); | |
125 | } | |
126 | ||
127 | require_once 'CRM/Auction/BAO/Item.php'; | |
128 | if (CRM_Auction_BAO_Item::isEmailInProfile($profileID)) { | |
129 | $this->assign('profileDisplay', TRUE); | |
130 | } | |
131 | ||
132 | $fields = NULL; | |
133 | require_once "CRM/Core/BAO/UFGroup.php"; | |
134 | if ($this->_donorID) { | |
135 | if (CRM_Core_BAO_UFGroup::filterUFGroups($profileID, $this->_donorID)) { | |
136 | $fields = CRM_Core_BAO_UFGroup::getFields($profileID, FALSE, CRM_Core_Action::ADD); | |
137 | } | |
138 | $this->addFormRule(array('CRM_Auction_Form_ItemAccount', 'formRule'), $this); | |
139 | } | |
140 | else { | |
141 | require_once 'CRM/Core/BAO/CMSUser.php'; | |
142 | CRM_Core_BAO_CMSUser::buildForm($this, $profileID, TRUE); | |
143 | ||
144 | $fields = CRM_Core_BAO_UFGroup::getFields($profileID, FALSE, CRM_Core_Action::ADD); | |
145 | } | |
146 | ||
147 | if ($fields) { | |
148 | $this->assign('fields', $fields); | |
149 | $addCaptcha = FALSE; | |
150 | foreach ($fields as $key => $field) { | |
151 | if (isset($field['data_type']) && $field['data_type'] == 'File') { | |
152 | // ignore file upload fields | |
153 | continue; | |
154 | } | |
155 | require_once "CRM/Core/BAO/UFGroup.php"; | |
156 | require_once "CRM/Profile/Form.php"; | |
157 | CRM_Core_BAO_UFGroup::buildProfile($this, $field, CRM_Profile_Form::MODE_CREATE); | |
158 | $this->_fields[$key] = $field; | |
159 | if ($field['add_captcha']) { | |
160 | $addCaptcha = TRUE; | |
161 | } | |
162 | } | |
163 | ||
164 | if ($addCaptcha) { | |
165 | require_once 'CRM/Utils/ReCAPTCHA.php'; | |
166 | $captcha = &CRM_Utils_ReCAPTCHA::singleton(); | |
167 | $captcha->add($this); | |
168 | $this->assign("isCaptcha", TRUE); | |
169 | } | |
170 | } | |
171 | ||
172 | $button[] = array('type' => 'next', | |
173 | 'name' => ts('Continue >>'), | |
174 | 'spacing' => ' ', | |
175 | 'isDefault' => TRUE, | |
176 | ); | |
177 | ||
178 | $this->addButtons($button); | |
179 | } | |
180 | ||
181 | /** | |
182 | * global form rule | |
183 | * | |
2a6da8d7 EM |
184 | * @param array $fields the input form values |
185 | * @param array $files the uploaded files if any | |
186 | * @param $self | |
187 | * | |
188 | * @internal param array $options additional user data | |
6a488035 TO |
189 | * |
190 | * @return true if no errors, else array of errors | |
191 | * @access public | |
192 | * @static | |
193 | */ | |
194 | static | |
195 | function formRule($fields, $files, $self) { | |
196 | $errors = array(); | |
197 | require_once "CRM/Utils/Rule.php"; | |
198 | foreach ($fields as $key => $value) { | |
199 | if (strpos($key, 'email-') !== FALSE) { | |
200 | $ufContactId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFMatch', $value, 'contact_id', 'uf_name'); | |
201 | if ($ufContactId && $ufContactId != $self->_donorID) { | |
202 | $errors[$key] = ts('There is already an user associated with this email address. Please enter different email address.'); | |
203 | } | |
204 | } | |
205 | } | |
206 | return empty($errors) ? TRUE : $errors; | |
207 | } | |
208 | ||
209 | /** | |
210 | * Function to process the form | |
211 | * | |
212 | * @access public | |
213 | * | |
214 | * @return None | |
215 | */ | |
216 | public function postProcess() { | |
217 | $params = $this->controller->exportValues($this->getName()); | |
218 | if (!$this->_donorID) { | |
219 | foreach ($params as $key => $value) { | |
220 | if (substr($key, 0, 5) == 'email' && !empty($value)) { | |
221 | $params['email'] = $value; | |
222 | } | |
223 | } | |
224 | } | |
225 | $donorID = &CRM_Contact_BAO_Contact::createProfileContact($params, $this->_fields, | |
226 | $this->_donorID, $addToGroups | |
227 | ); | |
228 | $this->set('donorID', $donorID); | |
229 | ||
230 | require_once "CRM/Contribute/BAO/Contribution/Utils.php"; | |
231 | CRM_Contribute_BAO_Contribution_Utils::createCMSUser($params, $donorID, 'email'); | |
232 | } | |
233 | } | |
234 |