Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
ListViewMergeUtil.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  {
50  public static function resolveElementDuringFormLayoutRenderForListViewMerge( & $element,
51  $preContentViewClass,
52  $selectedModels,
53  $primaryModel,
54  $modelAttributeAndElementDataToMergeItemClass
55  )
56  {
57  assert('is_string($preContentViewClass)');
58  assert('is_array($selectedModels)');
59  assert('$primaryModel instanceof RedBeanModel');
60  assert('is_string($modelAttributeAndElementDataToMergeItemClass)');
61 
62  if ($element->getAttribute() != 'null')
63  {
64  $attributes = array($element->getAttribute());
65  }
66  else
67  {
68  $elementClassName = get_class($element);
69  $attributes = $elementClassName::getModelAttributeNames();
70  }
71  $preContent = Yii::app()->getController()->widget(
72  $preContentViewClass,
73  array(
74  'selectedModels' => $selectedModels,
75  'attributes' => $attributes,
76  'primaryModel' => $primaryModel,
77  'element' => $element,
78  'modelAttributeAndElementDataToMergeItemClass'
79  => $modelAttributeAndElementDataToMergeItemClass
80  ),
81  true);
82  $element->editableTemplate = '<th>{label}</th><td colspan="{colspan}">' . $preContent . '{content}{error}</td>';
83  }
84 
90  public static function setPrimaryModelForListViewMerge($model, $getData)
91  {
92  assert('$model instanceof ModelsListDuplicateMergedModelForm');
93  assert('is_array($getData)');
94  $modelsList = $model->selectedModels;
95  if (isset($getData['primaryModelId']))
96  {
97  $model->primaryModel = $modelsList[$getData['primaryModelId']];
98  }
99  else
100  {
101  $models = array_values($modelsList);
102  if (!empty($models))
103  {
104  $model->primaryModel = $models[0];
105  }
106  }
107  }
108 
115  public static function getSelectedModelsListForMerge($modelClassName, $getData)
116  {
117  assert('is_string($modelClassName)');
118  assert('is_array($getData)');
119  $modelsList = array();
120  if (isset($getData['selectedIds']) && $getData['selectedIds'] != null)
121  {
122  $selectedIds = explode(',', $getData['selectedIds']); // Not Coding Standard
123  foreach ($selectedIds as $id)
124  {
125  $model = $modelClassName::getById(intval($id));
126  $modelsList[$id] = $model;
127  }
128  }
129  return $modelsList;
130  }
131 
137  public static function processCopyRelationsAndDeleteNonPrimaryModelsInMerge($primaryModel, $getData)
138  {
139  assert('$primaryModel instanceof RedBeanModel');
140  assert('is_array($getData)');
141  $modelClassName = get_class($primaryModel);
142  $selectedModelsList = self::getSelectedModelsListForMerge($modelClassName, $getData);
143  self::processAssignRelationsToMergedModelFromModelsToBeDeleted($selectedModelsList, $primaryModel);
144  foreach ($selectedModelsList as $selectedModel)
145  {
146  if ($selectedModel->id != $primaryModel->id
147  &&(get_class($selectedModel) == get_class($primaryModel)))
148  {
150  $selectedModel->delete();
151  }
152  }
153  }
154 
160  protected static function processAssignRelationsToMergedModelFromModelsToBeDeleted($selectedModelsList, $primaryModel)
161  {
162  assert('is_array($selectedModelsList)');
163  foreach ($selectedModelsList as $selectedModel)
164  {
165  if ($selectedModel->getClassId('Item') != $primaryModel->getClassId('Item') &&
166  (get_class($selectedModel) == get_class($primaryModel)))
167  {
168  self::processNonDerivedRelationsAssignment($primaryModel, $selectedModel);
169  self::processDerivedRelationsAssignment($primaryModel, $selectedModel);
170  if ($primaryModel instanceof Account || $primaryModel instanceof Contact)
171  {
172  self::processCopyEmailActivity($primaryModel, $selectedModel);
173  }
174  }
175  }
176  $primaryModel->save();
177  }
178 
184  protected static function processNonDerivedRelationsAssignment($primaryModel, $selectedModel)
185  {
186  assert('$primaryModel instanceof RedBeanModel');
187  assert('$selectedModel instanceof RedBeanModel');
188  $modelClassName = get_class($primaryModel);
189  foreach ($selectedModel->attributeNames() as $attribute)
190  {
191  if ($attribute == 'owner')
192  {
193  continue;
194  }
195  if ($modelClassName::isRelation($attribute) &&
196  !$modelClassName::isOwnedRelation($attribute) &&
197  !$primaryModel->isAttributeReadOnly($attribute))
198  {
199  //Has one
200  if ($modelClassName::isRelationTypeAHasOneVariant($attribute))
201  {
202  $primaryModel->$attribute = $selectedModel->$attribute;
203  }
204  //Has many || Many many
205  if (($modelClassName::isRelationTypeAHasManyVariant($attribute) ||
206  $modelClassName::isRelationTypeAManyManyVariant($attribute)) &&
207  ($modelClassName::getRelationType($attribute) != RedBeanModel::HAS_MANY_BELONGS_TO)
208  )
209  {
210  foreach ($selectedModel->$attribute as $offset => $relatedModel)
211  {
212  if (!$primaryModel->$attribute->contains($relatedModel))
213  {
214  $primaryModel->$attribute->add($relatedModel);
215  }
216  }
217  }
218  }
219  }
220  }
221 
227  protected static function processDerivedRelationsAssignment($primaryModel, $selectedModel)
228  {
229  assert('$primaryModel instanceof RedBeanModel');
230  assert('$selectedModel instanceof RedBeanModel');
231  $metadata = $selectedModel->getMetadata();
232  foreach ($metadata as $modelClassName => $modelClassMetadata)
233  {
234  if (isset($metadata[$modelClassName]["derivedRelationsViaCastedUpModel"]))
235  {
236  foreach ($metadata[$modelClassName]["derivedRelationsViaCastedUpModel"] as $relation => $derivedRelationData)
237  {
238  $opposingModelClassName = $derivedRelationData[1];
239  $opposingRelation = $derivedRelationData[2];
240  if ($opposingRelation == 'activityItems' &&
241  is_subclass_of($opposingModelClassName, 'Activity'))
242  {
243  $opposingModels = $opposingModelClassName::getByActivityItemsCastedDown($selectedModel->getClassId('Item'));
244  if ($opposingModels != null)
245  {
246  foreach ($opposingModels as $opposingModel)
247  {
248  $opposingModel->activityItems->add($primaryModel);
249  $opposingModel->save();
250  }
251  }
252  }
253  }
254  }
255  }
256  }
257 
263  public static function resolveFormLayoutMetadataForOneColumnDisplay($metadata)
264  {
265  $modifiedElementsData = array();
266  foreach ($metadata['global']['panels'] as $panel)
267  {
268  foreach ($panel['rows'] as $row)
269  {
270  foreach ($row['cells'] as $cell)
271  {
272  foreach ($cell['elements'] as $elementData)
273  {
274  if ($elementData['attributeName'] == 'null' && !class_exists($elementData['type'] . 'Element'))
275  {
276  continue;
277  }
278  elseif ($elementData['type'] == 'TitleFullName')
279  {
280  $modifiedElementsData[] = array('attributeName' => 'title', 'type' => 'DropDown', 'addBlank' => true);
281  $modifiedElementsData[] = array('attributeName' => 'firstName', 'type' => 'Text');
282  $modifiedElementsData[] = array('attributeName' => 'lastName', 'type' => 'Text');
283  }
284  else
285  {
286  $modifiedElementsData[] = $elementData;
287  }
288  }
289  }
290  }
291  }
292  //Prepare panels data
293  $panelsData = array();
294  foreach ($modifiedElementsData as $row => $elementData)
295  {
296  $panelsData[0]['rows'][$row]['cells'][0]['elements'][0] = $elementData;
297  }
298  $metadata['global']['panels'] = $panelsData;
299  return $metadata;
300  }
301 
308  public static function processCopyEmailActivity($primaryModel, $selectedModel)
309  {
310  $searchAttributesData = LatestActivitiesUtil::
312  array($selectedModel->getClassId('Item')),
313  LatestActivitiesConfigurationForm::OWNED_BY_FILTER_ALL);
314 
315  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('EmailMessage');
316  $where = RedBeanModelDataProvider::makeWhere('EmailMessage', $searchAttributesData[0]['EmailMessage'], $joinTablesAdapter);
317  $models = EmailMessage::getSubset($joinTablesAdapter, null, null, $where, null);
318  if (isset($searchAttributesData[1]['EmailMessage']))
319  {
320  $where = RedBeanModelDataProvider::makeWhere('EmailMessage', $searchAttributesData[1]['EmailMessage'], $joinTablesAdapter);
321  $models = array_merge($models, EmailMessage::getSubset($joinTablesAdapter, null, null, $where, null));
322  }
323  foreach ($models as $model)
324  {
325  //Resolve sender
326  if ($model->sender->personsOrAccounts->contains($selectedModel))
327  {
328  $model->sender->personsOrAccounts->remove($selectedModel);
329  if (!$model->sender->personsOrAccounts->contains($primaryModel))
330  {
331  $model->sender->personsOrAccounts->add($primaryModel);
332  }
333  }
334  //recipients
335  foreach ($model->recipients as $key => $unused)
336  {
337  if ($model->recipients[$key]->personsOrAccounts->contains($selectedModel))
338  {
339  $model->recipients[$key]->personsOrAccounts->remove($selectedModel);
340  if (!$model->recipients[$key]->personsOrAccounts->contains($primaryModel))
341  {
342  $model->recipients[$key]->personsOrAccounts->add($primaryModel);
343  }
344  }
345  }
346  $model->save();
347  }
348  }
349  }
350 ?>
static processCopyEmailActivity($primaryModel, $selectedModel)
static processCopyRelationsAndDeleteNonPrimaryModelsInMerge($primaryModel, $getData)
static processDerivedRelationsAssignment($primaryModel, $selectedModel)
static getSelectedModelsListForMerge($modelClassName, $getData)
static resolveAccessCanCurrentUserDeleteModel(RedBeanModel $model, $fromAjax=false)
static getSearchAttributesDataByModelClassNamesAndRelatedItemIds($modelClassNames, $relationItemIds, $ownedByFilter)
static makeWhere($modelClassName, array $metadata, &$joinTablesAdapter)
static setPrimaryModelForListViewMerge($model, $getData)
static resolveFormLayoutMetadataForOneColumnDisplay($metadata)
static getSubset(RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter=null, $offset=null, $count=null, $where=null, $orderBy=null, $modelClassName=null, $selectDistinct=false)
static processNonDerivedRelationsAssignment($primaryModel, $selectedModel)
static processAssignRelationsToMergedModelFromModelsToBeDeleted($selectedModelsList, $primaryModel)
const HAS_MANY_BELONGS_TO
Definition: BeanModel.php:58
static resolveElementDuringFormLayoutRenderForListViewMerge(&$element, $preContentViewClass, $selectedModels, $primaryModel, $modelAttributeAndElementDataToMergeItemClass)
Generated on Sat Nov 28 2020 07:10:48
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.