<?php

defined('_JEXEC') or die('Restricted access');

/**
* @package OS CCK
* @copyright 2020 OrdaSoft.
* @author Andrey Kvasnevskiy(akbet@mail.ru),Roman Akoev (akoevroman@gmail.com)
* @link http://ordasoft.com/cck-content-construction-kit-for-joomla.html
* @description OrdaSoft Content Construction Kit
*/

class os_cckContentInstance extends JTable
{
  /** @var array with all fields */

  private $eid;
  private $price_query = '';
  private $audio_query = '';
  private $image_query = '';
  private $file_query = '';
  private $video_query = '';
  private $track_query = '';
  private $category_connect_query = '';
  

  /**
   * @param database - A database connector object
   */
  function __construct(&$db, $eid)
  {
      $this->eid = $eid;
      parent::__construct('#__os_cck_content_entity_'.$eid, 'ceid', $db);
      
  }
  
  

  /**
   * Binds a named array/hash to this object
   *
   * Can be overloaded/supplemented by the child class
   *
   * @access  public
   * @param $from mixed An associative array or object
   * @param $ignore mixed An array or space separated list of fields not to bind
   * @return  boolean
   */
  function bind($from, $ignore = array()){

    $fromArray = is_array($from);
    $fromObject = is_object($from);
    if (!$fromArray && !$fromObject) {
      $this->setError(get_class($this) . '::bind failed. Invalid from argument');
      return false;
    }
    if($fromObject){
        $from = (array) $from;
    }

    if (!is_array($ignore)) {
        $ignore = explode(' ', $ignore);
    }

    if(!empty($from) && count($from) > 0 ){
      foreach($from as $key => $value){
          if(count($value) == 1){
               $from[$key][0] = $value[1];
               unset($from[$key][1]);
          }else{
              ksort($value);
              $temp_val = $value;
              
              // var_dump($value); exit;
              if(stripos($key, 'text_textfield') !== false){
                  $temp_val = implode('', $temp_val);
                  // var_dump($temp_val);
                  if(strlen($temp_val) > 255){
                      $temp_val = substr($temp_val, 0, 254);
                  }
                  $from[$key][0] = $temp_val;
              }else{
                  $from[$key] = $temp_val;
              }

              
          }
      }
      // var_dump($instance_data); exit;
    //   $update_and = '';
    //   $query = "UPDATE #__os_cck_content_entity_$eid SET ";
    //   var_dump($from);
      foreach($from as $key => $value){
          if(is_array($value) && isset($value[0]) && stripos($key, 'galleryfield') === false){
              $value = $value[0];
          }
          if(stripos($key, 'imagefield') !== false  || stripos($key, 'filefield') !== false){
              // if( $query != "UPDATE #__os_cck_content_entity_$eid SET " && stripos($key, 'pricefield') === false) $query .= ", ";
              $file_headers = @get_headers($value);    
              if (stripos($file_headers[0],"404 Not Found") >0  || (stripos($file_headers[0], "302 Found") > 0 && stripos($file_headers[7],"404 Not Found") > 0)) {
                  // return false;
              }else{
                  if(stripos($key, 'imagefield') !== false){
                      // $url = 'http://img.yandex.net/i/www/logo.png';
                      // /home/vlados/public_html/joomla42/components/com_os_cck/files/images

                      $file_inf = pathinfo($value);

                      if(!isset($file_inf['extension'])) continue;
                      
                      $file_name = $file_inf['filename'];
                      $file_name = str_replace('imagefield', $key, $file_name);
                      $file_type = '.' . $file_inf['extension'];
                      
                      // var_dump($key);
                      // var_dump($file_name);
                      $path = JPATH_SITE . '/components/com_os_cck/files/images/'.$file_name.$file_type;
                      file_put_contents($path, file_get_contents($value));
                      $value = '/components/com_os_cck/files/images/'.$file_name.$file_type;
                  }elseif(stripos($key, 'filefield') !== false){
                      $file_inf = pathinfo($value);

                      if(!isset($file_inf['extension'])) continue;
                      
                      $file_name = $file_inf['filename'];
                      $file_name = str_replace('imagefield', $key, $file_name);
                      $file_type = '.' . $file_inf['extension'];
                      
                      // var_dump($key);
                      // var_dump($file_name);
                      $path = JPATH_SITE . '/components/com_os_cck/files/images/'.$file_name.$file_type;
                      file_put_contents($path, file_get_contents($value));
                      $value = '/components/com_os_cck/files/'.$file_name.$file_type;
                  }
                  // var_dump($value);
              }
          }elseif(stripos($key, 'galleryfield') !== false){
              // $aaaa = '[{"file":"hansel-2022-10-21.png","alt":"","name":""},{"file":"2023-01-28-17-41-431.png","alt":"","name":""},{"file":"2023-02-04-13-28-131.png","alt":"","name":""},{"file":"2023-02-06-17-38-241.png","alt":"","name":""},{"file":"photo-2023-02-07-14-32-18.jpg","alt":"","name":""}]';
              // if( $query != "UPDATE #__os_cck_content_entity_$eid SET " && stripos($key, 'pricefield') === false) $query .= ", ";
              if(!is_array($value)){
                  $temp_val = json_decode($value);
              }else{
                  if(count($value) == 1){
                      $temp_json_decode_val = json_decode($value[0]);
                      if(is_array( $temp_json_decode_val)){
                          $temp_val = $temp_json_decode_val;
                      }else{
                          $temp_val = $value;
                      }
                      // var_dump( $temp_json_decode_val);
                  }else{
                      $temp_val = $value;
                  }
                  // $temp_val = $value;
              }
              // var_dump($temp_val);
              $fid = substr($key, strripos($key, '_')+1);
              // var_dump($temp_val); exit;
              $new_val = array();
              if($temp_val && !empty($temp_val)){
                  // echo '222222222222'; exit;
                  foreach($temp_val as $gal_row){
                      if(is_object($gal_row)){
                          $file = $gal_row->file;
                      }else{
                          $file = $gal_row;
                          $gal_row = new stdClass();
                          $gal_row->alt = '';
                          $gal_row->name = '';
                      }
                      
                      if($file != ''){
                          $file_headers = @get_headers($file);
                          // var_dump($file_headers);
                          if (!is_bool($file_headers) && (stripos($file_headers[0],"404 Not Found") >0  || (stripos($file_headers[0], "302 Found") > 0 && stripos($file_headers[7],"404 Not Found") > 0))) {
                              // return false;
                          }else{
                              $file_inf = pathinfo($file);

                              if(!isset($file_inf['extension'])) continue;
                              
                              $file_name = $file_inf['filename'];
                              
                              
                              // $file_name = str_replace('imagefield', $key, $file_name);
                              $file_type = '.' . $file_inf['extension'];
                              $gall_path = JPATH_SITE . '/images/com_os_cck'.$fid.'/original/'.$file_name.$file_type;
                              if(file_exists($gall_path)){
                                  $file_name = $file_name . rand();
                              }
                              
                              // var_dump($gall_path);
                              // var_dump($file_name);
                              $path = JPATH_SITE . '/images/com_os_cck'.$fid.'/original/'.$file_name.$file_type;
                              $path_thmb = JPATH_SITE . '/images/com_os_cck'.$fid.'/thumbnail/'.$file_name.$file_type;
                              // var_dump($file);
                              file_put_contents($path, file_get_contents($file));
                              file_put_contents($path_thmb, file_get_contents($file));
                              $gal_row->file = $file_name.$file_type;
                              $new_val[] = $gal_row;
                          }
                      }
                      // var_dump($new_val);
                  }
                  $value = json_encode($new_val);
                  // var_dump($value); exit;
              }else{
                  // $new_val[] = array();
                  // var_dump($value); exit;
                  if($value != ''){
                      $file_headers = @get_headers($value);
                      if (stripos($file_headers[0],"404 Not Found") >0  || (stripos($file_headers[0], "302 Found") > 0 && stripos($file_headers[7],"404 Not Found") > 0)) {
                          // return false;
                      }else{
                          $gal_row = new stdClass();
                          $gal_row->alt = '';
                          $gal_row->name = '';
                          $file_inf = pathinfo($value);

                          if(!isset($file_inf['extension'])) continue;
                          
                          $file_name = $file_inf['filename'];
                          
                          
                          // $file_name = str_replace('imagefield', $key, $file_name);
                          $file_type = '.' . $file_inf['extension'];
                          $gall_path = JPATH_SITE . '/images/com_os_cck'.$fid.'/original/'.$file_name.$file_type;
                          if(file_exists($gall_path)){
                              $file_name = $file_name . rand();
                          }
                          
                          // var_dump($gall_path);
                          // var_dump($file_name);
                          $path = JPATH_SITE . '/images/com_os_cck'.$fid.'/original/'.$file_name.$file_type;
                          $path_thmb = JPATH_SITE . '/images/com_os_cck'.$fid.'/thumbnail/'.$file_name.$file_type;
                          file_put_contents($path, file_get_contents($value));
                          file_put_contents($path_thmb, file_get_contents($value));
                          $gal_row->file = $file_name.$file_type;
                          // var_dump($gal_row);
                          $new_val[] = $gal_row;
                      }
                  }
                  $value = json_encode($new_val);
                  
                  // var_dump($value ); exit;
              }
              // exit;
              // var_dump(json_decode($aaaa)); exit;
              // $file_inf = pathinfo($value);

              // if(!isset($file_inf['extension'])) continue;
              
              // $file_name = $file_inf['filename'];
              // $file_name = str_replace('imagefield', $key, $file_name);
              // $file_type = '.' . $file_inf['extension'];
              
              // // var_dump($key);
              // // var_dump($file_name);
              // $path = JPATH_SITE . '/components/com_os_cck/files/images/'.$file_name.$file_type;
              // file_put_contents($path, file_get_contents($value));
              // $value = '/components/com_os_cck/files/'.$file_name.$file_type;
          }elseif(stripos($key, 'videofield') !== false){
              $temp_val = json_decode($value);
              // var_dump($temp_val); exit;
              $new_val = array();
              if(is_array($temp_val) && !empty($temp_val)){
                  for($j = 0; $j < count($temp_val); $j++){
                      $val = $temp_val[$j];
                  // foreach($temp_val as $key1 => $val){
                      if($j == 0){
                          if(!empty($val)){
                              
                              foreach($val as $key2 => $video){
                                  if(property_exists($video, 'src') && $video->src != '' && $video->type != ''){
                                      $file_headers = @get_headers($video->src);
                                      if (stripos($file_headers[0],"404 Not Found") >0  || (stripos($file_headers[0], "302 Found") > 0 && stripos($file_headers[7],"404 Not Found") > 0)) {
                                          // return false;
                                      }else{
                                          $file_inf = pathinfo($video->src);

                                          if(!isset($file_inf['extension'])) continue;
                                          
                                          $file_name = $file_inf['filename'];
                                          
                                          
                                          // $file_name = str_replace('imagefield', $key, $file_name);
                                          $file_type = '.' . $file_inf['extension'];
                                          $gall_path = JPATH_SITE . '/components/com_os_cck/files/video/'.$file_name.$file_type;
                                          if(file_exists($gall_path)){
                                              $file_name = $file_name . rand();
                                          }
                                          
                                          // var_dump($gall_path);
                                          // var_dump($file_name);
                                          $path = JPATH_SITE . '/components/com_os_cck/files/video/'.$file_name.$file_type;
                                          
                                          file_put_contents($path, file_get_contents($video->src));
                                          
                                          $temp_val[$j][$key2]->src = '/components/com_os_cck/files/video/'.$file_name.$file_type;
                                          // $new_val[] = $video->src;
                                      }
                                  }
                                  
                              }
                          }
                          
                      }elseif($j == 1){
                          if(!empty($val)){
                              
                              foreach($val as $key2 => $track){
                                  if(property_exists($track, 'src') && $track->src != ''){
                                      $file_headers = @get_headers($track->src);
                                      if (stripos($file_headers[0],"404 Not Found") >0  || (stripos($file_headers[0], "302 Found") > 0 && stripos($file_headers[7],"404 Not Found") > 0)) {
                                          // return false;
                                      }else{
                                          $file_inf = pathinfo($track->src);

                                          if(!isset($file_inf['extension'])) continue;
                                          
                                          $file_name = $file_inf['filename'];
                                          
                                          
                                          // $file_name = str_replace('imagefield', $key, $file_name);
                                          $file_type = '.' . $file_inf['extension'];
                                          $gall_path = JPATH_SITE . '/components/com_os_cck/files/track/'.$file_name.$file_type;
                                          if(file_exists($gall_path)){
                                              $file_name = $file_name . rand();
                                          }
                                          
                                          // var_dump($gall_path);
                                          // var_dump($file_name);
                                          $path = JPATH_SITE . '/components/com_os_cck/files/track/'.$file_name.$file_type;
                                          
                                          file_put_contents($path, file_get_contents($track->src));
                                          
                                          $temp_val[$j][$key2]->src = '/components/com_os_cck/files/track/'.$file_name.$file_type;
                                          // $new_val[] = $video->src;
                                      }
                                  }
                                  
                              }
                          }
                      }
                  }
              }
              $value = $temp_val;
              
          }elseif(stripos($key, 'audiofield') !== false){
              $temp_val = json_decode($value);
              // var_dump($temp_val); exit;
              $new_val = array();
              if(is_array($temp_val) && !empty($temp_val)){
                  foreach($temp_val as $key1 => $val){
                      
                         
                      if(property_exists($val, 'src') && $val->src != '' && $val->type != ''){
                          $file_headers = @get_headers($val->src);
                          if (stripos($file_headers[0],"404 Not Found") >0  || (stripos($file_headers[0], "302 Found") > 0 && stripos($file_headers[7],"404 Not Found") > 0)) {
                              // return false;
                          }else{
                              $file_inf = pathinfo($val->src);

                              if(!isset($file_inf['extension'])) continue;
                              
                              $file_name = $file_inf['filename'];
                              
                              
                              // $file_name = str_replace('imagefield', $key, $file_name);
                              $file_type = '.' . $file_inf['extension'];
                              $gall_path = JPATH_SITE . '/components/com_os_cck/files/audio/'.$file_name.$file_type;
                              if(file_exists($gall_path)){
                                  $file_name = $file_name . rand();
                              }
                              
                              // var_dump($gall_path);
                              // var_dump($file_name);
                              $path = JPATH_SITE . '/components/com_os_cck/files/audio/'.$file_name.$file_type;
                              
                              file_put_contents($path, file_get_contents($val->src));
                              
                              $temp_val[$key1]->src = 'components/com_os_cck/files/audio/'.$file_name.$file_type;
                              // $new_val[] = $video->src;
                          }
                      }
                              
                          
                      
                      
                  }
              }
              $value = $temp_val;
              
          }elseif(stripos($key, 'categoryfield') !== false){
            //   var_dump($value);
              if($value != ''){
                  $value = json_decode($value);
                  if(!is_array($value)){
                    $temp_value = array();
                    $temp_value[] = $value;
                    $value = $temp_value;
                  }
                  $cat_con_query = array();
                  foreach($value as $temp_val){
                    $category_query = "SELECT cid FROM #__os_cck_categories WHERE title='$temp_val'";
                    $this->_db->setQuery($category_query);
                    $cid = $this->_db->loadResult();
                    
                    if(!$cid){
                        $category = new os_cckCategory($this->_db);
                        $category->title = $temp_val;
                        $category->checked_out_time = '0000-00-00 00:00:00';
                        $category->name = $temp_val;
                        // $category->name = $value;
                        $category->section = 'com_os_cck';
                        $category->parent_id = 0;
                        $category->fk_eid = 0;
                        $category->description = '';
                        $category->params = '1';
                        $category->published = '1';
                        // var_dump($category); exit;
                        if (!$category->store()) {
                            echo "<script> alert('" . addslashes($category->getError()) . "'); window.history.go(-1); </script>\n";
                            exit();
                        }
                        // $category->store();

                        $temp_val = $category->cid;

                        $cat_con_query[] = "INSERT INTO #__os_cck_categories_connect (`fk_eiid`, `fk_cid`) VALUES ('[fk_eiid_val]', '".$category->cid."');";
                        


                    }else{
                        $temp_val = $cid;
                        $cat_con_query[] = "INSERT INTO #__os_cck_categories_connect (`fk_eiid`, `fk_cid`) VALUES ('[fk_eiid_val]', '".$cid."');";
                        
                    }
                  }
                //   $this->category_connect_query = implode(' ', $cat_con_query);
                $this->category_connect_query = $cat_con_query;
                  // var_dump($value);
              }
          }else{
              // echo '1111111111111111<br>';
              // var_dump($key);
              
          }

        //   if( $query != "UPDATE #__os_cck_content_entity_$eid SET " && stripos($key, 'pricefield') === false){
        //       $temp_query = strrev($query);
        //       // echo $query . '<br>';
        //       if(stripos($temp_query, ', ') !== 0 && stripos($temp_query, ',') !== 0 && stripos($temp_query, ' ,') !== 0){
        //           $query .= ", ";
        //       }

               
        //   }
          if(stripos($key, 'imagefield') !== false  || stripos($key, 'filefield') !== false || stripos($key, 'galleryfield') !== false){
            if(property_exists($this, $key.'_data')){
              $temp_key = $key.'_data';
              $this->$temp_key = $value;
            }

              // $query .= " " . $key . "_data=".$db->Quote($value)." ";
              // if(stripos($key, 'galleryfield') !== false){
              //     $query .= ", ";
              // }
          }elseif(stripos($key, 'videofield') !== false){
              // var_dump($value); exit;
              $fid = str_replace('videofield_', '', $key);
              
              if(!empty($value)){
                  for($j = 0; $j < count($value); $j++){
                      $temp_val = $value[$j];
                  // foreach($value as $temp_val){
                      if($j == 0){
                          if(!empty($temp_val)){
                              $video_query = array();
                              foreach($temp_val as $video){
                                  $video_query[] = "INSERT INTO `#__os_cck_video_source` (`fk_eiid`, `fk_fid`, `src`, `type`, `media`, `youtube`) VALUE ('[fk_eiid_val]', '$fid', " . $db->Quote(($video->src) ? $video->src : '') . ", '$video->type', '$video->media', '$video->youtube')";
                                  
                                  // $db->setQuery($video_query);
                                  // try{
                                  //     $db->execute();
                                  // }catch (RuntimeException $e) {
                                  //     echo $video_query;
                                  //     echo $e->getMessage();
                                  // }
                                  
                              }
                              $this->video_query = implode('; ', $video_query);
                          }
                      }elseif($j == 1){
                          if(!empty($temp_val)){
                              $track_query = array();
                              foreach($temp_val as $track){
                                  $sequence_number = (is_int($track->sequence_number)) ? $track->sequence_number : 0;
                                  // var_dump($sequence_number);
                                  $track_query[] = "INSERT INTO `#__os_cck_track_source` (`fk_eiid`, `fk_fid`, `fk_video_id`, `sequence_number`, `src`, `kind`, `scrlang`, `label`) VALUE ('[fk_eiid_val]', '$fid', '$track->fk_video_id', '$sequence_number', " . $db->Quote(($track->src) ? $track->src : '') . ", '$track->kind', '$track->scrlang', '$track->label')";
                                  // $db->setQuery($video_query);
                                  // try{
                                  //     $db->execute();
                                  // }catch (RuntimeException $e) {
                                  //     echo $video_query;
                                  //     echo $e->getMessage();
                                  // }
                                  
                              }

                              $this->track_query = implode('; ', $track_query);
                          }
                      }
                  }
              }
              // var_dump($value);
              // var_dump($fid); exit;
          }elseif(stripos($key, 'audiofield') !== false){
              $fid = str_replace('audiofield_', '', $key);
              
              if(!empty($value)){
                  $audio_query = array();
                  foreach($value as $temp_val){
                      
                      
                      $audio_query[] = "INSERT INTO `#__os_cck_audio_source` (`fk_eiid`, `fk_fid`, `sequence_number`, `src`, `type`) VALUE ('[fk_eiid_val]', '$fid', ". $db->Quote(($temp_val->sequence_number) ? $temp_val->sequence_number : '0') . "," . $db->Quote(($temp_val->src) ? $temp_val->src : '') . ", '$temp_val->type')";
                      
                      // $db->setQuery($audio_query);
                      // try{
                      //     $db->execute();
                      // }catch (RuntimeException $e) {
                      //     echo $audio_query;
                      //     echo $e->getMessage();
                      // }
                          
                      
                      
                  }
                  $this->audio_query = implode('; ', $audio_query);
              }
          }elseif(stripos($key, 'pricefield') !== false){
                // echo '7777777777777777';
              $temp_val = json_decode($value);
              // var_dump($temp_val); exit;
              $temp_key = trim($key, "'");

              if(stripos($temp_key, 'pricefield_number') !== false){
                  $fid = str_replace('pricefield_number_', '', $temp_key);    
              }elseif(stripos($temp_key, 'pricefield_radio_buttons') !== false){
                  $fid = str_replace('pricefield_radio_buttons_', '', $temp_key);    
              }elseif(stripos($temp_key, 'pricefield_select_list') !== false){
                  $fid = str_replace('pricefield_select_list_', '', $temp_key);    
              }
              // var_dump($instance->fk_eid); exit;
              $price_query = array();
              if(is_array($temp_val) && !empty($temp_val)){
                  foreach($temp_val as $temp_price){
                      $price_query[] = "INSERT INTO `#__os_cck_content_instances_price` (`fk_eid`, `fk_eiid`, `fk_fid`, `price_name`, `price_value`, `quantity`, `ordering`) 
                      VALUE ('$this->eid', '[fk_eiid_val]', '$fid', '$temp_price->price_name', '$temp_price->price_value', '$temp_price->quantity', '$temp_price->ordering')";

                      
                  }
              }elseif(is_string($temp_val) || is_int($temp_val)){
                  $price_query[] = "INSERT INTO `#__os_cck_content_instances_price` (`fk_eid`, `fk_eiid`, `fk_fid`, `price_name`, `price_value`, `quantity`, `ordering`) 
                      VALUE ('$this->eid', '[fk_eiid_val]', '$fid', '$temp_val', '$temp_val', '-1', '0')";

                  
              }

              $this->price_query = implode('; ', $price_query);

              // var_dump($temp_val);
          }elseif(stripos($key, 'locationfield') !== false){
              if($value != ''){
                  $temp_lalue = json_decode($value);
                  if(is_array($temp_lalue)){
                      $temp_lalue = $temp_lalue[0];
                  }

                  if(is_object($temp_lalue)){
                      $vlat = (isset($temp_lalue->vlat)) ? $temp_lalue->vlat : 0;
                      $vlong = (isset($temp_lalue->vlong)) ? $temp_lalue->vlong : 0;
                      $zoom = (isset($temp_lalue->zoom)) ? $temp_lalue->zoom : 0;
                      $address = (isset($temp_lalue->address)) ? $temp_lalue->address : '';
                      $country = (isset($temp_lalue->country)) ? $temp_lalue->country : '';
                      $region = (isset($temp_lalue->region)) ? $temp_lalue->region : '';
                      $city = (isset($temp_lalue->city)) ? $temp_lalue->city : '';
                      $zipcode = (isset($temp_lalue->zipcode)) ? $temp_lalue->zipcode : '';

                      $key_location = array();
                      $key_location[$key."_vlat"] = $vlat;
                      $key_location[$key."_vlong"] = $vlong;
                      $key_location[$key."_zoom"] = $zoom;
                      $key_location[$key."_address"] = $address;
                      $key_location[$key."_country"] = $country;
                      $key_location[$key."_region"] = $region;
                      $key_location[$key."_city"] = $city;
                      $key_location[$key."_zipcode"] = $zipcode;
                      foreach($key_location as $temp_key_location => $temp_location_val){
                        if(property_exists($this, $temp_key_location)){
                          $this->$temp_key_location = $temp_location_val;
                        }
                      }
                      // var_dump(" ".$key."_vlat='" . $vlat . "' , ");
                      // $query .= " ".$key."_vlat='" . $vlat . "' , ";
                      // $query .= " ".$key."_vlong='" . $vlong ."' , ";
                      // $query .= " ".$key."_zoom='" . $zoom  ."' , ";
                      // $query .= " ".$key."_address='" . $address ."' , ";
                      // $query .= " ".$key."_country='" . $country ."' , ";
                      // $query .= " ".$key."_region='" . $region ."' , ";
                      // $query .= " ".$key."_city='" . $city ."' , ";
                      // $query .= " ".$key."_zipcode='" . $zipcode ."'";
                      // echo $query;
                  }
                  // var_dump($temp_lalue); exit;
              }else{
                    $key_location = array();
                    $key_location[$key."_vlat"] = '';
                    $key_location[$key."_vlong"] = '';
                    $key_location[$key."_zoom"] = '';
                    $key_location[$key."_address"] = '';
                    $key_location[$key."_country"] = '';
                    $key_location[$key."_region"] = '';
                    $key_location[$key."_city"] = '';
                    $key_location[$key."_zipcode"] = '';
                    foreach($key_location as $temp_key_location => $temp_location_val){
                        if(property_exists($this, $temp_key_location)){
                            $this->$temp_key_location = $temp_location_val;
                        }
                    }
                //   $query .= " ".$key."_vlat='0' , ";
                //   $query .= " ".$key."_vlong='0' , ";
                //   $query .= " ".$key."_zoom='0' , ";
                //   $query .= " ".$key."_address='' , ";
                //   $query .= " ".$key."_country='' , ";
                //   $query .= " ".$key."_region='' , ";
                //   $query .= " ".$key."_city='' , ";
                //   $query .= " ".$key."_zipcode=''";
              }
          }else{
            if (!in_array($key, $ignore)) {
                if (property_exists($this, $key)){
                    $this->$key = $value;
                }
            }
              // var_dump($key);
            //   $key_string = trim($key, "'");
            //   $query .= " $key_string=".$db->Quote($value)." ";
          }
          
      }
    //   var_dump($this);
      return true;
      // exit;
    //   $query = trim($query);
    //   if((strlen($query)-1) == strripos($query, ',')){
    //       $query = trim($query, ',');
    //   }
    //   // var_dump(strlen($query));
    //   // var_dump(strripos($query, ','));
    //   // var_dump($query);
    //   if( $query != "UPDATE #__os_cck_content_entity_$eid SET"){
    //       $query .= " WHERE fk_eiid=$instance->eiid;" ;
    //   }

  }

    
    
    // foreach ($this->getProperties() as $k => $v) {
    //   // internal attributes of an object are ignored
      
    // }
    return true;
  }
  
    
  /**
   * Inserts a new row if id is zero or updates an existing row in the database table
   *
   * Can be overloaded/supplemented by the child class
   *
   * @access public
   * @param boolean If false, null object variables are not updated
   * @return null|string null if successful otherwise returns and error message
   */
  function store($updateNulls = false, $eiid = -1){
    global $app, $global_settings, $input;
    if($eiid == -1) return false;
    $this->fk_eiid = $eiid;

    $query = "SELECT ceid FROM #__os_cck_content_entity_$this->eid WHERE fk_eiid = $eiid";
    $this->_db->setQuery($query);
    $ceid = $this->_db->loadResult();
    $this->ceid = $ceid;
// echo '11111111111111';
// var_dump($this); exit;
// // print_r($this);
// var_dump(parent::store());
    // var_dump($ceid); exit;
    if(!parent::store()){
        
        // var_dump($this->getError()); exit;
        echo $this->getError();
        // exit;
        return false;
    }
    // parent::store();
    
    if($this->price_query != ''){
        $query = str_replace('[fk_eiid_val]', $eiid, $this->price_query);
        // var_dump($query);
        $this->_db->setQuery($query);
        $this->_db->execute();
    }
    if($this->audio_query != ''){
        $query = str_replace('[fk_eiid_val]', $eiid, $this->audio_query);
        // var_dump($query);
        $this->_db->setQuery($query);
        $this->_db->execute();
    }
    if($this->image_query != ''){
        $query = str_replace('[fk_eiid_val]', $eiid, $this->image_query);
        // var_dump($query);
        $this->_db->setQuery($query);
        $this->_db->execute();
    }
    if($this->file_query != ''){
        $query = str_replace('[fk_eiid_val]', $eiid, $this->file_query);
        // var_dump($query);
        $this->_db->setQuery($query);
        $this->_db->execute();
    }
    if($this->video_query != ''){
        $query = str_replace('[fk_eiid_val]', $eiid, $this->video_query);
        // var_dump($query);
        $this->_db->setQuery($query);
        $this->_db->execute();
    }
    if($this->track_query != ''){
        $query = str_replace('[fk_eiid_val]', $eiid, $this->track_query);
        // var_dump($query);
        $this->_db->setQuery($query);
        $this->_db->execute();
    }
    if($this->category_connect_query != ''){
        foreach($this->category_connect_query as $query){
            $query = str_replace('[fk_eiid_val]', $eiid, $query);
            // var_dump($query);
            try{
                $this->_db->setQuery($query);
                $this->_db->execute();
            }catch (RuntimeException $e) {
                echo $query;
                echo $e->getMessage();
                exit;
            }
        }
        
    }
    // var_dump($this); exit;

  }
  
  

  
  

}



