public function __construct()
 {
     WiseChatContainer::load('model/WiseChatAction');
     $this->options = WiseChatOptions::getInstance();
     $this->table = WiseChatInstaller::getActionsTable();
 }
 /**
  * Returns array of various statistics for each channel.
  *
  * @return array Array of objects (fields: channel, messages, users, last_message)
  */
 public function getChannelsSummary()
 {
     global $wpdb;
     $table = WiseChatInstaller::getMessagesTable();
     $conditions = array();
     $conditions[] = "user != 'System'";
     $sql = "SELECT channel, count(*) AS messages, max(time) AS last_message FROM {$table} " . " WHERE " . implode(" AND ", $conditions) . " GROUP BY channel " . " ORDER BY channel ASC " . " LIMIT 1000;";
     $mainSummary = $wpdb->get_results($sql);
     $usersSummary = $this->channelUsersDAO->getAllChannelsStats();
     $usersSummaryMap = array();
     foreach ($usersSummary as $userDetails) {
         $usersSummaryMap[$userDetails->getChannel()->getName()] = intval($userDetails->getNumberOfUsers());
     }
     $mainSummaryMap = array();
     foreach ($mainSummary as $mainDetails) {
         $mainDetails->users = array_key_exists($mainDetails->channel, $usersSummaryMap) ? $usersSummaryMap[$mainDetails->channel] : 0;
         $mainSummaryMap[$mainDetails->channel] = $mainDetails;
     }
     $channels = $this->channelsDAO->getAll();
     $fullSummary = array();
     foreach ($channels as $channel) {
         if (array_key_exists($channel->getName(), $mainSummaryMap)) {
             $channelPrepared = $mainSummaryMap[$channel->getName()];
             $channelPrepared->secured = strlen($channel->getPassword()) > 0;
             $fullSummary[] = $channelPrepared;
         } else {
             $fullSummary[] = (object) array('channel' => $channel->getName(), 'messages' => 0, 'users' => array_key_exists($channel->getName(), $usersSummaryMap) ? $usersSummaryMap[$channel->getName()] : 0, 'last_message' => null, 'secured' => strlen($channel->getPassword()) > 0);
         }
     }
     return $fullSummary;
 }
 /**
  * Deletes the channel by ID.
  *
  * @param integer $id
  *
  * @return null
  */
 public function deleteById($id)
 {
     global $wpdb;
     $id = intval($id);
     $table = WiseChatInstaller::getChannelsTable();
     $wpdb->get_results(sprintf("DELETE FROM %s WHERE id = '%d';", $table, $id));
 }
 /**
  * Creates or updates the user and returns it.
  *
  * @param WiseChatUser $user
  *
  * @return WiseChatUser
  * @throws Exception On validation error
  */
 public function save($user)
 {
     global $wpdb;
     // low-level validation:
     if ($user->getName() === null) {
         throw new Exception('Name of the user cannot equal null');
     }
     if ($user->getSessionId() === null) {
         throw new Exception('Session ID of the user cannot equal null');
     }
     // prepare user data:
     $table = WiseChatInstaller::getUsersTable();
     $columns = array('name' => $user->getName(), 'session_id' => $user->getSessionId(), 'data' => json_encode($user->getData()), 'ip' => $user->getIp());
     // update or insert:
     if ($user->getId() !== null) {
         $columns['wp_id'] = $user->getWordPressId();
         $wpdb->update($table, $columns, array('id' => $user->getId()), '%s', '%d');
     } else {
         if ($user->getWordPressId() > 0) {
             $columns['wp_id'] = $user->getWordPressId();
         }
         $columns['created'] = time();
         $wpdb->insert($table, $columns);
         $user->setId($wpdb->insert_id);
     }
     return $user;
 }