Файл: adultscript-2.0.3-pro/files/admin/modules/channel/models/channel.php
Строк: 281
<?php
class VModel_Admin_channel extends VModel
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function add($channel)
    {
        $this->db->query("
            INSERT INTO #__channel
            SET user_id = ".(int) $channel['user_id'].",
                network_id = ".(int) $channel['network_id'].",
                name = '".$this->db->escape($channel['name'])."',
                slug = '".$this->db->escape($channel['slug'])."',
                description = '".$this->db->escape($channel['description'])."',
                url = '".$this->db->escape($channel['url'])."',
                thumb = '".$this->db->escape($channel['thumb'])."',
                logo = '".$this->db->escape($channel['logo'])."',
                adv_bottom_id = ".$channel['adv_bottom_id'].",
                adv_right_id = ".$channel['adv_right_id'].",
                adv_player_id = ".$channel['adv_player_id'].",
                add_time = ".time()
        );
        
        if ($this->db->affected_rows()) {
            $channel_id    = $this->db->get_last_insert_id('#__channel');
            
            $this->db->query("
                UPDATE #__channel_networks
                SET total_channels = total_channels+1
                WHERE network_id = ".(int) $channel['network_id']."
                LIMIT 1");
        
            return $channel_id;
        }
        
        return false;
    }
    
    public function update($channel_id, $channel)
    {
        $this->db->query("
            UPDATE #__channel
            SET user_id = ".(int) $channel['user_id'].",
                network_id = ".(int) $channel['network_id'].",
                name = '".$this->db->escape($channel['name'])."',
                slug = '".$this->db->escape($channel['slug'])."',
                description = '".$this->db->escape($channel['description'])."',
                url = '".$this->db->escape($channel['url'])."',
                thumb = '".$this->db->escape($channel['thumb'])."',
                logo = '".$this->db->escape($channel['logo'])."',
                total_videos = ".$channel['total_videos'].",
                total_views = ".$channel['total_videos'].",
                total_duration = ".$channel['total_duration'].",
                total_likes = ".$channel['total_likes'].",
                total_votes = ".$channel['total_votes'].",
                total_subscribers = ".$channel['total_subscribers'].",
                total_size = ".$channel['total_size'].",
                adv_bottom_id = ".$channel['adv_bottom_id'].",
                adv_right_id = ".$channel['adv_right_id'].",
                adv_player_id = ".$channel['adv_player_id'].",
                add_time = ".$channel['add_time'].",
                last_add_time = ".$channel['last_add_time'].",
                update_time = ".time()."
            WHERE channel_id = ".$channel_id."
            LIMIT 1
        ");
        
        return true;
    }
    
    public function get($channel_id, $fields = array())
    {
        $fields    = ($fields) ? $fields : array(
            'c.channel_id', 'c.user_id', 'c.network_id', 'c.name', 'c.slug', 'c.description', 'c.url', 'c.thumb', 'c.logo',
            'c.total_videos', 'c.total_views', 'c.total_duration', 'c.total_likes', 'c.total_votes', 'c.total_subscribers',
            'c.total_size', 'c.last_add_time', 'c.adv_bottom_id', 'c.adv_right_id', 'c.adv_player_id',
            'c.add_time', 'c.update_time', 'n.name AS network', 'u.username'
        );
        
        $this->db->query("SELECT ".implode(', ', $fields)."
                          FROM #__channel AS c
                          LEFT JOIN #__channel_networks AS n ON (n.network_id = c.network_id)
                          LEFT JOIN #__user AS u ON (u.user_id = c.user_id)
                          WHERE channel_id = ".$channel_id."
                          LIMIT 1");
        
        if ($this->db->affected_rows()) {
            return $this->db->fetch_assoc();
        } 
        
        return false;
    }
    public function exists($column, $value, $channel_id = 0)
    {
          $this->db->query("
              SELECT channel_id
              FROM #__channel
              WHERE ".$column." = '".$this->db->escape($value)."'
              AND channel_id != ".(int) $channel_id."
              LIMIT 1"
          );
          
          return $this->db->affected_rows();
      }
      
    public function delete($channel_id)
    {
          $channels = (is_array($channel_id)) ? $channel_id : array($channel_id);
        
        foreach ($channels as $channel_id) {
              $this->db->query("SELECT network_id, total_videos
                                FROM #__channel
                                WHERE channel_id = ".$channel_id."
                                LIMIT 1");
              if ($this->db->affected_rows()) {
                  $network_id        = (int) $this->db->fetch_field('network_id');
                  $total_videos    = (int) $this->db->fetch_field('total_videos');
                  $this->db->query("
                      UPDATE #__channel_networks
                    SET total_channels = CASE WHEN total_channels > 0 THEN total_channels-1 ELSE 0 END,
                          total_videos = CASE WHEN total_videos > 0 THEN total_videos-1 ELSE 0 END
                      WHERE network_id = ".$network_id."
                      LIMIT 1
                  ");
              }
              
              $this->db->query("DELETE FROM #__channel WHERE channel_id = ".$channel_id." LIMIT 1");
        }    
        
        return true;
    }      
    
    public function count($params)
    {
        $where    = ($params['where']) ? ' WHERE '.$params['where'] : '';
        $this->db->query("
            SELECT COUNT(*) AS total_channels
            FROM #__channel AS c".$where
        );
        
        return $this->db->fetch_field('total_channels');
    }
    
    public function channels($fields = array(), $params = array(), $limit = 0)
    {
        $fields = ($fields) ? $fields : array(
            'c.channel_id', 'c.network_id', 'c.user_id', 'c.name', 'c.slug', 'c.description',
            'c.thumb', 'c.total_videos', 'c.total_views', 'c.total_duration',
            'c.total_likes', 'c.total_votes', 'c.total_subscribers', 'c.add_time', 'c.update_time',
            'c.last_add_time', 'n.name AS network', 'u.username'
        );
        $where    = (isset($params['where']) && $params['where']) ? ' WHERE '.$params['where'] : '';
        $sort    = (isset($params['sort'])) ? $params['sort'] : 'c.channel_id';
        $order    = (isset($params['order'])) ? $params['order'] : 'DESC';
        $limit    = ($limit != '0') ? " LIMIT ".$limit : '';
        
        $this->db->query("
            SELECT ".implode(', ', $fields)."
            FROM #__channel AS c
            LEFT JOIN #__channel_networks AS n ON (n.network_id = c.network_id)
            LEFT JOIN #__user AS u ON (u.user_id = c.user_id)".$where."
            ORDER BY ".$sort.' '.$order.$limit
        );
        
        return ($this->db->affected_rows()) 
            ? $this->db->fetch_rows()
            : array();
    }
    
    public function add_video($channel_id, $video_id)
    {
        $this->db->query("
            SELECT network_id
            FROM #__channel
            WHERE channel_id = ".$channel_id."
            LIMIT 1
        ");
        
        if ($this->db->affected_rows()) {
            $network_id    = (int) $this->db->fetch_field('network_id');
            $this->db->query("
                UPDATE #__channel
                SET total_videos = total_videos+1
                WHERE channel_id = ".$channel_id."
                LIMIT 1
            ");
            $this->db->query("
                UPDATE #__channel_networkds
                SET total_videos = total_videos+1
                WHERE network_id = ".$network_id."
                LIMIT 1
            ");
        }
    }
}