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