<?php
  
function delete_tmp_gifts($dir) {
  
  $includes = glob($dir.'/{,.}*', GLOB_BRACE);
  $systemDots = preg_grep('/\.+$/', $includes);
  
  foreach ($systemDots as $index => $dot) {
    
    unset($includes[$index]);
  
  }
  
  foreach ($includes as $include) {
    
    if (is_dir($include) && !is_link($include)) {
      
      delete_tmp_gifts($include);
    
    }else{
      
      unlink($include);
    
    }
  
  }
  
  rmdir($dir);

}

/*
-------
8 марта
-------
*/

if (db::get_column("SELECT COUNT(*) FROM `GIFTS_DIR` WHERE `NAME` = '8 марта' LIMIT 1") == 0) {
  
  $ID_DIR = db::get_add("INSERT INTO `GIFTS_DIR` (`NAME`) VALUES ('8 марта')");
  $result = scandir(ROOT.'/files/upload/gifts/tmp_8_mart/', SCANDIR_SORT_ASCENDING);
  $s1 = 0;
  
  for ($i = 0; $i < count($result); $i++){
    
    if (preg_match('#\.png$#i',$result[$i])){
    
      $s1++;    
      $gift = array('10', '10', '5', '10', '10', '10', '10', '30', '50', '10', '20', '20', '10', '20', '30', '30', '10', '50');
      $rand_gift = array_rand($gift, 1);
      $gift_us = $gift[$rand_gift];     
      $ID = db::get_add("INSERT INTO `GIFTS` (`SUM`, `NAME`, `ID_DIR`, `EXT`) VALUES ('".$gift_us."', '8 марта #".$s1."', '".$ID_DIR."', 'png')");    
      $CopyFile = copy(ROOT.'/files/upload/gifts/tmp_8_mart/'.$result[$i], ROOT.'/files/upload/gifts/'.$ID.'.png');
      
    }
  
  }

}

delete_tmp_gifts(ROOT.'/files/upload/gifts/tmp_8_mart/');

/*
-------------
Еда и напитки
-------------
*/
  
if (db::get_column("SELECT COUNT(*) FROM `GIFTS_DIR` WHERE `NAME` = 'Еда и напитки' LIMIT 1") == 0) {
  
  $ID_DIR = db::get_add("INSERT INTO `GIFTS_DIR` (`NAME`) VALUES ('Еда и напитки')");
  $result = scandir(ROOT.'/files/upload/gifts/tmp_eda/', SCANDIR_SORT_ASCENDING);
  $s1 = 0;
  
  for ($i = 0; $i < count($result); $i++){
    
    if (preg_match('#\.png$#i',$result[$i])){
    
      $s1++;    
      $gift = array('10', '10', '5', '10', '10', '10', '10', '30', '50', '10', '20', '20', '10', '20', '30', '30', '10', '50');
      $rand_gift = array_rand($gift, 1);
      $gift_us = $gift[$rand_gift];     
      $ID = db::get_add("INSERT INTO `GIFTS` (`SUM`, `NAME`, `ID_DIR`, `EXT`) VALUES ('".$gift_us."', 'Еда и напитки #".$s1."', '".$ID_DIR."', 'png')");    
      $CopyFile = copy(ROOT.'/files/upload/gifts/tmp_eda/'.$result[$i], ROOT.'/files/upload/gifts/'.$ID.'.png');
      
    }
  
  }

}

delete_tmp_gifts(ROOT.'/files/upload/gifts/tmp_eda/');

/*
--------
Хэллоуин
--------
*/
  
