Initial commit

This commit is contained in:
Joey Kimsey
2024-08-04 21:15:59 -04:00
parent c9d1fb983f
commit 0d469501ee
695 changed files with 70184 additions and 71 deletions

View File

@ -0,0 +1,165 @@
<?php
/**
* app/plugins/contacts/models/phonebook.php
*
* This class is used for the manipulation of the phonebook database table.
*
* @package TP Contacts
* @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\Canary as Debug;
use TheTempusProject\Classes\DatabaseModel;
use TheTempusProject\TheTempusProject as App;
use TheTempusProject\Houdini\Classes\Filters;
use TheTempusProject\Bedrock\Classes\CustomException;
class Phonebook extends DatabaseModel {
public $tableName = 'phonebooks';
public $databaseMatrix = [
[ 'title', 'varchar', '128' ],
[ 'description', 'text', '' ],
[ 'color', 'varchar', '48' ],
[ 'icon', 'varchar', '48' ],
[ 'createdBy', 'int', '11' ],
[ 'createdAt', 'int', '11' ],
];
/**
* The model constructor.
*/
public function __construct() {
parent::__construct();
}
public function create( $title, $description = '', $color = 'default', $icon = '' ) {
if ( ! Check::dataTitle( $title ) ) {
Debug::info( 'Phonebooks: illegal title.' );
return false;
}
$fields = [
'title' => $title,
'description' => $description,
'color' => $color,
'icon' => $icon,
'createdBy' => App::$activeUser->ID,
'createdAt' => time(),
];
if ( ! self::$db->insert( $this->tableName, $fields ) ) {
new CustomException( 'phonebookCreate' );
Debug::error( "Phonebooks: not created " . var_export($fields,true) );
return false;
}
return self::$db->lastId();
}
public function update( $id, $title, $description = '', $color = 'default', $icon = '' ) {
if ( !Check::id( $id ) ) {
Debug::info( 'Phonebooks: illegal ID.' );
return false;
}
if ( !Check::dataTitle( $title ) ) {
Debug::info( 'Phonebooks: illegal title.' );
return false;
}
$fields = [
'title' => $title,
'description' => $description,
'color' => $color,
'icon' => $icon,
];
if ( !self::$db->update( $this->tableName, $id, $fields ) ) {
new CustomException( 'phonebookUpdate' );
Debug::error( "Phonebooks: $id not updated" );
return false;
}
return true;
}
public function byUser( $limit = null ) {
$whereClause = ['createdBy', '=', App::$activeUser->ID];
if ( empty( $limit ) ) {
$phonebooks = self::$db->get( $this->tableName, $whereClause );
} else {
$phonebooks = self::$db->get( $this->tableName, $whereClause, 'ID', 'DESC', [0, $limit] );
}
if ( !$phonebooks->count() ) {
Debug::info( 'No Phonebooks found.' );
return false;
}
return $this->filter( $phonebooks->results() );
}
public function getName( $id ) {
$phonebook = self::findById( $id );
return $phonebook->title;
}
public function simpleList( $param = '') {
$whereClause = ['createdBy', '=', App::$activeUser->ID];
$phonebooks = self::$db->get( $this->tableName, $whereClause );
if ( !$phonebooks->count() ) {
Debug::warn( 'Could not find any Phonebooks' );
return [];
}
$phonebooks = $phonebooks->results();
$out = [ 'None' => '0'];
foreach ( $phonebooks as &$phonebook ) {
$out[ $phonebook->title ] = $phonebook->ID;
}
return $out;
}
public function simpleObjectByUser() {
$whereClause = ['createdBy', '=', App::$activeUser->ID];
$phonebooks = self::$db->get( $this->tableName, $whereClause );
if ( !$phonebooks->count() ) {
Debug::warn( 'Could not find any Phonebooks' );
return false;
}
$phonebooks = $phonebooks->results();
$out = [];
foreach ( $phonebooks as &$phonebook ) {
$obj = new \stdClass();
$obj->title = $phonebook->title;
$obj->ID = $phonebook->ID;
$out[] = $obj;
}
return $out;
}
public function getTree() {
$whereClause = ['createdBy', '=', App::$activeUser->ID];
$phonebooks = self::$db->get( $this->tableName, $whereClause );
if ( !$phonebooks->count() ) {
Debug::warn( 'Could not find any Phonebooks' );
return [];
}
$phonebooks = $phonebooks->results();
$formattedPhonebooks = [];
foreach ($phonebooks as $phonebook) {
if ( !empty($phonebook->phonebookID) ) {
$phonebookID = $phonebook->phonebookID;
if ( ! isset( $formattedPhonebooks[ $phonebookID ])) {
$formattedPhonebooks[ $phonebookID ][ 'phonebooks' ] = [];
}
$formattedPhonebooks[ $phonebookID ][ 'phonebooks' ][] = $phonebook;
} else {
$phonebookID = $phonebook->ID;
$formattedPhonebooks[ $phonebookID ][ 'phonebook' ] = $phonebook;
}
}
return $formattedPhonebooks;
}
}