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