From 4d8945716feb5f322347b139f4199da40e978bdf Mon Sep 17 00:00:00 2001 From: Lars Kneschke Date: Mon, 29 Dec 2014 08:34:27 +0100 Subject: [PATCH] implemented users count function in user backend MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Change-Id: I5a50a45c5daefcaf71b0475a437403054e53be97 Reviewed-on: http://gerrit.tine20.com/customers/1486 Tested-by: Jenkins CI (http://ci.tine20.com/) Reviewed-by: Philipp Schüle --- tine20/Admin/Controller/User.php | 3 +-- tine20/Tinebase/User/Sql.php | 48 +++++++++++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/tine20/Admin/Controller/User.php b/tine20/Admin/Controller/User.php index 739ee68..e4715e4 100644 --- a/tine20/Admin/Controller/User.php +++ b/tine20/Admin/Controller/User.php @@ -100,8 +100,7 @@ class Admin_Controller_User extends Tinebase_Controller_Abstract { $this->checkRight('VIEW_ACCOUNTS'); - $users = $this->_userBackend->getUsers($_filter, NULL, 'ASC', NULL, NULL, 'Tinebase_Model_FullUser'); - $result = count($users); + $result = $this->_userBackend->getUsersCount($_filter); return $result; } diff --git a/tine20/Tinebase/User/Sql.php b/tine20/Tinebase/User/Sql.php index e743d85..79f7a16 100644 --- a/tine20/Tinebase/User/Sql.php +++ b/tine20/Tinebase/User/Sql.php @@ -132,20 +132,20 @@ class Tinebase_User_Sql extends Tinebase_User_Abstract if ($_sort !== NULL && isset($this->rowNameMapping[$_sort])) { $select->order($this->_db->table_prefix . $this->_tableName . '.' . $this->rowNameMapping[$_sort] . ' ' . $_dir); } - + if (!empty($_filter)) { $whereStatement = array(); - $defaultValues = array( + $defaultValues = array( $this->rowNameMapping['accountLastName'], $this->rowNameMapping['accountFirstName'], $this->rowNameMapping['accountLoginName'] ); + // prepare for case insensitive search - $db = Tinebase_Core::getDb(); foreach ($defaultValues as $defaultValue) { - $whereStatement[] = Tinebase_Backend_Sql_Command::factory($db)->prepareForILike($this->_db->quoteIdentifier($defaultValue)) . ' LIKE ' . Tinebase_Backend_Sql_Command::factory($db)->prepareForILike('?'); + $whereStatement[] = $this->_dbCommand->prepareForILike($this->_db->quoteIdentifier($defaultValue)) . ' LIKE ' . $this->_dbCommand->prepareForILike('?'); } - + $select->where('(' . implode(' OR ', $whereStatement) . ')', '%' . $_filter . '%'); } @@ -154,16 +154,48 @@ class Tinebase_User_Sql extends Tinebase_User_Abstract if ($_accountClass == 'Tinebase_Model_User') { $select->where($this->_db->quoteInto($this->_db->quoteIdentifier('status') . ' = ?', 'enabled')); } - + $stmt = $select->query(); - $rows = $stmt->fetchAll(Zend_Db::FETCH_ASSOC); - + $result = new Tinebase_Record_RecordSet($_accountClass, $rows, TRUE); return $result; } + + /** + * get total count of users + * + * @param string $_filter + * @return int + */ + public function getUsersCount($_filter = null) + { + $select = $this->_db->select() + ->from(SQL_TABLE_PREFIX . 'accounts', array('count' => 'COUNT(' . $this->_db->quoteIdentifier('id') . ')')); + if (!empty($_filter)) { + $whereStatement = array(); + $defaultValues = array( + $this->rowNameMapping['accountLastName'], + $this->rowNameMapping['accountFirstName'], + $this->rowNameMapping['accountLoginName'] + ); + + // prepare for case insensitive search + foreach ($defaultValues as $defaultValue) { + $whereStatement[] = $this->_dbCommand->prepareForILike($this->_db->quoteIdentifier($defaultValue)) . ' LIKE ' . $this->_dbCommand->prepareForILike('?'); + } + + $select->where('(' . implode(' OR ', $whereStatement) . ')', '%' . $_filter . '%'); + } + + $stmt = $select->query(); + $rows = $stmt->fetchAll(Zend_Db::FETCH_COLUMN); + + return $rows[0]; + } + /** * get user by property * -- 2.7.4