PageRenderTime 38ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/app/protected/modules/import/utils/sanitizers/RelatedModelNameOrIdValueTypeSanitizerUtil.php

https://bitbucket.org/ddonthula/zurmozoo
PHP | 127 lines | 81 code | 4 blank | 42 comment | 10 complexity | f5b82fc3ad5bc7930819b9242a75885d MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, LGPL-3.0, LGPL-2.1, BSD-2-Clause
  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. * Sanitizer to handle attribute values that are possible a model name not just a model id.
  28. */
  29. class RelatedModelNameOrIdValueTypeSanitizerUtil extends ExternalSystemIdSuppportedSanitizerUtil
  30. {
  31. public static function supportsSqlAttributeValuesDataAnalysis()
  32. {
  33. return false;
  34. }
  35. public static function getBatchAttributeValueDataAnalyzerType()
  36. {
  37. return 'RelatedModelNameOrIdValueType';
  38. }
  39. public static function getLinkedMappingRuleType()
  40. {
  41. return 'RelatedModelValueType';
  42. }
  43. /**
  44. * Given a value that is either a zurmo id or an external system id, resolve that the
  45. * value is valid. The value presented can also be a 'name' value. If the name is not found as a model
  46. * in the system, then a new related model will be created using this name.
  47. * NOTE - If the related model has other required attributes that have no default values,
  48. * then there will be a problem saving this new model. This is too be resolved at some point.
  49. * If the value is not valid then an InvalidValueToSanitizeException is thrown.
  50. * @param string $modelClassName
  51. * @param string $attributeName
  52. * @param mixed $value
  53. * @param array $mappingRuleData
  54. */
  55. public static function sanitizeValue($modelClassName, $attributeName, $value, $mappingRuleData)
  56. {
  57. assert('is_string($modelClassName)');
  58. assert('is_string($attributeName) && $attributeName != "id"');
  59. assert('$mappingRuleData["type"] == RelatedModelValueTypeMappingRuleForm::ZURMO_MODEL_ID ||
  60. $mappingRuleData["type"] == RelatedModelValueTypeMappingRuleForm::EXTERNAL_SYSTEM_ID ||
  61. $mappingRuleData["type"] == RelatedModelValueTypeMappingRuleForm::ZURMO_MODEL_NAME');
  62. if ($value == null)
  63. {
  64. return $value;
  65. }
  66. $model = new $modelClassName(false);
  67. $relationModelClassName = $model->getRelationModelClassName($attributeName);
  68. if ($mappingRuleData["type"] == RelatedModelValueTypeMappingRuleForm::ZURMO_MODEL_ID)
  69. {
  70. try
  71. {
  72. if ((int)$value <= 0)
  73. {
  74. throw new NotFoundException();
  75. }
  76. return $relationModelClassName::getById((int)$value);
  77. }
  78. catch (NotFoundException $e)
  79. {
  80. throw new InvalidValueToSanitizeException(Yii::t('Default', 'The id specified did not match any existing records.'));
  81. }
  82. }
  83. elseif ($mappingRuleData["type"] == RelatedModelValueTypeMappingRuleForm::EXTERNAL_SYSTEM_ID)
  84. {
  85. try
  86. {
  87. return static::getModelByExternalSystemIdAndModelClassName($value, $relationModelClassName);
  88. }
  89. catch (NotFoundException $e)
  90. {
  91. throw new InvalidValueToSanitizeException(Yii::t('Default', 'The other id specified did not match any existing records.'));
  92. }
  93. }
  94. else
  95. {
  96. if (!method_exists($relationModelClassName, 'getByName'))
  97. {
  98. throw new NotSupportedException();
  99. }
  100. $modelsFound = $relationModelClassName::getByName($value);
  101. if (count($modelsFound) == 0)
  102. {
  103. $newRelatedModel = new $relationModelClassName();
  104. $newRelatedModel->name = $value;
  105. $saved = $newRelatedModel->save();
  106. //Todo: need to handle this more gracefully. The use case where a related model is needed to be made
  107. //but there are some required attributes that do not have defaults. As a result, since those extra
  108. //defaults cannot be specified at this time, an error must be thrown.
  109. if (!$saved)
  110. {
  111. throw new InvalidValueToSanitizeException(Yii::t('Default',
  112. 'A new related model could not be created because there are unspecified required attributes on that related model.'));
  113. }
  114. return $newRelatedModel;
  115. }
  116. else
  117. {
  118. return $modelsFound[0];
  119. }
  120. }
  121. }
  122. }
  123. ?>