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