Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.97 KB | None | 0 0
  1. /**
  2.      * Select limited stories where they are less than the given timestamp
  3.      *
  4.      * @param  int       $limit
  5.      * @param  int       $timestamp
  6.      * @param  Mongo_ID  $category
  7.      * @return array
  8.      */
  9.     public function select_stories($limit, $timestamp, $category = null)
  10.     {
  11.         $where = array('timestamp' => array('$lt' => $timestamp), 'status' => 1);
  12.  
  13.         if(!is_null($category))
  14.         {
  15.             $where += array('category_id' => $category);
  16.         }
  17.  
  18.         $stories = $this->find($where)->sort(array('timestamp' => '-1'))->limit($limit);
  19.  
  20.         return $this->build_stories_data($stories);
  21.     }
  22.  
  23.     public function build_stories_data($stories)
  24.     {
  25.         $tags = $data = $categories = $users = $domains = array();
  26.         $count = 0;
  27.  
  28.         foreach($stories as $row)
  29.         {
  30.             $data[$count] = $row;
  31.             $data[$count]['tags'] = array();
  32.  
  33.             /* Build story tags */
  34.             foreach($row['tags'] as $tag)
  35.             {
  36.                 $tag_key = (string) $tag;
  37.  
  38.                 if(!isset($tags[$tag_key]))
  39.                 {
  40.                     $tags[$tag_key] = \Tag::instance()->get(array('_id' => $tag), array('tag', 'link'));
  41.                 }
  42.  
  43.                 $data[$count]['tags'][] = $tags[$tag_key];
  44.             }
  45.  
  46.             /* Build Category */
  47.             $category = (string) $row['category_id'];
  48.  
  49.             if(!isset($categories[$category]))
  50.             {
  51.                 $categories[$category] = \Category::instance()->get(array('_id' => $row['category_id']), array('category', 'link'));
  52.             }
  53.  
  54.             $data[$count]['category'] = $categories[(string) $row['category_id']];
  55.  
  56.             /* Build user */
  57.             $user = (string) $row['user_id'];
  58.  
  59.             if(!isset($users[$user]))
  60.             {
  61.                 $users[$user] = \User::instance()->get(array('_id' => $row['user_id']), array('username', 'username_link'));
  62.             }
  63.  
  64.             /* Build domain */
  65.             $domain = (string) $row['domain_id'];
  66.  
  67.             if(!isset($domains[$domain]))
  68.             {
  69.                 $domains[$domain] = \Domain::instance()->get(array('_id' => $row['domain_id']), array('domain', 'link'));
  70.             }
  71.  
  72.             $data[$count]['domain'] = $domains[$domain];
  73.  
  74.             /* Build Votes */
  75.             if(!isset($row['votes']))
  76.             {
  77.                 $data[$count]['votes'] = array(
  78.                     'ratio' => 0
  79.                 );
  80.             }
  81.  
  82.             $count++;
  83.         }
  84.  
  85.         return $data;
  86.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement