Example #1
0
 /**
  * Get all users method
  *
  * @param  int         $typeId
  * @param  \Pop\Config $config
  * @param  string      $sort
  * @param  string      $page
  * @return void
  */
 public function getAll($typeId, $config, $sort = null, $page = null)
 {
     $userView = array();
     if (null !== $config->user_view) {
         $uv = $config->user_view->asArray();
         if (isset($uv[$typeId]) && count($uv[$typeId]) > 0) {
             $userView = $uv[$typeId];
         }
     }
     $order = $this->getSortOrder($sort, $page, 'DESC');
     $sql = Table\Users::getSql();
     $order['field'] = $order['field'] == 'id' ? DB_PREFIX . 'users.id' : $order['field'];
     $searchString = null;
     if (isset($_GET['search_by']) && isset($_GET['search_for'])) {
         $searchString = '&search_by=' . $_GET['search_by'] . '&search_for=' . $_GET['search_for'];
     }
     // Build the SQL statement to get users
     if (isset($_GET['field_id'])) {
         $sql->select(array(0 => DB_PREFIX . 'users.id', 1 => DB_PREFIX . 'users.type_id', 2 => DB_PREFIX . 'users.role_id', 3 => DB_PREFIX . 'user_types.type', 'role' => DB_PREFIX . 'user_roles.name', 4 => DB_PREFIX . 'users.username', 5 => DB_PREFIX . 'users.email', 6 => DB_PREFIX . 'users.logins', 7 => DB_PREFIX . 'field_values.field_id', 8 => DB_PREFIX . 'field_values.value'))->join(DB_PREFIX . 'user_types', array('type_id', 'id'), 'LEFT JOIN')->join(DB_PREFIX . 'user_roles', array('role_id', 'id'), 'LEFT JOIN')->join(DB_PREFIX . 'field_values', array('id', 'model_id'), 'LEFT JOIN')->orderBy('value', $order['order']);
         $sql->select()->where()->equalTo(DB_PREFIX . 'field_values.field_id', ':field_id');
         $params = array('field_id' => (int) $_GET['field_id'], 'type_id' => $typeId);
     } else {
         $sql->select(array(0 => DB_PREFIX . 'users.id', 1 => DB_PREFIX . 'users.type_id', 2 => DB_PREFIX . 'users.role_id', 3 => DB_PREFIX . 'user_types.type', 'role' => DB_PREFIX . 'user_roles.name', 4 => DB_PREFIX . 'users.username', 5 => DB_PREFIX . 'users.email', 6 => DB_PREFIX . 'users.logins'))->join(DB_PREFIX . 'user_types', array('type_id', 'id'), 'LEFT JOIN')->join(DB_PREFIX . 'user_roles', array('role_id', 'id'), 'LEFT JOIN')->orderBy($order['field'], $order['order']);
         $params = array('type_id' => $typeId);
     }
     $sql->select()->where()->equalTo(DB_PREFIX . 'users.type_id', ':type_id');
     $search = false;
     $searchByMarked = null;
     $searchFor = null;
     $rowCount = null;
     if (isset($_GET['search_by'])) {
         $search = true;
         if ($_GET['search_by'] == 'username') {
             $sql->select()->where()->like(DB_PREFIX . 'users.username', ':username');
             $searchByMarked = 'username';
             $searchFor = htmlentities(strip_tags($_GET['search_for']), ENT_QUOTES, 'UTF-8');
             $params['username'] = '******' . $searchFor . '%';
         } else {
             if ($_GET['search_by'] == 'email') {
                 $sql->select()->where()->like(DB_PREFIX . 'users.email', ':email');
                 $searchByMarked = 'email';
                 $searchFor = htmlentities(strip_tags($_GET['search_for']), ENT_QUOTES, 'UTF-8');
                 $params['email'] = '%' . $searchFor . '%';
             } else {
                 if (strpos($_GET['search_by'], 'field_') !== false) {
                     $id = (int) substr($_GET['search_by'], strrpos($_GET['search_by'], '_') + 1);
                     if (!isset($_GET['field_id'])) {
                         $sql->select()->join(DB_PREFIX . 'field_values', array('id', 'model_id'), 'LEFT JOIN');
                         $sql->select()->where()->equalTo(DB_PREFIX . 'field_values.field_id', ':field_id');
                         $params['field_id'] = $id;
                     }
                     $sql->select()->where()->like(DB_PREFIX . 'field_values.value', ':value');
                     $searchByMarked = $_GET['search_by'];
                     $searchFor = htmlentities(strip_tags($_GET['search_for']), ENT_QUOTES, 'UTF-8');
                     $params['value'] = '%' . $searchFor . '%';
                 }
             }
         }
     }
     if (null !== $order['limit']) {
         $rowCount = Table\Users::execute($sql->render(true), $params)->count();
         $sql->select()->limit($order['limit'])->offset($order['offset']);
         $users = Table\Users::execute($sql->render(true), $params);
     } else {
         $users = Table\Users::execute($sql->render(true), $params);
     }
     $userType = Table\UserTypes::findById($typeId);
     if (null === $rowCount && $search) {
         $rowCount = $users->count();
     } else {
         if (null === $rowCount) {
             $rowCount = Table\Users::getCount(array('type_id' => $typeId));
         }
     }
     $this->data['title'] = isset($userType->id) ? ucwords(str_replace('-', ' ', $userType->type)) : null;
     $this->data['type'] = $userType->type;
     if ($this->data['acl']->isAuth('Phire\\Controller\\Phire\\User\\IndexController', 'remove') && $this->data['acl']->isAuth('Phire\\Controller\\Phire\\User\\IndexController', 'remove_' . $typeId)) {
         $removeCheckbox = '<input type="checkbox" name="remove_users[]" id="remove_users[{i}]" value="[{id}]" />';
         $removeCheckAll = '<input type="checkbox" id="checkall" name="checkall" value="remove_users" />';
         $submit = array('class' => 'remove-btn', 'value' => $this->i18n->__('Remove'));
     } else {
         $removeCheckbox = '&nbsp;';
         $removeCheckAll = '&nbsp;';
         $submit = array('class' => 'remove-btn', 'value' => $this->i18n->__('Remove'), 'style' => 'display: none;');
     }
     $options = array('form' => array('id' => 'user-remove-form', 'action' => BASE_PATH . APP_URI . '/users/remove/' . $typeId, 'method' => 'post', 'process' => $removeCheckbox, 'submit' => $submit), 'table' => array('headers' => array('id' => '<a href="' . BASE_PATH . APP_URI . '/users/index/' . $typeId . '?sort=id' . $searchString . '">#</a>', 'edit' => '<span style="display: block; margin: 0 auto; width: 100%; text-align: center;">' . $this->i18n->__('Edit') . '</span>', 'role' => '<a href="' . BASE_PATH . APP_URI . '/users/index/' . $typeId . '?sort=role' . $searchString . '">' . $this->i18n->__('Role') . '</a>', 'username' => '<a href="' . BASE_PATH . APP_URI . '/users/index/' . $typeId . '?sort=username' . $searchString . '">' . $this->i18n->__('Username') . '</a>', 'email' => '<a href="' . BASE_PATH . APP_URI . '/users/index/' . $typeId . '?sort=email' . $searchString . '">' . $this->i18n->__('Email') . '</a>', 'last_login' => $this->i18n->__('Logins') . ' <span style="font-weight: normal;">[ ' . $this->i18n->__('Last Login') . ' ]</span>', 'process' => $removeCheckAll), 'class' => 'data-table', 'cellpadding' => 0, 'cellspacing' => 0, 'border' => 0), 'separator' => '', 'exclude' => array('type_id', 'role_id', 'logins', 'process' => array('id' => $this->data['user']->id)), 'indent' => '        ');
     // Clean up user data
     $userRows = $users->rows;
     $userAry = array();
     $searchByAry = array();
     foreach ($userRows as $key => $value) {
         $logins = unserialize($value->logins);
         if (is_array($logins)) {
             $lastAry = end($logins);
             $last = date('D  M j, Y H:i:s', key($logins)) . ' (' . $lastAry['ip'] . '), ' . $lastAry['ua'];
             if ($this->data['acl']->isAuth('Phire\\Controller\\Phire\\User\\IndexController', 'logins') && $this->data['acl']->isAuth('Phire\\Controller\\Phire\\User\\IndexController', 'logins_' . $typeId)) {
                 $count = '<a href="' . BASE_PATH . APP_URI . '/users/logins/' . $value->id . '">' . count($logins) . '</a>';
             } else {
                 $count = count($logins);
             }
         } else {
             $last = '(N/A)';
             $count = 0;
         }
         if ($this->data['acl']->isAuth('Phire\\Controller\\Phire\\User\\IndexController', 'edit') && $this->data['acl']->isAuth('Phire\\Controller\\Phire\\User\\IndexController', 'edit_' . $typeId)) {
             $edit = '<a class="edit-link" title="' . $this->i18n->__('Edit') . '" href="' . BASE_PATH . APP_URI . '/users/edit/' . $userRows[$key]->id . '">Edit</a>';
         } else {
             $edit = null;
         }
         if ($this->data['acl']->isAuth('Phire\\Controller\\Phire\\User\\IndexController', 'type') && $this->data['acl']->isAuth('Phire\\Controller\\Phire\\User\\IndexController', 'type_' . $typeId)) {
             $userRows[$key]->type = '<a href="' . BASE_PATH . APP_URI . '/users/type/' . $userRows[$key]->id . '">' . $userRows[$key]->type . '</a>';
         }
         $userRows[$key]->role = null !== $value->role ? $value->role : '(Blocked)';
         $userRows[$key]->last_login = $last;
         $userRows[$key]->login_count = $count;
         $lastLogin = $userRows[$key]->last_login;
         $lastLoginShort = strlen($lastLogin) > 100 ? substr($lastLogin, 0, 100) . '...' : $lastLogin;
         if (count($userView) > 0) {
             $searchByAry = array();
             $fieldValues = FieldValue::getAll($userRows[$key]->id, FieldValue::GET_BOTH);
             $uAry = array('id' => $userRows[$key]->id);
             foreach ($userView as $name) {
                 if (isset($userRows[$key]->{$name})) {
                     $uAry[$name] = $userRows[$key]->{$name};
                     if ($name !== 'username' && $name !== 'email') {
                         $searchByAry[$name] = ucwords(str_replace('_', ' ', $name));
                     } else {
                         $searchByAry[$name] = ucwords($name);
                     }
                 } else {
                     if (isset($fieldValues[$name])) {
                         $uAry[$name] = $fieldValues[$name]['value'];
                         $searchByAry[$fieldValues[$name]['id']] = ucwords(str_replace('_', ' ', $name));
                         if (null !== $searchString && $_GET['search_by'] == $fieldValues[$name]['id']) {
                             $realSearchString = $searchString;
                         } else {
                             $realSearchString = null;
                         }
                         $options['table']['headers'][$name] = '<a href="' . BASE_PATH . APP_URI . '/users/index/' . $typeId . '?sort=field_id' . $realSearchString . '&field_id=' . substr($fieldValues[$name]['id'], strrpos($fieldValues[$name]['id'], '_') + 1) . '">' . ucwords(str_replace('_', ' ', $name)) . '</a>';
                     } else {
                         $uAry[$name] = '';
                     }
                 }
             }
         } else {
             $searchByAry = array('username' => 'Username', 'email' => 'Email');
             $uAry = array('id' => $userRows[$key]->id);
             if (!$userType->email_as_username) {
                 $uAry['username'] = $userRows[$key]->username;
             }
             $uAry['email'] = $userRows[$key]->email;
             $uAry['role'] = $userRows[$key]->role;
             $uAry['type'] = $userRows[$key]->type;
             $uAry['last_login'] = $userRows[$key]->login_count . ' &nbsp; <span title="' . $lastLogin . '">[ ' . $lastLoginShort . ' ]</span>';
         }
         if (null !== $edit) {
             $uAry['edit'] = $edit;
         }
         $userAry[] = $uAry;
     }
     if ($userType->email_as_username) {
         unset($options['table']['headers']['username']);
         unset($searchByAry['username']);
     }
     if (isset($userRows[0])) {
         $this->data['table'] = Html::encode($userAry, $options, $this->config->pagination_limit, $this->config->pagination_range, $rowCount);
     }
     $this->data['searchBy'] = new \Pop\Form\Element\Select('search_by', $searchByAry, $searchByMarked);
     $this->data['searchFor'] = $searchFor;
 }