/**
  * used for Category,Brand,Series,(They have something in common)
  * @param $method | callback function name
  * @param $table | table in db
  * @param $base_path | base path for image file in db
  * @param $pk | primary key in current website's table
  * @param $dh_pk | primary key in datahouse's table, it will do matching with $pk
  * @param $image_fields | which fields need update
  * @param $name_field | when create new image file, this will be used as a name prefix
  * @param $photo | the array data from dh
  * @param &$tmp_update | record flag
  * @param $update_mapping | array('CURRENT_FIELD'=>'DH_FIELD')if need some base field for updating
  */
 public function sync_common($method, $table, $base_path, $pk, $dh_pk = 'sourceid', $image_fields = array(), $name_field, &$photo, &$tmp_update, $update_mapping = NULL)
 {
     //---get this (MATCHED PK=sourceid)
     $sql = "SELECT *\n                    FROM [|PREFIX|]{$table}\n                    WHERE {$pk}='" . $this->db->Quote($photo[$dh_pk]) . "'";
     $result = $this->db->Query($sql);
     if ($row = $this->db->Fetch($result)) {
         //---[DATA FIELDS]update the base information according to the field mapping
         if ($update_mapping) {
             $set_base = array();
             foreach ($update_mapping as $cur_field => $dh_field) {
                 if (is_array($dh_field)) {
                     //if array,it shows that be dh fields
                     $field_values = array();
                     foreach ($dh_field as $key) {
                         $field_values[] = $photo[$key];
                     }
                     $set_base[$cur_field] = implode(',', $field_values);
                 } else {
                     //not array, it's a real value
                     $set_base[$cur_field] = $dh_field;
                 }
             }
             if ($this->db->UpdateQuery($table, $set_base, "{$pk}='" . $photo[$dh_pk] . "'") !== FALSE) {
                 $tmp_update['data'] = '1';
             } else {
                 $tmp_update['data'] = '0';
             }
         }
         //---[IMAGE FIELDS]image field that to be update
         $last_size = '';
         //last size that has been update successfully
         $last_imagefile = '';
         //last imagefile that has been saved successfully
         foreach ($image_fields as $field => $image_size) {
             $source_file = $this->dh_sourcefile($image_size);
             //image field
             $imagefile = NULL;
             //---default sync status
             $tmp_update[$image_size] = '0';
             //failed
             //---imagefile exist?
             $dest_file = $base_path . $row[$field];
             if (is_file($dest_file)) {
                 //exist,overwrite him
                 if ($last_size == $image_size) {
                     //it seems that image have transed before,just copy it
                     $source_file = $base_path . $last_imagefile;
                 }
                 //--write new extension
                 $imagefile = copy_fileext($source_file, $row[$field]);
                 //--change dest to use a new extension
                 $dest_file = $base_path . $imagefile;
                 //--transfer
                 if (trans_file($source_file, $dest_file)) {
                     //---if not a same file, delete the old one
                     if ($imagefile != $row[$field]) {
                         unlink($base_path . $row[$field]);
                     }
                 } else {
                     //---mark to not update into database
                     $imagefile = NULL;
                 }
             } else {
                 //not exist,create a new
                 //--build new image path
                 if ($last_size == $image_size) {
                     //its no need to do transfer,just update the field only
                     $imagefile = $last_imagefile;
                 } else {
                     //a new transfer must start
                     $imagefile = $this->{$method}($row[$name_field], basename($this->current_dhpath), $image_size);
                     $dest_file = $base_path . $imagefile;
                     remkdir(dirname($dest_file));
                     if (!trans_file($source_file, $dest_file)) {
                         //---mark to not update into database
                         $imagefile = NULL;
                     }
                 }
             }
             //---update new imagefile to database
             if ($imagefile) {
                 $set[$field] = $imagefile;
                 if ($this->db->UpdateQuery($table, $set, "{$pk}='" . $photo[$dh_pk] . "'") !== FALSE) {
                     //---set sync status
                     $tmp_update[$image_size] = '1';
                     //success
                     //---record last successed item
                     $last_size = $image_size;
                     $last_imagefile = $imagefile;
                 }
             }
         }
     }
 }
