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