if (db::get_column("SELECT COUNT(*) FROM `GIFTS_DIR` WHERE `NAME` = 'Хэллоуин' LIMIT 1") == 0) {
  
  $ID_DIR = db::get_add("INSERT INTO `GIFTS_DIR` (`NAME`) VALUES ('Хэллоуин')");
  $result = scandir(ROOT.'/files/upload/gifts/tmp_hallow/', SCANDIR_SORT_ASCENDING);
  $s1 = 0;
  
  for ($i = 0; $i < count($result); $i++){
    
    if (preg_match('#\.png$#i',$result[$i])){
    
      $s1++;    
      $gift = array('10', '10', '5', '10', '10', '10', '10', '30', '50', '10', '20', '20', '10', '20', '30', '30', '10', '50');
      $rand_gift = array_rand($gift, 1);
      $gift_us = $gift[$rand_gift];     
      $ID = db::get_add("INSERT INTO `GIFTS` (`SUM`, `NAME`, `ID_DIR`, `EXT`) VALUES ('".$gift_us."', 'Хэллоуин #".$s1."', '".$ID_DIR."', 'png')");    
      $CopyFile = copy(ROOT.'/files/upload/gifts/tmp_hallow/'.$result[$i], ROOT.'/files/upload/gifts/'.$ID.'.png');
      
    }
  
  }

}

delete_tmp_gifts(ROOT.'/files/upload/gifts/tmp_hallow/');

/*
------
Любовь
------
*/
  
if (db::get_column("SELECT COUNT(*) FROM `GIFTS_DIR` WHERE `NAME` = 'Любовь' LIMIT 1") == 0) {
  
  $ID_DIR = db::get_add("INSERT INTO `GIFTS_DIR` (`NAME`) VALUES ('Любовь')");
  $result = scandir(ROOT.'/files/upload/gifts/tmp_love/', SCANDIR_SORT_ASCENDING);
  $s1 = 0;
  
  for ($i = 0; $i < count($result); $i++){
    
    if (preg_match('#\.png$#i',$result[$i])){
    
      $s1++;    
      $gift = array('10', '10', '5', '10', '10', '10', '10', '30', '50', '10', '20', '20', '10', '20', '30', '30', '10', '50');
      $rand_gift = array_rand($gift, 1);
      $gift_us = $gift[$rand_gift];     
      $ID = db::get_add("INSERT INTO `GIFTS` (`SUM`, `NAME`, `ID_DIR`, `EXT`) VALUES ('".$gift_us."', 'Любовь #".$s1."', '".$ID_DIR."', 'png')");    
      $CopyFile = copy(ROOT.'/files/upload/gifts/tmp_love/'.$result[$i], ROOT.'/files/upload/gifts/'.$ID.'.png');
      
    }
  
  }

}

delete_tmp_gifts(ROOT.'/files/upload/gifts/tmp_love/');

/*
--------
Мужчинам
--------
*/
  
if (db::get_column("SELECT COUNT(*) FROM `GIFTS_DIR` WHERE `NAME` = 'Мужчинам' LIMIT 1") == 0) {
  
  $ID_DIR = db::get_add("INSERT INTO `GIFTS_DIR` (`NAME`) VALUES ('Мужчинам')");
  $result = scandir(ROOT.'/files/upload/gifts/tmp_man/', SCANDIR_SORT_ASCENDING);
  $s1 = 0;
  
  for ($i = 0; $i < count($result); $i++){
    
    if (preg_match('#\.png$#i',$result[$i])){
    
      $s1++;    
      $gift = array('10', '10', '5', '10', '10', '10', '10', '30', '50', '10', '20', '20', '10', '20', '30', '30', '10', '50');
      $rand_gift = array_rand($gift, 1);
      $gift_us = $gift[$rand_gift];     
      $ID = db::get_add("INSERT INTO `GIFTS` (`SUM`, `NAME`, `ID_DIR`, `EXT`) VALUES ('".$gift_us."', 'Мужчинам #".$s1."', '".$ID_DIR."', 'png')");    
      $CopyFile = copy(ROOT.'/files/upload/gifts/tmp_man/'.$result[$i], ROOT.'/files/upload/gifts/'.$ID.'.png');
      
    }
  
  }

}

delete_tmp_gifts(ROOT.'/files/upload/gifts/tmp_man/');

/*
---------
Новый год
---------
*/
  
if (db::get_column("SELECT COUNT(*) FROM `GIFTS_DIR` WHERE `NAME` = 'Новый год' LIMIT 1") == 0) {
  
  $ID_DIR = db::get_add("INSERT INTO `GIFTS_DIR` (`NAME`) VALUES ('Новый год')");
  $result = scandir(ROOT.'/files/upload/gifts/tmp_ng/', SCANDIR_SORT_ASCENDING);
  $s1 = 0;
  
  for ($i = 0; $i < count($result); $i++){
    
    if (preg_match('#\.png$#i',$result[$i])){
    
      $s1++;    
      $gift = array('10', '10', '5', '10', '10', '10', '10', '30', '50', '10', '20', '20', '10', '20', '30', '30', '10', '50');
      $rand_gift = array_rand($gift, 1);
      $gift_us = $gift[$rand_gift];     
      $ID = db::get_add("INSERT INTO `GIFTS` (`SUM`, `NAME`, `ID_DIR`, `EXT`) VALUES ('".$gift_us."', 'Новый год #".$s1."', '".$ID_DIR."', 'png')");    
      $CopyFile = copy(ROOT.'/files/upload/gifts/tmp_ng/'.$result[$i], ROOT.'/files/upload/gifts/'.$ID.'.png');
      
    }
  
  }

}

delete_tmp_gifts(ROOT.'/files/upload/gifts/tmp_ng/');

/*
---------
Праздники
---------
*/
  
if (db::get_column("SELECT COUNT(*) FROM `GIFTS_DIR` WHERE `NAME` = 'Праздники' LIMIT 1") == 0) {
  
  $ID_DIR = db::get_add("INSERT INTO `GIFTS_DIR` (`NAME`) VALUES ('Праздники')");
  $result = scandir(ROOT.'/files/upload/gifts/tmp_prazdnik/', SCANDIR_SORT_ASCENDING);
  $s1 = 0;
  
  for ($i = 0; $i < count($result); $i++){
    
    if (preg_match('#\.png$#i',$result[$i])){
    
      $s1++;    
      $gift = array('10', '10', '5', '10', '10', '10', '10', '30', '50', '10', '20', '20', '10', '20', '30', '30', '10', '50');
      $rand_gift = array_rand($gift, 1);
      $gift_us = $gift[$rand_gift];     
      $ID = db::get_add("INSERT INTO `GIFTS` (`SUM`, `NAME`, `ID_DIR`, `EXT`) VALUES ('".$gift_us."', 'Праздники #".$s1."', '".$ID_DIR."', 'png')");
      $CopyFile = copy(ROOT.'/files/upload/gifts/tmp_prazdnik/'.$result[$i], ROOT.'/files/upload/gifts/'.$ID.'.png');
      
    }
  
  }

}

delete_tmp_gifts(ROOT.'/files/upload/gifts/tmp_prazdnik/');

/*
--------
Женщинам
--------
*/
  
if (db::get_column("SELECT COUNT(*) FROM `GIFTS_DIR` WHERE `NAME` = 'Женщинам' LIMIT 1") == 0) {
  
  $ID_DIR = db::get_add("INSERT INTO `GIFTS_DIR` (`NAME`) VALUES ('Женщинам')");
  $result = scandir(ROOT.'/files/upload/gifts/tmp_woman/', SCANDIR_SORT_ASCENDING);
  $s1 = 0;
  
  for ($i = 0; $i < count($result); $i++){
    
    if (preg_match('#\.png$#i',$result[$i])){
      
      $s1++;    
      $gift = array('10', '10', '5', '10', '10', '10', '10', '30', '50', '10', '20', '20', '10', '20', '30', '30', '10', '50');
      $rand_gift = array_rand($gift, 1);
      $gift_us = $gift[$rand_gift];     
      $ID = db::get_add("INSERT INTO `GIFTS` (`SUM`, `NAME`, `ID_DIR`, `EXT`) VALUES ('".$gift_us."', 'Женщинам #".$s1."', '".$ID_DIR."', 'png')"); 
      $CopyFile = copy(ROOT.'/files/upload/gifts/tmp_woman/'.$result[$i], ROOT.'/files/upload/gifts/'.$ID.'.png');
      
    }
  
  }

}

delete_tmp_gifts(ROOT.'/files/upload/gifts/tmp_woman/');