Index: core/kernel/utility/validator.php =================================================================== --- core/kernel/utility/validator.php +++ core/kernel/utility/validator.php @@ -331,8 +331,20 @@ $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 - $res_live = $current_table_only ? 0 : $this->Conn->GetOne( str_replace('%s', $this->Application->GetLiveName($this->dataSource->TableName), $sql) ); + if ( getArrayValue($params, 'current_table_only') || !$this->dataSource->IsTempTable() ) { + // 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); @@ -346,6 +358,56 @@ } /** + * 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 * * @param string $field field name