Example #1
0
 /**
  * Loads an existing ThumbsUp item.
  *
  * @param   mixed  item name or id
  * @return  mixed  ThumbsUp_Item object if the item could be found, FALSE otherwise
  */
 public static function load($name)
 {
     // Are we loading by id or by name?
     $key = is_int($name) ? 'id' : 'name';
     // Load the item
     $sth = ThumbsUp::db()->prepare('SELECT id, name, date, closed, votes_up, votes_down FROM ' . ThumbsUp::config('database_table_prefix') . 'items WHERE ' . $key . ' = ?');
     $sth->execute(array($name));
     // Fetch the item record if it was found
     if (!($row = $sth->fetch(PDO::FETCH_OBJ))) {
         return FALSE;
     }
     // Setup the item object
     $item = new self();
     // Store the item values as properties
     $item->id = (int) $row->id;
     $item->name = $row->name;
     $item->date = (int) $row->date;
     $item->closed = (bool) $row->closed;
     $item->votes_up = (int) $row->votes_up;
     $item->votes_down = (int) $row->votes_down;
     // Calculate the vote results
     $item->calculate_votes();
     // Initial default value
     $item->user_voted = FALSE;
     // Check cookie for a vote
     if (ThumbsUp::config('cookie_check') and ThumbsUp_Cookie::find_id($item->id)) {
         $item->user_voted = TRUE;
     }
     // Check for a previous vote by IP
     if (!$item->user_voted and ThumbsUp::config('ip_check') and $ip = ThumbsUp::get_ip()) {
         // Because of the ip_lifetime config setting, it's possible multiple records contain the same IP.
         // We only load the most recent one to check the lifetime later on.
         $sth = ThumbsUp::db()->prepare('SELECT date FROM ' . ThumbsUp::config('database_table_prefix') . 'votes WHERE item_id = ? AND ip = ? ORDER BY date DESC LIMIT 1');
         $sth->execute(array($item->id, $ip));
         // A record with the IP was found
         if ($date = (int) $sth->fetchColumn()) {
             if (!ThumbsUp::config('ip_lifetime') or $date > time() - ThumbsUp::config('ip_lifetime')) {
                 // If the IP lifetime is unlimited or the vote date
                 // still falls within the lifetime, mark the item as voted on.
                 $item->user_voted = TRUE;
             }
         }
     }
     // Check for a previous vote by user id
     if (!$item->user_voted and ThumbsUp::config('user_id_check') and $user_id = ThumbsUp::get_user_id()) {
         $sth = ThumbsUp::db()->prepare('SELECT 1 FROM ' . ThumbsUp::config('database_table_prefix') . 'votes WHERE item_id = ? AND user_id = ? LIMIT 1');
         $sth->execute(array($item->id, $user_id));
         $item->user_voted = (bool) $sth->fetchColumn();
     }
     return $item;
 }