CommunityID/modules/default/models/Histories.php

72 lines
2.0 KiB
PHP
Raw Normal View History

2019-07-17 20:08:50 +00:00
<?php
/*
* @copyright Copyright (C) 2005-2009 Keyboard Monkeys Ltd. http://www.kb-m.com
* @license http://creativecommons.org/licenses/BSD/ BSD License
* @author Keyboard Monkey Ltd
* @since CommunityID 0.9
* @package CommunityID
* @packager Keyboard Monkeys
*/
2019-07-17 20:16:19 +00:00
class Model_Histories extends Monkeys_Db_Table_Gateway
2019-07-17 20:08:50 +00:00
{
2019-07-17 20:16:19 +00:00
const DIR_ASC = 0;
const DIR_DESC = 1;
private $_sortFields = array(
'date' => array('date', 'site', 'ip', 'result'),
'site' => array('site', 'date', 'ip', 'result'),
'ip' => array('ip', 'date', 'site', 'result'),
'result' => array('result', 'date', 'site', 'ip'),
);
2019-07-17 20:08:50 +00:00
protected $_name = 'history';
protected $_primary = 'id';
2019-07-17 20:16:19 +00:00
protected $_rowClass = 'Model_History';
2019-07-17 20:08:50 +00:00
2019-07-17 20:16:19 +00:00
public function get(Users_Model_User $user, $startIndex = false, $results = false, $sort = false, $dir = false)
2019-07-17 20:08:50 +00:00
{
$select = $this->select()
->where('user_id=?', $user->id);
2019-07-17 20:16:19 +00:00
if ($sort && isset($this->_sortFields[$sort])) {
$dir = ($dir == self::DIR_ASC? 'ASC' : 'DESC');
$sortSql = array();
foreach ($this->_sortFields[$sort] as $field) {
$sortSql[] = "$field $dir";
}
$select = $select->order($sortSql);
}
2019-07-17 20:08:50 +00:00
if ($startIndex !== false && $results !== false) {
$select = $select->limit($results, $startIndex);
}
return $this->fetchAll($select);
}
2019-07-17 20:16:19 +00:00
public function getNumHistories(Users_Model_User $user)
2019-07-17 20:08:50 +00:00
{
2019-07-17 20:16:19 +00:00
$sites = $this->get($user);
2019-07-17 20:08:50 +00:00
return count($sites);
}
2019-07-17 20:16:19 +00:00
public function clear(Users_Model_User $user)
2019-07-17 20:08:50 +00:00
{
$where = $this->getAdapter()->quoteInto('user_id=?', $user->id);
$this->delete($where);
}
public function clearOldEntries()
{
$days = Zend_Registry::get('config')->environment->keep_history_days;
$where = $this->getAdapter()->quoteInto('date < ?', date('Y-m-d', time() - $days * 86400));
$this->delete($where);
}
}