Ejemplo n.º 1
0
 public static function getStatusByInfo($info)
 {
     $timeout = self::$options['online_timeout'];
     // in sec
     if (isset($info['last_datetime']) && $info['last_datetime'] && $info['last_datetime'] != '0000-00-00 00:00:00') {
         if (time() - strtotime($info['last_datetime']) < $timeout) {
             $m = new waLoginLogModel();
             $datetime_out = $m->select('datetime_out')->where('contact_id = i:0', array($this->id))->order('id DESC')->limit(1)->fetchField();
             if (!$datetime_out) {
                 return 'online';
             } else {
                 return 'offline';
             }
         }
     }
     return 'offline';
 }
Ejemplo n.º 2
0
 /**
  * Get data for contacts in this collection.
  * @param string|array $fields
  * @param int $offset
  * @param int $limit
  * @return array [contact_id][field] = field value in appropriate field format
  * @throws waException
  */
 public function getContacts($fields = "id", $offset = 0, $limit = 50)
 {
     $sql = "SELECT " . $this->getFields($fields) . " " . $this->getSQL();
     $sql .= $this->getGroupBy();
     $sql .= $this->getHaving();
     $sql .= $this->getOrderBy();
     $sql .= " LIMIT " . ($offset ? $offset . ',' : '') . (int) $limit;
     //header("X-SQL-". mt_rand() . ": ". str_replace("\n", " ", $sql));
     $data = $this->getModel()->query($sql)->fetchAll('id');
     $ids = array_keys($data);
     //
     // Load fields from other storages
     //
     if ($ids && $this->post_fields) {
         // $fill[table][field] = null
         // needed for all rows to always contain all apropriate keys
         // in case when we're asked to load all fields from that table
         $fill = array_fill_keys(array_keys($this->post_fields), array());
         foreach (waContactFields::getAll('enabled') as $fid => $field) {
             /**
              * @var waContactField $field
              */
             $fill[$field->getStorage(true)][$fid] = false;
         }
         foreach ($this->post_fields as $table => $fields) {
             if ($table == '_internal') {
                 foreach ($fields as $f) {
                     /**
                      * @var $f string
                      */
                     if ($f == 'photo_url' || substr($f, 0, 10) == 'photo_url_') {
                         if ($f == 'photo_url') {
                             $size = null;
                         } else {
                             $size = substr($f, 10);
                         }
                         $retina = isset($this->options['photo_url_2x']) ? $this->options['photo_url_2x'] : null;
                         foreach ($data as $id => &$v) {
                             $v[$f] = waContact::getPhotoUrl($id, $v['photo'], $size, $size, $v['is_company'] ? 'company' : 'person', $retina);
                         }
                         unset($v);
                     } else {
                         switch ($f) {
                             case '_online_status':
                                 $llm = new waLoginLogModel();
                                 $contact_ids_map = $llm->select('DISTINCT contact_id')->where('datetime_out IS NULL')->fetchAll('contact_id');
                                 $timeout = waUser::getOption('online_timeout');
                                 foreach ($data as &$v) {
                                     if (isset($v['last_datetime']) && $v['last_datetime'] && $v['last_datetime'] != '0000-00-00 00:00:00') {
                                         if (time() - strtotime($v['last_datetime']) < $timeout) {
                                             if (isset($contact_ids_map[$v['id']])) {
                                                 $v['_online_status'] = 'online';
                                             } else {
                                                 $v['_online_status'] = 'offline';
                                             }
                                         }
                                     }
                                     $v['_online_status'] = 'offline';
                                 }
                                 unset($v);
                                 break;
                             case '_access':
                                 $rm = new waContactRightsModel();
                                 $accessStatus = $rm->getAccessStatus($ids);
                                 foreach ($data as $id => &$v) {
                                     if (!isset($accessStatus[$id])) {
                                         $v['_access'] = '';
                                         continue;
                                     }
                                     $v['_access'] = $accessStatus[$id];
                                 }
                                 unset($v);
                                 break;
                             default:
                                 throw new waException('Unknown internal field: ' . $f);
                         }
                     }
                 }
                 continue;
             }
             $data_fields = $fields;
             foreach ($data_fields as $k => $field_id) {
                 $f = waContactFields::get($field_id);
                 if ($f && $f instanceof waContactCompositeField) {
                     unset($data_fields[$k]);
                     $data_fields = array_merge($data_fields, $f->getField());
                 }
             }
             $model = $this->getModel($table);
             $post_data = $model->getData($ids, $data_fields);
             foreach ($post_data as $contact_id => $contact_data) {
                 foreach ($contact_data as $field_id => $value) {
                     if (!($f = waContactFields::get($field_id))) {
                         continue;
                     }
                     if (!$f->isMulti()) {
                         $post_data[$contact_id][$field_id] = isset($value[0]['data']) ? $value[0]['data'] : (is_array($value[0]) ? $value[0]['value'] : $value[0]);
                     }
                 }
             }
             if ($fields) {
                 $fill[$table] = array_fill_keys($fields, '');
             } else {
                 if (!isset($fill[$table])) {
                     $fill[$table] = array();
                 }
             }
             foreach ($data as $contact_id => $v) {
                 if (isset($post_data[$contact_id])) {
                     $data[$contact_id] += $post_data[$contact_id];
                 }
                 $data[$contact_id] += $fill[$table];
             }
         }
     }
     return $data;
 }
Ejemplo n.º 3
0
 /**
  * Returns user's status: "online", "offline"
  *
  * @return string
  */
 public function getStatus()
 {
     $timeout = self::$options['online_timeout'];
     // in sec
     if (($last = $this->get('last_datetime')) && $last != '0000-00-00 00:00:00') {
         if (time() - strtotime($last) < $timeout) {
             $m = new waLoginLogModel();
             $datetime_out = $m->select('datetime_out')->where('contact_id = i:0', array($this->id))->order('id DESC')->limit(1)->fetchField();
             if ($datetime_out === null) {
                 return 'online';
             } else {
                 return 'offline';
             }
         }
     }
     return 'offline';
 }