/**
 * used for Category,Brand,Series,(They have something in common)
 * @param $method | callback function name
 * @param $table | table in db
 * @param $base_path | base path for image file in db
 * @param $pk | primary key in current website's table
 * @param $dh_pk | primary key in datahouse's table, it will do matching with $pk
 * @param $image_fields | which fields need update
 * @param $name_field | when create new image file, this will be used as a name prefix
 * @param $photo | the array data from dh
 * @param &$tmp_update | record flag
 * @param $relative_fullpath | the path that build with photopath & photoid & locationid & extension
 * @param $update_mapping | array('CURRENT_FIELD'=>'DH_FIELD')if need some base field for updating
*/
function sync_common($method, $table, $base_path, $pk, $dh_pk = 'sourceid', $image_fields = array(), $name_field, $photo, &$tmp_update, $relative_fullpath, $update_mapping = NULL)
{
    //---get this (MATCHED PK=sourceid)
    $sql = "SELECT *\n\t\tFROM [|PREFIX|]{$table}\n\t\tWHERE {$pk}='" . $GLOBALS['ISC_CLASS_DB']->Quote($photo[$dh_pk]) . "'";
    $result = $GLOBALS['ISC_CLASS_DB']->Query($sql);
    if ($row = $GLOBALS['ISC_CLASS_DB']->Fetch($result)) {
        //---[DATA FIELDS]update the base information according to the field mapping
        if ($update_mapping) {
            $set_base = array();
            foreach ($update_mapping as $cur_field => $dh_field) {
                $set_base[$cur_field] = $photo[$dh_field];
            }
            if ($GLOBALS['ISC_CLASS_DB']->UpdateQuery($table, $set_base, "{$pk}='" . $photo[$dh_pk] . "'") !== FALSE) {
                $tmp_update['data'] = '1';
            } else {
                $tmp_update['data'] = '0';
            }
        }
        //---[IMAGE FIELDS]image field that to be update
        $last_size = '';
        //last size that has been update successfully
        $last_imagefile = '';
        //last imagefile that has been saved successfully
        foreach ($image_fields as $field => $image_size) {
            //--use a compatible size[NOTE:the $tmp_update log will use it too]
            $image_size = get_better_size($photo, $image_size);
            if ($image_size === FALSE) {
                //have no suitable image
                $tmp_update[$image_size] = '0';
                //failed
                continue;
            }
            $source_file = DH_WEB_PHOTOS . '/' . $image_size . '/' . $relative_fullpath;
            //image field
            $imagefile = NULL;
            //---default sync status
            $tmp_update[$image_size] = '0';
            //failed
            //---imagefile exist?
            $dest_file = $base_path . $row[$field];
            if (is_file($dest_file)) {
                //exist,overwrite him
                if ($last_size == $image_size) {
                    //it seems that image have transed before,just copy it
                    $source_file = $base_path . $last_imagefile;
                }
                //--write new extension
                $imagefile = copy_fileext($source_file, $row[$field]);
                //--change dest to use a new extension
                $dest_file = $base_path . $imagefile;
                //--transfer
                if (trans_file($source_file, $dest_file)) {
                    //---if not a same file, delete the old one
                    if ($imagefile != $row[$field]) {
                        unlink($base_path . $row[$field]);
                    }
                } else {
                    //---mark to not update into database
                    $imagefile = NULL;
                }
            } else {
                //not exist,create a new
                //--build new image path
                if ($last_size == $image_size) {
                    //its no need to do transfer,just update the field only
                    $imagefile = $last_imagefile;
                } else {
                    //a new transfer must start
                    $imagefile = $method($row[$name_field], basename($relative_fullpath), $image_size);
                    $dest_file = $base_path . $imagefile;
                    remkdir(dirname($dest_file));
                    if (!trans_file($source_file, $dest_file)) {
                        //---mark to not update into database
                        $imagefile = NULL;
                    }
                }
            }
            //---update to database
            if ($imagefile) {
                $set[$field] = $imagefile;
                if ($GLOBALS['ISC_CLASS_DB']->UpdateQuery($table, $set, "{$pk}='" . $photo[$dh_pk] . "'") !== FALSE) {
                    //---set sync status
                    $tmp_update[$image_size] = '1';
                    //success
                    //---record last successed item
                    $last_size = $image_size;
                    $last_imagefile = $imagefile;
                }
            }
        }
    }
}