TimeTrex Community Edition v16.2.0

This commit is contained in:
2022-12-13 07:10:06 +01:00
commit 472f000c1b
6810 changed files with 2636142 additions and 0 deletions

View File

@@ -0,0 +1,384 @@
<?php
/*********************************************************************************
*
* TimeTrex is a Workforce Management program developed by
* TimeTrex Software Inc. Copyright (C) 2003 - 2021 TimeTrex Software Inc.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License version 3 as published by
* the Free Software Foundation with the addition of the following permission
* added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
* WORK IN WHICH THE COPYRIGHT IS OWNED BY TIMETREX, TIMETREX DISCLAIMS THE
* WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
*
* You should have received a copy of the GNU Affero General Public License along
* with this program; if not, see http://www.gnu.org/licenses or write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*
*
* You can contact TimeTrex headquarters at Unit 22 - 2475 Dobbin Rd. Suite
* #292 West Kelowna, BC V4T 2E9, Canada or at email address info@timetrex.com.
*
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
*
* In accordance with Section 7(b) of the GNU Affero General Public License
* version 3, these Appropriate Legal Notices must retain the display of the
* "Powered by TimeTrex" logo. If the display of the logo is not reasonably
* feasible for technical reasons, the Appropriate Legal Notices must display
* the words "Powered by TimeTrex".
*
********************************************************************************/
/**
* @package Modules\Notification
*/
class Notification {
/**
* @param $data_array
* @param int $created_before_timestamp
* @return bool
*/
public static function sendNotification( $data_array, $created_before_timestamp = 0 ) {
if ( !isset( $data_array['user_id'] ) || $data_array['user_id'] == '' || $data_array['user_id'] == TTUUID::getZeroID() ) {
return false;
}
if ( !isset( $data_array['type_id'] ) || $data_array['type_id'] == '' ) {
return false;
}
if ( !isset( $data_array['object_id'] ) || $data_array['object_id'] == '' ) {
$data_array['object_id'] = TTUUID::getZeroID();
}
//If no title is specified, can't save the notification to the DB, so try and send it through as a background notification without saving.
if ( ( ( isset( $data_array['title_short'] ) && $data_array['title_short'] != '' ) || ( isset( $data_array['title_long'] ) && $data_array['title_long'] != '' ) ) ) {
$is_title = true;
} else {
$is_title = false;
}
$nf = TTnew( 'NotificationFactory' ); /** @var NotificationFactory $nf */
$nf->setUser( $data_array['user_id'] );
$nf->setType( $data_array['type_id'] );
//Check preferences before the duplication check in getRecentSystemNotificationByUserIdAndObjectAndCreatedBefore(),
// because if the user doesn't have the notification enabled to begin with, no point in checking if they have received one recently.
//
// Also if no title is specified (background notification) always allow that to be sent regardless of preferences.
// However now that we may need to send background notifications that get changed into local foreground notifications on iOS/Android devices (must get attention notificaitons)
// we made need to reconsider what constitutes a background notification.
if ( $is_title == false || $nf->isNotificationEnabledByUserPreference() == true ) {
if ( $created_before_timestamp !== 0 ) {
$ntlf = TTnew( 'NotificationListFactory' ); /** @var NotificationListFactory $ntlf */
$ntlf->getRecentSystemNotificationByUserIdAndObjectAndCreatedBefore( $data_array['user_id'], $data_array['object_id'], $created_before_timestamp );
if ( $ntlf->getRecordCount() > 0 ) {
Debug::Text( 'Notification has already been sent since: ' . TTDate::getDate('DATE+TIME', $created_before_timestamp ) .' not sending again...', __FILE__, __LINE__, __METHOD__, 10 );
return false;
}
}
if ( isset( $data_array['body_short'] ) && $data_array['body_short'] != '' ) {
$nf->setBodyShortText( $data_array['body_short'] );
}
if ( isset( $data_array['body_long'] ) && $data_array['body_long'] != '' ) {
$nf->setBodyLongText( $data_array['body_long'] );
}
if ( isset( $data_array['body_long_html'] ) && $data_array['body_long_html'] != '' ) {
$nf->setBodyLongHtml( $data_array['body_long_html'] );
}
if ( isset( $data_array['title_short'] ) && $data_array['title_short'] != '' ) {
$nf->setTitleShort( $data_array['title_short'] );
}
if ( isset( $data_array['title_long'] ) && $data_array['title_long'] != '' ) {
$nf->setTitleLong( $data_array['title_long'] );
}
if ( isset( $data_array['object_id'] ) && $data_array['object_id'] != '' ) {
$nf->setObject( $data_array['object_id'] );
}
if ( isset( $data_array['object_type_id'] ) && $data_array['object_type_id'] != '' ) {
$nf->setObjectType( $data_array['object_type_id'] );
}
if ( isset( $data_array['save_notification'] ) && $data_array['save_notification'] !== '' ) {
$nf->setEnableSaveNotification( $data_array['save_notification'] );
}
//If reminder delays are specified in user preferences, apply them here.
if ( is_object( $nf->getUserPreferenceNotificationObject() ) ) {
$reminder_delay = (int)$nf->getUserPreferenceNotificationObject()->getReminderDelay();
Debug::Text( ' Reminder Delay: '. $reminder_delay, __FILE__, __LINE__, __METHOD__, 10 );
} else {
$reminder_delay = 0;
}
if ( isset( $data_array['effective_date'] ) && $data_array['effective_date'] != '' ) {
$nf->setEffectiveDate( ( $data_array['effective_date'] + $reminder_delay ) );
} else {
$nf->setEffectiveDate( ( time() + $reminder_delay ) ); //Always set effective date, as that is the date of the notification rather than the created date.
}
if ( isset( $data_array['time_to_live'] ) && $data_array['time_to_live'] != '' ) {
$nf->setTimeToLive( $data_array['time_to_live'] );
}
$priority = 5;
if ( $nf->getType() != 'system' ) {
$priority = $nf->getUserPreferenceNotificationObject()->getPriority(); //If no user preferences exist, this could return false.
} else if ( isset( $data_array['priority_id'] ) && $data_array['priority_id'] != '' ) {
$priority = $data_array['priority_id'];
}
$nf->setPriority( ( ( !empty( $priority ) ) ? $priority : 5 ) );
if ( !isset( $data_array['payload'] ) ) {
$data_array['payload'] = [];
}
// Ensure TimeTrex payload is always set along with user_id.
$data_array['payload']['timetrex']['user_id'] = $data_array['user_id'];
// Set priority in the TimeTrex key so when receiving the push notification on the frontend we can react differently based on the priority.
$data_array['payload']['timetrex']['priority'] = $nf->getPriority();
// The notification is sent to the proxy during the preSave() of the NotificationFactory, because of that we manually set the notification ID ahead of time.
// The ID is required in the notification payload so that the notification can be marked as read once the push notification is clicked on and to create a link to the notification itself.
$nf->setId( $nf->getNextInsertId() );
$data_array['payload']['timetrex']['id'] = $nf->getId();
// Push notifications without a link, link to themselves.
// This means the payload requires the id of the notitication before the notification is saved and sent.
if ( !isset( $data_array['payload']['link'] ) || $data_array['payload']['link'] == '' ) {
$data_array['payload']['link'] = Misc::getURLProtocol() . '://' . Misc::getHostName() . Environment::getDefaultInterfaceBaseURL() . 'html5/#!m=Notification&a=view&id=' . $nf->getId() . '&tab=Notification';
}
$nf->setPayloadData( $data_array['payload'] );
if ( isset( $data_array['device_id'] ) ) {
//Only sending notification to specific devices and not the devices set by user preferences.
//This is useful in the case of multifactor authentication, where we want to send a notification to the mobile app.
$nf->setDeviceIds( $data_array['device_id'] );
}
//Notifications without titles are background notifications, and should not be saved.
//Specific notifications can also be set to not be saved such as MFA notifications that we do not want to show up in the notification list.
if ( $is_title == true && $nf->getEnableSaveNotification() == true ) {
if ( $nf->isValid() == true ) {
Debug::Text( 'Notification ID: ' . $nf->getId() . ' Effective Date: ' . TTDate::getDate( 'DATE+TIME', $nf->getEffectiveDate() ), __FILE__, __LINE__, __METHOD__, 10 );
$nf->save( true, true );
return true;
} else {
Debug::Text( 'WARNING: Notification failed validation!', __FILE__, __LINE__, __METHOD__, 10 );
}
} else {
Debug::Text( 'Not saving notification, sending it directly instead... Notification ID: ' . $nf->getId() . ' Effective Date: ' . TTDate::getDate( 'DATE+TIME', $nf->getEffectiveDate() ), __FILE__, __LINE__, __METHOD__, 10 );
$nf->sendNotification();
return true;
}
} else {
Debug::Text( 'Notification preferences not enabled, skipping notification... IsTitle: '. (int)$is_title, __FILE__, __LINE__, __METHOD__, 10 );
}
return false;
}
/**
* Finds the next weekday at 12PM noon to make the notification effective date.
* This is useful for maintenance jobs that run in the middle of the night or on weekends that send notifications, so users aren't getting woken up for things like "New Version" notices.
* @param $epoch
* @return false|int
*/
static function getNextDecentEffectiveDate( $epoch = null ) {
if ( empty( $epoch ) ) {
$epoch = time();
}
$next_week_day_epoch = TTDate::getNearestWeekDay( $epoch, 2 );
$retval = TTDate::getTimeLockedDate( strtotime( '12:00 PM', $next_week_day_epoch ), $next_week_day_epoch ); //12PM on the day.
if ( $retval < $epoch ) {
//Increment to the next weekday
$next_week_day_epoch = TTDate::incrementDate( $epoch, 1, 'day' );
$retval = TTDate::getTimeLockedDate( strtotime( '12:00 PM', $next_week_day_epoch ), $next_week_day_epoch ); //12PM on the day.
}
Debug::Text( ' Decent Epoch: '. TTDate::getDate('DATE+TIME', $retval ) .' Current Epoch: '. TTDate::getDate('DATE+TIME', $epoch ) .' Next Weekday: '. TTDate::getDate('DATE+TIME', $next_week_day_epoch ), __FILE__, __LINE__, __METHOD__, 10 );
return $retval;
}
/**
* @param $user_ids
* @param $type_id
* @param null $epoch
* @param null $object_id
* @return bool
*/
static function deletePendingNotifications( $type_id, $user_ids = null, $object_id = null, $epoch = null ) {
$nlf = TTnew( 'NotificationListFactory' ); /** @var NotificationListFactory $nlf */
if ( $user_ids !== null ) {
//If user ids are supplied, delete pending notifications by user id and optional parameters.
if ( !is_array( $user_ids ) ) {
$user_ids = [ $user_ids ];
}
$nlf->getPendingByUserIdsAndTypeIdAndObjectId( $user_ids, $type_id, $epoch, $object_id );
} else if ( $object_id !== null ) {
//If no user ids are supplied and object id is not null, delete all notications for that type and object id.
$nlf->getPendingByObjectIdAndTypeId( $object_id, $type_id, $epoch );
} else {
Debug::Text( ' No user id or object id provided. Cannot delete pending notifications.', __FILE__, __LINE__, __METHOD__, 10 );
return false;
}
if ( $nlf->getRecordCount() > 0 ) {
foreach( $nlf as $n_obj ) {
$n_obj->setDeleted( true );
if ( $n_obj->isValid() ) {
Debug::Text( ' Deleting pending notification: '. $n_obj->getId(), __FILE__, __LINE__, __METHOD__, 10 );
$n_obj->Save();
}
}
return true;
}
Debug::Text( ' No pending notifications to delete...', __FILE__, __LINE__, __METHOD__, 10 );
return false;
}
/**
* @param $minimum_permission_level
* @param $per_user_check_function
* @param $per_company_check_function
* @param $notification_data
* @param $minimum_time_between_duplicates
* @param null $company_id
*/
static function sendNotificationToAllUsers( $minimum_permission_level, $per_user_check_function, $per_company_check_function, $notification_data, $minimum_time_between_duplicates, $company_id = null, $append_email_footer = true ) {
$clf = TTnew('CompanyListFactory' ); /** @var CompanyListFactory $clf */
if ( !empty( $company_id ) ) {
$clf->getById( $company_id );
} else {
$clf->getByStatusID( [ 10, 20 ], null, [ 'a.id' => 'asc' ] ); //10=Active, 20=Hold -- Companies on hold should still get notifications, as they can be prime candidates for some notification.
}
//Save the original data so we can always revert back to it at the beginning of each loop.
$original_notification_data = $notification_data;
if ( $clf->getRecordCount() > 0 ) {
Debug::Text( 'Attempting to send notification to total companies: ' . $clf->getRecordCount() .' Type: '. $notification_data['type_id'] .' Object ID: '. $notification_data['object_id'], __FILE__, __LINE__, __METHOD__, 10 );
foreach ( $clf as $c_obj ) { /** @var CompanyFactory $c_obj */
if ( $per_company_check_function instanceof Closure ) {
$per_company_result = $per_company_check_function( $c_obj );
}
if ( ( is_bool( $per_company_check_function ) && $per_company_check_function == true ) || ( isset( $per_company_result ) && $per_company_result['status'] == true ) ) {
$company_notification_data = $original_notification_data; //Make sure we start with the fresh original notification data for each iteration to be absolutely sure we avoid cross contamination between companies.
// Merge in result of company closure if not empty return data.
if ( isset( $per_company_result ) && isset( $per_company_result['notification_data'] ) && !empty( $per_company_result['notification_data'] ) ) {
$company_notification_data = array_merge( $company_notification_data, $per_company_result['notification_data'] );
}
//If this causes too many problems with HTML emails, we could use a place holder like "#email_footer#" instead if needed?
//We need to replace $notification_data['body_long_html'] for each company, so copy the original value and replace it in the company loop below.
// *NOTE: We since we appending this, we have ot make sure we start with the original notification data for each iteration!
if ( $append_email_footer == true && isset( $company_notification_data['body_long_html'] ) && $company_notification_data['body_long_html'] != '' ) {
$company_notification_data['body_long_html'] .= '<pre>'. NotificationFactory::addEmailFooter( ( ( is_object( $c_obj ) ) ? $c_obj->getName() : null ) ) .'</pre>';
}
$created_before_timestamp = ( TTDate::getTime() - $minimum_time_between_duplicates ); // Check if users already received notification in last 14 days.
$ulf = TTnew( 'UserListFactory' ); /** @var UserListFactory $ulf */
$ulf->getByCompanyIDAndStatusIdAndMinimumPermissionLevelAndRecentNotification( $c_obj->getId(), 10, $minimum_permission_level, $company_notification_data['object_id'], $created_before_timestamp );
if ( $ulf->getRecordCount() > 0 ) {
Debug::Text( ' Attempting to send notification to total users: ' . $ulf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10 );
foreach ( $ulf as $u_obj ) { /** @var UserFactory $u_obj */
$notification_data = $company_notification_data; //Make sure we start with the fresh company specific notification data for each iteration to be absolutely sure we avoid cross contamination between users.
if ( $per_user_check_function instanceof Closure ) {
$per_user_result = $per_user_check_function( $u_obj, $c_obj );
}
if ( ( is_bool( $per_user_check_function ) && $per_user_check_function == true ) || ( isset( $per_user_result ) && $per_user_result['status'] == true ) ) {
// Merge in result of user closure if not empty return data.
if ( isset( $per_user_result ) && isset( $per_user_result['notification_data'] ) && !empty( $per_user_result['notification_data'] ) ) {
$notification_data = array_merge( $notification_data, $per_user_result['notification_data'] );
}
$notification_data['user_id'] = $u_obj->getId();
Notification::sendNotification( $notification_data );
} else {
Debug::Text( ' Per User Check failed for User: ' . $u_obj->getFullName() .'('. $u_obj->getId() .')', __FILE__, __LINE__, __METHOD__, 10 );
}
}
}
} else {
Debug::Text( ' Per Company Check failed for Company: ' . $c_obj->getName() .'('. $c_obj->getId() .')', __FILE__, __LINE__, __METHOD__, 10 );
}
}
}
}
/**
* @param $last_check_epoch
* @return bool
*/
static function syncNotifications( $last_check_epoch ) {
Debug::Text( 'Syncing notifications since: ' . TTDate::getDate('DATE+TIME', $last_check_epoch ), __FILE__, __LINE__, __METHOD__, 10 );
$ttsc = new TimeTrexSoapClient();
$retval = $ttsc->syncNotifications( $last_check_epoch );
if ( is_array( $retval ) ) {
Debug::Text( ' Notifications Received: ' . count( $retval ), __FILE__, __LINE__, __METHOD__, 10 );
foreach ( $retval as $sync_notification ) {
if ( is_string( $sync_notification['per_user_check_function'] ) ) {
try {
eval( '$per_user_check_function = ' . $sync_notification['per_user_check_function'] );
} catch ( ParseError $e ) {
//Error parsing closure. Set result to false so we do not send notifications to unintended users.
$per_user_check_function = false;
Debug::Text( 'Evalulation failed for $per_user_check_function. ' . $e->getMessage(), __FILE__, __LINE__, __METHOD__, 10 );
}
} else {
$per_user_check_function = $sync_notification['per_user_check_function'];
}
if ( is_string( $sync_notification['per_company_check_function'] ) ) {
try {
eval( '$per_company_check_function = ' . $sync_notification['per_company_check_function'] );
} catch ( ParseError $e ) {
//Error parsing closure. Set result to false so we do not send notifications to unintended users.
$per_company_check_function = false;
Debug::Text( 'Evalulation failed for $per_company_check_function. ' . $e->getMessage(), __FILE__, __LINE__, __METHOD__, 10 );
}
} else {
$per_company_check_function = $sync_notification['per_company_check_function'];
}
Notification::sendNotificationToAllUsers( $sync_notification['minimum_permission_level'], $per_user_check_function, $per_company_check_function, $sync_notification['notification_data'], $sync_notification['minimum_time_between_duplicates'] ); //Send to all companies as this as per company checks.
}
} else {
Debug::Text( ' No Notifications Received...', __FILE__, __LINE__, __METHOD__, 10 );
}
SystemSettingFactory::setSystemSetting( 'last_notification_sync_time', ( (int)$last_check_epoch > 0 ) ? $last_check_epoch : TTDate::getTime() );
return true;
}
}
?>

