All Data Structures Functions Variables Pages
Controller.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
41  abstract class Controller extends CController
42  {
43  public function __construct($id, $module = null)
44  {
45  parent::__construct($id, $module);
46  }
47 
51  public function resolveAndGetModuleId()
52  {
53  return $this->getModule()->getId();
54  }
55 
56  public function renderBeginWidget($className, $properties = array())
57  {
58  ob_start();
59  $form = $this->beginWidget($className, $properties);
60  $content = ob_get_contents();
61  ob_end_clean();
62  return array($form, $content);
63  }
64 
65  public function renderEndWidget()
66  {
67  ob_start();
68  $this->endWidget();
69  $content = ob_get_contents();
70  ob_end_clean();
71  return $content;
72  }
73 
84  $searchModel,
85  $pageSize,
86  $stateMetadataAdapterClassName = null,
87  $dataCollection = null)
88  {
89  assert('is_int($pageSize) || $pageSize == null');
90  assert('$stateMetadataAdapterClassName == null || is_string($stateMetadataAdapterClassName)');
91  assert('$dataCollection instanceof SearchAttributesDataCollection || $dataCollection == null');
92  $listModelClassName = get_class($searchModel->getModel());
93  if ($dataCollection == null)
94  {
95  $dataCollection = new SearchAttributesDataCollection($searchModel);
96  }
97  $searchAttributes = $dataCollection->resolveSearchAttributesFromSourceData();
98  $dataCollection->resolveAnyMixedAttributesScopeForSearchModelFromSourceData();
99  $dataCollection->resolveSelectedListAttributesForSearchModelFromSourceData();
100  $dataCollection->resolveFilterByStarredFromSourceData();
101  $dataCollection->resolveFilteredByFromSourceData();
102  $dataCollection->resolveKanbanBoardOptionsForSearchModelFromSourceData();
103  $sanitizedSearchAttributes = GetUtil::sanitizePostByDesignerTypeForSavingModel($searchModel,
104  $searchAttributes);
105  $sortAttribute = $dataCollection->resolveSortAttributeFromSourceData($listModelClassName);
106  $sortDescending = $dataCollection->resolveSortDescendingFromSourceData($listModelClassName);
107  $metadataAdapter = new SearchDataProviderMetadataAdapter(
108  $searchModel,
109  Yii::app()->user->userModel->id,
110  $sanitizedSearchAttributes
111  );
112  $metadata = static::resolveDynamicSearchMetadata($searchModel, $metadataAdapter->getAdaptedMetadata(),
113  $dataCollection);
114  $this->resolveKanbanBoardMetadataBeforeMakingDataProvider($searchModel, $metadata);
115  $this->resolveFilteredByMetadataBeforeMakingDataProvider($searchModel, $metadata);
116  $this->resolveMetadataBeforeMakingDataProvider($metadata);
117  $dataProviderClassName = Yii::app()->custom->resolveDataProviderClassNameForControllerBySearchModel($searchModel);
119  $metadata,
120  $listModelClassName,
121  $dataProviderClassName,
122  $sortAttribute,
123  $sortDescending,
124  $pageSize,
125  $stateMetadataAdapterClassName
126  );
127  }
128 
129  protected function resolveKanbanBoardMetadataBeforeMakingDataProvider($searchForm, & $metadata)
130  {
131  if ($searchForm instanceof SearchForm)
132  {
133  if ($searchForm instanceof SearchForm && !Yii::app()->userInterface->isMobile() &&
134  $searchForm->getKanbanBoard() != null && $searchForm->getKanbanBoard()->getIsActive())
135  {
136  $searchForm->getKanbanBoard()->resolveVisibleValuesForAdaptedMetadata($metadata);
137  }
138  }
139  }
140 
141  protected function resolveFilteredByMetadataBeforeMakingDataProvider($searchForm, & $metadata)
142  {
143  }
144 
145  protected function resolveMetadataBeforeMakingDataProvider(& $metadata)
146  {
147  }
148 
149  protected static function resolveDynamicSearchMetadata($searchModel, $metadata, SearchAttributesDataCollection $dataCollection)
150  {
151  $sanitizedDynamicSearchAttributes = $dataCollection->getSanitizedDynamicSearchAttributes();
152  if ($sanitizedDynamicSearchAttributes == null)
153  {
154  return $metadata;
155  }
156  $dynamicStructure = $dataCollection->getDynamicStructure();
157  if ($sanitizedDynamicSearchAttributes != null)
158  {
159  $dynamicSearchMetadataAdapter = new DynamicSearchDataProviderMetadataAdapter($metadata,
160  $searchModel,
161  Yii::app()->user->userModel->id,
162  $sanitizedDynamicSearchAttributes,
163  $dynamicStructure);
164  $metadata = $dynamicSearchMetadataAdapter->getAdaptedDataProviderMetadata();
165  }
166  return $metadata;
167  }
168 
169  protected function makeDetailsAndRelationsView($model, $moduleClassName, $viewClassName, $redirectUrl, $breadCrumbView = null)
170  {
171  assert('$model instanceof RedBeanModel || $model instanceof CModel');
172  assert('$breadCrumbView == null || $breadCrumbView instanceof BreadCrumbView');
173  if ($breadCrumbView != null)
174  {
175  $verticalColumns = 2;
176  $primaryViewColumn = 1;
177  }
178  else
179  {
180  $verticalColumns = 1;
181  $primaryViewColumn = 0;
182  }
183 
184  $params = array_merge(
185  array(
186  'controllerId' => $this->getId(),
187  'relationModuleId' => $this->getModule()->getId(),
188  'relationModel' => $model,
189  'redirectUrl' => $redirectUrl,
190  ), $this->resolveAdditionalParametersForDetailsAndRelationsView($viewClassName));
191  $gridView = new GridView($verticalColumns, 1);
192  if ($breadCrumbView != null)
193  {
194  $gridView->setView($breadCrumbView, 0, 0);
195  }
196  $gridView->setView(new $viewClassName( $this->getId(),
197  $this->getModule()->getId(),
198  $params), $primaryViewColumn, 0);
199  return $gridView;
200  }
201 
202  protected function resolveAdditionalParametersForDetailsAndRelationsView($viewClassName)
203  {
204  return array();
205  }
206 
207  protected function makeTitleBarAndEditAndDetailsView($model, $renderType,
208  $titleBarAndEditViewClassName = 'TitleBarAndEditAndDetailsView')
209  {
210  assert('$model != null');
211  assert('$renderType == "Edit" || $renderType == "Details"');
212  assert('$titleBarAndEditViewClassName != null && is_string($titleBarAndEditViewClassName)');
213  return new $titleBarAndEditViewClassName(
214  $this->getId(),
215  $this->getModule()->getId(),
216  $model,
217  $this->getModule()->getPluralCamelCasedName(),
218  $renderType
219  );
220  }
221 
222  protected function makeEditAndDetailsView($model, $renderType)
223  {
224  assert('$model != null');
225  assert('$renderType == "Edit" || $renderType == "Details"');
226  $editViewClassName = get_class($model) . 'EditAndDetailsView';
227  return new $editViewClassName($renderType, $this->getId(), $this->getModule()->getId(), $model);
228  }
229 
230  protected function makeTitleBarAndEditView($model, $titleBarAndEditViewClassName)
231  {
232  assert('$model != null');
233  assert('$titleBarAndEditViewClassName != null && is_string($titleBarAndEditViewClassName)');
234  return new $titleBarAndEditViewClassName(
235  $this->getId(),
236  $this->getModule()->getId(),
237  $model,
238  $this->getModule()->getPluralCamelCasedName()
239  );
240  }
241 
242  protected function makeTitleBarAndDetailsView($model, $titleBarAndDetailsViewClassName = 'TitleBarAndDetailsView')
243  {
244  assert('$model != null');
245  assert('$titleBarAndDetailsViewClassName != null && is_string($titleBarAndDetailsViewClassName)');
246  return new $titleBarAndDetailsViewClassName(
247  $this->getId(),
248  $this->getModule()->getId(),
249  $model,
250  $this->getModule()->getPluralCamelCasedName()
251  );
252  }
253 
254  protected static function resolveActiveAttributesFromPostForMassAction($actionId)
255  {
256  // TODO: @Shoaibi/@Jason: Low: Candidate for MassActionController
257  return Yii::app()->request->getPost(ucfirst($actionId), array());
258  }
259 
260  protected function resolveActiveAttributesFromMassEditPost()
261  {
262  // TODO: @Shoaibi/@Jason: Low: Deprecated, Better to use resolveActiveAttributesFromPostForMassAction directly inside main code with actionId
263  // trigger_error('Deprecated: Recommended to use resolveActiveAttributesFromPostForMassAction.');
264  return static::resolveActiveAttributesFromPostForMassAction('massEdit');
265  }
266 
271  {
272  // TODO: @Shoaibi/@Jason: Low: Deprecated, Better to use resolveActiveAttributesFromPostForMassAction directly inside main code with actionId
273  // trigger_error('Deprecated: Recommended to use resolveActiveAttributesFromPostForMassAction.');
274  return static::resolveActiveAttributesFromPostForMassAction('massDelete');
275  }
276 
277  protected function makeMassEditView(
278  $model,
279  $activeAttributes,
280  $selectedRecordCount,
281  $title)
282  {
283  // TODO: @Shoaibi/@Jason: Low: Deprecated
284  // trigger_error('Deprecated');
285  $alertMessage = static::getMassEditAlertMessage(get_class($model));
286  $moduleName = $this->getModule()->getPluralCamelCasedName();
287  $moduleClassName = $moduleName . 'Module';
288  $title = Zurmo::t('Core', 'Mass Update') . ': ' . $title;
289  $massEditViewClassName = $moduleName . 'MassEditView';
290  $view = new $massEditViewClassName($this->getId(), $this->getModule()->getId(), $model, $activeAttributes,
291  $selectedRecordCount, $title, $alertMessage);
292  return $view;
293  }
294 
296  protected function makeMassDeleteView(
297  $model,
298  $activeAttributes,
299  $selectedRecordCount,
300  $title,
301  $massDeleteViewClassName = 'MassDeleteView',
302  $useModuleClassNameForItemLabel = true
303  )
304  {
305  // TODO: @Shoaibi/@Jason: Low: Deprecated
306  // trigger_error('Deprecated');
307  $moduleName = $this->getModule()->getPluralCamelCasedName();
308  $moduleClassName = $moduleName . 'Module';
309  $title = Zurmo::t('Core', 'Mass Delete') . ': ' . $title;
310  $view = new $massDeleteViewClassName($this->getId(), $this->getModule()->getId(), $model, $activeAttributes,
311  $selectedRecordCount, $title, null,
312  $moduleClassName, $useModuleClassNameForItemLabel);
313  return $view;
314  }
315 
316  protected static function getSelectedRecordCountByResolvingSelectAllFromGet($dataProvider, $countEmptyStringAsElement = true)
317  {
318  if (Yii::app()->request->getQuery('totalCount'))
319  {
320  return Yii::app()->request->getQuery('totalCount');
321  }
322  elseif (Yii::app()->request->getQuery('selectAll'))
323  {
324  return intval($dataProvider->calculateTotalItemCount());
325  }
326  else
327  {
328  if ($countEmptyStringAsElement)
329  {
330  return count(explode(",", trim(Yii::app()->request->getQuery('selectedIds'), ', '))); // Not Coding Standard
331  }
332  else
333  {
334  return count(array_filter(explode(",", trim(Yii::app()->request->getQuery('selectedIds'), " ,")))); // Not Coding Standard
335  }
336  }
337  }
338 
339  protected static function getMassActionProgressStartFromGet($pageVariableName, $pageSize)
340  {
341  // TODO: @Shoaibi/@Jason: Low: Candidate for MassActionController
342  $page = Yii::app()->request->getQuery($pageVariableName);
343  if ($page == 1)
344  {
345  return 1;
346  }
347  elseif ($page > 1)
348  {
349  return ((($page - 1) * $pageSize) +1);
350  }
351  else
352  {
353  throw new NotSupportedException();
354  }
355  }
356 
357  protected function getMassEditProgressStartFromGet($getVariableName, $pageSize)
358  {
359  // TODO: @Shoaibi/@Jason: Low: Deprecated
360  // trigger_error('Deprecated: Recommended to use getMassActionProgressStartFromGet. Pay close attention to arguments.');
361  return static::getMassActionProgressStartFromGet($getVariableName . '_page', $pageSize);
362  }
363 
367  protected function getMassDeleteProgressStartFromGet($getVariableName, $pageSize)
368  {
369  // TODO: @Shoaibi/@Jason: Low: Deprecated
370  // trigger_error('Deprecated: Recommended to use getMassActionProgressStartFromGet. Pay close attention to arguments.');
371  return static::getMassActionProgressStartFromGet($getVariableName . '_page', $pageSize);
372  }
373 
374  protected function attemptToValidateAjaxFromPost($model, $postVariableName)
375  {
376  if (isset($_POST['ajax']) && $_POST['ajax'] == 'edit-form')
377  {
378  $model->setAttributes($_POST[$postVariableName]);
379  $model->validate();
381  echo CJSON::encode($errorData);
382  Yii::app()->end(0, false);
383  }
384  }
385 
386  protected static function getModelsToUpdate($modelClassName, $dataProvider, $selectedRecordCount, $page, $pageSize)
387  {
388  // TODO: @Shoaibi/@Jason: Low: Candidate for MassActionController
389  if ($dataProvider === null)
390  {
391  $modelsToUpdate = array();
392  $IdsToUpdate = explode(",", Yii::app()->request->getQuery('selectedIds')); // Not Coding Standard
393  if ($page == 1)
394  {
395  $start = 0;
396  }
397  elseif ($page > 1)
398  {
399  $start = ($page - 1) * $pageSize;
400  }
401  else
402  {
403  throw new NotSupportedException();
404  }
405  if (($pageSize * $page) > $selectedRecordCount)
406  {
407  $end = $selectedRecordCount;
408  }
409  else
410  {
411  $end = $pageSize * $page;
412  }
413  for ($i = $start; $i < $end; ++$i) // Not Coding Standard
414  {
415  $modelsToUpdate[] = $modelClassName::getById(intval($IdsToUpdate[$i]));
416  }
417  return $modelsToUpdate;
418  }
419  else
420  {
421  $data = $dataProvider->getData();
422  return $data;
423  }
424  }
425 
426  protected function getModelsToSave($modelClassName, $dataProvider, $selectedRecordCount, $page, $pageSize)
427  {
428  // TODO: @Shoaibi/@Jason: Low: Deprecated
429  // trigger_error('Deprecated: Recommended to use getModelsToUpdate.');
430  return static::getModelsToUpdate($modelClassName, $dataProvider, $selectedRecordCount, $page, $pageSize);
431  }
432 
434  protected function getModelsToDelete($modelClassName, $dataProvider, $selectedRecordCount, $page, $pageSize)
435  {
436  // TODO: @Shoaibi/@Jason: Low: Deprecated
437  // trigger_error('Deprecated: Recommended to use getModelsToUpdate.');
438  return static::getModelsToUpdate($modelClassName, $dataProvider, $selectedRecordCount, $page, $pageSize);
439  }
440 
441  protected static function getMassEditAlertMessage($postVariableName)
442  {
443  // TODO: @Shoaibi/@Jason: Low: Deprecated
444  // trigger_error('Deprecated: Recommended to use resolveMassEditAlertMessage.');
445  return static::resolveMassEditAlertMessage($postVariableName);
446  }
447 
448  protected static function resolveMassEditAlertMessage($postVariableName)
449  {
450  // TODO: @Shoaibi/@Jason: Low: Candidate for MassActionController
451  $form = Yii::app()->request->getPost($postVariableName);
452  $save = Yii::app()->request->getPost('save');
453  if (!isset($form) && isset($save))
454  {
455  return Zurmo::t('Core', 'You must select at least one field to modify.');
456  }
457  }
458 
459  protected function makeListMergeView($model, $viewPrefix, $selectedList = array())
460  {
461  assert('$viewPrefix != null');
462  $editViewClassName = $viewPrefix . 'EditAndDetailsView';
463  return new $editViewClassName('Edit', $this->getId(), $this->getModule()->getId(), $model, $selectedList);
464  }
465  }
466 ?>
resolveAndGetModuleId()
Definition: Controller.php:51
makeMassDeleteView($model, $activeAttributes, $selectedRecordCount, $title, $massDeleteViewClassName= 'MassDeleteView', $useModuleClassNameForItemLabel=true)
Definition: Controller.php:296
getMassDeleteProgressStartFromGet($getVariableName, $pageSize)
Definition: Controller.php:367
resolveActiveAttributesFromMassDeletePost()
Definition: Controller.php:270
static makeErrorsDataAndResolveForOwnedModelAttributes($model)
makeRedBeanDataProviderByDataCollection($searchModel, $pageSize, $stateMetadataAdapterClassName=null, $dataCollection=null)
Definition: Controller.php:83
static sanitizePostByDesignerTypeForSavingModel($model, $postData)
Definition: GetUtil.php:98
getModelsToDelete($modelClassName, $dataProvider, $selectedRecordCount, $page, $pageSize)
Definition: Controller.php:434
static makeDataProvider(array $metadata, $listModelClassName, $dataProviderClassName, $sortAttribute, $sortDescending, $pageSize, $stateMetadataAdapterClassName=null)
Generated on Sat Mar 28 2020 07:10:38