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

Language PHP Lines 162
MD5 Hash 7caca3bc1c2b2751473d614d3a0934ea Estimated Cost $2,433 (why?)
Repository https://bitbucket.org/ddonthula/zurmouser View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
    /*********************************************************************************
     * Zurmo is a customer relationship management program developed by
     * Zurmo, Inc. Copyright (C) 2012 Zurmo Inc.
     *
     * Zurmo is free software; you can redistribute it and/or modify it under
     * the terms of the GNU General Public License version 3 as published by the
     * Free Software Foundation with the addition of the following permission added
     * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
     * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
     * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
     *
     * Zurmo is distributed in the hope that it will be useful, but WITHOUT
     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
     * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
     * details.
     *
     * You should have received a copy of the GNU General Public License along with
     * this program; if not, see http://www.gnu.org/licenses or write to the Free
     * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
     * 02110-1301 USA.
     *
     * You can contact Zurmo, Inc. with a mailing address at 113 McHenry Road Suite 207,
     * Buffalo Grove, IL 60089, USA. or at email address contact@zurmo.com.
     ********************************************************************************/

    /**
     * Override to accomodate a value type of 'ZURMO_MODEL_NAME'. This would represent a model 'name' attribute value
     * that can be used as a unique identifier to map to existing models. An example is when importing a contact, and
     * the account name is provided. If the name is found, then the contact will be connected to the existing account
     * otherwise a new account is created with the name provided.
     * @see IdValueTypeBatchAttributeValueDataAnalyzer
     */
    class RelatedModelNameOrIdValueTypeBatchAttributeValueDataAnalyzer extends IdValueTypeBatchAttributeValueDataAnalyzer
    {
        /**
         * The max allowed length of the name.
         * @var integer
         */
        protected $maxNameLength;

        /**
         * If the name provide is larger than the $maxNameLength
         * @var string
         */
        const NEW_NAME_TO0_LONG = 'New name too long';

        /**
         * Override to ensure the $attributeName is a single value and also to resolve the max name length.
         * @param string $modelClassName
         * @param string $attributeName
         */
        public function __construct($modelClassName, $attributeName)
        {
            parent:: __construct($modelClassName, $attributeName);
            assert('is_string($attributeName)');
            $attributeModelClassName                        = $this->attributeModelClassName;
            $model                                          = new $attributeModelClassName(false);
            assert('$model->isAttribute("name")');
            $this->maxNameLength                            = StringValidatorHelper::
                                                              getMaxLengthByModelAndAttributeName($model, 'name');
            $this->messageCountData[static::NEW_NAME_TO0_LONG] = 0;
        }

        /**
         * @see IdValueTypeBatchAttributeValueDataAnalyzer::ensureTypeValueIsValid()
         */
        protected function ensureTypeValueIsValid($type)
        {
            assert('$type == RelatedModelValueTypeMappingRuleForm::ZURMO_MODEL_ID ||
                    $type == RelatedModelValueTypeMappingRuleForm::EXTERNAL_SYSTEM_ID ||
                    $type == RelatedModelValueTypeMappingRuleForm::ZURMO_MODEL_NAME');
        }

        /**
         * @see IdValueTypeBatchAttributeValueDataAnalyzer::analyzeByValue()
         */
        protected function analyzeByValue($value)
        {
            $modelClassName = $this->attributeModelClassName;
            if ($this->type == RelatedModelValueTypeMappingRuleForm::ZURMO_MODEL_ID)
            {
                $found = $this->resolveFoundIdByValue($value);
            }
            elseif ($this->type == RelatedModelValueTypeMappingRuleForm::ZURMO_MODEL_NAME)
            {
                if ($value == null)
                {
                    $found = false;
                }
                else
                {
                    $modelClassName = $this->attributeModelClassName;
                    if (!method_exists($modelClassName, 'getByName'))
                    {
                        throw new NotSupportedException();
                    }
                    $modelsFound = $modelClassName::getByName($value);
                    if (count($modelsFound) == 0)
                    {
                        $found = false;
                        if (strlen($value) > $this->maxNameLength)
                        {
                            $this->messageCountData[static::NEW_NAME_TO0_LONG] ++;
                        }
                    }
                    else
                    {
                        $found = true;
                    }
                }
            }
            else
            {
                $found = $this->resolveFoundExternalSystemIdByValue($value);
            }
            if ($found)
            {
                $this->messageCountData[static::FOUND] ++;
            }
            else
            {
                $this->messageCountData[static::UNFOUND] ++;
            }
            if ($this->type == IdValueTypeMappingRuleForm::EXTERNAL_SYSTEM_ID)
            {
                if (strlen($value) > $this->externalSystemIdMaxLength)
                {
                    $this->messageCountData[static::EXTERNAL_SYSTEM_ID_TOO_LONG] ++;
                }
            }
        }

        /**
         * @see IdValueTypeBatchAttributeValueDataAnalyzer::makeMessages()
         */
        protected function makeMessages()
        {
            if ($this->type == RelatedModelValueTypeMappingRuleForm::ZURMO_MODEL_ID ||
               $this->type == RelatedModelValueTypeMappingRuleForm::EXTERNAL_SYSTEM_ID)
            {
                $label   = Zurmo::t('ImportModule', '{found} record(s) will be updated and {unfound} record(s) will be skipped during import.',
                                             array('{found}'   => $this->messageCountData[static::FOUND],
                                                   '{unfound}' => $this->messageCountData[static::UNFOUND]));
            }
            else
            {
                $label   = Zurmo::t('ImportModule', '{found} record(s) will be updated and {unfound} record(s) will be created during the import.',
                                             array('{found}'   => $this->messageCountData[static::FOUND],
                                                   '{unfound}' => $this->messageCountData[static::UNFOUND]));
            }
            $this->addMessage($label);
            if ($this->messageCountData[static::NEW_NAME_TO0_LONG] > 0)
            {
                $label   = Zurmo::t('ImportModule', '{invalid} name value(s) is/are too long. These records will be skipped during import.',
                                             array('{invalid}' => $this->messageCountData[static::NEW_NAME_TO0_LONG]));
                $this->addMessage($label);
            }
            $this->resolveMakeExternalSystemIdTooLargeMessage();
        }
    }
?>
Back to Top