UserModel::searchCount PHP Method

searchCount() public method

Count search results.
public searchCount ( array | string $Filter = '' ) : integer
$Filter array | string
return integer
    public function searchCount($Filter = '')
    {
        if (is_array($Filter)) {
            $Where = $Filter;
            $Keywords = $Where['Keywords'];
            unset($Where['Keywords'], $Where['Optimize']);
        } else {
            $Keywords = $Filter;
        }
        $Keywords = trim($Keywords);
        // Check to see if the search exactly matches a role name.
        $RoleID = false;
        if (strtolower($Keywords) == 'banned') {
            $this->SQL->where('u.Banned >', 0);
        } else {
            $RoleID = $this->SQL->getWhere('Role', ['Name' => $Keywords])->value('RoleID');
        }
        if (isset($Where)) {
            $this->SQL->where($Where);
        }
        $this->SQL->select('u.UserID', 'count', 'UserCount')->from('User u');
        // Check for an IP address.
        if (preg_match('`\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}`', $Keywords)) {
            $fields = ['LastIPAddress'];
            $this->addIpFilters($Keywords, $fields);
        } else {
            if ($RoleID) {
                $this->SQL->join('UserRole ur2', "u.UserID = ur2.UserID and ur2.RoleID = {$RoleID}");
            } else {
                // Search on the user table.
                $Like = trim($Keywords) == '' ? false : ['u.Name' => $Keywords, 'u.Email' => $Keywords];
                if (is_array($Like)) {
                    $this->SQL->orOp()->beginWhereGroup()->orLike($Like, '', 'right')->endWhereGroup();
                }
            }
        }
        $this->SQL->where('u.Deleted', 0);
        $Data = $this->SQL->get()->firstRow();
        return $Data === false ? 0 : $Data->UserCount;
    }

Usage Example

Example #1
0
 /**
  * User management list.
  *
  * @since 2.0.0
  * @access public
  * @param mixed $Keywords Term or array of terms to filter list of users.
  * @param int $Page Page number.
  * @param string $Order Sort order for list.
  */
 public function index($Keywords = '', $Page = '', $Order = '')
 {
     $this->permission(array('Garden.Users.Add', 'Garden.Users.Edit', 'Garden.Users.Delete'), '', false);
     // Page setup
     $this->addJsFile('jquery.gardenmorepager.js');
     $this->addJsFile('user.js');
     $this->title(t('Users'));
     $this->setHighlightRoute('dashboard/user');
     Gdn_Theme::section('Moderation');
     // Form setup
     $this->Form->Method = 'get';
     // Input Validation.
     list($Offset, $Limit) = offsetLimit($Page, PagerModule::$DefaultPageSize);
     if (!$Keywords) {
         $Keywords = $this->Form->getFormValue('Keywords');
         if ($Keywords) {
             $Offset = 0;
         }
     }
     if (!is_string($Keywords)) {
         $Keywords = '';
     }
     // Put the Keyword back in the form
     if ($Keywords) {
         $this->Form->setFormValue('Keywords', $Keywords);
     }
     $UserModel = new UserModel();
     list($Offset, $Limit) = offsetLimit($Page, 30);
     // Determine our data filters.
     $Filter = $this->_getFilter();
     if ($Filter) {
         $Filter['Keywords'] = $Keywords;
     } else {
         $Filter = array('Keywords' => (string) $Keywords);
     }
     $Filter['Optimize'] = Gdn::userModel()->pastUserThreshold();
     // Sorting
     if (in_array($Order, array('DateInserted', 'DateFirstVisit', 'DateLastActive'))) {
         $Order = 'u.' . $Order;
         $OrderDir = 'desc';
     } else {
         $Order = 'u.Name';
         $OrderDir = 'asc';
     }
     // Get user list
     $this->UserData = $UserModel->search($Filter, $Order, $OrderDir, $Limit, $Offset);
     $this->setData('Users', $this->UserData);
     // Figure out our number of results and users.
     $showUserCount = $this->UserData->count();
     if (!Gdn::userModel()->pastUserThreshold()) {
         // Pfft, query that sucker however you want.
         $this->setData('RecordCount', $UserModel->searchCount($Filter));
     } else {
         // We have a user search, so at least set enough data for the Next pager.
         if ($showUserCount) {
             $this->setData('_CurrentRecords', $showUserCount);
         } else {
             // No search was done. Just give the total users overall. First, zero-out our pager.
             $this->setData('_CurrentRecords', 0);
             if (!Gdn::userModel()->pastUserMegaThreshold()) {
                 // Restoring this semi-optimized counter is our compromise to let non-mega sites know their exact total users.
                 $this->setData('UserCount', $UserModel->getCount());
             } else {
                 // Dang, yo. Get a table status guess instead of really counting.
                 $this->setData('UserEstimate', Gdn::userModel()->countEstimate());
             }
         }
     }
     // Add roles to the user data.
     RoleModel::setUserRoles($this->UserData->result());
     // Deliver json data if necessary
     if ($this->_DeliveryType != DELIVERY_TYPE_ALL && $this->_DeliveryMethod == DELIVERY_METHOD_XHTML) {
         $this->setJson('LessRow', $this->Pager->toString('less'));
         $this->setJson('MoreRow', $this->Pager->toString('more'));
         $this->View = 'users';
     }
     $this->render();
 }
UserModel