All Data Structures Functions Variables Pages
MassActionMenuActionElement.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  {
42  const SELECTED_MENU_TYPE = 1;
43 
44  const ALL_MENU_TYPE = 0;
45 
46  const DROPDOWN_ID = 'ListViewExportActionMenu';
47 
48  protected $gridId;
49 
50  protected $selectedMenuItemName;
51 
52  protected $allMenuItemName;
53 
54  abstract protected function getActionId();
55 
56  abstract protected function getSelectedMenuNameSuffix();
57 
58  abstract protected function getAllMenuNameSuffix();
59 
60  abstract protected function getScriptNameSuffixForSelectedMenu();
61 
62  abstract protected function getScriptNameSuffixForAllMenu();
63 
64  public static function getDropDownId()
65  {
66  return static::DROPDOWN_ID;
67  }
68 
69  public function __construct($controllerId, $moduleId, $modelId, $params = array())
70  {
71  parent::__construct($controllerId, $moduleId, $modelId, $params);
72  $this->gridId = $this->getListViewGridId();
73  $this->selectedMenuItemName = $this->gridId . $this->getSelectedMenuNameSuffix();
74  $this->allMenuItemName = $this->gridId . $this->getAllMenuNameSuffix();
75  $this->params['redirectUrl'] = $this->resolveRedirectUrl();
76  $this->registerUnifiedEventHandler();
77  }
78 
79  public function render()
80  {
81  $this->registerMenuScripts();
82  return parent::render();
83  }
84 
85  public function getActionNameForCurrentElement()
86  {
87  return $this->getActionId();
88  }
89 
90  public function getActionType()
91  {
92  throw new NotSupportedException();
93  }
94 
95  public function registerUnifiedEventHandler()
96  {
97  if (Yii::app()->clientScript->isScriptRegistered('massActionMenuActionElementEventHandler'))
98  {
99  return;
100  }
101  else
102  {
103  // Begin Not Coding Standard
104  Yii::app()->clientScript->registerScript('massActionMenuActionElementEventHandler', "
105  function massActionMenuActionElementEventHandler(elementType, gridId, baseUrl, actionId,
106  pageVarName, minSelected, alertMessage)
107  {
108  selectAll = '';
109  if (elementType == " . static::SELECTED_MENU_TYPE . ")
110  {
111  var selectedIdsString = $('#' + gridId + '-selectedIds').val();
112  var numberSelected = selectedIdsString == '' ? 0 : selectedIdsString.split(',').length;
113  if (numberSelected < minSelected)
114  {
115  alert(alertMessage);
116  $(this).val('');
117  return false;
118  }
119  }
120  else
121  {
122  selectAll = 1;
123  }
124  var options =
125  {
126  url : $.fn.yiiGridView.getUrl(gridId),
127  baseUrl : baseUrl
128  }
129  if (options.url.split( '?' ).length == 2)
130  {
131  options.url = options.baseUrl + '/' + actionId + '?' + options.url.split( '?' )[1];
132  }
133  else
134  {
135  options.url = options.baseUrl + '/' + actionId;
136  }
137  if (elementType == " . static::SELECTED_MENU_TYPE . ")
138  {
139  addListViewSelectedIdsToUrl(gridId, options);
140  }
141  var data = '' + actionId + '=' + '&selectAll=' + selectAll + '&ajax=&' + pageVarName + '=1';
142  url = $.param.querystring(options.url, data);
143  url += '" . $this->resolveAdditionalQueryStringData() ."';
144  window.location.href = url;
145  return false;
146  }
147  ");
148  // End Not Coding Standard
149  }
150  }
151 
152  public function getOptGroup()
153  {
154  return $this->getMenuHeader();
155  }
156 
157  public function getOptions()
158  {
159  return $this->getMenuItems();
160  }
161 
162  public function getElementValue()
163  {
164  return null; // because Selected and All Results would have their own and we can't determine that here.
165  }
166 
167  public function registerDropDownScripts($dropDownId = null, $scriptName = null)
168  {
169  if (!$dropDownId)
170  {
171  $dropDownId = static::getDropDownId();
172  }
173  if (!$scriptName)
174  {
175  $scriptName = $dropDownId;
176  }
177  if (Yii::app()->clientScript->isScriptRegistered($scriptName))
178  {
179  return;
180  }
181  else
182  {
183  // Begin Not Coding Standard
184  Yii::app()->clientScript->registerScript($scriptName, "
185  $('#" . $dropDownId . "').unbind('change.action').bind('change.action', function()
186  {
187  // Heavy dependence on DOM...
188  selectedOption = $(this).find(':selected');
189  selectedOptionId = selectedOption.attr('id');
190  if (selectedOptionId)
191  {
192  selectedOptionValue = selectedOption.val();
193  optionType = selectedOptionId.slice(-3);
194  actionName = selectedOptionValue.slice(0, selectedOptionValue.indexOf('_'));
195  if (optionType == 'All')
196  {
197  menuType = " . static::ALL_MENU_TYPE . ";
198  }
199  else
200  {
201  menuType = " . static::SELECTED_MENU_TYPE . ";
202  }
203  $('#" . $dropDownId . "').val('');
204  massActionMenuActionElementEventHandler(" .
205  "menuType, " .
206  " '" . $this->gridId. "'," .
207  " '" . Yii::app()->createUrl($this->moduleId . '/' . $this->getControllerId()) . "'," .
208  " actionName," .
209  " '" . $this->getPageVarName() . "'," .
210  " '" . $this->getMinimumNumberOfSelectedElements() . "'," .
211  " '" . $this->getAlertMessage() . "'" .
212  ");
213  }
214  }
215  );
216  ");
217  // End Not Coding Standard
218  }
219  }
220 
221  protected function resolveAdditionalQueryStringData()
222  {
223  return null;
224  }
225 
226  protected function registerMenuScripts()
227  {
228  $this->registerScriptForAllMenu();
229  $this->registerScriptForSelectedMenu();
230  }
231 
232  protected function registerScriptForSelectedMenu()
233  {
234  $this->registerScriptForMenuType(static::SELECTED_MENU_TYPE);
235  }
236 
237  protected function registerScriptForAllMenu()
238  {
239  $this->registerScriptForMenuType(static::ALL_MENU_TYPE);
240  }
241 
242  protected function registerScriptForMenuType($menuType)
243  {
244  if ($menuType === static::SELECTED_MENU_TYPE)
245  {
246  $scriptNameSuffix = $this->getScriptNameSuffixForSelectedMenu();
247  $menuItemName = $this->selectedMenuItemName;
248  }
249  else
250  {
251  $scriptNameSuffix = $this->getScriptNameSuffixForAllMenu();
252  $menuItemName = $this->allMenuItemName;
253  }
254  Yii::app()->clientScript->registerScript($this->gridId . $scriptNameSuffix,
255  "$('#" . $menuItemName . "').unbind('click.action').bind('click.action', function()
256  {
257  " . $this->getEventHandlerScriptContentForMenuType($menuType) ."
258  }
259  );");
260  }
261 
262  protected function getEventHandlerScriptContentForMenuType($menuType)
263  {
264  // Begin Not Coding Standard
265  return "massActionMenuActionElementEventHandler(" .
266  $menuType . ",".
267  " '" . $this->gridId. "',".
268  " '" . Yii::app()->createUrl($this->moduleId . '/' . $this->getControllerId()) . "'," .
269  " '" . $this->getActionId(). "'," .
270  " '" . $this->getPageVarName() . "'," .
271  " '" . $this->getMinimumNumberOfSelectedElements() . "'," .
272  " '" . $this->getAlertMessage() . "'" .
273  ")";
274  // End Not Coding Standard
275  }
276 
277  protected function getMenuItems()
278  {
279  return array(
280  array('label' => Zurmo::t('Core', 'Selected'),
281  'url' => '#',
282  'itemOptions' => array( 'id' => $this->selectedMenuItemName)),
283  array('label' => Zurmo::t('Core', 'All Results'),
284  'url' => '#',
285  'itemOptions' => array( 'id' => $this->allMenuItemName)));
286  }
287 
288  protected function getMenuHeader()
289  {
290  return $this->getLabel();
291  }
292 
293  protected function getMenuId()
294  {
295  return get_class($this);
296  }
297 
298  protected function getListViewGridId()
299  {
300  // TODO: @Shoaibi/@Jason: Low: Create a common parent for Element and ActionElement, put this there.
301  return ArrayUtil::getArrayValueWithExceptionIfNotFound($this->params, 'listViewGridId');
302  }
303 
304  protected function getPageVarName()
305  {
306  // TODO: @Shoaibi/@Jason: Low: Create a common parent for Element and ActionElement, put this there.
307  return ArrayUtil::getArrayValueWithExceptionIfNotFound($this->params, 'pageVarName');
308  }
309 
310  protected function getDefaultRoute()
311  {
312  return null;
313  }
314 
315  protected function getDefaultLabel()
316  {
317  throw new NotSupportedException;
318  }
319 
320  protected function getControllerId()
321  {
322  $controllerId = ArrayUtil::getArrayValue($this->params, 'controllerId');
323  if ($controllerId)
324  {
325  return $controllerId;
326  }
327  else
328  {
329  return $this->controllerId;
330  }
331  }
332 
333  protected function resolveRedirectUrl()
334  {
335  return Yii::app()->createUrl($this->moduleId . '/' . $this->controllerId . '/details',
336  array('id' => $this->modelId));
337  }
338 
344  {
345  return 1;
346  }
347 
348  protected function getAlertMessage()
349  {
350  return Zurmo::t('Core', 'At least {n} record must be selected|At least {n} records must be selected',
352  }
353  }
354 ?>
static getArrayValue($array, $element, $defaultValue=null)
Definition: ArrayUtil.php:45
static getArrayValueWithExceptionIfNotFound($array, $element)
Definition: ArrayUtil.php:72
Generated on Fri Jun 5 2020 07:10:26