Advertisement
kevin25

Untitled

Oct 30th, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.64 KB | None | 0 0
  1. <?php
  2. class adsModel extends Model {
  3.  
  4.     private $_uploaded;
  5.  
  6.  
  7.     public function getStoresAds($id)
  8.     {
  9.         $query = $this->db->query("SELECT * FROM stores_ads WHERE stores_id = " . $id . " ORDER BY sort");
  10.         return $query->fetchAll(PDO::FETCH_ASSOC);
  11.     }
  12.  
  13.     public function getStores()
  14.     {
  15.         $query = $this->db->query("SELECT * FROM stores ORDER BY friendly_name ASC");
  16.         $data = $query->fetchAll(PDO::FETCH_ASSOC);
  17.         if(is_array($data)) {
  18.             foreach($data as& $item) {
  19.                 $item['total_ads'] = $this->getStoreAdsTotal($item['id']);
  20.             }
  21.         }
  22.  
  23.         return $data;
  24.     }
  25.  
  26.     public function getProductsById($id)
  27.     {
  28.         $query = $this->db->query("SELECT * FROM products WHERE stores_id = " . $id . " AND enabled = 1");
  29.         return $query->fetchAll(PDO::FETCH_ASSOC);
  30.     }
  31.  
  32.     public function getAdById($id)
  33.     {
  34.         $query = $this->db->query("SELECT * FROM stores_ads WHERE id = " . $id);
  35.         return $query->fetch(PDO::FETCH_ASSOC);
  36.     }
  37.  
  38.     public function getStoreNameById($id)
  39.     {
  40.         $query = $this->db->query("SELECT name FROM stores WHERE id = " . $id);
  41.         return $query->fetch(PDO::FETCH_COLUMN);
  42.     }
  43.  
  44.     public function getStoreAdsTotal($id)
  45.     {
  46.         $query = $this->db->query("SELECT id FROM stores_ads WHERE stores_id = " . $id);
  47.         return $query->RowCount();
  48.     }
  49.  
  50.     public function upload($id)
  51.     {
  52.         $this->setUploadedFiles();
  53.  
  54.         $query = $this->db->query("SELECT friendly_name FROM stores WHERE id = " . $id);
  55.         $friendly_name = $query->fetch(PDO::FETCH_COLUMN);
  56.         $ad_count = 0;
  57.  
  58.         foreach($this->_uploaded as $name) {
  59.             $file = pathinfo($name);
  60.             $ad_count = $file['filename'];
  61.             $path = '/images/ads/'.$friendly_name.'-black-friday-'.($ad_count).'-' . $this->createRandomString() . '.' . $file['extension'];
  62.  
  63.             if(!copy(__SITE_PATH . "/public/tmp/" . $name, __FRONTEND_PATH . $path)) {
  64.                 return false;
  65.             }
  66.             $this->db->query("INSERT INTO stores_ads (stores_id, path, sort) VALUES (".$id.", '".$path."', ".(int)$ad_count.")");
  67.             @unlink(__SITE_PATH . "/public/tmp/" . $name);
  68.         }
  69.         apc_clear_cache();
  70.         return true;
  71.     }
  72.  
  73.     public function save_hover()
  74.     {
  75.         $post = $_POST;
  76.         switch($post['action']) {
  77.             case 'save':
  78.                 $this->db->query("INSERT INTO products_hover (products_id, stores_ads_id, tag_width, tag_height, tag_top, tag_left) VALUES ('".$post['products_id']."', '".$post['stores_ads_id']."', '".$post['width']."', '".$post['height']."', '".$post['top']."', '".$post['left']."')");
  79.                 print $this->db->lastInsertId();
  80.                 break;
  81.             case 'delete':
  82.                 $this->db->query("DELETE FROM products_hover WHERE id = " . $post['id']);
  83.                 break;
  84.             case 'list':
  85.                 $tags = '';
  86.                 $query = $this->db->query("SELECT * FROM products_hover WHERE stores_ads_id = " . $post['stores_ads_id']);
  87.                 if($query->rowCount() > 0) {
  88.                     $results = $query->fetchAll(PDO::FETCH_ASSOC);
  89.                     foreach($results as $item) {
  90.                         $tags[] = array("id" => $item['id'], "label" => $item['products_id'], "width" => $item['tag_width'], "height" => $item['tag_height'], "top" => $item['tag_top'], "left" => $item['tag_left']);
  91.                     }
  92.  
  93.                 }
  94.                 print json_encode($tags);
  95.                 break;
  96.         }
  97.  
  98.         apc_clear_cache();
  99.     }
  100.  
  101.     public function delete_image($id)
  102.     {
  103.         $query = $this->db->query("SELECT * FROM stores_ads WHERE id = " . $id);
  104.         $row = $query->fetch(PDO::FETCH_ASSOC);
  105.  
  106.         if(!empty($row['path'])) {
  107.             @unlink(__FRONTEND_PATH . $row['path']);
  108.             @unlink(__SITE_PATH . '/tmp/images/ads/' . $id);
  109.             $this->db->query("DELETE FROM stores_ads WHERE id = " . $id);
  110.             apc_clear_cache();
  111.             return true;
  112.         }
  113.  
  114.         return false;
  115.     }
  116.  
  117.     public function delete_all($store_id)
  118.     {
  119.         $query = $this->db->query("SELECT * FROM stores_ads WHERE stores_id = " . $store_id);
  120.         $data = $query->fetchAll(PDO::FETCH_ASSOC);
  121.  
  122.         if(!empty($data)) {
  123.             foreach($data as $row) {
  124.                 if(!empty($row['path'])) {
  125.                     @unlink(__FRONTEND_PATH . $row['path']);
  126.                     @unlink(__SITE_PATH . '/tmp/images/ads/' . $row['id']);
  127.                     $this->db->query("DELETE FROM stores_ads WHERE id = " . $row['id']);
  128.                 }
  129.             }
  130.  
  131.             apc_clear_cache();
  132.             return true;
  133.         }
  134.  
  135.         return false;
  136.     }
  137.  
  138.     private function setUploadedFiles()
  139.     {
  140.         $dir= __SITE_PATH . "/public/tmp";
  141.  
  142.         if($dir_list = opendir($dir)) {
  143.             while(($filename = readdir($dir_list)) !== false) {
  144.                 if(($filename == '.') or ($filename == '..')) {
  145.                 } else {
  146.                     $this->_uploaded[] = $filename;
  147.                 }
  148.             }
  149.             closedir($dir_list);
  150.         }
  151.     }
  152.  
  153.     private function createRandomString($length = 4) {
  154.  
  155.         $chars = "abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  156.         srand((double)microtime()*1000000);
  157.         $i = 0;
  158.         $pass = '' ;
  159.  
  160.         while ($i <= $length) {
  161.             $num = rand() % 33;
  162.             $tmp = substr($chars, $num, 1);
  163.             $pass = $pass . $tmp;
  164.             $i++;
  165.         }
  166.         return $pass;
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement