Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
group.php
1<?php
9
10use Bitrix\Main;
14
17
18Loc::loadMessages(__FILE__);
19
36class GroupTable extends Entity\DataManager
37{
38 const PROJECT_USES_GROUPS_OPT = 'project_uses_groups';
39
40 public static function getFilePath()
41 {
42 return __FILE__;
43 }
44
45 public static function getTableName()
46 {
47 return 'b_sale_location_group';
48 }
49
50 public static function add(array $data)
51 {
52 if(isset($data['NAME']))
53 {
54 $name = $data['NAME'];
55 unset($data['NAME']);
56 }
57
58 $addResult = parent::add($data);
59
60 // add connected data
61 if($addResult->isSuccess())
62 {
63 $primary = $addResult->getId();
64
65 // names
66 if(isset($name))
67 Name\GroupTable::addMultipleForOwner($primary, $name);
68
69 // set flag that indicates whether project still uses groups or not
70 self::setGroupUsage();
71 }
72
73 return $addResult;
74 }
75
76 public static function update($primary, array $data)
77 {
78 $primary = Assert::expectIntegerPositive($primary, '$primary');
79
80 // first update parent, and if it succeed, do updates of the connected data
81
82 if(isset($data['NAME']))
83 {
84 $name = $data['NAME'];
85 unset($data['NAME']);
86 }
87
88 $updResult = parent::update($primary, $data);
89
90 // update connected data
91 if($updResult->isSuccess())
92 {
93 // names
94 if(isset($name))
95 Name\GroupTable::updateMultipleForOwner($primary, $name);
96 }
97
98 return $updResult;
99 }
100
101 public static function delete($primary)
102 {
103 $primary = Assert::expectIntegerPositive($primary, '$primary');
104
105 $delResult = parent::delete($primary);
106
107 // delete connected data
108 if($delResult->isSuccess())
109 {
110 Name\GroupTable::deleteMultipleForOwner($primary);
111 GroupLocationTable::deleteByGroupId($primary);
112
113 // set flag that indicates whether project still uses groups or not
114 self::checkGroupUsage();
115 }
116
117 return $delResult;
118 }
119
120 public static function checkGroupUsage()
121 {
122 $optValue = Config\Option::get("sale", self::PROJECT_USES_GROUPS_OPT, '', '');
123 if(!$optValue) // option is undefined, we are not sure if there are groups or not
124 return self::getGroupUsage();
125
126 return $optValue == 'Y';
127 }
128
129 public static function getGroupUsage()
130 {
131 $isUsing = !!GroupTable::getList(array('limit' => 1, 'select' => array('ID')))->fetch();
132 Config\Option::set("sale", self::PROJECT_USES_GROUPS_OPT, $isUsing ? 'Y' : 'N', '');
133
134 return $isUsing;
135 }
136
137 public static function setGroupUsage()
138 {
139 Config\Option::set("sale", self::PROJECT_USES_GROUPS_OPT, 'Y');
140 }
141
142 public static function getCodeValidators()
143 {
144 return array(
145 new Entity\Validator\Unique(),
146 );
147 }
148
149 public static function getMap()
150 {
151 return array(
152
153 'ID' => array(
154 'data_type' => 'integer',
155 'primary' => true,
156 'autocomplete' => true,
157 ),
158 'CODE' => array(
159 'data_type' => 'string',
160 'required' => true,
161 'title' => Loc::getMessage('SALE_LOCATION_GROUP_ENTITY_CODE_FIELD'),
162 'validation' => array(__CLASS__, 'getCodeValidators')
163 ),
164 'SORT' => array(
165 'data_type' => 'integer',
166 'title' => Loc::getMessage('SALE_LOCATION_GROUP_ENTITY_SORT_FIELD'),
167 'default_value' => '100'
168 ),
169
170 // virtual
171 'NAME' => array(
172 'data_type' => '\Bitrix\Sale\Location\Name\Group',
173 'reference' => array(
174 '=this.ID' => 'ref.LOCATION_GROUP_ID'
175 )
176 ),
177 'LOCATION' => array(
178 'data_type' => '\Bitrix\Sale\Location\Location',
179 'reference' => array(
180 '=this.ID' => 'ref.LOCATION_GROUP_ID'
181 )
182 )
183 );
184 }
185}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getList(array $parameters=array())
static add(array $data)
Definition group.php:50
static update($primary, array $data)
Definition group.php:76