All Data Structures Functions Variables Pages
MultipleValuesCustomField.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  {
42  public function __toString()
43  {
44  if ($this->values->count() == 0)
45  {
46  return Zurmo::t('Core', '(None)');
47  }
48  $s = null;
49 
50  $dataAndLabels = CustomFieldDataUtil::
51  getDataIndexedByDataAndTranslatedLabelsByLanguage($this->data, Yii::app()->language);
52  $s = null;
53  foreach ($this->values as $customFieldValue)
54  {
55  if ($s != null)
56  {
57  $s .= ', ';
58  }
59  $s .= ArrayUtil::getArrayValue($dataAndLabels, strval($customFieldValue));
60  }
61  return $s;
62  }
63 
64  public static function getDefaultMetadata()
65  {
66  $metadata = parent::getDefaultMetadata();
67  $metadata[__CLASS__] = array(
68  'members' => array(
69  ),
70  'relations' => array(
71  'values' => array(static::HAS_MANY, 'CustomFieldValue', static::OWNED),
72  ),
73  'rules' => array(
74  ),
75  'indexes' => array(
76  'basecustomfield_id' => array(
77  'members' => array('basecustomfield_id'),
78  'unique' => false),
79  ),
80  );
81  return $metadata;
82  }
83 
84  public static function updateValueByDataIdAndOldValueAndNewValue($customFieldDataId, $oldValue, $newValue)
85  {
87  $customFieldTableName = MultipleValuesCustomField::getTableName();
88  $baseCustomFieldTableName = BaseCustomField::getTableName();
89  $customFieldValueTableName = CustomFieldValue::getTableName();
90  $baseCustomFieldJoinColumnName = $baseCustomFieldTableName . '_id';
91  $valueAttributeColumnName = 'value';
92  $dataAttributeColumnName = static::getForeignKeyName('BaseCustomField', 'data');
93  $sql = "update {$quote}{$customFieldValueTableName}{$quote}, {$quote}{$customFieldTableName}{$quote}, ";
94  $sql .= "{$quote}{$baseCustomFieldTableName}{$quote} ";
95  $sql .= "set {$quote}{$customFieldValueTableName}{$quote}.{$valueAttributeColumnName} = '{$newValue}' ";
96  $sql .= "where {$quote}{$customFieldTableName}{$quote}.$baseCustomFieldJoinColumnName = "; // Not Coding Standard
97  $sql .= "{$quote}{$baseCustomFieldTableName}{$quote}.id ";
98  $sql .= "AND {$quote}{$dataAttributeColumnName}{$quote} = $customFieldDataId ";
99  $sql .= "AND {$quote}{$customFieldTableName}{$quote}.id = {$quote}{$customFieldValueTableName}{$quote}.{$customFieldTableName}_id ";
100  $sql .= "AND {$quote}{$customFieldValueTableName}{$quote}.{$valueAttributeColumnName} = '{$oldValue}'";
101  ZurmoRedBean::exec($sql);
102  }
103 
104  public function setValues($values)
105  {
106  $customFieldValueObject = array();
107  if (count($values) == 0)
108  {
109  $this->values->removeAll();
110  }
111  else
112  {
113  if ($this->values->count() > 0)
114  {
115  foreach ($this->values as $customFieldValue)
116  {
117  if (!in_array($customFieldValue->value, $values))
118  {
119  $customFieldValueObject[] = $customFieldValue;
120  }
121  else
122  {
123  $key = array_search($customFieldValue->value, $values);
124  unset($values[$key]);
125  }
126  }
127  foreach ($customFieldValueObject as $customFieldValue)
128  {
129  $this->values->remove($customFieldValue);
130  }
131  }
132 
133  foreach ($values as $value)
134  {
135  $customFieldValue = new CustomFieldValue();
136  $customFieldValue->value = $value;
137  $this->values->add($customFieldValue);
138  }
139  }
140  }
141 
148  public function stringifyOneToManyRelatedModelsValues($values)
149  {
150  assert('is_array($values)');
151  $dataAndLabels = CustomFieldDataUtil::
152  getDataIndexedByDataAndTranslatedLabelsByLanguage($this->data, Yii::app()->language);
153  foreach ($values as $key => $value)
154  {
155  if (ArrayUtil::getArrayValue($dataAndLabels, $value) != null)
156  {
157  $values[$key] = ArrayUtil::getArrayValue($dataAndLabels, $value);
158  }
159  }
160  return ArrayUtil::stringify($values);
161  }
162 
167  public function getValues()
168  {
169  $values = array();
170  if ($this->values->count() > 0)
171  {
172  foreach ($this->values as $customFieldValue)
173  {
174  $values[] = $customFieldValue->value;
175  }
176  }
177  return $values;
178  }
179  }
180 ?>
static getDataIndexedByDataAndTranslatedLabelsByLanguage(CustomFieldData $customFieldData, $language)
static getArrayValue($array, $element, $defaultValue=null)
Definition: ArrayUtil.php:45
static stringify($data)
Definition: ArrayUtil.php:102
static getTableName()
Generated on Tue Apr 7 2020 07:10:39