Guest
Public paste!

Untitled

By: a guest | Mar 20th, 2010 | Syntax: PHP | Size: 11.14 KB | Hits: 81 | Expires: Never
Copy text to clipboard
  1. <?php
  2.  
  3. /*
  4.   @version: 1.0b
  5.   @author: mateusz 'matix' nowak <www.matix.info.pl>
  6. */
  7.  
  8. final class Movie_Model extends Aframe_Opt_Model implements Countable
  9. {
  10.  
  11.   public function search($sQuery, $iOffset = 0, $iLimit = 10)
  12.   {
  13.     $oQ = $this->q();
  14.     $oDb = $this->db();
  15.    
  16.     $aConditionOr = array();
  17.     $aCondition = array('movie_state = 1');
  18.    
  19.     $aFields = array('movie_title', 'movie_tags', 'movie_text', 'movie_need', 'category_name');
  20.     foreach($aFields as $sField)
  21.       $aConditionOr[] = sprintf(' MATCH(%s) AGAINST ("%s" IN BOOLEAN MODE)', $sField, $sQuery);
  22.    
  23.     $aCondition[] = '('. join(' OR ', $aConditionOr). ')';
  24.    
  25.     $oQueryCount = $this->q();
  26.     $oQueryCount->select($this->t('movie_likes'), 'count(like_movie_id)')
  27.                 ->where('like_movie_id = movie_id');
  28.  
  29.     $mSelect = join(', ', array('*', '('.$oQueryCount.') as movie_like_count'));
  30.  
  31.     $oQ->select($this->t('movies'), $mSelect)
  32.        ->join($this->t('categories'), 'category_id = movie_category_id')
  33.        ->join($this->t('users'), 'user_id = movie_user_id')
  34.        ->where($aCondition)
  35.        ->order('movie_id DESC')
  36.        ->limit($iOffset, $iLimit);
  37.    
  38.     return $oDb->fetchAll($oQ);
  39.   }
  40.  
  41.   public function UpdateStep($sPhoto = null, array $aSteps = array(), $iMid, $oStep)
  42.   {
  43.     $aStepDataRow = array();
  44.     if(count($aSteps))
  45.     {
  46.       $oStep->deleteAll($iMid);
  47.      
  48.       foreach($aSteps['type'] as $iKey => $sStep)
  49.         if((isset($aSteps['title'][$iKey]) && $aSteps['title'][$iKey]) || (isset($aSteps['text'][$iKey]) && $aSteps['text'][$iKey]))
  50.         {
  51.           $aData = array
  52.           (
  53.             'step_type' => isset($aSteps['type'][$iKey]) ? $aSteps['type'][$iKey] : 1,
  54.             'step_text' => isset($aSteps['text'][$iKey]) ? $aSteps['text'][$iKey] : '',
  55.             'step_title' => isset($aSteps['title'][$iKey]) ? $aSteps['title'][$iKey] : '',
  56.             'step_movie_id' => (int)$iMid,
  57.           );
  58.          
  59.           // Try to put new step to the database
  60.           try
  61.           {
  62.             $oStep->SetArray($aData);
  63.           }
  64.          
  65.           // Return false if error occured
  66.           catch(Exception $e)
  67.           {
  68.             return false;
  69.           }
  70.         }
  71.     }
  72.    
  73.     // If avatar changed
  74.     if($sPhoto)
  75.     {
  76.       $oQ = $this->q();
  77.       $oDb = $this->db();
  78.       $aDataUpdate = array
  79.       (
  80.         'movie_thumb' => $sPhoto,
  81.       );
  82.      
  83.       $oQ->update($this->t('movies'), $aDataUpdate)
  84.          ->where('movie_id = '.$iMid);
  85.        
  86.        return $oDb->query($oQ);
  87.     }
  88.      
  89.     return true;
  90.   }
  91.  
  92.   public function count($bFromLastQuery = false)
  93.   {
  94.     $oQ = $this->q();
  95.     $oDb = $this->db();
  96.    
  97.     if (!$bFromLastQuery)
  98.       $oQ->select($this->t('movies'), 'COUNT(*) as movie_count');
  99.     else
  100.       $oQ = $this->_oLast->select($this->t('movies'), 'COUNT(*) as movie_count')->limit(0, 0);
  101.    
  102.     return $oDb->fetchRow('movie_count', $oQ);
  103.   }
  104.  
  105.   public function urlExists($sUrl)
  106.   {
  107.     // Query connection
  108.     $oQ = $this->q();    
  109.     $oDb = $this->db();
  110.    
  111.     $oQ->select($this->t('movies'))
  112.        ->where(sprintf('movie_url = "%s"', $sUrl));
  113.    
  114.     return $oDb->fetchOne($oQ);
  115.   }
  116.  
  117.   public function ListingCategoryItem($sNested, $iLimit = 5, $iOffset = 1)
  118.   {
  119.     $oQ = $this->q();
  120.     $oDb = $this->db();
  121.    
  122.     $iOffset = $iOffset;
  123.    
  124.     $aCondition = array();
  125.     $aCondition[] = 'movie_state != 3 AND movie_state != 0';
  126.     $aCondition[] = 'movie_rang != 0';
  127.     $aCondition[] = sprintf('category_nested LIKE "%s"', $sNested . '%');
  128.    
  129.     $oQueryCount = $this->q();
  130.     $oQueryCount->select($this->t('movie_likes'), 'count(like_movie_id)')
  131.                 ->where('like_movie_id = movie_id');
  132.  
  133.     $mSelect = join(', ', array('*', '('.$oQueryCount.') as movie_like_count'));      
  134.     $oQ->select($this->t('movies'), $mSelect)
  135.        ->join($this->t('categories'), 'category_id = movie_category_id')
  136.        ->join($this->t('users'), 'user_id = movie_user_id')
  137.        ->where($aCondition)
  138.        ->order('movie_state ASC, movie_id DESC')
  139.        ->limit($iOffset, $iLimit);
  140.    
  141.     $this->_oLast = $oQ;
  142.    
  143.     return $oDb->fetchAll($oQ);
  144.   }
  145.  
  146.   public function Listing($iCategory = NULL, $iLimit = 5, $iOffset = 0, $mIgnoreState = false, $iUser = null, $bExcludeUserMovie = false)
  147.   {
  148.     $oQ = $this->q();
  149.     $oDb = $this->db();
  150.    
  151.     $aCondition = array();
  152.  
  153.     if($mIgnoreState && ($mIgnoreState != 3))
  154.       $aCondition[] = 'movie_state != 3';
  155.    
  156.     if($iCategory)
  157.       $aCondition[] = 'movie_category_id = '.$iCategory;
  158.    
  159.     if($iUser)
  160.       $aCondition[] = 'movie_user_id = '.$iUser;
  161.    
  162.     if(!$bExcludeUserMovie && (($mIgnoreState == 0) || ($mIgnoreState === null)))
  163.       $aCondition[] = '(movie_state is NULL or movie_state = 0)';
  164.     elseif(is_numeric($mIgnoreState))
  165.       $aCondition[] = 'movie_state = ' . $mIgnoreState;
  166.     elseif($mIgnoreState === false)
  167.       $aCondition[] = 'movie_state = 1';
  168.    
  169.     if($bExcludeUserMovie)
  170.       $aCondition[] = 'movie_rang != 0';
  171.      
  172.     $oQueryCount = $this->q();
  173.     $oQueryCount->select($this->t('movie_likes'), 'count(like_movie_id)')
  174.                 ->where('like_movie_id = movie_id AND movie_state != 0');
  175.  
  176.     $mSelect = join(', ', array('*', '('.$oQueryCount.') as movie_like_count'));
  177.    
  178.     $oQ->select($this->t('movies'), $mSelect)
  179.        ->join($this->t('categories'), 'category_id = movie_category_id')
  180.        ->join($this->t('users'), 'user_id = movie_user_id')
  181.        ->where($aCondition)
  182.        ->order('movie_state ASC, movie_id DESC')
  183.        ->limit($iOffset, $iLimit);
  184.    
  185.     $this->_oLast = $oQ;
  186.    
  187.     return $oDb->fetchAll($oQ);
  188.   }
  189.  
  190.   public function CategoryListing($sNested, $iLimit, $iPage = 0, $sFilter = '', $sOrder = '')
  191.   {
  192.     $oQ = $this->q();
  193.     $oDb = $this->db();
  194.    
  195.     $aCondition = array('movie_state = 1');    
  196.     if($sNested)
  197.       $aCondition[] = sprintf('category_nested LIKE "%s"', $sNested . '%');
  198.    
  199.     $aCondition[] = 'movie_state != 3';
  200.    
  201.     if($sFilter)
  202.     {
  203.       $aRangs = array_flip(Aframe::Config('Movies')->rang);
  204.  
  205.       if(isset($aRangs[ucwords($sFilter)]))
  206.         $aCondition[] = sprintf('movie_rang = ' . $aRangs[ucwords($sFilter)]);
  207.     }
  208.    
  209.     $oQueryCount = $this->q();
  210.     $oQueryCount->select($this->t('movie_likes'), 'count(like_movie_id)')
  211.                 ->where('like_movie_id = movie_id');
  212.  
  213.     $mSelect = join(', ', array('*', '('.$oQueryCount.') as movie_like_count'));
  214.    
  215.     $oQ->select($this->t('movies'), $mSelect)
  216.        ->join($this->t('categories'), 'category_id = movie_category_id')
  217.        ->join($this->t('users'), 'user_id = movie_user_id')
  218.        ->where($aCondition)
  219.        ->limit((!$iPage ? 0 : $iPage-1), $iLimit);
  220.    
  221.     if($sOrder)
  222.     {
  223.       $aReplace = array
  224.       (
  225.         'top' => '('.$oQueryCount.') DESC',
  226.         'recent' => 'movie_date DESC',
  227.         'views' => 'movie_views DESC',
  228.       );
  229.      
  230.       $oQ->order($aReplace[$sOrder]);      
  231.     }else{
  232.       $oQ->order('movie_state ASC, movie_id DESC');
  233.     }
  234.    
  235.     $this->_oLast = $oQ;
  236.  
  237.     return $oDb->fetchAll($oQ);
  238.   }
  239.  
  240.   public function Delete($iId)
  241.   {
  242.     $oQ = $this->q();
  243.     $oDb = $this->db();
  244.    
  245.     $oQ->delete($this->t('movies'))
  246.        ->where('movie_id  = '.$iId);
  247.    
  248.     return $oDb->query($oQ);
  249.   }
  250.  
  251.   public function User($iUid, $iExc = null, $iLimit =5)
  252.   {
  253.     // Query connection
  254.     $oQ = $this->q();
  255.    
  256.     // Datbase Connection
  257.     $oDb = $this->db();
  258.    
  259.     $oQueryCount = $this->q();
  260.     $oQueryCount->select($this->t('movie_likes'), 'count(like_movie_id)')
  261.                 ->where('like_movie_id = movie_id');
  262.    
  263.     $aCondition = array
  264.     (
  265.       'movie_state != 3',
  266.       'user_id = '.$iUid,
  267.     );
  268.    
  269.     if($iExc)
  270.       $aCondition[] = 'movie_id != ' . $iExc;
  271.    
  272.     $oQ->select($this->t('movies'), join(', ', array('*', '('.$oQueryCount.') as  movie_like_count')))
  273.        ->join($this->t('categories'), 'category_id = movie_category_id')
  274.        ->join($this->t('users'), 'user_id = movie_user_id')
  275.        ->where($aCondition)
  276.        ->limit(0, $iLimit);
  277.  
  278.     return $oDb->fetchAll($oQ);
  279.   }
  280.  
  281.   public function find($iId, $bIncludeLikeCount = false)
  282.   {
  283.     // Query connection
  284.     $oQ = $this->q();
  285.     $oQueryCount = $this->q();
  286.    
  287.     // Datbase Connection
  288.     $oDb = $this->db();
  289.    
  290.     $oQueryCount->select($this->t('movie_likes'), 'count(like_movie_id)')
  291.                 ->where('like_movie_id = movie_id');
  292.  
  293.     if($bIncludeLikeCount)
  294.       $mSelect = join(', ', array('*', '('.$oQueryCount.') as  movie_like_count'));
  295.     else
  296.       $mSelect = true;
  297.    
  298.     $oQ->select($this->t('movies'), $mSelect)
  299.        ->join($this->t('categories'), 'category_id = movie_category_id')
  300.        ->join($this->t('users'), 'user_id = movie_user_id')
  301.        ->where('movie_id = '.$iId);
  302.    
  303.     return $oDb->fetchOne($oQ);
  304.   }
  305.  
  306.   public function updateArray($aArray, $iMovie)
  307.   {
  308.     $oQ = $this->q();
  309.     $oDb = $this->db();
  310.    
  311.     $oQ->update($this->t('movies'), $aArray)
  312.        ->where('movie_id = '. $iMovie);
  313.    
  314.     return $oDb->query($oQ);
  315.   }
  316.  
  317.   public function Submit($sTitle, $iCat, $sImage, $sTag, $sText, $iUid, $sNeed, $sUrl, $iRank = NULL)
  318.   {
  319.     $oQ = $this->q();
  320.     $oDb = $this->db();
  321.    
  322.     $aData = array
  323.     (
  324.       'movie_title' => $sTitle,
  325.       'movie_category_id' => $iCat,
  326.       'movie_user_id' => (int)$iUid,
  327.       'movie_tags' => $sTag,
  328.       'movie_need' => $sNeed,
  329.       'movie_date' => date('Y-m-d G:i:s'),
  330.       'movie_thumb' => $sImage,
  331.       'movie_text' => $sText,
  332.       'movie_url' => $sUrl
  333.     );
  334.    
  335.     if($iRank)
  336.     {
  337.       $aData['movie_rang'] = $iRank;
  338.       // $aData['movie_state'] = 1;
  339.     }
  340.    
  341.     $oQ->insert($this->t('movies'), $aData);
  342.  
  343.     if ($oDb->query($oQ))
  344.       return $oDb->lastId();
  345.   }
  346.  
  347.   public function counter($iMov)
  348.   {
  349.     $oQ = $this->q();
  350.     $oDb = $this->db();
  351.    
  352.     $aData = array
  353.     (
  354.       'movie_views' => array('movie_views+1')
  355.     );
  356.  
  357.     $oQ->update($this->t('movies'), $aData)
  358.        ->where('movie_id = '.$iMov);
  359.            
  360.     return $oDb->query($oQ);
  361.   }
  362.  
  363.   public function deleteThumb($iId)
  364.   {
  365.     $oQ = $this->q();
  366.     $oDb = $this->db();
  367.    
  368.     $aData = array
  369.     (
  370.       'movie_thumb' => NULL
  371.     );
  372.  
  373.     $oQ->update($this->t('movies'), $aData)
  374.        ->where('movie_id = '.$iId);
  375.            
  376.     return $oDb->query($oQ);
  377.   }
  378.  
  379.   public function Subscribe($iUid, $iLimit = 6)
  380.   {
  381.     $oQ = $this->q();
  382.     $oDb = $this->db();
  383.    
  384.     $oQ->select($this->t('user_subscribes'), 'sub_object_id')
  385.        ->where('sub_user_id = '.$iUid);
  386.    
  387.     $aCondition = array('movie_state = 1', 'movie_user_id IN ('.$oQ.')');
  388.    
  389.     $oQ = $this->q();
  390.     $oQ->select($this->t('movies'))
  391.        ->join($this->t('categories'), 'category_id = movie_category_id')
  392.        ->join($this->t('users'), 'user_id = movie_user_id')
  393.        ->where($aCondition)
  394.        ->order('movie_state ASC, movie_id DESC')
  395.        ->limit(0, $iLimit);
  396.  
  397.     return $oDb->fetchAll($oQ);
  398.   }
  399.  
  400. }
  401.  
  402. ?>