1 <?php
2
3 4 5 6 7 8 9 10
11 class CatalogAdmin extends ModelAdmin {
12
13 public static $managed_models = array(
14 'Product',
15 );
16
17 static $url_segment = 'catalog';
18 static $url_rule = '/$Action';
19 static = 'Catalog';
20 static $allowed_actions = array('edit', 'view');
21 public static $collection_controller_class = "CatalogAdmin_CollectionController";
22 public static $record_controller_class = "CatalogAdmin_RecordController";
23
24 protected $resultsTableClassName = 'TableField';
25
26
27
28 protected static $table_fields = array('ImportID' => 'ReadonlyField', 'Title' => 'ReadonlyField', 'BasePrice' => 'ReadonlyField', 'CostPrice' => 'ReadonlyField', 'Vendor' => 'ReadonlyField', 'Available' => 'CheckboxField', 'AllowPurchase' => 'CheckboxField');
29
30 static function set_table_fields($fields) {
31 return self::$table_fields = $fields;
32 }
33
34 static function get_table_fields() {
35 return self::$table_fields;
36 }
37
38 function init(){
39 parent::init();
40
41 Requirements::javascript('catalog/javascript/CatalogAdmin.js');
42 Requirements::css('catalog/css/CatalogAdmin.css');
43 }
44 }
45
46 class CatalogAdmin_CollectionController extends ModelAdmin_CollectionController {
47
48 49 50 51 52
53 public function ImportForm() {
54 return false;
55 }
56
57 58 59 60
61 public function CreateForm() {
62 return false;
63 }
64
65 public function SearchForm() {
66
67 $form = parent::SearchForm();
68 if (($catalogs = DataObject::get('Catalog')) && ($rubricField = $form->Fields()->dataFieldByName('ParentID'))) {
69 $rubricField->setTitle(_t('CatalogAdmin.ParentIDTitle'));
70 $rubricField->setSource($catalogs->map());
71 }
72 return $form;
73 }
74
75 function ResultsForm($searchCriteria) {
76
77 if($searchCriteria instanceof SS_HTTPRequest) $searchCriteria = $searchCriteria->getVars();
78
79 $tf = $this->getResultsTable($searchCriteria);
80
81
82
83 $form = new Form(
84 $this,
85 'ResultsForm',
86 new FieldSet(
87 new HeaderField('SearchResultsHeader',_t('ModelAdmin.SEARCHRESULTS','Search Results'), 2),
88 $tf
89 ),
90 new FieldSet(
91 new FormAction("doSave", _t('CatalogAdmin.doSave', "Save"))
92 )
93 );
94
95
96 $filteredCriteria = $searchCriteria;
97 unset($filteredCriteria['ctf']);
98 unset($filteredCriteria['url']);
99 unset($filteredCriteria['action_search']);
100 if (isset($filteredCriteria['Available']) && !$filteredCriteria['Available']) {
101 unset($filteredCriteria['Available']);
102 }
103 if (isset($filteredCriteria['AllowPurchase']) && !$filteredCriteria['AllowPurchase']) {
104 unset($filteredCriteria['AllowPurchase']);
105 }
106
107 $form->setFormAction($this->Link() . '/ResultsForm?' . http_build_query($filteredCriteria));
108 return $form;
109 }
110
111 function doSave($data, $form) {
112 if (isset($data['Product']) && is_array($data['Product']) && count($data['Product'])) {
113 foreach($data['Product'] as $productID=>$productData) {
114 if ($product = DataObject::get_by_id('Product', (int)$productID)) {
115 foreach(CatalogAdmin::get_table_fields() as $fieldName=>$fieldClass) {
116 if ($fieldClass == 'CheckboxField') {
117 $product->{$fieldName} = 0;
118 if (isset($productData[$fieldName])) {
119 $product->{$fieldName} = $productData[$fieldName];
120 }
121 }
122 }
123 $product->writeToStage('Stage');
124 if ($product->isPublished()) {
125 $product->publish('Stage', 'Live');
126 }
127 }
128 }
129 }
130 }
131
132 function getResultsTable($searchCriteria) {
133 $summaryFields = array();
134 $nonCheckboxSummaryFields = array();
135 foreach(CatalogAdmin::get_table_fields() as $fieldName=>$fieldClass) {
136 $title = singleton('Product')->fieldLabel($fieldName);
137 if ($fieldClass == 'CheckboxField') {
138 $title .= ' (<span class="select_all_checkboxes" field="'.$fieldName.'"></span> / <span class="unselect_all_checkboxes" field="'.$fieldName.'"></span>)';
139 } else {
140 $nonCheckboxSummaryFields[] = $fieldName;
141 }
142 $summaryFields[$fieldName] = $title;
143 }
144
145 $className = $this->parentController->resultsTableClassName();
146 $tf = new $className(
147 $this->modelClass,
148 $this->modelClass,
149 $summaryFields,
150 CatalogAdmin::get_table_fields()
151 );
152
153 $tf->setCustomQuery($this->getSearchQuery($searchCriteria));
154 $tf->setPageSize($this->parentController->stat('page_length'));
155 $tf->setShowPagination(true);
156 $tf->showAddRow = false;
157
158 $tf->setPermissions(array('view','export','edit'));
159
160
161 $exportFields = $this->getResultColumns($searchCriteria, false);
162 $tf->setFieldListCsv($exportFields);
163
164 $url = '<a href=\"' . $this->Link() . '/$ID/edit\">$value</a>';
165 $tf->setFieldFormatting(array_combine($nonCheckboxSummaryFields, array_fill(0,count($nonCheckboxSummaryFields), $url)));
166
167 return $tf;
168 }
169 }
170
171 class CatalogAdmin_RecordController extends ModelAdmin_RecordController{
172 static $allowed_actions = array('add', 'edit', 'view', 'EditForm', 'ViewForm');
173
174 public function EditForm() {
175 $fields = $this->currentRecord->getCMSFields();
176 $fields->push(new HiddenField("ID"));
177
178 $validator = ($this->currentRecord->hasMethod('getCMSValidator')) ? $this->currentRecord->getCMSValidator() : new RequiredFields();
179 $validator->setJavascriptValidationHandler('none');
180 $actions = $this->currentRecord->getCMSActions();
181
182 if($this->currentRecord->canEdit(Member::currentUser())){
183
184 }else{
185 $fields = $fields->makeReadonly();
186 }
187
188 if(!$this->currentRecord->canEdit(Member::currentUser())){
189 $fields = $fields->makeReadonly();
190 }
191 $actions->dataFieldByName('action_goBack')->setTitle(_t('CatalogAdmin.GOBACK', 'Go Back'));
192 $actions->removeByName('action_rollback');
193
194 $actions->dataFieldByName('action_delete')->setTitle(_t('CatalogAdmin.DELETE', 'Delete'));
195
196 $form = new Form($this, "EditForm", $fields, $actions, $validator);
197 $form->loadDataFrom($this->currentRecord);
198
199 return $form;
200 }
201
202
203 function publish($data, $form, $request){
204 $form->saveInto($this->currentRecord);
205
206 try {
207 $this->currentRecord->write();
208 } catch(ValidationException $e) {
209 $form->sessionMessage($e->getResult()->message(), 'bad');
210 }
211 $this->currentRecord->doPublish();
212 return $this->result($request);
213 }
214
215
216 function unpublish($data, $form, $request){
217 $this->currentRecord->doUnpublish();
218 return $this->result($request);
219 }
220
221
222 function save($data, $form, $request){
223 $form->saveInto($this->currentRecord);
224
225 try {
226 $this->currentRecord->write();
227 } catch(ValidationException $e) {
228 $form->sessionMessage($e->getResult()->message(), 'bad');
229 }
230
231 return $this->result($request);
232 }
233
234
235 function delete($data, $form, $request){
236 $this->currentRecord->doUnpublish();
237 $this->currentRecord->delete();
238 return;
239 }
240
241 function result($request){
242
243 if(Director::is_ajax()) {
244 return $this->edit($request);
245 } else {
246 Director::redirectBack();
247 }
248 }
249
250 }
251
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-