Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
Element.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 
44  abstract class Element
45  {
46  public $params;
47 
48  public $editableTemplate = '<th>{label}</th><td colspan="{colspan}">{content}{error}</td>';
49 
50  public $nonEditableTemplate = '<th>{label}</th><td colspan="{colspan}">{content}</td>';
51 
52  protected $model;
53 
54  protected $attribute;
55 
56  protected $form;
57 
66  public function __construct($model, $attribute, $form = null, array $params = array())
67  {
68  assert('$attribute == null || is_string($attribute)');
69  assert('is_array($params)');
70  $this->model = $model;
71  $this->attribute = $attribute;
72  $this->form = $form;
73  $this->params = $params;
74  }
75 
80  public function render()
81  {
82  $className = get_called_class();
83  if ($this->form === null || $className::isReadOnly())
84  {
85  return $this->renderNonEditable();
86  }
87  return $this->renderEditable();
88  }
89 
94  protected function renderLabel()
95  {
96  if ($this->form === null)
97  {
98  return $this->getFormattedAttributeLabel();
99  }
100  $htmlOptions = array('for' => $this->resolveIdForLabel());
101  if (isset($this->params['labelHtmlOptions']))
102  {
103  $htmlOptions = CMap::mergeArray($htmlOptions, $this->params['labelHtmlOptions']);
104  }
105  return $this->form->labelEx($this->model, $this->attribute, $htmlOptions);
106  }
107 
108  protected function resolveIdForLabel()
109  {
110  return $this->getEditableInputId();
111  }
112 
113  protected function getFormattedAttributeLabel()
114  {
115  if (isset($this->params['labelHtmlOptions']['label']))
116  {
117  $label = $this->params['labelHtmlOptions']['label'];
118  }
119  else
120  {
121  $label = $this->model->getAttributeLabel($this->attribute);
122  }
123  return Yii::app()->format->text($label);
124  }
125 
126  protected function resolveNonActiveFormFormattedLabel($label)
127  {
128  if ($this->form === null)
129  {
130  return $label;
131  }
132  return ZurmoHtml::label($label, false);
133  }
134 
139  protected function renderError()
140  {
141  return $this->form->error($this->model, $this->attribute,
142  array('inputID' => $this->getEditableInputId()));
143  }
144 
152  protected function renderScopedErrorId($inputNameIdPrefix, $attribute)
153  {
154  assert('is_string($inputNameIdPrefix)');
155  assert('is_string($attribute)');
156  return get_class($this->model) . '_' . $inputNameIdPrefix . '_' . $attribute;
157  }
158 
164  protected function renderEditable()
165  {
166  $data = array();
167  $data['label'] = $this->renderLabel();
168  $data['content'] = $this->renderControlEditable();
169  $data['error'] = $this->renderError();
170  $data['colspan'] = $this->getColumnSpan();
171  return $this->resolveContentTemplate($this->editableTemplate, $data);
172  }
173 
179  protected function renderNonEditable()
180  {
181  $data = array();
182  $data['label'] = $this->renderLabel();
183  $data['content'] = $this->renderControlNonEditable();
184  $data['colspan'] = $this->getColumnSpan();
185  return $this->resolveContentTemplate($this->nonEditableTemplate, $data);
186  }
187 
188  abstract protected function renderControlEditable();
189 
190  abstract protected function renderControlNonEditable();
191 
195  protected function getColumnSpan()
196  {
197  if (ArrayUtil::getArrayValue($this->params, 'wide'))
198  {
199  return 3;
200  }
201  else
202  {
203  return 1;
204  }
205  }
206 
207  protected function getDisabledValue()
208  {
209  if (isset($this->params['disabled']) && $this->params['disabled'])
210  {
211  return 'disabled';
212  }
213  return null;
214  }
215 
216  protected function getHtmlOptions()
217  {
218  if (!isset($this->params['htmlOptions']))
219  {
220  return array();
221  }
222  return $this->params['htmlOptions'];
223  }
224 
229  public function getEditableNameIds()
230  {
231  $htmlOptions = array();
232  ZurmoHtml::resolveNameID($this->model, $this->attribute, $htmlOptions);
233  return array(
234  $htmlOptions['id']
235  );
236  }
237 
238  public static function getDisplayName()
239  {
240  return get_class();
241  }
242 
243  public static function isReadOnly()
244  {
245  return false;
246  }
247 
253  protected function resolveContentTemplate($template, $data)
254  {
255  assert('is_string($template)');
256  assert('is_array($data)');
257  $preparedContent = array();
258  foreach ($data as $templateVar => $content)
259  {
260  $preparedContent["{" . $templateVar . "}"] = $content;
261  }
262  return strtr($template, $preparedContent);
263  }
264 
265  public function getAttribute()
266  {
267  return $this->attribute;
268  }
269 
277  protected function getEditableInputId($attributeName = null, $relationAttributeName = null)
278  {
279  assert('$attributeName == null || is_string($attributeName)');
280  assert('$relationAttributeName == null || is_string($relationAttributeName)');
281  if ($attributeName == null)
282  {
283  $attributeName = $this->attribute;
284  }
285  $inputPrefix = $this->resolveInputIdPrefix();
286  $id = $inputPrefix . '_' . $attributeName;
287  if ($relationAttributeName != null)
288  {
289  $id .= '_' . $relationAttributeName;
290  }
291  return ZurmoHtml::getIdByName($id);
292  }
293 
303  protected function getEditableInputName($attributeName = null, $relationAttributeName = null)
304  {
305  assert('$attributeName == null || is_string($attributeName)');
306  assert('$relationAttributeName == null || is_string($relationAttributeName)');
307  if ($attributeName == null)
308  {
309  $attributeName = $this->attribute;
310  }
311  $inputPrefix = $this->resolveInputNamePrefix();
312  $name = $inputPrefix . static::resolveInputNameForEditableInput($attributeName);
313  if ($relationAttributeName != null)
314  {
315  assert('strpos($relationAttributeName, "[") === false && strpos($relationAttributeName, "]") === false');
316  $name .= '[' . $relationAttributeName . ']';
317  }
318  return $name;
319  }
320 
325  public static function resolveInputNameForEditableInput($attributeName)
326  {
327  assert('is_string($attributeName)');
328  $modifiedAttributeName = str_replace('[', '][', $attributeName);
329  $modifiedAttributeName = '[' . $modifiedAttributeName . ']';
330  $modifiedAttributeName = str_replace(']]', ']', $modifiedAttributeName);
331  return $modifiedAttributeName;
332  }
333 
340  protected function resolveInputPrefix()
341  {
342  if (isset($this->params['inputPrefix']) && $this->params['inputPrefix'])
343  {
344  assert('(is_array($this->params["inputPrefix"]) && count($this->params["inputPrefix"]) > 0) ||
345  (is_string($this->params["inputPrefix"]) && $this->params["inputPrefix"] != "")');
346  return $this->params['inputPrefix'];
347  }
348  return get_class($this->model);
349  }
350 
358  private function getInputForIdPrefix()
359  {
360  if (isset($this->params['inputIdPrefix']) && $this->params['inputIdPrefix'])
361  {
362  assert('(is_array($this->params["inputIdPrefix"]) && count($this->params["inputIdPrefix"]) > 0) ||
363  (is_string($this->params["inputIdPrefix"]) && $this->params["inputIdPrefix"] != "")');
364  return $this->params['inputIdPrefix'];
365  }
366  }
367 
368  protected function resolveInputIdPrefix()
369  {
370  $inputIdPrefix = $this->getInputForIdPrefix();
371  if ($inputIdPrefix == null)
372  {
373  $inputIdPrefix = $this->resolveInputPrefix();
374  }
375  if (is_array($inputIdPrefix))
376  {
377  if (count($inputIdPrefix) > 1)
378  {
379  $inputPrefixContent = null;
380  foreach ($inputIdPrefix as $value)
381  {
382  if ($inputPrefixContent != null)
383  {
384  $inputPrefixContent .= '_';
385  }
386  $inputPrefixContent .= $value;
387  }
388  return $inputPrefixContent;
389  }
390  }
391  elseif (!is_string($inputIdPrefix))
392  {
393  throw new NotSupportedException();
394  }
395  return $inputIdPrefix;
396  }
397 
398  protected function resolveInputNamePrefix()
399  {
400  return static::resolveInputNamePrefixIntoString($this->resolveInputPrefix());
401  }
402 
403  public static function resolveInputNamePrefixIntoString($inputNamePrefix)
404  {
405  assert('is_string($inputNamePrefix) || is_array($inputNamePrefix)');
406  if (is_array($inputNamePrefix))
407  {
408  if (count($inputNamePrefix) > 1)
409  {
410  $inputPrefixContent = null;
411  $firstPrefixPlaced = false;
412  foreach ($inputNamePrefix as $value)
413  {
414  if (!$firstPrefixPlaced)
415  {
416  $inputPrefixContent .= $value;
417  $firstPrefixPlaced = true;
418  }
419  else
420  {
421  $inputPrefixContent .= '[' . $value . ']';
422  }
423  }
424  return $inputPrefixContent;
425  }
426  }
427  elseif (!is_string($inputNamePrefix))
428  {
429  throw new NotSupportedException();
430  }
431  return $inputNamePrefix;
432  }
433 
434  public static function resolveInputIdPrefixIntoString($inputIdPrefix)
435  {
436  assert('is_string($inputIdPrefix) || is_array($inputIdPrefix)');
437  if (is_array($inputIdPrefix))
438  {
439  if (count($inputIdPrefix) > 1)
440  {
441  $inputPrefixContent = null;
442  foreach ($inputIdPrefix as $value)
443  {
444  if ($inputPrefixContent != null)
445  {
446  $inputPrefixContent .= '_';
447  }
448  $inputPrefixContent .= $value;
449  }
450  return $inputPrefixContent;
451  }
452  }
453  elseif (!is_string($inputIdPrefix))
454  {
455  throw new NotSupportedException();
456  }
457  return $inputIdPrefix;
458  }
459 
460  protected function getListViewGridId()
461  {
462  return ArrayUtil::getArrayValueWithExceptionIfNotFound($this->params, 'listViewGridId');
463  }
464 
465  public function renderAsControlNonEditable()
466  {
467  return $this->renderControlNonEditable();
468  }
469  }
470 ?>
renderScopedErrorId($inputNameIdPrefix, $attribute)
Definition: Element.php:152
renderLabel()
Definition: Element.php:94
static resolveInputNameForEditableInput($attributeName)
Definition: Element.php:325
resolveInputPrefix()
Definition: Element.php:340
static getArrayValue($array, $element, $defaultValue=null)
Definition: ArrayUtil.php:45
getColumnSpan()
Definition: Element.php:195
__construct($model, $attribute, $form=null, array $params=array())
Definition: Element.php:66
getEditableInputName($attributeName=null, $relationAttributeName=null)
Definition: Element.php:303
render()
Definition: Element.php:80
resolveContentTemplate($template, $data)
Definition: Element.php:253
getEditableNameIds()
Definition: Element.php:229
renderNonEditable()
Definition: Element.php:179
static getArrayValueWithExceptionIfNotFound($array, $element)
Definition: ArrayUtil.php:72
renderError()
Definition: Element.php:139
renderEditable()
Definition: Element.php:164
getEditableInputId($attributeName=null, $relationAttributeName=null)
Definition: Element.php:277
Generated on Mon Jul 6 2020 07:10:29
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.