Merge pull request #5288 from colemanw/CRM-15932
[civicrm-core.git] / tools / CRM / Auction / Form / ItemAccount.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
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 */
35require_once 'CRM/Core/Form.php';
36
37/**
38 * This class generates form components for adding an account if not already exists
39 *
40 */
41class 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
6a488035
TO
47 */
48 public $_aid = NULL;
49
50 /**
51 * the id of the item we are processing
52 *
53 * @var int
6a488035
TO
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
a1a55b61
EM
75 /**
76 * This virtual function is used to set the default values of
77 * various form elements
a1a55b61 78 * @return array reference to the array of default values
a1a55b61 79 */
6a488035
TO
80 function setDefaultValues() {
81 if (!$this->_donorID) {
82 return;
83 }
84
85 foreach ($this->_fields as $name => $dontcare) {
86 $fields[$name] = 1;
87 }
88
89 require_once "CRM/Core/BAO/UFGroup.php";
90 CRM_Core_BAO_UFGroup::setProfileDefaults($this->_donorID, $fields, $this->_defaults);
91
92 //set custom field defaults
93 require_once "CRM/Core/BAO/CustomField.php";
94 foreach ($this->_fields as $name => $field) {
95 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) {
96 if (!isset($this->_defaults[$name])) {
97 CRM_Core_BAO_CustomField::setProfileDefaults($customFieldID, $name, $this->_defaults,
98 NULL, CRM_Profile_Form::MODE_REGISTER
99 );
100 }
101 }
102 }
103
104 return $this->_defaults;
105 }
106
107 /**
c490a46a 108 * Build the form object
6a488035
TO
109 */
110 public function buildQuickForm() {
111 $profileID = CRM_Core_DAO::getFieldValue('CRM_Auction_DAO_Auction',
112 $this->_aid, 'donor_profile_id'
113 );
114 if (!$profileID) {
115 CRM_Core_Error::fatal('Profile not configured for this auction.');
116 }
117
118 require_once 'CRM/Auction/BAO/Item.php';
119 if (CRM_Auction_BAO_Item::isEmailInProfile($profileID)) {
120 $this->assign('profileDisplay', TRUE);
121 }
122
123 $fields = NULL;
124 require_once "CRM/Core/BAO/UFGroup.php";
125 if ($this->_donorID) {
126 if (CRM_Core_BAO_UFGroup::filterUFGroups($profileID, $this->_donorID)) {
127 $fields = CRM_Core_BAO_UFGroup::getFields($profileID, FALSE, CRM_Core_Action::ADD);
128 }
129 $this->addFormRule(array('CRM_Auction_Form_ItemAccount', 'formRule'), $this);
130 }
131 else {
132 require_once 'CRM/Core/BAO/CMSUser.php';
133 CRM_Core_BAO_CMSUser::buildForm($this, $profileID, TRUE);
134
135 $fields = CRM_Core_BAO_UFGroup::getFields($profileID, FALSE, CRM_Core_Action::ADD);
136 }
137
138 if ($fields) {
139 $this->assign('fields', $fields);
140 $addCaptcha = FALSE;
141 foreach ($fields as $key => $field) {
142 if (isset($field['data_type']) && $field['data_type'] == 'File') {
143 // ignore file upload fields
144 continue;
145 }
146 require_once "CRM/Core/BAO/UFGroup.php";
147 require_once "CRM/Profile/Form.php";
148 CRM_Core_BAO_UFGroup::buildProfile($this, $field, CRM_Profile_Form::MODE_CREATE);
149 $this->_fields[$key] = $field;
150 if ($field['add_captcha']) {
151 $addCaptcha = TRUE;
152 }
153 }
154
155 if ($addCaptcha) {
156 require_once 'CRM/Utils/ReCAPTCHA.php';
157 $captcha = &CRM_Utils_ReCAPTCHA::singleton();
158 $captcha->add($this);
159 $this->assign("isCaptcha", TRUE);
160 }
161 }
162
163 $button[] = array('type' => 'next',
164 'name' => ts('Continue >>'),
165 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
166 'isDefault' => TRUE,
167 );
168
169 $this->addButtons($button);
170 }
171
172 /**
173 * global form rule
174 *
2a6da8d7
EM
175 * @param array $fields the input form values
176 * @param array $files the uploaded files if any
177 * @param $self
178 *
b39bd74f 179 * @return bool|array
6a488035 180 */
b39bd74f 181 static function formRule($fields, $files, $self) {
6a488035 182 $errors = array();
6a488035
TO
183 foreach ($fields as $key => $value) {
184 if (strpos($key, 'email-') !== FALSE) {
185 $ufContactId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFMatch', $value, 'contact_id', 'uf_name');
186 if ($ufContactId && $ufContactId != $self->_donorID) {
187 $errors[$key] = ts('There is already an user associated with this email address. Please enter different email address.');
188 }
189 }
190 }
191 return empty($errors) ? TRUE : $errors;
192 }
193
194 /**
c490a46a 195 * Process the form submission
6a488035
TO
196 */
197 public function postProcess() {
198 $params = $this->controller->exportValues($this->getName());
199 if (!$this->_donorID) {
200 foreach ($params as $key => $value) {
201 if (substr($key, 0, 5) == 'email' && !empty($value)) {
202 $params['email'] = $value;
203 }
204 }
205 }
206 $donorID = &CRM_Contact_BAO_Contact::createProfileContact($params, $this->_fields,
207 $this->_donorID, $addToGroups
208 );
209 $this->set('donorID', $donorID);
210
211 require_once "CRM/Contribute/BAO/Contribution/Utils.php";
212 CRM_Contribute_BAO_Contribution_Utils::createCMSUser($params, $donorID, 'email');
213 }
214}
215