<?php
print_r("HERE");
die();
$children_clean = array();
$all_cats = array();
$catFolder_idx = array();
foreach($cat_parents as $cat_parent)
{
  $children_clean[$cat_parent['parent_id']][] = $cat_parent;
  $all_cats[$cat_parent['cat_id']][] = $cat_parent;
}

$root_cats = array();
foreach ($cats as $k => $cat)
{
  $catFolder_idx[$cat['id']] = $k;
  if(!$cat['parent_id'])
  {
    $root_cats[] = $k + 1;
  }
}

$catFolder[] = "{id:0,title:'Каталог',parents:[0],children:[" . implode(',', $root_cats) . "],all_children:false}";

foreach ($cats as $k => $cat)
{
  $children = array();
  $parents = array();
  $check_pr = array();
  if(isset($children_clean[$cat['id']]))
  {
    foreach($children_clean[$cat['id']] as $child)
    {
      if($child['order_id'] == 0)
      {
        $children[] = $catFolder_idx[$child['cat_id']] + 1;
      }
    }
  }

  if(isset($all_cats[$cat['id']]))
  {
    foreach($all_cats[$cat['id']] as $record)
    {
      $parents[] = $catFolder_idx[$record['parent_id']] + 1;   
    }
    $parents[] = $catFolder_idx[$cat['id']] + 1;
  }


  $catFolder[] = "{id:" . $cat['id'] . ",parent_id:" . ($cat['parent_id'] ? ($catFolder_idx[$cat['parent_id']] + 1) : '0') . ",title:'" . $cat['title'] . "',parents:[0," .(empty($parents)? ($catFolder_idx[$cat['id']] + 1) :  implode(',', $parents))  . "],children:[" . implode(',', $children) . "]}";


}

$products_a = array();

$context = sfContext::getInstance();
$d = $context->getDatabaseManager()->getDatabase('doctrine');
$dsn = explode(';', $d->getParameter('dsn'));
$e = explode('=', $dsn[0]);
mysql_connect($e[1], $d->getParameter('username'), $d->getParameter('password'));
$e = explode('=', $dsn[1]);
mysql_select_db($e[1]);
mysql_query('SET CHARACTER SET utf8');





$q = "SELECT p.*, cp.*, pi.*, psc.value AS stock_count, pp.value AS price
      FROM product AS p
      INNER JOIN cat_product as cp ON p.id = cp.product_id
      LEFT JOIN product_image as pi ON p.id = pi.product_id
      LEFT JOIN product_stock_count as psc ON p.id = psc.product_id
      INNER JOIN product_price as pp ON p.id = pp.product_id
      WHERE (pp.price_type_id = 1)";

$res = mysql_query($q);

$idx = 1;
$cat_idx = array();
$product_idx = array();

while($product = mysql_fetch_assoc($res))
{
  /*print_r($product);
  die();*/
  if (isset($product['src']) && $product['sizes'] != 0 && $product['src'] != 'no image')
  {
    $path = sfConfig::get('sf_data_dir') . '/1cbitrix/' . $product['src'];
    /*print_r($path);
    die();*/
    $type = pathinfo($path, PATHINFO_EXTENSION);
    $data = file_get_contents($path);
    $base64 = base64_encode($data);

    $products_a[] = "{id:" . $product['id'] . ",article:'" . $product['product_code'] . "',name:'" . addslashes($product['title']) . "',price:" . rand(1000, 10000) . ",image:'" . $base64 . "',min_purchase:1,number_product:" . $product['stock_count'] . "}";
  }
  else
  {
    $products_a[] = "{id:" . $product['id'] . ",article:'" . $product['product_code'] . "',name:'" . addslashes($product['title']) . "',price:" . rand(1000, 10000) . ",min_purchase:1,number_product:" . $product['stock_count'] . "}";
  }
 /* $products_a[] = "{id:" . $product['id'] . ",article:'" . $product['product_code'] . "',name:'" . addslashes($product['title']) . "',price:" . rand(100, 10000) . ",min_purchase:10,number_product:" . rand(1000, 10000) . "}";*/
  $cat_idx[$product['cat_id']][$idx] = true;
  $product_idx[$product['id']] = $idx;
  $idx++;
}

$cat_idx_var = array('[]');
foreach($catFolder_idx as $catFolder_idx_k => $catFolder_idx)
{
  $products = array();
  if(isset($cat_idx[$catFolder_idx_k]))
  {
    $products = array_keys($cat_idx[$catFolder_idx_k]);
  }
  if(isset($children_clean[$catFolder_idx_k]))
  {
    foreach($children_clean[$catFolder_idx_k] as $child)
    {
      if(isset($cat_idx[$child['cat_id']]))
      {
        $products = array_merge($products, array_keys($cat_idx[$child['cat_id']]));
      }
    }
  }
  $cat_idx_var[] = '[' . implode(',', array_unique($products)) . ']';
}

$q = "SELECT k.* FROM keyword k ORDER BY k.rank DESC LIMIT 10000";

$res = mysql_query($q);

$idx = 0;
$keywords = array();
$keywords_idx = array();

while($keyword = mysql_fetch_assoc($res))
{
  $keywords[] = "'" . $keyword['word'] . "'";
  $pidx = array();
  foreach(unserialize($keyword['ids']) as $id)
  {
    if(isset($product_idx[$id]))
    {
      $pidx[] = $product_idx[$id];
    }
  }
  $keywords_idx[] = '[' . implode(',', $pidx) . ']';
}


?>
var db = {
  catFolder: [
<?php echo implode(",\n", $catFolder);?>
  ],
  cat: [
<?php echo implode(",\n", $products_a);?>
  ],
  cat_idx: {
    title: [
<?php echo implode(",\n", $cat_idx_var);?>
    ]
  },
  keywords: [<?php echo implode(",", $keywords);?>],
  keywords_idx: [<?php echo implode(",", $keywords_idx);?>]
};

