Merge pull request #4185 from samuelsov/CRM-15330
[civicrm-core.git] / tools / CRM / Auction / Form / Auction.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
38 /**
39 * This class manages the auction form
40 *
41 */
42 class CRM_Auction_Form_Auction extends CRM_Core_Form {
43
44 /**
45 * the id of the auction we are processsing
46 *
47 * @var int
48 */
49 public $_id;
50
51 protected $_dates;
52
53 protected $_checkboxes;
54
55 protected $_numbers;
56
57 /**
58 * set variables up before form is built
59 *
60 * @return void
61 */
62 function preProcess() {
63 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE);
64
65 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
66
67 if (!CRM_Core_Permission::checkActionPermission('CiviAuction', $this->_action)) {
68 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
69 }
70
71 if (($this->_action & CRM_Core_Action::VIEW ||
72 $this->_action & CRM_Core_Action::UPDATE ||
73 $this->_action & CRM_Core_Action::DELETE
74 ) &&
75 !$this->_id
76 ) {
77 CRM_Core_Error::fatal();
78 }
79 }
80
81 /**
82 * Set default values for the form.
83 * the default values are retrieved from the database
84 * @return array
85 */
86 function setDefaultValues() {
87
88 $defaults = array();
89
90 if (isset($this->_id)) {
91 $params = array('id' => $this->_id);
92 CRM_Auction_BAO_Auction::retrieve($params, $defaults);
93 }
94 else {
95 $defaults['is_active'] = 1;
96 }
97
98 return $defaults;
99 }
100
101 /**
102 * Build the form object
103 *
104 * @return array
105 */
106 public function buildQuickForm() {
107 $this->applyFilter('__ALL__', 'trim');
108
109 $attributes = CRM_Core_DAO::getAttribute('CRM_Auction_DAO_Auction');
110 $this->add('text',
111 'title',
112 ts('Auction Title'),
113 $attributes['auction_title'],
114 TRUE
115 );
116
117 $this->addWysiwyg('description',
118 ts('Complete Description'),
119 $attributes['description']
120 );
121
122 $this->_dates = array('start_date' => ts('Auction Start Date'),
123 'end_date' => ts('Auction End Date'),
124 'item_start_date' => ts('Upload Item Start Date'),
125 'item_end_date' => ts('Upload Item End Date'),
126 );
127 foreach ($this->_dates as $name => $title) {
128 $this->add('date',
129 $name,
130 $title,
131 CRM_Core_SelectValues::date('datetime')
132 );
133 $this->addRule($name, ts('Please select a valid date.'), 'qfDate');
134 }
135
136 $this->_checkboxes = array('is_active' => ts('Is Active?'),
137 'is_approval_needed' => ts('Do items need to be approved?'),
138 'is_item_groups' => ts('Can items be grouped?'),
139 );
140
141 foreach ($this->_checkboxes as $name => $title) {
142 $this->addElement('checkbox',
143 $name,
144 $title
145 );
146 }
147
148 $this->_numbers = array('max_items_user' => ts('Maximum number of items per user'),
149 'max_items' => ts('Maximum number of items for the auction'),
150 );
151 foreach ($this->_numbers as $name => $title) {
152 $this->addElement('text',
153 $name,
154 $title,
155 $attributes[$name]
156 );
157 $this->addRule($name,
158 ts('%1 should be a postive number',
159 array(1 => $title)
160 ),
161 'positiveInteger'
162 );
163 }
164
165 if ($this->_action & CRM_Core_Action::VIEW) {
166 $buttons = array(array('type' => 'upload',
167 'name' => ts('Done'),
168 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
169 'isDefault' => TRUE,
170 ));
171 $this->freeze();
172 }
173 elseif ($this->_action & CRM_Core_Action::DELETE) {
174 $this->freeze();
175 $buttons = array(array('type' => 'upload',
176 'name' => ts('Delete'),
177 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
178 'isDefault' => TRUE,
179 ),
180 array('type' => 'cancel',
181 'name' => ts('Cancel'),
182 ),
183 );
184 }
185 else {
186 $buttons = array(array('type' => 'upload',
187 'name' => ts('Save'),
188 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
189 'isDefault' => TRUE,
190 ),
191 array('type' => 'cancel',
192 'name' => ts('Cancel'),
193 ),
194 );
195 }
196 $this->addButtons($buttons);
197
198 $this->addFormRule(array('CRM_Auction_Form_Auction', 'formRule'), $this);
199 }
200
201 /**
202 * global form rule
203 *
204 * @param array $fields the input form values
205 * @param array $files the uploaded files if any
206 * @param $self
207 * @return true if no errors, else array of errors
208 */
209 static function formRule($fields, $files, $self) {
210 $errors = array();
211
212 // add rules to validate dates and overlap
213 return empty($errors) ? TRUE : $errors;
214 }
215
216 /**
217 * Process the form submission
218 */
219 public function postProcess() {
220 if ($this->_action & CRM_Core_Action::VIEW) {
221 return;
222 }
223 elseif ($this->_action & CRM_Core_Action::DELETE) {
224 CRM_Auction_BAO_Auction::del($this->_id);
225 return;
226 }
227
228 $params = $this->controller->exportValues($this->_name);
229
230 $params['id'] = $this->_id;
231
232 // format date params
233 foreach ($this->_dates as $name => $title) {
234 $params[$name] = CRM_Utils_Date::format($params[$name]);
235 }
236
237 // format checkboxes
238 foreach ($this->_checkboxes as $name => $title) {
239 $params[$name] = CRM_Utils_Array::value($name, $params, FALSE);
240 }
241
242 CRM_Auction_BAO_Auction::add($params);
243 }
244 }
245