Initial commit
This commit is contained in:
88
app/plugins/contacts/models/contact.php
Normal file
88
app/plugins/contacts/models/contact.php
Normal file
@ -0,0 +1,88 @@
|
||||
<?php
|
||||
/**
|
||||
* app/plugins/contacts/models/contact.php
|
||||
*
|
||||
* This class is used for the manipulation of the contacts 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\Canary\Canary as Debug;
|
||||
use TheTempusProject\Classes\DatabaseModel;
|
||||
use TheTempusProject\TheTempusProject as App;
|
||||
|
||||
class Contact extends DatabaseModel {
|
||||
public $tableName = 'contacts';
|
||||
|
||||
public $databaseMatrix = [
|
||||
[ 'avatar', 'text', ''],
|
||||
[ 'first_name', 'varchar', '128'],
|
||||
[ 'middle_name', 'varchar', '128'],
|
||||
[ 'last_name', 'varchar', '128'],
|
||||
[ 'nickname', 'varchar', '128'],
|
||||
[ 'company', 'varchar', '128'],
|
||||
[ 'job_title', 'varchar', '128'],
|
||||
[ 'email', 'varchar', '128'],
|
||||
[ 'email_2', 'varchar', '128'],
|
||||
[ 'phone', 'varchar', '128'],
|
||||
[ 'phone_2', 'varchar', '128'],
|
||||
[ 'address_1_primary', 'varchar', '128'],
|
||||
[ 'address_1_secondary', 'varchar', '128'],
|
||||
[ 'city', 'varchar', '128'],
|
||||
[ 'state', 'varchar', '128'],
|
||||
[ 'zipcode', 'varchar', '128'],
|
||||
[ 'country', 'varchar', '128'],
|
||||
[ 'address_2_primary', 'varchar', '128'],
|
||||
[ 'address_2_secondary', 'varchar', '128'],
|
||||
[ 'city_2', 'varchar', '128'],
|
||||
[ 'state_2', 'varchar', '128'],
|
||||
[ 'zipcode_2', 'varchar', '128'],
|
||||
[ 'country_2', 'varchar', '128'],
|
||||
[ 'notes', 'text', ''],
|
||||
[ 'color', 'varchar', '48' ],
|
||||
[ 'icon', 'varchar', '48' ],
|
||||
[ 'createdAt', 'int', '11'],
|
||||
[ 'createdBy', 'int', '11'],
|
||||
[ 'phonebookID', 'int', '11'],
|
||||
];
|
||||
|
||||
/**
|
||||
* The model constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
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 byPhonebook( $phonebookID, $limit = null ) {
|
||||
$whereClause = [ 'phonebookID', '=', $phonebookID ];
|
||||
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() );
|
||||
}
|
||||
}
|
165
app/plugins/contacts/models/phonebook.php
Normal file
165
app/plugins/contacts/models/phonebook.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user