View File

@@ -0,0 +1,291 @@
<?php
/*********************************************************************************
*
* TimeTrex is a Workforce Management program developed by
* TimeTrex Software Inc. Copyright (C) 2003 - 2021 TimeTrex Software Inc.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License version 3 as published by
* the Free Software Foundation with the addition of the following permission
* added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
* WORK IN WHICH THE COPYRIGHT IS OWNED BY TIMETREX, TIMETREX DISCLAIMS THE
* WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
*
* You should have received a copy of the GNU Affero General Public License along
* with this program; if not, see http://www.gnu.org/licenses or write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*
*
* You can contact TimeTrex headquarters at Unit 22 - 2475 Dobbin Rd. Suite
* #292 West Kelowna, BC V4T 2E9, Canada or at email address info@timetrex.com.
*
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
*
* In accordance with Section 7(b) of the GNU Affero General Public License
* version 3, these Appropriate Legal Notices must retain the display of the
* "Powered by TimeTrex" logo. If the display of the logo is not reasonably
* feasible for technical reasons, the Appropriate Legal Notices must display
* the words "Powered by TimeTrex".
*
********************************************************************************/
/**
* @package Modules\Notification
*/
class NotificationDeviceTokenFactory extends Factory {
protected $table = 'device_token';
protected $pk_sequence_name = 'device_token_id_seq'; //PK Sequence name
public $user_obj = null;
/**
* @param $name
* @param null $parent
* @return array|null
*/
function _getFactoryOptions( $name, $parent = null ) {
$retval = null;
switch ( $name ) {
case 'platform':
$retval = [
100 => TTi18n::gettext( 'Web' ),
200 => TTi18n::gettext( 'iOS' ),
300 => TTi18n::gettext( 'Android' ),
];
break;
}
return $retval;
}
/**
* @param $data
* @return array
*/
function _getVariableToFunctionMap( $data ) {
$variable_function_map = [
'id' => 'ID',
'platform_id' => 'Platform',
'device_token' => 'DeviceToken',
'user_agent' => 'UserAgent',
'user_id' => 'User',
'deleted' => 'Deleted',
];
return $variable_function_map;
}
/**
* @return bool|string
*/
function getPlatform() {
return $this->getGenericDataValue( 'platform_id' );
}
/**
* @param $value
* @return bool
*/
function setPlatform( $value ) {
$value = (int)$value;
return $this->setGenericDataValue( 'platform_id', $value );
}
/**
* @return bool|string
*/
function getDeviceToken() {
return (string)$this->getGenericDataValue( 'device_token' );
}
/**
* @param $value
* @return bool
*/
function setDeviceToken( $value ) {
$value = trim( $value );
return $this->setGenericDataValue( 'device_token', $value );
}
/**
* @return bool|string
*/
function getUserAgent() {
return (string)$this->getGenericDataValue( 'user_agent' );
}
/**
* @param $value
* @return bool
*/
function setUserAgent( $value ) {
$value = trim( $value );
return $this->setGenericDataValue( 'user_agent', $value );
}
/**
* @return bool
*/
function getUserObject() {
return $this->getGenericObject( 'UserListFactory', $this->getUser(), 'user_obj' );
}
/**
* @return bool|mixed
*/
function getUser() {
return $this->getGenericDataValue( 'user_id' );
}
/**
* @param string $value UUID
* @return bool
*/
function setUser( $value ) {
$value = TTUUID::castUUID( $value );
return $this->setGenericDataValue( 'user_id', $value );
}
/**
* @param string $user_id UUID
* @param string $device_token
* @param string $id UUID
* @return bool
*/
function isUnique($user_id, $device_token, $id ) {
$ph = [
'id' => TTUUID::castUUID( $id ),
'user_id' => TTUUID::castUUID( $user_id ),
'device_token' => (string)$device_token
];
$query = 'select id from ' . $this->getTable() . ' where id != ? AND user_id = ? AND device_token = ? AND deleted = 0';
$id = $this->db->GetOne( $query, $ph );
if ( $id === false ) {
return true;
} else {
if ( $id == $this->getId() ) {
return true;
}
}
return false;
}
/**
* @param bool $ignore_warning
* @return bool
*/
function Validate( $ignore_warning = true ) {
//
// BELOW: Validation code moved from set*() functions.
//
if ( $this->getUser() != TTUUID::getZeroID() ) {
$ulf = TTnew( 'UserListFactory' ); /** @var UserListFactory $ulf */
$this->Validator->isResultSetWithRows( 'user',
$ulf->getByID( $this->getUser() ),
TTi18n::gettext( 'Invalid Employee' )
);
}
// Platform
$this->Validator->inArrayKey( 'platform',
$this->getPlatform(),
TTi18n::gettext( 'Incorrect Platform for device token.' ),
$this->getOptions( 'platform' )
);
if ( $this->isUnique($this->getUser(), $this->getDeviceToken(), $this->getID() ) == false ) {
$this->Validator->isTrue( 'device_id',
false,
TTi18n::gettext( 'Duplicate device token already exists' ) );
}
return true;
}
/**
* @param $data
* @return bool
*/
function setObjectFromArray( $data ) {
if ( is_array( $data ) ) {
$variable_function_map = $this->getVariableToFunctionMap();
foreach ( $variable_function_map as $key => $function ) {
if ( isset( $data[$key] ) ) {
$function = 'set' . $function;
switch ( $key ) {
default:
if ( method_exists( $this, $function ) ) {
$this->$function( $data[$key] );
}
break;
}
}
}
$this->setCreatedAndUpdatedColumns( $data );
return true;
}
return false;
}
/**
* @param null $include_columns
* @return array
* @noinspection PhpMissingBreakStatementInspection
*/
function getObjectAsArray( $include_columns = null ) {
$data = [];
$variable_function_map = $this->getVariableToFunctionMap();
if ( is_array( $variable_function_map ) ) {
foreach ( $variable_function_map as $variable => $function_stub ) {
if ( $include_columns == null || ( isset( $include_columns[$variable] ) && $include_columns[$variable] == true ) ) {
$function = 'get' . $function_stub;
switch ( $variable ) {
default:
if ( method_exists( $this, $function ) ) {
$data[$variable] = $this->$function();
}
break;
}
}
}
$this->getCreatedAndUpdatedColumns( $data, $include_columns );
}
return $data;
}
/**
* This is called for every record everytime, and doesn't help much because of that.
* This has to be enabled to properly log modifications though.
* @param $log_action
* @return bool
*/
function addLog( $log_action ) {
return TTLog::addEntry( $this->getId(), $log_action, TTi18n::getText( 'Notification Device Token' ) . ' - ' . TTi18n::getText( 'Type' ) . ': ' . Option::getByKey( $this->getPlatform(), $this->getOptions( 'platform' ) ), null, $this->getTable(), $this );
}
}

