Merge pull request #5288 from colemanw/CRM-15932
[civicrm-core.git] / tools / CRM / Auction / Form / Item.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 *
31 * @package CRM
34cd78e1 32 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
33 * $Id$
34 *
35 */
36
37require_once 'CRM/Core/Form.php';
38
39/**
40 * This class manages the auction form
41 *
42 */
43class CRM_Auction_Form_Item extends CRM_Core_Form {
44
45 /**
46 * the id of the item we are processing
47 *
48 * @var int
6a488035
TO
49 */
50 public $_id;
51
52 /**
53 * the id of the auction for this item
54 *
55 * @var int
6a488035
TO
56 */
57 public $_aid;
58
59 /**
60 * the id of the person donating this item
61 *
62 * @var int
6a488035
TO
63 */
64 public $_donorID;
65
66 /**
c490a46a 67 * set variables up before form is built
6a488035
TO
68 *
69 * @return void
95ea96be
EM
70 */
71 function preProcess() {
6a488035
TO
72 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add');
73
74 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
75 $this->_aid = CRM_Utils_Request::retrieve('aid', 'Positive', $this, TRUE);
76
77 if (($this->_action & CRM_Core_Action::VIEW ||
78 $this->_action & CRM_Core_Action::UPDATE ||
79 $this->_action & CRM_Core_Action::DELETE
80 ) &&
81 !$this->_id
82 ) {
83 CRM_Core_Error::fatal("I am not sure which item you looking for.");
84 }
85
86 require_once 'CRM/Auction/BAO/Auction.php';
87 $params = array('id' => $this->_aid);
88 $this->_auctionValues = array();
89 CRM_Auction_BAO_Auction::retrieve($params, $this->_auctionValues);
90
91 $this->assign('auctionTitle', $this->_auctionValues['auction_title']);
92
93 // set donor id
94 $session = CRM_Core_Session::singleton();
95 $this->_donorID = $this->get('donorID');
96
97 $this->assign('donorName',
98 CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
99 $this->_donorID,
100 'display_name'
101 )
102 );
103
104 // also set user context
105 $session->pushUserContext(CRM_Utils_System::url('civicrm/auction/item',
106 "reset=1&aid={$this->_aid}"
107 ));
108 }
109
110 /**
c490a46a 111 * Set default values for the form.
6a488035 112 * the default values are retrieved from the database
b39bd74f 113 * @return array
6a488035
TO
114 */
115 function setDefaultValues() {
6a488035
TO
116 $defaults = array();
117
118 if (isset($this->_id)) {
119 $params = array('id' => $this->_id);
120 CRM_Auction_BAO_Item::retrieve($params, $defaults);
121 }
122 else {
123 $defaults['is_active'] = 1;
124 $defaults['auction_type_id'] = 1;
125 }
126
127 return $defaults;
128 }
129
130 /**
c490a46a 131 * Build the form object
6a488035
TO
132 */
133 public function buildQuickForm() {
134 $this->applyFilter('__ALL__', 'trim');
135
136 $attributes = CRM_Core_DAO::getAttribute('CRM_Auction_DAO_Item');
137 $this->add('text',
138 'title',
139 ts('Item Label'),
140 $attributes['title'],
141 TRUE
142 );
143
144 $this->addWysiwyg('description',
145 ts('Complete Description'),
146 $attributes['description']
147 );
148
149 $auctionTypes = CRM_Core_OptionGroup::values('auction_item_type');
150 $this->add('select', 'auction_item_type_id', ts('Item Type'),
151 array('' => ts('- select -')) + $auctionTypes
152 );
153
154 $this->add('text', 'url', ts('Item URL'),
155 array_merge($attributes['description'],
156 array('onfocus' => "if (!this.value) { this.value='http://';} else return false",
157 'onblur' => "if ( this.value == 'http://') { this.value='';} else return false",
158 )
159 )
160 );
161
162
163 $this->_checkboxes = array('is_active' => ts('Is Active?'),
164 'is_group' => ts('Does this item have other items associated with it?'),
165 );
166 foreach ($this->_checkboxes as $name => $title) {
167 $this->addElement('checkbox',
168 $name,
169 $title
170 );
171 }
172
173 $this->_numbers = array('quantity' => ts('Number of units available'),
174 'retail_value' => ts('Retail value of item'),
175 'min_bid_value' => ts('Minimum bid accepted'),
176 'min_bid_increment' => ts('Minimum bid increment'),
177 'buy_now_value' => ts('Buy it now value'),
178 );
179
180 foreach ($this->_numbers as $name => $title) {
181 $this->addElement('text',
182 $name,
183 $title,
184 $attributes[$name]
185 );
186 if ($name == 'quantity') {
187 $this->addRule($name,
188 ts('%1 should be a postive number',
189 array(1 => $title)
190 ),
191 'positiveInteger'
192 );
193 }
194 else {
195 $this->addRule($name,
196 ts('%1 should be a valid money value',
197 array(1 => $title)
198 ),
199 'money'
200 );
201 }
202 }
203
204 $maxAttachments = 1;
205 require_once 'CRM/Core/BAO/File.php';
206 CRM_Core_BAO_File::buildAttachment($this, 'civicrm_pcp', $this->_pageId, $maxAttachments);
207
208
209 if ($this->_action & CRM_Core_Action::VIEW) {
210 $buttons = array(array('type' => 'upload',
211 'name' => ts('Done'),
212 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
213 'isDefault' => TRUE,
214 ));
215 $this->freeze();
216 }
217 elseif ($this->_action & CRM_Core_Action::DELETE) {
218 $this->freeze();
219 $buttons = array(array('type' => 'upload',
220 'name' => ts('Delete'),
221 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
222 'isDefault' => TRUE,
223 ),
224 array('type' => 'cancel',
225 'name' => ts('Cancel'),
226 ),
227 );
228 }
229 else {
230 $buttons = array(array('type' => 'upload',
231 'name' => ts('Save'),
232 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
233 'isDefault' => TRUE,
234 ));
235
236 $session = CRM_Core_Session::singleton();
237 if ($session->get('userID')) {
238 $buttons[] = array('type' => 'next',
239 'name' => ts('Save and New'),
240 'subName' => 'new',
241 );
242 }
243 $buttons[] = array('type' => 'cancel',
244 'name' => ts('Cancel'),
245 );
246 }
247 $this->addButtons($buttons);
248
249 $this->addFormRule(array('CRM_Auction_Form_Item', 'formRule'), $this);
250 }
251
252 /**
253 * global form rule
254 *
2a6da8d7
EM
255 * @param array $fields the input form values
256 * @param array $files the uploaded files if any
257 * @param $self
6a488035 258 * @return true if no errors, else array of errors
6a488035 259 */
b39bd74f 260 static function formRule($fields, $files, $self) {
6a488035
TO
261 $errors = array();
262
263 if (isset($files['attachFile_1'])) {
264 list($width, $height) = getimagesize($files['attachFile_1']['tmp_name']);
265 if ($width > 360 || $height > 360) {
266 $errors['attachFile_1'] = "Your picture or image file can not be larger than 360 x 360 pixels in size." . " The dimensions of the image you've selected is " . $width . " x " . $height . ". Please shrink or crop the file or find another smaller image and try again.";
267 }
268 }
269
270 return empty($errors) ? TRUE : $errors;
271 }
272
273 /**
c490a46a 274 * Process the form submission
6a488035
TO
275 */
276 public function postProcess() {
277 if ($this->_action & CRM_Core_Action::VIEW) {
278 return;
279 }
280 elseif ($this->_action & CRM_Core_Action::DELETE) {
281 CRM_Auction_BAO_Item::del($this->_id);
282 return;
283 }
284
285 $params = $this->controller->exportValues($this->_name);
286
287 $params['id'] = $this->_id;
288 $params['auction_id'] = $this->_aid;
289
290 $params['donor_id'] = $this->_donorID;
291
292 if ($this->_action == CRM_Core_Action::ADD) {
293 $params['creator_id'] = $this->_donorID;
294 $params['created_date'] = date('YmdHis');
295 }
296
297 // format checkboxes
298 foreach ($this->_checkboxes as $name => $title) {
299 $params[$name] = CRM_Utils_Array::value($name, $params, FALSE);
300 }
301
302 // does this auction require approval
303 $params['is_approved'] = $this->_auctionValues['is_approval_needed'] ? 0 : 1;
304
305 CRM_Auction_BAO_Item::add($params);
306
307 if ($this->controller->getButtonName() == $this->getButtonName('next', 'new')) {
308 $session = CRM_Core_Session::singleton();
309 //CRM_Core_Session::setStatus(ts(' You can add another profile field.'));
310 $session->replaceUserContext(CRM_Utils_System::url('civicrm/auction/item',
311 "reset=1&action=add&aid={$this->_aid}"
312 ));
313 }
314 }
315}
316