Files
thetempusproject/app/plugins/initiativetracker/models/inithistory.php
2024-08-13 01:19:05 -04:00

106 lines
3.2 KiB
PHP

<?php
/**
* app/plugins/feedback/models/feedback.php
*
* This class is used for the manipulation of the feedback database table.
*
* @todo make this send a confirmation email
*
* @package TP Feedback
* @version 3.0
* @author Joey Kimsey <Joey@thetempusproject.com>
* @link https://TheTempusProject.com
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
*/
namespace TheTempusProject\Models;
use TheTempusProject\Bedrock\Classes\Config;
use TheTempusProject\Bedrock\Functions\Check;
use TheTempusProject\Canary\Bin\Canary as Debug;
use TheTempusProject\Classes\DatabaseModel;
use TheTempusProject\Plugins\Feedback as Plugin;
use TheTempusProject\TheTempusProject as App;
class Inithistory extends DatabaseModel {
public $tableName = 'initiative_history';
public $databaseMatrix = [
[ 'name', 'varchar', '128' ],
[ 'time', 'int', '10' ],
[ 'email', 'varchar', '128' ],
[ 'ip', 'varchar', '64' ],
[ 'feedback', 'text', '' ],
];
public $plugin;
/**
* The model constructor.
*/
public function __construct() {
parent::__construct();
$this->plugin = new Plugin;
}
/**
* Saves a feedback form to the db.
*
* @param string $name -the name on the form
* @param string $email -the email provided
* @param string $feedback -contents of the feedback form.
* @return bool
*/
public function create( $name, $email, $feedback ) {
if ( !$this->plugin->checkEnabled() ) {
Debug::info( 'Feedback is disabled in the config.' );
return false;
}
$fields = [
'name' => $name,
'email' => $email,
'feedback' => $feedback,
'time' => time(),
'ip' => $_SERVER['REMOTE_ADDR'],
];
if ( !self::$db->insert( $this->tableName, $fields ) ) {
Debug::info( 'Feedback::create - failed to insert to db' );
return false;
}
return self::$db->lastId();
}
/**
* Function to clear feedback from the DB.
*
* @todo is there a way i could check for success here I'm pretty sure this is just a bad idea?
* @return bool
*/
public function clear() {
if ( empty( self::$log ) ) {
self::$log = new Log;
}
self::$db->delete( $this->tableName, ['ID', '>=', '0'] );
self::$log->admin( 'Cleared Feedback' );
Debug::info( 'Feedback Cleared' );
return true;
}
public function userHistory( $limit = 0 ) {
$whereClause = [
'userID', '=', App::$activeUser->ID,
'AND',
'deletedAt', '=', '0',
'AND',
'expiresAt', '<', time(),
];
if ( empty( $limit ) ) {
$notifications = self::$db->get( $this->tableName, $whereClause );
} else {
$notifications = self::$db->get( $this->tableName, $whereClause, 'ID', 'DESC', [0, $limit] );
}
if ( !$notifications->count() ) {
Debug::info( 'No Notifications found.' );
return false;
}
return $this->filter( $notifications->results() );
}
}