View File

@@ -0,0 +1,379 @@
<?php
/*********************************************************************************
*
* TimeTrex is a Workforce Management program developed by
* TimeTrex Software Inc. Copyright (C) 2003 - 2021 TimeTrex Software Inc.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License version 3 as published by
* the Free Software Foundation with the addition of the following permission
* added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
* WORK IN WHICH THE COPYRIGHT IS OWNED BY TIMETREX, TIMETREX DISCLAIMS THE
* WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
*
* You should have received a copy of the GNU Affero General Public License along
* with this program; if not, see http://www.gnu.org/licenses or write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*
*
* You can contact TimeTrex headquarters at Unit 22 - 2475 Dobbin Rd. Suite
* #292 West Kelowna, BC V4T 2E9, Canada or at email address info@timetrex.com.
*
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
*
* In accordance with Section 7(b) of the GNU Affero General Public License
* version 3, these Appropriate Legal Notices must retain the display of the
* "Powered by TimeTrex" logo. If the display of the logo is not reasonably
* feasible for technical reasons, the Appropriate Legal Notices must display
* the words "Powered by TimeTrex".
*
********************************************************************************/
/**
* @package Modules\Notification
*/
class NotificationDeviceTokenListFactory extends NotificationDeviceTokenFactory implements IteratorAggregate {
/**
* @param int $limit Limit the number of records returned
* @param int $page Page number of records to return for pagination
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return $this
*/
function getAll( $limit = null, $page = null, $where = null, $order = null ) {
$query = '
select *
from ' . $this->getTable() . '
WHERE deleted = 0';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order );
$this->rs = $this->ExecuteSQL( $query, null, $limit, $page );
return $this;
}
/**
* @param string $id UUID
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|NotificationDeviceTokenListFactory
*/
function getById( $id, $where = null, $order = null ) {
if ( $id == '' ) {
return false;
}
$ph = [
'id' => TTUUID::castUUID( $id ),
];
$query = '
select *
from ' . $this->getTable() . '
where id = ?
AND deleted = 0';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param string $id UUID
* @param string $company_id UUID
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|NotificationDeviceTokenListFactory
*/
function getByCompanyIdAndId( $company_id, $id, $where = null, $order = null ) {
if ( $id == '' ) {
return false;
}
if ( $company_id == '' ) {
return false;
}
$uf = new UserFactory();
$ph = [
'company_id' => TTUUID::castUUID( $company_id ),
'id' => TTUUID::castUUID( $id ),
];
$query = '
select a.*
from ' . $this->getTable() . ' as a,
' . $uf->getTable() . ' as b
where a.user_id = b.id
AND b.company_id = ?
AND a.id = ?
AND a.deleted = 0';
$query .= $this->getSortSQL( $order );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param string $company_id UUID
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|NotificationDeviceTokenListFactory
*/
function getByCompanyId( $company_id, $where = null, $order = null ) {
if ( $company_id == '' ) {
return false;
}
$uf = new UserFactory();
$ph = [
'company_id' => TTUUID::castUUID( $company_id ),
];
$query = '
select a.*
from ' . $this->getTable() . ' as a,
' . $uf->getTable() . ' as b
where a.user_id = b.id
AND b.company_id = ?
AND ( a.deleted = 0 AND b.deleted = 0 )';
$query .= $this->getSortSQL( $order );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param string $id UUID
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|NotificationDeviceTokenListFactory
*/
function getByUserId( $id, $where = null, $order = null ) {
if ( $id == '' ) {
return false;
}
$ph = [
'id' => TTUUID::castUUID( $id ),
];
$query = '
select *
from ' . $this->getTable() . '
where user_id = ?
AND deleted = 0';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param string $id UUID
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|NotificationDeviceTokenListFactory
*/
function getByUserIdAndDeviceToken( $id, $device_token, $where = null, $order = null ) {
if ( $id == '' ) {
return false;
}
$ph = [
'id' => TTUUID::castUUID( $id ),
'device_token' => (string)$device_token,
];
$query = '
select *
from ' . $this->getTable() . '
where user_id = ?
AND device_token = ?
AND deleted = 0';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param string $id UUID
* @param string $company_id UUID
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|NotificationDeviceTokenListFactory
*/
function getByCompanyIdAndUserID( $company_id, $id, $where = null, $order = null ) {
if ( $id == '' ) {
return false;
}
if ( $company_id == '' ) {
return false;
}
$uf = new UserFactory();
$ph = [
'id' => TTUUID::castUUID( $id ),
'company_id' => TTUUID::castUUID( $company_id ),
];
$query = '
select a.*
from ' . $this->getTable() . ' as a,
' . $uf->getTable() . ' as b
where a.user_id = b.id
AND a.user_id = ?
AND b.company_id = ?
AND (a.deleted = 0 AND b.deleted = 0)';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param $id
* @param $user_id
* @param null $where
* @param null $order
* @return $this|false
* @throws DBError
*/
function getByIDAndUserID( $id, $user_id, $where = null, $order = null ) {
if ( $id == '' ) {
return false;
}
if ( $user_id == '' ) {
return false;
}
$uf = new UserFactory();
$ph = [
'id' => TTUUID::castUUID( $id ),
'user_id' => TTUUID::castUUID( $user_id ),
];
$query = '
select a.*
from ' . $this->getTable() . ' as a,
' . $uf->getTable() . ' as b
where a.id = b.id
AND a.id = ?
AND b.id = ?
AND (a.deleted = 0 AND b.deleted = 0)';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param string $id UUID
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|NotificationDeviceTokenListFactory
*/
function getAPISearchByCompanyIdAndArrayCriteria( $company_id, $filter_data, $limit = null, $page = null, $where = null, $order = null ) {
if ( $company_id == '' ) {
return false;
}
if ( !is_array( $order ) ) {
//Use Filter Data ordering if its set.
if ( isset( $filter_data['sort_column'] ) && $filter_data['sort_order'] ) {
$order = [ Misc::trimSortPrefix( $filter_data['sort_column'] ) => $filter_data['sort_order'] ];
}
}
$additional_order_fields = [ 'type_id' ];
$sort_column_aliases = [
'platform' => 'platform_id',
];
$order = $this->getColumnsFromAliases( $order, $sort_column_aliases );
if ( $order == null ) {
$order = [ 'platform_id' => 'asc' ];
$strict = false;
} else {
$strict = true;
}
//Debug::Arr($order, 'Order Data:', __FILE__, __LINE__, __METHOD__, 10);
//Debug::Arr($filter_data, 'Filter Data:', __FILE__, __LINE__, __METHOD__, 10);
$uf = new UserFactory();
$ph = [
'company_id' => TTUUID::castUUID( $company_id ),
];
$query = '
select a.*,
y.first_name as created_by_first_name,
y.middle_name as created_by_middle_name,
y.last_name as created_by_last_name,
z.first_name as updated_by_first_name,
z.middle_name as updated_by_middle_name,
z.last_name as updated_by_last_name
from ' . $this->getTable() . ' as a
LEFT JOIN ' . $uf->getTable() . ' as b ON ( a.user_id = b.id AND b.deleted = 0 )
LEFT JOIN ' . $uf->getTable() . ' as y ON ( a.created_by = y.id AND y.deleted = 0 )
LEFT JOIN ' . $uf->getTable() . ' as z ON ( a.updated_by = z.id AND z.deleted = 0 )
where b.company_id = ?';
$query .= ( isset( $filter_data['user_id'] ) ) ? $this->getWhereClauseSQL( 'b.id', $filter_data['user_id'], 'uuid_list', $ph ) : null;
$query .= ( isset( $filter_data['platform_id'] ) ) ? $this->getWhereClauseSQL( 'a.platform_id', $filter_data['platform_id'], 'smallint', $ph ) : null;
$query .= ( isset( $filter_data['exclude_id'] ) ) ? $this->getWhereClauseSQL( 'a.id', $filter_data['exclude_id'], 'not_uuid_list', $ph ) : null;
$query .= ( isset( $filter_data['created_by'] ) ) ? $this->getWhereClauseSQL( [ 'a.created_by', 'y.first_name', 'y.last_name' ], $filter_data['created_by'], 'user_id_or_name', $ph ) : null;
$query .= ( isset( $filter_data['updated_by'] ) ) ? $this->getWhereClauseSQL( [ 'a.updated_by', 'z.first_name', 'z.last_name' ], $filter_data['updated_by'], 'user_id_or_name', $ph ) : null;
$query .= ' AND a.deleted = 0 ';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order, $strict, $additional_order_fields );
$this->rs = $this->ExecuteSQL( $query, $ph, $limit, $page );
return $this;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,792 @@
<?php
/*********************************************************************************
*
* TimeTrex is a Workforce Management program developed by
* TimeTrex Software Inc. Copyright (C) 2003 - 2021 TimeTrex Software Inc.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License version 3 as published by
* the Free Software Foundation with the addition of the following permission
* added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
* WORK IN WHICH THE COPYRIGHT IS OWNED BY TIMETREX, TIMETREX DISCLAIMS THE
* WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
*
* You should have received a copy of the GNU Affero General Public License along
* with this program; if not, see http://www.gnu.org/licenses or write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*
*
* You can contact TimeTrex headquarters at Unit 22 - 2475 Dobbin Rd. Suite
* #292 West Kelowna, BC V4T 2E9, Canada or at email address info@timetrex.com.
*
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
*
* In accordance with Section 7(b) of the GNU Affero General Public License
* version 3, these Appropriate Legal Notices must retain the display of the
* "Powered by TimeTrex" logo. If the display of the logo is not reasonably
* feasible for technical reasons, the Appropriate Legal Notices must display
* the words "Powered by TimeTrex".
*
********************************************************************************/
/**
* @package Modules\Notification
*/
class NotificationListFactory extends NotificationFactory implements IteratorAggregate {
/**
* @param int $limit Limit the number of records returned
* @param int $page Page number of records to return for pagination
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return $this
*/
function getAll( $limit = null, $page = null, $where = null, $order = null ) {
$query = '
select *
from ' . $this->getTable() . '
WHERE deleted = 0';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order );
$this->rs = $this->ExecuteSQL( $query, null, $limit, $page );
return $this;
}
/**
* @param string $id UUID
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|NotificationListFactory
*/
function getById( $id, $where = null, $order = null ) {
if ( $id == '' ) {
return false;
}
$ph = [
'id' => TTUUID::castUUID( $id ),
];
$query = '
select *
from ' . $this->getTable() . '
where id = ?
AND deleted = 0';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param string $id UUID
* @param string $user_id UUID
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|NotificationListFactory
*/
function getByIdAndUserId( $id, $user_id, $where = null, $order = null ) {
if ( $id == '' ) {
return false;
}
if ( $user_id == '' ) {
return false;
}
$ph = [
'user_id' => TTUUID::castUUID( $user_id ),
'id' => TTUUID::castUUID( $id ),
];
$query = '
select *
from ' . $this->getTable() . '
where user_id = ?
AND id = ?
';
$query .= $this->getSortSQL( $order );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param string $company_id UUID
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|NotificationListFactory
*/
function getByCompanyId( $company_id, $where = null, $order = null ) {
if ( $company_id == '' ) {
return false;
}
$uf = new UserFactory();
$ph = [
'company_id' => TTUUID::castUUID( $company_id ),
];
$query = '
select a.*
from ' . $this->getTable() . ' as a,
' . $uf->getTable() . ' as b
where a.user_id = b.id
AND b.company_id = ?
AND ( a.deleted = 0 AND b.deleted = 0 )';
$query .= $this->getSortSQL( $order );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param string|string[] $id UUID
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|NotificationListFactory
*/
function getByCompanyIdAndUserIdAndStatus( $company_id, $id, $status, $where = null, $order = null ) {
// 10 unread, 20 read
if ( $id == '' ) {
return false;
}
if ( $company_id == '' ) {
return false;
}
if ( $status == '' ) {
return false;
}
$uf = new UserFactory();
$ph = [
'id' => TTUUID::castUUID( $id ),
'company_id' => TTUUID::castUUID( $company_id ),
'status_id' => (int)$status,
];
$query = '
select a.*
from ' . $this->getTable() . ' as a,
' . $uf->getTable() . ' as b
where a.user_id = b.id
AND a.user_id = ?
AND b.company_id = ?
AND a.status_id = ?
AND (a.deleted = 0 AND b.deleted = 0)';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param string|string[] $id UUID
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|NotificationListFactory
*/
function getByCompanyIdAndUserIdAndSentStatus( $company_id, $id, $sent_status, $where = null, $order = null ) {
// 10=Pending, 50=Fail 100 = success
if ( $id == '' ) {
return false;
}
if ( $company_id == '' ) {
return false;
}
if ( $sent_status == '' ) {
return false;
}
$uf = new UserFactory();
$ph = [
'id' => TTUUID::castUUID( $id ),
'company_id' => TTUUID::castUUID( $company_id ),
'sent_status_id' => (int)$sent_status,
];
$query = '
select a.*
from ' . $this->getTable() . ' as a,
' . $uf->getTable() . ' as b
where a.user_id = b.id
AND a.user_id = ?
AND b.company_id = ?
AND a.sent_status_id = ?
AND (a.deleted = 0 AND b.deleted = 0)';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param string|string[] $id UUID
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|NotificationListFactory
*/
function getBySentStatus( $sent_status, $where = null, $order = null ) {
// 10=Pending, 50=Fail 100 = success
if ( $sent_status == '' ) {
return false;
}
$uf = new UserFactory();
$ph = [
'sent_status_id' => (int)$sent_status,
];
$query = '
select a.*
from ' . $this->getTable() . ' as a,
' . $uf->getTable() . ' as b
where a.user_id = b.id
AND a.sent_status_id = ?
AND (a.deleted = 0 AND b.deleted = 0)';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param string|string[] $id UUID
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|NotificationListFactory
*/
function getByCompanyIdAndUserIdAndType( $company_id, $id, $type, $where = null, $order = null ) {
if ( $id == '' ) {
return false;
}
if ( $company_id == '' ) {
return false;
}
if ( $type == '' ) {
return false;
}
$uf = new UserFactory();
$ph = [
'id' => TTUUID::castUUID( $id ),
'company_id' => TTUUID::castUUID( $company_id ),
'type_id' => (int)$type,
];
$query = '
select a.*
from ' . $this->getTable() . ' as a,
' . $uf->getTable() . ' as b
where a.user_id = b.id
AND a.user_id = ?
AND b.company_id = ?
AND a.type_id = ?
AND (a.deleted = 0 AND b.deleted = 0)';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param string|string[] $id UUID
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|NotificationListFactory
*/
function getByCompanyIdAndUserIdAndDate( $company_id, $id, $date = null, $where = null, $order = null ) {
if ( $company_id == '' ) {
return false;
}
if ( $id == '' ) {
return false;
}
if ( $date == '' ) {
$date = 0;
}
$uf = new UserFactory();
$ph = [
'id' => TTUUID::castUUID( $id ),
'company_id' => TTUUID::castUUID( $company_id )
];
$query = '
select a.*
from ' . $this->getTable() . ' as a,
' . $uf->getTable() . ' as b
where a.user_id = b.id
AND a.user_id = ?
AND b.company_id = ?
';
if ( isset( $date ) && $date > 0 ) {
//Append the same date twice for created and updated.
$ph[] = $date;
$ph[] = $date;
$query .= ' AND ( a.created_date >= ? OR a.updated_date >= ? )';
}
$query .= ' AND (a.deleted = 0 AND b.deleted = 0)';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
function getUnreadCountByUserIdAndCompanyId($user_id, $company_id, $where = null) {
if ( $user_id == '' ) {
return false;
}
if ( $company_id == '' ) {
return false;
}
$uf = new UserFactory();
$ph = [
'user_id' => TTUUID::castUUID( $user_id ),
'company_id' => TTUUID::castUUID( $company_id ),
'current_time' => $this->db->bindTimeStamp( TTDate::getTime() ),
];
//Be sure to ignore notifications in the future.
$query = '
select COUNT(*)
from ' . $this->getTable() . ' as a,
' . $uf->getTable() . ' as b
where a.user_id = b.id
AND a.status_id = 10
AND a.user_id = ?
AND b.company_id = ?
AND (a.effective_date <= ?)
AND (a.deleted = 0 AND b.deleted = 0)';
$query .= $this->getWhereSQL( $where );
$total = $this->db->GetOne( $query, $ph );
return $total;
}
/**
* @param string $id UUID
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|NotificationListFactory
*/
function getByUserId( $id, $where = null, $order = null ) {
if ( $id == '' ) {
return false;
}
$ph = [
'id' => TTUUID::castUUID( $id ),
];
$query = '
select *
from ' . $this->getTable() . '
where user_id = ?
AND deleted = 0';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param $company_id
* @return $this|false
*/
function getPending( $epoch = null ) {
if ( empty( $epoch ) ) {
$epoch = time();
}
$uf = new UserFactory();
$cf = new CompanyFactory();
$ph = [
'epoch' => $this->db->bindTimeStamp( (int)$epoch ),
];
//Make sure we only return pending notifications for active companies, and active users.
$query = '
SELECT a.*
FROM ' . $this->getTable() . ' as a
LEFT JOIN ' . $uf->getTable() . ' as b ON a.user_id = b.id
LEFT JOIN ' . $cf->getTable() . ' as c ON b.company_id = c.id
WHERE
a.sent_status_id = 10
AND a.status_id = 10
AND ( a.effective_date < ? )
AND b.status_id = 10
AND c.status_id = 10
AND a.deleted = 0
';
$this->rs = $this->ExecuteSQL( $query, $ph );
//Debug::Query( $query, $ph, __FILE__, __LINE__, __METHOD__, 10 );
return $this;
}
/**
* @param $user_ids
* @param $type_id
* @param null $epoch
* @param null $object_id
* @return $this|false
*/
function getPendingByUserIdsAndTypeIdAndObjectId( $user_ids, $type_id, $epoch = null, $object_id = null ) {
if ( $user_ids == '' ) {
return false;
}
if ( $type_id == '' ) {
return false;
}
$ph = [];
$query = '
SELECT a.*
FROM ' . $this->getTable() . ' as a
WHERE
a.user_id in (' . $this->getListSQL( $user_ids, $ph, 'uuid' ) . ')
AND a.type_id in (' . $this->getListSQL( $type_id, $ph, 'string' ) . ')
AND a.sent_status_id = 10
AND a.status_id = 10
';
$query .= ( isset( $object_id ) && TTUUID::isUUID( $object_id ) ) ? $this->getWhereClauseSQL( 'a.object_id', $object_id, 'uuid_list', $ph ) : null;
if ( isset( $epoch ) && !empty( $epoch ) && is_numeric( $epoch ) ) {
$query .= ' AND ( a.effective_date >= ' . $this->db->qstr( $this->db->bindTimeStamp( (int)$epoch ) ) . ' ) ';
}
$query .= ' AND a.deleted = 0';
$this->rs = $this->ExecuteSQL( $query, $ph );
//Debug::Query( $query, $ph, __FILE__, __LINE__, __METHOD__, 10 );
return $this;
}
/**
* @param $object_id
* @param $type_id
* @param null $epoch
* @return $this|false
*/
function getPendingByObjectIdAndTypeId( $object_id, $type_id, $epoch = null ) {
if ( $object_id == '' ) {
return false;
}
if ( $type_id == '' ) {
return false;
}
$ph = [];
$query = '
SELECT a.*
FROM ' . $this->getTable() . ' as a
WHERE
a.status_id = 10
AND a.sent_status_id = 10
AND a.type_id in (' . $this->getListSQL( $type_id, $ph, 'string' ) . ')
';
$query .= ( isset( $object_id ) && TTUUID::isUUID( $object_id ) ) ? $this->getWhereClauseSQL( 'a.object_id', $object_id, 'uuid_list', $ph ) : null;
if ( isset( $epoch ) && !empty( $epoch ) && is_numeric( $epoch ) ) {
$query .= ' AND ( a.effective_date >= ' . $this->db->qstr( $this->db->bindTimeStamp( (int)$epoch ) ) . ' ) ';
}
$query .= ' AND a.deleted = 0';
$this->rs = $this->ExecuteSQL( $query, $ph );
//Debug::Query( $query, $ph, __FILE__, __LINE__, __METHOD__, 10 );
return $this;
}
/**
* @param $user_id
* @param $object_id
* @param $created_before
* @return $this|false
*/
function getRecentSystemNotificationByUserIdAndObjectAndCreatedBefore( $user_id, $object_id, $created_before ) {
if ( $user_id == '' ) {
return false;
}
if ( $object_id == '' ) {
return false;
}
$ph = [
'user_id' => TTUUID::castUUID( $user_id ),
'object_id' => TTUUID::castUUID( $object_id ),
'created_before' => (int)$created_before,
];
//Do we ignore deleted notifications or not?
// If the user deletes one, then another one could appear immediately, but if they just mark it as read then it wouldn't.
$query = '
SELECT *
FROM ' . $this->getTable() . '
WHERE
user_id = ?
AND object_id = ?
AND created_date > ?
AND deleted = 0
';
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param string $company_id UUID
* @param string $user_id UUID
* @param int $status_id
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|NotificationListFactory
*/
function getByCompanyIdAndUserIdAndIdAndStatus( $company_id, $user_id, $id, $status_id, $where = null, $order = null ) {
if ( $company_id == '' ) {
return false;
}
if ( $user_id == '' ) {
return false;
}
if ( $id == '' ) {
return false;
}
if ( $status_id == '' ) {
return false;
}
$uf = new UserFactory();
$ph = [
'company_id' => TTUUID::castUUID( $company_id ),
'user_id' => TTUUID::castUUID( $user_id ),
'status_id' => (int)$status_id,
];
$query = '
SELECT a.*
FROM ' . $this->getTable() . ' as a
LEFT JOIN ' . $uf->getTable() . ' as b ON a.user_id = b.id
WHERE
b.company_id = ?
AND a.user_id = ?
AND a.status_id = ?
AND a.id in (' . $this->getListSQL( $id, $ph, 'uuid' ) . ')
AND a.deleted = 0
';
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param string $id UUID
* @param string $company_id UUID
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|NotificationListFactory
*/
function getByCompanyIdAndUserID( $company_id, $id, $where = null, $order = null ) {
if ( $id == '' ) {
return false;
}
if ( $company_id == '' ) {
return false;
}
$uf = new UserFactory();
$ph = [
'id' => TTUUID::castUUID( $id ),
'company_id' => TTUUID::castUUID( $company_id ),
];
$query = '
select a.*
from ' . $this->getTable() . ' as a,
' . $uf->getTable() . ' as b
where a.user_id = b.id
AND a.user_id = ?
AND b.company_id = ?
AND (a.deleted = 0 AND b.deleted = 0)';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order );
$this->rs = $this->ExecuteSQL( $query, $ph );
return $this;
}
/**
* @param string $id UUID
* @param array $where Additional SQL WHERE clause in format of array( $column => $filter, ... ). ie: array( 'id' => 1, ... )
* @param array $order Sort order passed to SQL in format of array( $column => 'asc', 'name' => 'desc', ... ). ie: array( 'id' => 'asc', 'name' => 'desc', ... )
* @return bool|NotificationListFactory
*/
function getAPISearchByCompanyIdAndArrayCriteria( $company_id, $filter_data, $limit = null, $page = null, $where = null, $order = null ) {
if ( $company_id == '' ) {
return false;
}
if ( !isset( $filter_data['current_user_id'] ) ) {
return false;
}
if ( !isset( $filter_data['effective_date'] ) ) {
$filter_data['effective_date'] = TTDate::getTime(); //Always hide notifications that are future dated.
}
if ( !is_array( $order ) ) {
//Use Filter Data ordering if its set.
if ( isset( $filter_data['sort_column'] ) && $filter_data['sort_order'] ) {
$order = [ Misc::trimSortPrefix( $filter_data['sort_column'] ) => $filter_data['sort_order'] ];
}
}
$additional_order_fields = [ 'type_id' ];
$sort_column_aliases = [
'notification_type' => 'a.type_id',
'type' => 'a.type_id',
'sent_status' => 'a.sent_status_id',
'status' => 'a.status_id',
'priority' => 'a.priority_id',
'acknowledged_status' => 'a.acknowledged_status_id',
'created_date' => 'a.created_date',
];
$order = $this->getColumnsFromAliases( $order, $sort_column_aliases );
if ( $order == null ) {
$order = [ 'status_id' => 'asc', 'a.effective_date' => 'desc' ];
$strict = false;
} else {
$strict = true;
}
//Debug::Arr($order, 'Order Data:', __FILE__, __LINE__, __METHOD__, 10);
//Debug::Arr($filter_data, 'Filter Data:', __FILE__, __LINE__, __METHOD__, 10);
$uf = new UserFactory();
$ph = [
'user_id' => $filter_data['current_user_id'],
'company_id' => TTUUID::castUUID( $company_id ),
];
$query = '
select a.*,
y.first_name as created_by_first_name,
y.middle_name as created_by_middle_name,
y.last_name as created_by_last_name,
z.first_name as updated_by_first_name,
z.middle_name as updated_by_middle_name,
z.last_name as updated_by_last_name
from ' . $this->getTable() . ' as a
LEFT JOIN ' . $uf->getTable() . ' as b ON ( a.user_id = b.id AND b.deleted = 0 )
LEFT JOIN ' . $uf->getTable() . ' as y ON ( a.created_by = y.id AND y.deleted = 0 )
LEFT JOIN ' . $uf->getTable() . ' as z ON ( a.updated_by = z.id AND z.deleted = 0 )
where a.user_id = ?
AND b.company_id = ?';
$query .= ( isset( $filter_data['user_id'] ) ) ? $this->getWhereClauseSQL( 'a.id', $filter_data['user_id'], 'uuid_list', $ph ) : null;
$query .= ( isset( $filter_data['id'] ) ) ? $this->getWhereClauseSQL( 'a.id', $filter_data['id'], 'uuid_list', $ph ) : null;
$query .= ( isset( $filter_data['exclude_id'] ) ) ? $this->getWhereClauseSQL( 'a.id', $filter_data['exclude_id'], 'not_uuid_list', $ph ) : null;
$query .= ( isset( $filter_data['object_type_id'] ) ) ? $this->getWhereClauseSQL( 'a.object_type_id', $filter_data['object_type_id'], 'numeric_list', $ph ) : null;
$query .= ( isset( $filter_data['object_id'] ) ) ? $this->getWhereClauseSQL( 'a.object_id', $filter_data['object_id'], 'uuid_list', $ph ) : null;
$query .= ( isset( $filter_data['title_short'] ) ) ? $this->getWhereClauseSQL( 'a.title_short', $filter_data['title_short'], 'text', $ph ) : null;
$query .= ( isset( $filter_data['status_id'] ) ) ? $this->getWhereClauseSQL( 'a.status_id', $filter_data['status_id'], 'numeric_list', $ph ) : null;
$query .= ( isset( $filter_data['sent_status_id'] ) ) ? $this->getWhereClauseSQL( 'a.sent_status_id', $filter_data['sent_status_id'], 'numeric_list', $ph ) : null;
$query .= ( isset( $filter_data['sent_device_id'] ) ) ? $this->getWhereClauseSQL( 'a.sent_device_id', $filter_data['sent_device_id'], 'numeric_list', $ph ) : null;
$query .= ( isset( $filter_data['type_id'] ) ) ? $this->getWhereClauseSQL( 'a.type_id', $filter_data['type_id'], 'text_list', $ph ) : null;
$query .= ( isset( $filter_data['acknowledged_status_id'] ) ) ? $this->getWhereClauseSQL( 'a.acknowledged_status_id', $filter_data['acknowledged_status_id'], 'numeric_list', $ph ) : null;
if ( isset( $filter_data['effective_date'] ) && !is_array( $filter_data['effective_date'] ) && trim( $filter_data['effective_date'] ) != '' ) {
$ph[] = $this->db->BindTimeStamp( (int)TTDate::parseDateTime( $filter_data['effective_date'] ) );
$query .= ' AND a.effective_date <= ?';
}
$query .= ( isset( $filter_data['created_by'] ) ) ? $this->getWhereClauseSQL( [ 'a.created_by', 'y.first_name', 'y.last_name' ], $filter_data['created_by'], 'user_id_or_name', $ph ) : null;
$query .= ( isset( $filter_data['updated_by'] ) ) ? $this->getWhereClauseSQL( [ 'a.updated_by', 'z.first_name', 'z.last_name' ], $filter_data['updated_by'], 'user_id_or_name', $ph ) : null;
$query .= ' AND a.deleted = 0 ';
$query .= $this->getWhereSQL( $where );
$query .= $this->getSortSQL( $order, $strict, $additional_order_fields );
$this->rs = $this->ExecuteSQL( $query, $ph, $limit, $page );
return $this;
}
}