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