Merge branch 'CRM-14696-v2' of https://github.com/JKingsnorth/civicrm-core into CRM...
[civicrm-core.git] / tools / CRM / Auction / Form / ItemAccount.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 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
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 */
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 * Build the form object
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' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
175 'isDefault' => TRUE,
176 );
177
178 $this->addButtons($button);
179 }
180
181 /**
182 * global form rule
183 *
184 * @param array $fields the input form values
185 * @param array $files the uploaded files if any
186 * @param $self
187 *
188 *
189 * @return true if no errors, else array of errors
190 * @access public
191 * @static
192 */
193 static
194 function formRule($fields, $files, $self) {
195 $errors = array();
196 require_once "CRM/Utils/Rule.php";
197 foreach ($fields as $key => $value) {
198 if (strpos($key, 'email-') !== FALSE) {
199 $ufContactId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFMatch', $value, 'contact_id', 'uf_name');
200 if ($ufContactId && $ufContactId != $self->_donorID) {
201 $errors[$key] = ts('There is already an user associated with this email address. Please enter different email address.');
202 }
203 }
204 }
205 return empty($errors) ? TRUE : $errors;
206 }
207
208 /**
209 * Process the form submission
210 *
211 * @access public
212 *
213 * @return None
214 */
215 public function postProcess() {
216 $params = $this->controller->exportValues($this->getName());
217 if (!$this->_donorID) {
218 foreach ($params as $key => $value) {
219 if (substr($key, 0, 5) == 'email' && !empty($value)) {
220 $params['email'] = $value;
221 }
222 }
223 }
224 $donorID = &CRM_Contact_BAO_Contact::createProfileContact($params, $this->_fields,
225 $this->_donorID, $addToGroups
226 );
227 $this->set('donorID', $donorID);
228
229 require_once "CRM/Contribute/BAO/Contribution/Utils.php";
230 CRM_Contribute_BAO_Contribution_Utils::createCMSUser($params, $donorID, 'email');
231 }
232 }
233