Changeset View
Changeset View
Standalone View
Standalone View
branches/5.2.x/core/kernel/utility/validator.php
| Show First 20 Lines • Show All 339 Lines • ▼ Show 20 Line(s) | |||||
| // if (!$where) return true; | // if (!$where) return true; | ||||
| $sql = 'SELECT COUNT(*) | $sql = 'SELECT COUNT(*) | ||||
| FROM %s | FROM %s | ||||
| WHERE (' . implode(') AND (', $where) . ') AND (' . $this->dataSource->IDField . ' <> ' . (int)$this->dataSource->GetID() . ')'; | WHERE (' . implode(') AND (', $where) . ') AND (' . $this->dataSource->IDField . ' <> ' . (int)$this->dataSource->GetID() . ')'; | ||||
| $res_temp = $this->Conn->GetOne( str_replace('%s', $this->dataSource->TableName, $sql) ); | $res_temp = $this->Conn->GetOne( str_replace('%s', $this->dataSource->TableName, $sql) ); | ||||
| $current_table_only = getArrayValue($params, 'current_table_only'); // check unique record only in current table | if ( getArrayValue($params, 'current_table_only') || !$this->dataSource->IsTempTable() ) { | ||||
| $res_live = $current_table_only ? 0 : $this->Conn->GetOne( str_replace('%s', $this->Application->GetLiveName($this->dataSource->TableName), $sql) ); | // Check unique record only in current table. | ||||
| $res_live = 0; | |||||
| } | |||||
| else { | |||||
| $deleted_ids = $this->getTempTableDeletedIDs(); | |||||
| $live_sql = str_replace('%s', $this->Application->GetLiveName($this->dataSource->TableName), $sql); | |||||
| if ( $deleted_ids ) { | |||||
| $live_sql .= ' AND (' . $this->dataSource->IDField . ' NOT IN (' . implode(',', $deleted_ids) . '))'; | |||||
| } | |||||
| $res_live = $this->Conn->GetOne($live_sql); | |||||
| } | |||||
| $res = ($res_temp == 0) && ($res_live == 0); | $res = ($res_temp == 0) && ($res_live == 0); | ||||
| if ( !$res ) { | if ( !$res ) { | ||||
| $this->SetError($field, 'unique'); | $this->SetError($field, 'unique'); | ||||
| return false; | return false; | ||||
| } | } | ||||
| return true; | return true; | ||||
| } | } | ||||
| /** | /** | ||||
| * Returns IDs deleted in temp table. | |||||
| * | |||||
| * @return array | |||||
| */ | |||||
| protected function getTempTableDeletedIDs() | |||||
| { | |||||
| $parent_prefix = $this->Application->getUnitOption($this->dataSource->Prefix, 'ParentPrefix'); | |||||
| if ( !$parent_prefix ) { | |||||
| return array(); | |||||
| } | |||||
| // 1. Get main IDs, that are edited in temp table. | |||||
| $parent_table_name = $this->Application->GetTempName( | |||||
| $this->Application->getUnitOption($parent_prefix, 'TableName'), | |||||
| 'prefix:' . $parent_prefix | |||||
| ); | |||||
| $sql = 'SELECT ' . $this->Application->getUnitOption($parent_prefix, 'IDField') . ' | |||||
| FROM ' . $parent_table_name; | |||||
| $parent_temp_ids = $this->Conn->GetCol($sql); | |||||
| // Main item not saved to db, but sub-item is validated (only possible via custom code). | |||||
| if ( !$parent_temp_ids ) { | |||||
| return array(); | |||||
| } | |||||
| // 2. From above found IDs find sub-item IDs in LIVE table. | |||||
| $foreign_key = $this->Application->getUnitOption($this->dataSource->Prefix, 'ForeignKey'); | |||||
| $foreign_key = is_array($foreign_key) ? $foreign_key[$parent_prefix] : $foreign_key; | |||||
| $sql = 'SELECT ' . $this->dataSource->IDField . ' | |||||
| FROM ' . $this->Application->GetLiveName($this->dataSource->TableName) . ' | |||||
| WHERE ' . $foreign_key . ' IN (' . implode(',', $parent_temp_ids) . ')'; | |||||
| $live_ids = $this->Conn->GetCol($sql); | |||||
| // Sub-items were never saved to LIVE table. | |||||
| if ( !$live_ids ) { | |||||
| return array(); | |||||
| } | |||||
| // 3. Return IDs of LIVE table, that are no longer present (deleted) in TEMP table. | |||||
| $sql = 'SELECT ' . $this->dataSource->IDField . ' | |||||
| FROM ' . $this->dataSource->TableName; | |||||
| $temp_ids = $this->Conn->GetCol($sql); | |||||
| return array_diff($live_ids, $temp_ids); | |||||
| } | |||||
| /** | |||||
| * Check field value by user-defined alghoritm | * Check field value by user-defined alghoritm | ||||
| * | * | ||||
| * @param string $field field name | * @param string $field field name | ||||
| * @param Array $params field options from config | * @param Array $params field options from config | ||||
| * @return bool | * @return bool | ||||
| */ | */ | ||||
| protected function CustomValidation($field, $params) | protected function CustomValidation($field, $params) | ||||
| { | { | ||||
| ▲ Show 20 Lines • Show All 172 Lines • Show Last 20 Lines | |||||