Example #1
0
 public function __construct()
 {
     parent::__construct('Fireside');
     // latest messages
     $messages = new Messages();
     $latestMessages = $messages->find()->orderBy('id DESC')->limit(20)->where(array('id' => '>0'));
     foreach ($latestMessages as $message) {
         $item = array('http://' . $_SERVER['HTTP_HOST'] . WWW_DIR . "/messages/show/{$message['topics_id']}/{$message['id']}" => array('title' => $message['name'], 'content' => 'Message by ' . $message['user'] . ' on ' . $message['created_at']));
         $this->atomise($item);
     }
     $this->generate();
 }
 public function debugAction()
 {
     $this->initalize();
     $messages = Messages::find();
     echo (new \Phalcon\Debug\Dump())->variable($messages, 'messages');
 }
 public function voteEntity($type, $entity_type)
 {
     //status is 1 for upvote, 0 for none, -1 for downvote
     $entity_id = Input::get('id');
     $member = Auth::user()->id;
     $status = 0;
     $user_exists = 0;
     if ($entity_type) {
         //Voting on a chat
         $entity = Chats::find($entity_id);
         $voted = new ChatsVoted();
         $temp = $voted->whereuser_id($member)->wherechat_id($entity_id)->first();
         $community = Communities::wherename(Auth::user()->page)->first();
         if ($temp) {
             $voted = $temp;
             $status = $voted->status;
         } else {
             $voted->user_id = $member;
             $voted->chat_id = $entity_id;
         }
         if (preg_match('/[a-zA-Z]/', $entity->admin)) {
             $user = new User();
             $user->name = $entity->admin;
             $user = $user->findAll();
             $user_exists = 1;
         }
     } else {
         //voting on a message
         $entity = Messages::find($entity_id);
         $voted = new MessagesVoted();
         $temp = $voted->whereuser_id($member)->wheremessage_id($entity_id)->first();
         if ($temp) {
             $voted = $temp;
             $status = $voted->status;
         } else {
             $voted->user_id = $member;
             $voted->message_id = $entity_id;
         }
         if (preg_match('/[a-zA-Z]/', $entity->author)) {
             $user = new User();
             $user->name = $entity->author;
             $user = $user->findAll();
             $user_exists = 1;
         }
     }
     /* MODIFY USER COGNIZANCE BASED ON UP OR DOWN VOTE */
     if ($user_exists) {
         if ($user->name != Auth::user()->name) {
             // create/modify asymmetric interaction
             $interaction_user = InteractionUsers::whereuser_id(Auth::user()->id)->whereentity_id($user->id)->wheretype(0)->first();
             if ($interaction_user) {
                 $interaction_user->bond = $interaction_user->bond - $status + $type;
                 if ($interaction_user->bond < 1) {
                     $interaction_user->delete();
                 } else {
                     $interaction_user->save();
                 }
                 $interaction_friend = InteractionUsers::whereentity_id(Auth::user()->id)->whereuser_id($user->id)->wheretype(0)->first();
                 if ($interaction_friend->bond < 1) {
                     $interaction_friend->delete();
                 } else {
                     $interaction_friend->save();
                 }
                 $interaction_friend->save();
             } else {
                 if ($type - $status > 0) {
                     $inter_user = new InteractionUsers();
                     $inter_user->user_id = Auth::user()->id;
                     $inter_user->entity_id = $user->id;
                     $inter_user->bond = $type - $status;
                     $inter_user->save();
                     $inter_friend = new InteractionUsers();
                     $inter_friend->user_id = $user->id;
                     $inter_friend->entity_id = Auth::user()->id;
                     $inter_friend->bond = 1;
                     $inter_friend->save();
                 }
             }
         }
         if ($user->level == 0) {
             if ($type == -1 && ($status == 0 || $status == -1)) {
             } else {
                 if ($type == 1 && $status == -1) {
                     $user->total_cognizance = $user->total_cognizance + 1;
                     if ($user->cognizance + 1 >= $user->next_level) {
                         $user->cognizance = ($user->cognizance + 1) % $user->next_level;
                         $user->level = $user->level + 1;
                         $user->next_level = $this->nextLevel($user->level);
                     } else {
                         $user->cognizance = ($user->cognizance + 1) % $user->next_level;
                     }
                 } else {
                     if ($type == -1 && $status == 1 || $type == 1 && $status == 1) {
                         $user->total_cognizance = $user->total_cognizance - 1;
                         if ($user->level > 0 && $user->cognizance - 1 < 0) {
                             $user->level = $user->level - 1;
                             $user->next_level = $this->nextLevel($user->level);
                             $user->cognizance = $user->next_level - 1;
                         } else {
                             $user->cognizance = ($user->cognizance - 1) % $user->next_level;
                         }
                     } else {
                         $user->total_cognizance = $user->total_cognizance - $status + $type;
                         if ($user->cognizance - $status + $type >= $user->next_level) {
                             $user->cognizance = ($user->cognizance - $status + $type) % $user->next_level;
                             $user->level = $user->level + 1;
                             $user->next_level = $this->nextLevel($user->level);
                         } else {
                             $user->cognizance = ($user->cognizance - $status + $type) % $user->next_level;
                         }
                     }
                 }
             }
         } else {
             $user->total_cognizance = $user->total_cognizance - $status + $type;
             if ($user->cognizance - $status + $type >= $user->next_level) {
                 $user->cognizance = ($user->cognizance - $status + $type) % $user->next_level;
                 $user->level = $user->level + 1;
                 $user->next_level = $this->nextLevel($user->level);
             } else {
                 if ($user->level > 0 && $user->cognizance - $status + $type < 0) {
                     $user->level = $user->level - 1;
                     $user->next_level = $this->nextLevel($user->level);
                     $user->cognizance = $user->next_level - $status + $type;
                 } else {
                     $user->cognizance = ($user->cognizance - $status + $type) % $user->next_level;
                 }
             }
         }
         $user->save();
     }
     if ($entity_type) {
         foreach ($entity->communities as $community) {
             $usercommunity = UsersToCommunities::wherecommunity_id($community->id)->whereuser_id(Auth::user()->id)->first();
             if ($usercommunity) {
                 $usercommunity->score = $usercommunity->score - $status + $type;
                 $usercommunity->save();
             }
             $community->popularity = $community->popularity - $status + $type;
             $community->save();
         }
     }
     /* MODIFY MESSAGE OR CHAT UPVOTES AND DOWNVOTES */
     if ($entity_type) {
         $chat_to_community = ChatsToCommunities::wherechat_id($entity->id)->wherecommunity_id($community->id)->first();
         if ($chat_to_community) {
             $chat_to_community->upvotes = $type == $status && $type == 1 ? $chat_to_community->upvotes - 1 : ($type != $status && $type == 1 ? $chat_to_community->upvotes + 1 : ($status == 1 && $type == -1 ? $chat_to_community->upvotes - 1 : $chat_to_community->upvotes));
             $chat_to_community->downvotes = $type == $status && $type == -1 ? $chat_to_community->downvotes - 1 : ($type != $status && $type == -1 ? $chat_to_community->downvotes + 1 : ($status == -1 && $type == 1 ? $chat_to_community->downvotes - 1 : $chat_to_community->downvotes));
             $chat_to_community->save();
         } else {
             $chat_to_communities = ChatsToCommunities::wherechat_id($chat_to_community->id)->get();
             foreach ($chat_to_communities as $chat_to_community) {
                 $chat_to_community->upvotes = $type == $status && $type == 1 ? $chat_to_community->upvotes - 1 : ($type != $status && $type == 1 ? $chat_to_community->upvotes + 1 : ($status == 1 && $type == -1 ? $chat_to_community->upvotes - 1 : $chat_to_community->upvotes));
                 $chat_to_community->downvotes = $type == $status && $type == -1 ? $chat_to_community->downvotes - 1 : ($type != $status && $type == -1 ? $chat_to_community->downvotes + 1 : ($status == -1 && $type == 1 ? $chat_to_community->downvotes - 1 : $chat_to_community->downvotes));
                 $chat_to_community->save();
             }
         }
     }
     $entity->upvotes = $type == $status && $type == 1 ? $entity->upvotes - 1 : ($type != $status && $type == 1 ? $entity->upvotes + 1 : ($status == 1 && $type == -1 ? $entity->upvotes - 1 : $entity->upvotes));
     $entity->downvotes = $type == $status && $type == -1 ? $entity->downvotes - 1 : ($type != $status && $type == -1 ? $entity->downvotes + 1 : ($status == -1 && $type == 1 ? $entity->downvotes - 1 : $entity->downvotes));
     $entity->save();
     if ($type == 1 && $status == -1) {
         //downvoted previously and now upvoted
         $voted->status = 1;
         $voted->save();
         return array('status' => 1, 'upvotes' => $entity->upvotes - $entity->downvotes);
     } elseif ($type == -1 && $status == 1) {
         // upvoted previously and now downvoted
         $voted->status = -1;
         $voted->save();
         return array('status' => 1, 'upvotes' => $entity->upvotes - $entity->downvotes);
     } elseif ($status == 1 && $type == 1 || $status == 0 && $type == -1) {
         //upvoted previously or first time downvoting
         if ($type == 1) {
             $voted->status = 0;
             $voted->save();
             return array('status' => 2, 'upvotes' => $entity->upvotes - $entity->downvotes);
         } else {
             $voted->status = -1;
             $voted->save();
             return array('status' => 3, 'upvotes' => $entity->upvotes - $entity->downvotes);
         }
     } else {
         //first time upvoting or downvoted previously now downvoted
         if ($type == 1) {
             $voted->status = 1;
             $voted->save();
             return array('status' => 3, 'upvotes' => $entity->upvotes - $entity->downvotes);
         } else {
             $voted->status = 0;
             $voted->save();
             return array('status' => 2, 'upvotes' => $entity->upvotes - $entity->downvotes);
         }
     }
 }