PageRenderTime 35ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/app/protected/modules/import/utils/analyzers/RelatedModelNameOrIdValueTypeBatchAttributeValueDataAnalyzer.php

https://bitbucket.org/borrame/zurmo-fork-test
PHP | 163 lines | 104 code | 6 blank | 53 comment | 17 complexity | ce0909e847f330911c1a472b0b13c85b MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0, LGPL-2.1, BSD-2-Clause, GPL-3.0
  1. <?php
  2. /*********************************************************************************
  3. * Zurmo is a customer relationship management program developed by
  4. * Zurmo, Inc. Copyright (C) 2012 Zurmo Inc.
  5. *
  6. * Zurmo is free software; you can redistribute it and/or modify it under
  7. * the terms of the GNU 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 General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU 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 113 McHenry Road Suite 207,
  24. * Buffalo Grove, IL 60089, USA. or at email address contact@zurmo.com.
  25. ********************************************************************************/
  26. /**
  27. * Override to accomodate a value type of 'ZURMO_MODEL_NAME'. This would represent a model 'name' attribute value
  28. * that can be used as a unique identifier to map to existing models. An example is when importing a contact, and
  29. * the account name is provided. If the name is found, then the contact will be connected to the existing account
  30. * otherwise a new account is created with the name provided.
  31. * @see IdValueTypeBatchAttributeValueDataAnalyzer
  32. */
  33. class RelatedModelNameOrIdValueTypeBatchAttributeValueDataAnalyzer extends IdValueTypeBatchAttributeValueDataAnalyzer
  34. {
  35. /**
  36. * The max allowed length of the name.
  37. * @var integer
  38. */
  39. protected $maxNameLength;
  40. /**
  41. * If the name provide is larger than the $maxNameLength
  42. * @var string
  43. */
  44. const NEW_NAME_TO0_LONG = 'New name too long';
  45. /**
  46. * Override to ensure the $attributeName is a single value and also to resolve the max name length.
  47. * @param string $modelClassName
  48. * @param string $attributeName
  49. */
  50. public function __construct($modelClassName, $attributeName)
  51. {
  52. parent:: __construct($modelClassName, $attributeName);
  53. assert('is_string($attributeName)');
  54. $attributeModelClassName = $this->attributeModelClassName;
  55. $model = new $attributeModelClassName(false);
  56. assert('$model->isAttribute("name")');
  57. $this->maxNameLength = StringValidatorHelper::
  58. getMaxLengthByModelAndAttributeName($model, 'name');
  59. $this->messageCountData[static::NEW_NAME_TO0_LONG] = 0;
  60. }
  61. /**
  62. * @see IdValueTypeBatchAttributeValueDataAnalyzer::ensureTypeValueIsValid()
  63. */
  64. protected function ensureTypeValueIsValid($type)
  65. {
  66. assert('$type == RelatedModelValueTypeMappingRuleForm::ZURMO_MODEL_ID ||
  67. $type == RelatedModelValueTypeMappingRuleForm::EXTERNAL_SYSTEM_ID ||
  68. $type == RelatedModelValueTypeMappingRuleForm::ZURMO_MODEL_NAME');
  69. }
  70. /**
  71. * @see IdValueTypeBatchAttributeValueDataAnalyzer::analyzeByValue()
  72. */
  73. protected function analyzeByValue($value)
  74. {
  75. $modelClassName = $this->attributeModelClassName;
  76. if ($this->type == RelatedModelValueTypeMappingRuleForm::ZURMO_MODEL_ID)
  77. {
  78. $found = $this->resolveFoundIdByValue($value);
  79. }
  80. elseif ($this->type == RelatedModelValueTypeMappingRuleForm::ZURMO_MODEL_NAME)
  81. {
  82. if ($value == null)
  83. {
  84. $found = false;
  85. }
  86. else
  87. {
  88. $modelClassName = $this->attributeModelClassName;
  89. if (!method_exists($modelClassName, 'getByName'))
  90. {
  91. throw new NotSupportedException();
  92. }
  93. $modelsFound = $modelClassName::getByName($value);
  94. if (count($modelsFound) == 0)
  95. {
  96. $found = false;
  97. if (strlen($value) > $this->maxNameLength)
  98. {
  99. $this->messageCountData[static::NEW_NAME_TO0_LONG] ++;
  100. }
  101. }
  102. else
  103. {
  104. $found = true;
  105. }
  106. }
  107. }
  108. else
  109. {
  110. $found = $this->resolveFoundExternalSystemIdByValue($value);
  111. }
  112. if ($found)
  113. {
  114. $this->messageCountData[static::FOUND] ++;
  115. }
  116. else
  117. {
  118. $this->messageCountData[static::UNFOUND] ++;
  119. }
  120. if ($this->type == IdValueTypeMappingRuleForm::EXTERNAL_SYSTEM_ID)
  121. {
  122. if (strlen($value) > $this->externalSystemIdMaxLength)
  123. {
  124. $this->messageCountData[static::EXTERNAL_SYSTEM_ID_TOO_LONG] ++;
  125. }
  126. }
  127. }
  128. /**
  129. * @see IdValueTypeBatchAttributeValueDataAnalyzer::makeMessages()
  130. */
  131. protected function makeMessages()
  132. {
  133. if ($this->type == RelatedModelValueTypeMappingRuleForm::ZURMO_MODEL_ID ||
  134. $this->type == RelatedModelValueTypeMappingRuleForm::EXTERNAL_SYSTEM_ID)
  135. {
  136. $label = '{found} record(s) will be updated ';
  137. $label .= 'and {unfound} record(s) will be skipped during import.';
  138. }
  139. else
  140. {
  141. $label = '{found} record(s) will be updated and ';
  142. $label .= '{unfound} record(s) will be created during the import.';
  143. }
  144. $this->addMessage(Yii::t('Default', $label,
  145. array('{found}' => $this->messageCountData[static::FOUND],
  146. '{unfound}' => $this->messageCountData[static::UNFOUND])));
  147. if ($this->messageCountData[static::NEW_NAME_TO0_LONG] > 0)
  148. {
  149. $label = '{invalid} name value(s) is/are too long.';
  150. $label .= 'These records will be skipped during import.';
  151. $this->addMessage(Yii::t('Default', $label,
  152. array('{invalid}' => $this->messageCountData[static::NEW_NAME_TO0_LONG])));
  153. }
  154. $this->resolveMakeExternalSystemIdTooLargeMessage();
  155. }
  156. }
  157. ?>