All Data Structures Functions Variables Pages
ModelAttributeAndElementDataToMergeItem.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  {
46  protected $model;
47 
52  protected $attribute;
53 
58  protected $element;
59 
64  protected $primaryModel;
65 
70  protected $position;
71 
79  public function __construct(RedBeanModel $model, $attribute, $element, RedBeanModel $primaryModel, $position)
80  {
81  assert('is_string($attribute)');
82  assert('is_int($position)');
83  assert('$element instanceof Element');
84  $this->model = $model;
85  $this->attribute = $attribute;
86  $this->element = $element;
87  $this->primaryModel = $primaryModel;
88  $this->position = $position;
89  }
90 
95  public function getAttributeRenderedContent()
96  {
97  return $this->decorateContent($this->model->{$this->attribute});
98  }
99 
105  {
106  $interfaces = class_implements($this->element);
107  $elementClassName = get_class($this->element);
108  if (in_array('DerivedElementInterface', $interfaces))
109  {
110  if ($this->element instanceof DropDownElement || $this->element instanceof TagCloudElement)
111  {
112  $attributeInputIdMap[] = $this->element->getIdForSelectInput();
113  }
114  else
115  {
116  $attributes = $elementClassName::getModelAttributeNames();
117  foreach ($attributes as $attribute)
118  {
119  $attributeInputIdMap[] = $this->getDerivedInputId($attribute);
120  }
121  }
122  }
123  elseif (in_array('MultipleAttributesElementInterface', $interfaces))
124  {
125  $relatedAttributes = $elementClassName::getModelAttributeNames();
126  foreach ($relatedAttributes as $relatedAttribute)
127  {
128  $attributeInputIdMap[] = $this->getDerivedInputId($this->attribute, $relatedAttribute);
129  }
130  }
131  else
132  {
133  if ($this->element instanceof ModelElement)
134  {
135  $attributeInputIdMap[] = $this->getDerivedInputId($this->attribute, 'name');
136  }
137  else
138  {
139  $attributeInputIdMap[] = $this->getNonDerivedInputId();
140  }
141  }
142  return $attributeInputIdMap;
143  }
144 
149  protected function getNonDerivedInputId()
150  {
151  return $this->resolveInputId($this->attribute);
152  }
153 
160  protected function getDerivedInputId($attribute, $relatedAttribute)
161  {
162  assert('is_string($attribute)');
163  assert('is_string($relatedAttribute)');
164  return $this->resolveInputId($attribute, $relatedAttribute);
165  }
166 
173  private function resolveInputId($attribute, $relatedAttribute = null)
174  {
175  assert('is_string($attribute)');
176  assert('is_string($relatedAttribute) || ($relatedAttribute === null)');
177  if ($this->model->$attribute instanceof CustomField)
178  {
179  $inputId = Element::resolveInputIdPrefixIntoString(array(get_class($this->model), $attribute, 'value'));
180  }
181  elseif ($relatedAttribute != null)
182  {
183  $inputId = Element::resolveInputIdPrefixIntoString(array(get_class($this->model), $attribute, $relatedAttribute));
184  }
185  else
186  {
187  $inputId = Element::resolveInputIdPrefixIntoString(array(get_class($this->model), $attribute));
188  }
189  return $inputId;
190  }
191 
197  protected function decorateContent($content)
198  {
199  $decoratedContent = null;
200 
201  $inputIds = $this->getAttributeInputIdsForOnClick();
202  if ($this->model->id == $this->primaryModel->id)
203  {
204  $class = ' selected';
205  }
206  else
207  {
208  $class = '';
209  }
210  foreach ($inputIds as $inputId)
211  {
212  $inputIdArray = explode('_', $inputId);
213  $attribute = $inputIdArray[1];
214  $relatedAttribute = null;
215  //If related attribute is there
216  if (count($inputIdArray) > 2)
217  {
218  $relatedAttribute = $inputIdArray[2];
219  }
220  if ($relatedAttribute == null)
221  {
222  $inputValue = $this->model->$attribute;
223  $displayValue = $inputValue;
224  if ($displayValue == null)
225  {
226  $displayValue = Zurmo::t('Core', '(None)');
227  }
228  }
229  elseif ($this->element instanceof ModelElement)
230  {
231  $inputValue = $this->model->$attribute->$relatedAttribute;
232  $displayValue = $this->resolveDisplayedValueForRelatedAttribute($attribute, $relatedAttribute);
233  $hiddenInputValue = $this->model->$attribute->id;
234  $hiddenInputId = $this->getDerivedInputId($attribute, 'id');
235  if ($displayValue == null)
236  {
237  $displayValue = Zurmo::t('Core', '(None)');
238  }
239  if ($hiddenInputValue == '-27')
240  {
241  $hiddenInputValue = '';
242  }
243  }
244  elseif ($this->element instanceof TagCloudElement)
245  {
246  $values = $this->model->$attribute->getValues();
247  $inputValue = $this->model->$attribute->stringifyOneToManyRelatedModelsValues($values);
248  $displayValue = $this->model->stringifyOneToManyRelatedModelsValues($values);
249 
250  if ($inputValue == null)
251  {
252  $inputValue = '';
253  }
254  if ($displayValue == null)
255  {
256  $displayValue = Zurmo::t('Core', '(None)');
257  }
258  }
259  else
260  {
261  $inputValue = $this->model->$attribute->$relatedAttribute;
262  $displayValue = $this->resolveDisplayedValueForRelatedAttribute($attribute, $relatedAttribute);
263  if (($displayValue == null && ($this->element instanceof DropDownElement && !empty($this->element->params) &&
264  $this->element->params['addBlank'])) || ($content == Zurmo::t('Core', '(None)') && !$this->element instanceof AddressElement))
265  {
266  $displayValue = Zurmo::t('Core', '(None)');
267  }
268  }
269  if ($displayValue != null)
270  {
271  if ($this->element instanceof ModelElement)
272  {
273  $decoratedContent .= ZurmoHtml::link($displayValue, '#', array('data-id' => $inputId,
274  'data-value' => $inputValue,
275  'data-hiddenid' => $hiddenInputId,
276  'data-hiddenvalue' => $hiddenInputValue,
277  'class' => 'possible attributePreElementContentModelElement merge-color-' . $this->position . $class));
278  }
279  elseif ($this->element instanceof TagCloudElement)
280  {
281  $decoratedContent .= ZurmoHtml::link($displayValue, '#', array('data-id' => $inputId,
282  'data-value' => $inputValue,
283  'class' => 'possible attributePreTagCloudElementContent merge-color-' . $this->position . $class));
284  }
285  elseif ($this->element instanceof MultiSelectDropDownElement)
286  {
287  $decoratedContent .= ZurmoHtml::link($displayValue, '#', array('data-id' => $inputId,
288  'data-value' => $inputValue,
289  'class' => 'possible attributePreMultiSelectDropDownElementContent merge-color-' . $this->position . $class));
290  }
291  else
292  {
293  $decoratedContent .= ZurmoHtml::link($displayValue, '#', array('data-id' => $inputId,
294  'data-value' => $inputValue,
295  'class' => 'possible attributePreElementContent merge-color-' . $this->position . $class));
296  }
297  }
298  }
299 
300  if ($decoratedContent == null)
301  {
302  if ($this->element instanceof DropDownElement)
303  {
304  $decoratedContent = null;
305  }
306  else
307  {
308  $decoratedContent = ZurmoHtml::tag('span', array('class' => 'possible merge-color-' . $this->position), Zurmo::t('Core', '(None)'));
309  }
310  }
311  if ($this->element instanceof AddressElement)
312  {
313  $decoratedContent .= '</br>';
314  }
315  return $decoratedContent;
316  }
317 
324  protected function resolveDisplayedValueForRelatedAttribute($attribute, $relatedAttribute)
325  {
326  return $this->model->$attribute->$relatedAttribute;
327  }
328  }
329 ?>
__construct(RedBeanModel $model, $attribute, $element, RedBeanModel $primaryModel, $position)
Generated on Thu May 28 2020 07:10:36