Commit | Line | Data |
---|---|---|
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 | ||
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 | * Function to 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 | * This function sets the 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 | * Function to build the form | |
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' => ' ', | |
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' => ' ', | |
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' => ' ', | |
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 | * | |
2a6da8d7 EM |
211 | * @param array $fields the input form values |
212 | * @param array $files the uploaded files if any | |
213 | * @param $self | |
214 | * | |
215 | * @internal param array $options additional user data | |
6a488035 TO |
216 | * |
217 | * @return true if no errors, else array of errors | |
218 | * @access public | |
219 | * @static | |
220 | */ | |
221 | static | |
222 | function formRule($fields, $files, $self) { | |
223 | $errors = array(); | |
224 | ||
225 | // add rules to validate dates and overlap | |
226 | return empty($errors) ? TRUE : $errors; | |
227 | } | |
228 | ||
229 | /** | |
230 | * Function to process the form | |
231 | * | |
232 | * @access public | |
233 | * | |
234 | * @return None | |
235 | */ | |
236 | public function postProcess() { | |
237 | if ($this->_action & CRM_Core_Action::VIEW) { | |
238 | return; | |
239 | } | |
240 | elseif ($this->_action & CRM_Core_Action::DELETE) { | |
241 | CRM_Auction_BAO_Auction::del($this->_id); | |
242 | return; | |
243 | } | |
244 | ||
245 | $params = $this->controller->exportValues($this->_name); | |
246 | ||
247 | $params['id'] = $this->_id; | |
248 | ||
249 | // format date params | |
250 | foreach ($this->_dates as $name => $title) { | |
251 | $params[$name] = CRM_Utils_Date::format($params[$name]); | |
252 | } | |
253 | ||
254 | // format checkboxes | |
255 | foreach ($this->_checkboxes as $name => $title) { | |
256 | $params[$name] = CRM_Utils_Array::value($name, $params, FALSE); | |
257 | } | |
258 | ||
259 | CRM_Auction_BAO_Auction::add($params); | |
260 | } | |
261 | } | |
262 |