All Data Structures Functions Variables Pages
ImportRules.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 ImportRules
42  {
48  private static $attributeImportRulesDataByModelAndImportRulesClassName = array();
49 
53  public static function resetCache()
54  {
55  self::$attributeImportRulesDataByModelAndImportRulesClassName = null;
56  }
57 
61  public static function getType()
62  {
63  $type = get_called_class();
64  $type = substr($type, 0, strlen($type) - strlen('ImportRules'));
65  return $type;
66  }
67 
72  public static function getModelClassName()
73  {
74  }
75 
80  public static function getModuleClassNames()
81  {
82  $modelClassName = static::getModelClassName();
83  assert('is_subclass_of($modelClassName, "Item")');
84  $moduleClassName = $modelClassName::getModuleClassName();
85  assert('$moduleClassName != null');
86  return array($moduleClassName);
87  }
88 
93  public static function getDisplayLabel()
94  {
95  $modelClassName = static::getModelClassName();
96  assert('is_subclass_of($modelClassName, "Item")');
97  $moduleClassName = $modelClassName::getModuleClassName();
98  assert('$moduleClassName != null');
99  return $moduleClassName::getModuleLabelByTypeAndLanguage('Plural');
100  }
101 
106  public static function getDerivedAttributeTypes()
107  {
108  return array();
109  }
110 
115  public static function getNonImportableAttributeNames()
116  {
117  return array();
118  }
119 
125  {
126  return array();
127  }
128 
138  {
139  $mappableAttributeIndicesAndDerivedTypes = array();
140  $modelClassName = static::getModelClassName();
141  $attributesCollection = static::getAttributesCollectionByModelClassName($modelClassName);
142  $model = new $modelClassName(false);
143  foreach ($attributesCollection as $attributeIndex => $attributeData)
144  {
145  if (!static::resolveIsAttributeANonImportableAttributeName($attributeData) &&
146  !in_array($attributeData['attributeImportRulesType'], static::getNonImportableAttributeImportRulesTypes()))
147  {
148  $mappableAttributeIndicesAndDerivedTypes[$attributeIndex] = $attributeData['attributeLabel'];
149  }
150  }
151  foreach (static::getDerivedAttributeTypes() as $derivedType)
152  {
153  $attributeImportRulesClassName = $derivedType . 'AttributeImportRules';
154  $attributeImportRules = new $attributeImportRulesClassName($model);
155  assert('$attributeImportRules instanceof DerivedAttributeImportRules');
156  $mappableAttributeIndicesAndDerivedTypes[$derivedType] = $attributeImportRules->getDisplayLabel();
157  }
158  natcasesort($mappableAttributeIndicesAndDerivedTypes);
159  return $mappableAttributeIndicesAndDerivedTypes;
160  }
161 
168  protected static function resolveIsAttributeANonImportableAttributeName($attributeData)
169  {
170  if (in_array($attributeData['attributeName'], static::getNonImportableAttributeNames()))
171  {
172  return true;
173  }
174  if (isset($attributeData['relationAttributeName']) &&
175  in_array($attributeData['attributeName'] . FormModelUtil::DELIMITER . $attributeData['relationAttributeName'],
176  static::getNonImportableAttributeNames()))
177  {
178  return true;
179  }
180  return false;
181  }
182 
189  protected static function getAttributesCollectionByModelClassName($modelClassName)
190  {
191  assert('$modelClassName != null && is_string($modelClassName)');
192  $modelAttributesAdapter = static::getModelAttributesImportMappingAdapterByModelClassName($modelClassName);
193  return $modelAttributesAdapter->getAttributes();
194  }
195 
196  protected static function getModelAttributesImportMappingAdapterByModelClassName($modelClassName)
197  {
198  assert('$modelClassName != null && is_string($modelClassName)');
199  return new ModelAttributesImportMappingAdapter(new $modelClassName(false));
200  }
201 
208  public static function getAttributesCollection()
209  {
210  return getAttributesCollectionByModelClassName(static::getModelClassName());
211  }
212 
217  public static function getModelClassNameByAttributeIndexOrDerivedType($attributeIndexOrDerivedType)
218  {
219  assert('is_string($attributeIndexOrDerivedType)');
220  return static::getModelClassName();
221  }
222 
229  public static function getAttributeImportRulesType($attributeIndexOrDerivedType)
230  {
231  assert('is_string($attributeIndexOrDerivedType)');
232  $modelClassName = static::getModelClassName();
233  $attributeImportRulesData = static::resolveAttributeImportRulesDataByModelClassNameAndCache($modelClassName);
234  if (isset($attributeImportRulesData[$attributeIndexOrDerivedType]))
235  {
236  return $attributeImportRulesData[$attributeIndexOrDerivedType];
237  }
238  throw new NotSupportedException('Model Class Name' . $modelClassName .
239  'Attribute Name or Derived Type: ' . $attributeIndexOrDerivedType);
240  }
241 
249  protected static function resolveAttributeImportRulesDataByModelClassNameAndCache($modelClassName)
250  {
251  assert('is_string($modelClassName)');
252  $importRulesClassName = get_called_class();
253  if (isset(self::$attributeImportRulesDataByModelAndImportRulesClassName
254  [$modelClassName . $importRulesClassName]))
255  {
256  return self::$attributeImportRulesDataByModelAndImportRulesClassName
257  [$modelClassName . $importRulesClassName];
258  }
259  else
260  {
261  $attributeImportRulesData = static::
262  getAttributeIndexOrDerivedTypeAndAttributeImportRuleTypes($modelClassName);
263  self::$attributeImportRulesDataByModelAndImportRulesClassName
264  [$modelClassName . $importRulesClassName] = $attributeImportRulesData;
265  return $attributeImportRulesData;
266  }
267  }
268 
274  protected static function getAttributeIndexOrDerivedTypeAndAttributeImportRuleTypes($modelClassName)
275  {
276  assert('$modelClassName != null && is_string($modelClassName)');
277  $attributesCollection = static::getAttributesCollectionByModelClassName($modelClassName);
278 
279  $attributeIndexOrDerivedTypeAndRuleTypes = array();
280  foreach ($attributesCollection as $attributeIndex => $attributeData)
281  {
282  if (!in_array($attributeData['attributeName'], static::getNonImportableAttributeNames()) &&
283  !in_array($attributeData['attributeImportRulesType'], static::getNonImportableAttributeImportRulesTypes()))
284  {
285  $attributeIndexOrDerivedTypeAndRuleTypes[$attributeIndex] = $attributeData['attributeImportRulesType'];
286  }
287  }
288  foreach (static::getDerivedAttributeTypes() as $derivedType)
289  {
290  $attributeIndexOrDerivedTypeAndRuleTypes[$derivedType] = $derivedType;
291  }
292  return $attributeIndexOrDerivedTypeAndRuleTypes;
293  }
294 
303  {
304  $modelClassName = static::getModelClassName();
305  $model = new $modelClassName(false);
306  $attributesCollection = static::getAttributesCollectionByModelClassName($modelClassName);
307  $requireAttributesCollection = array();
308  foreach ($attributesCollection as $attributeIndex => $attributeData)
309  {
310  if ($attributeData['attributeName'] != 'owner' &&
311  !in_array($attributeData['attributeName'], static::getNonImportableAttributeNames()) &&
312  !in_array($attributeData['attributeImportRulesType'], static::getNonImportableAttributeImportRulesTypes()) &&
313  $attributeData['isRequired'] &&
314  !$model->isAttributeReadOnly($attributeData['attributeName']))
315  {
316  $requireAttributesCollection[$attributeIndex] = $attributeData;
317  }
318  }
319  static::resolveRequiredDerivedAttributesCollection($requireAttributesCollection);
320  return $requireAttributesCollection;
321  }
322 
328  protected static function resolveRequiredDerivedAttributesCollection(& $attributesCollection)
329  {
330  $modelClassName = static::getModelClassName();
331  $model = new $modelClassName(false);
332  foreach (static::getDerivedAttributeTypes() as $derivedType)
333  {
334  $attributeImportRulesClassName = $derivedType . 'AttributeImportRules';
335  $attributeImportRules = new $attributeImportRulesClassName($model);
336  assert('$attributeImportRules instanceof DerivedAttributeImportRules');
337  $displayLabel = $attributeImportRules->getDisplayLabel();
338  $realAttributes = $attributeImportRules->getRealModelAttributeNames();
339  foreach ($realAttributes as $realAttribute)
340  {
341  if (!in_array($realAttribute, array_keys($attributesCollection)) &&
342  $model->isAttributeRequired($realAttribute))
343  {
345  $attributesCollection,
346  $realAttribute,
347  $displayLabel,
348  $realAttribute,
349  $derivedType,
350  null,
351  true
352  );
353  }
354  }
355  }
356  }
357 
361  public static function getRequiredAttributesLabelsData()
362  {
363  $requireAttributesCollection = static::getRequiredAttributesCollectionNotIncludingReadOnly();
364  $labelsData = array();
365  foreach ($requireAttributesCollection as $attributeData)
366  {
367  $labelsData[] = $attributeData['attributeLabel'];
368  }
369  return $labelsData;
370  }
371 
376  public static function getDedupeAttributes()
377  {
378  return array();
379  }
380  }
381 ?>
static getAttributeImportRulesType($attributeIndexOrDerivedType)
static getNonImportableAttributeNames()
static getRequiredAttributesLabelsData()
static getModelClassName()
Definition: ImportRules.php:72
static getDedupeAttributes()
static resolveAttributeImportRulesDataByModelClassNameAndCache($modelClassName)
static resolveRequiredDerivedAttributesCollection(&$attributesCollection)
static getMappableAttributeIndicesAndDerivedTypes()
static getNonImportableAttributeImportRulesTypes()
static getModuleClassNames()
Definition: ImportRules.php:80
static getModelClassNameByAttributeIndexOrDerivedType($attributeIndexOrDerivedType)
static resetCache()
Definition: ImportRules.php:53
static getAttributesCollectionByModelClassName($modelClassName)
static getAttributeIndexOrDerivedTypeAndAttributeImportRuleTypes($modelClassName)
static getRequiredAttributesCollectionNotIncludingReadOnly()
static getDerivedAttributeTypes()
static resolveIsAttributeANonImportableAttributeName($attributeData)
static populateCollection(&$collection, $attributeIndex, $attributeLabel, $attributeName, $elementType, $relationAttributeName=null, $isRequired=false)
static getDisplayLabel()
Definition: ImportRules.php:93
static getType()
Definition: ImportRules.php:61
static getAttributesCollection()
Generated on Thu Apr 2 2020 07:10:42