Merge branch 'master' into master-civimail-abtest
[civicrm-core.git] / tools / CRM / Auction / Form / Item.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 * @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 * 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 * Set 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 * Build the form object
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 *
266 * @param array $fields the input form values
267 * @param array $files the uploaded files if any
268 * @param $self
269 *
270 *
271 * @return true if no errors, else array of errors
272 * @access public
273 * @static
274 */
275 static
276 function formRule($fields, $files, $self) {
277 $errors = array();
278
279 if (isset($files['attachFile_1'])) {
280 list($width, $height) = getimagesize($files['attachFile_1']['tmp_name']);
281 if ($width > 360 || $height > 360) {
282 $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.";
283 }
284 }
285
286 return empty($errors) ? TRUE : $errors;
287 }
288
289 /**
290 * Process the form submission
291 *
292 * @access public
293 *
294 * @return None
295 */
296 public function postProcess() {
297 if ($this->_action & CRM_Core_Action::VIEW) {
298 return;
299 }
300 elseif ($this->_action & CRM_Core_Action::DELETE) {
301 CRM_Auction_BAO_Item::del($this->_id);
302 return;
303 }
304
305 $params = $this->controller->exportValues($this->_name);
306
307 $params['id'] = $this->_id;
308 $params['auction_id'] = $this->_aid;
309
310 $params['donor_id'] = $this->_donorID;
311
312 if ($this->_action == CRM_Core_Action::ADD) {
313 $params['creator_id'] = $this->_donorID;
314 $params['created_date'] = date('YmdHis');
315 }
316
317 // format checkboxes
318 foreach ($this->_checkboxes as $name => $title) {
319 $params[$name] = CRM_Utils_Array::value($name, $params, FALSE);
320 }
321
322 // does this auction require approval
323 $params['is_approved'] = $this->_auctionValues['is_approval_needed'] ? 0 : 1;
324
325 CRM_Auction_BAO_Item::add($params);
326
327 if ($this->controller->getButtonName() == $this->getButtonName('next', 'new')) {
328 $session = CRM_Core_Session::singleton();
329 //CRM_Core_Session::setStatus(ts(' You can add another profile field.'));
330 $session->replaceUserContext(CRM_Utils_System::url('civicrm/auction/item',
331 "reset=1&action=add&aid={$this->_aid}"
332 ));
333 }
334 }
335 }
336