Index: trunk/core/units/general/helpers/image_helper.php =================================================================== diff -u -N -r8687 -r8705 --- trunk/core/units/general/helpers/image_helper.php (.../image_helper.php) (revision 8687) +++ trunk/core/units/general/helpers/image_helper.php (.../image_helper.php) (revision 8705) @@ -161,6 +161,7 @@ if (preg_match('/^(Image[\d]+|PrimaryImage)$/', $field_name)) { $fields[$field_name] = $field_options; $virtual_fields[$field_name] = $field_options; + $virtual_fields['Delete'.$field_name] = Array ('type' => 'int', 'not_null' => 1, 'default' => 0); $image_count++; } } @@ -203,7 +204,7 @@ $image_counter = 1; foreach ($item_images as $item_image) { - $image_path = preg_replace('/^'.preg_quote(IMAGES_PATH, '/').'(.*)/', '\\1', $item_image['ThumbPath']); + $image_path = $item_image['ThumbPath']; if ($item_image['DefaultImg'] == 1 || $item_image['Name'] == 'main') { // process primary image separately $object->SetDBField('PrimaryImage', $image_path); @@ -216,6 +217,65 @@ $image_counter++; } } + + /** + * Saves newly uploaded images to external image table + * + * @param kCatDBItem $object + */ + function SaveItemImages(&$object) + { + $table_name = $this->Application->getUnitOption('img', 'TableName'); + $max_image_count = $this->Application->getUnitOption($object->Prefix, 'ImageCount'); // $this->Application->ConfigValue($object->Prefix.'_MaxImageCount'); + + $i = 0; + while ($i < $max_image_count) { + $field = $i ? 'Image'.$i : 'PrimaryImage'; + $field_options = $object->GetFieldOptions($field); + + $image_src = $object->GetDBField($field); + if ($image_src) { + if (isset($field_options['original_field'])) { + $key_clause = 'Name = '.$this->Conn->qstr($field_options['original_field']).' AND ResourceId = '.$object->GetDBField('ResourceId'); + + if ($object->GetDBField('Delete'.$field)) { + // if item was cloned, then new filename is in db (not in $image_src) + $sql = 'SELECT ThumbPath + FROM '.$table_name.' + WHERE '.$key_clause; + $image_src = $this->Conn->GetOne($sql); + if (@unlink(FULL_PATH.$image_src)) { + $sql = 'DELETE FROM '.$table_name.' + WHERE '.$key_clause; + $this->Conn->Query($sql); + } + } + else { + // image record found -> update + $fields_hash = Array ( + 'ThumbPath' => $image_src, + ); + + $this->Conn->doUpdate($fields_hash, $table_name, $key_clause); + } + } + else { + // image record not found -> create + $fields_hash = Array ( + 'ResourceId' => $object->GetDBField('ResourceId'), + 'Name' => $field, + 'AltName' => $field, + 'Enabled' => STATUS_ACTIVE, + 'DefaultImg' => $i ? 0 : 1, // first image is primary, others not primary + 'ThumbPath' => $image_src, + ); + + $this->Conn->doInsert($fields_hash, $table_name); + } + } + $i++; + } + } } ?> \ No newline at end of file