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,402 @@
<?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 API\Users
*/
class APIBankAccount extends APIFactory {
protected $main_class = 'BankAccountFactory';
/**
* APIBankAccount constructor.
*/
public function __construct() {
parent::__construct(); //Make sure parent constructor is always called.
return true;
}
/**
* Get default bank account data for creating new bank accountes.
* @param string $user_id UUID
* @return array
*/
function getBankAccountDefaultData( $user_id = null ) {
$company_obj = $this->getCurrentCompanyObject();
Debug::Text( 'Getting wage default data...', __FILE__, __LINE__, __METHOD__, 10 );
//If user_id is passed, check for other wage entries, if none, default to the employees hire date.
$data = [
'company_id' => $company_obj->getId(),
];
return $this->returnHandler( $data );
}
/**
* Get bank account data for one or more bank accountes.
* @param array $data filter data
* @param bool $disable_paging
* @return array|bool
*/
function getBankAccount( $data = null, $disable_paging = false ) {
$data = $this->initializeFilterAndPager( $data, $disable_paging );
if ( !$this->getPermissionObject()->Check( 'user', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'user', 'edit_bank' ) || $this->getPermissionObject()->Check( 'user', 'edit_own_bank' ) || $this->getPermissionObject()->Check( 'user', 'edit_child_bank' ) ) ) {
return $this->getPermissionObject()->PermissionDenied();
}
//Get Permission Hierarchy Children first, as this can be used for viewing, or editing.
if ( $this->getPermissionObject()->Check( 'user', 'edit_bank' ) == true ) {
//Don't set permission_children_ids.
$data['filter_data']['permission_children_ids'] = null;
} else {
$data['filter_data']['permission_children_ids'] = []; //#2243 - Make sure permission_children_ids is overwritten entirely to avoid security issues of appending to an array partially passed by user and partially set here.
if ( $this->getPermissionObject()->Check( 'user', 'edit_child_bank' ) == true ) {
//Manually handle the permission checks here because edit_child_bank doesn't fit with getPermissionChildren() appending "_own" or "_child" on the end.
$data['filter_data']['permission_children_ids'] = $this->getPermissionObject()->getPermissionHierarchyChildren( $this->getCurrentCompanyObject()->getId(), $this->getCurrentUserObject()->getId() );
}
if ( $this->getPermissionObject()->Check( 'user', 'edit_own_bank' ) == true ) {
$data['filter_data']['permission_children_ids'][] = $this->getCurrentUserObject()->getId();
}
Debug::Arr( $data['filter_data']['permission_children_ids'], 'Permission Children: ', __FILE__, __LINE__, __METHOD__, 10 );
}
$blf = TTnew( 'BankAccountListFactory' ); /** @var BankAccountListFactory $blf */
$blf->getAPISearchByCompanyIdAndArrayCriteria( $this->getCurrentCompanyObject()->getId(), $data['filter_data'], $data['filter_items_per_page'], $data['filter_page'], null, $data['filter_sort'] );
Debug::Text( 'Record Count: ' . $blf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10 );
if ( $blf->getRecordCount() > 0 ) {
$this->getProgressBarObject()->start( $this->getAPIMessageID(), $blf->getRecordCount() );
$this->setPagerObject( $blf );
$retarr = [];
foreach ( $blf as $b_obj ) {
$retarr[] = $b_obj->getObjectAsArray( $data['filter_columns'], $data['filter_data']['permission_children_ids'] );
$this->getProgressBarObject()->set( $this->getAPIMessageID(), $blf->getCurrentRow() );
}
$this->getProgressBarObject()->stop( $this->getAPIMessageID() );
Debug::Arr( $retarr, 'Record Count: ' . $blf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10 );
return $this->returnHandler( $retarr );
}
return $this->returnHandler( true ); //No records returned.
}
/**
* Export data to csv
* @param string $format file format (csv)
* @param array $data filter data
* @param bool $disable_paging
* @return array
*/
function exportBankAccount( $format = 'csv', $data = null, $disable_paging = true ) {
$result = $this->stripReturnHandler( $this->getBankAccount( $data, $disable_paging ) );
return $this->exportRecords( $format, 'export_bank_account', $result, ( ( isset( $data['filter_columns'] ) ) ? $data['filter_columns'] : null ) );
}
/**
* Get only the fields that are common across all records in the search criteria. Used for Mass Editing of records.
* @param array $data filter data
* @return array
*/
function getCommonBankAccountData( $data ) {
return Misc::arrayIntersectByRow( $this->stripReturnHandler( $this->getBankAccount( $data, true ) ) );
}
/**
* Validate bank account information.
* @param array $data bank account data
* @return array
*/
function validateBankAccount( $data ) {
return $this->setBankAccount( $data, true );
}
/**
* Set bank account data for one or more bank accountes.
* @param array $data bank account data
* @param bool $validate_only
* @param bool $ignore_warning
* @return array|bool
*/
function setBankAccount( $data, $validate_only = false, $ignore_warning = true ) {
$validate_only = (bool)$validate_only;
$ignore_warning = (bool)$ignore_warning;
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
if ( !$this->getPermissionObject()->Check( 'user', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'user', 'edit_bank' ) || $this->getPermissionObject()->Check( 'user', 'edit_own_bank' ) || $this->getPermissionObject()->Check( 'user', 'edit_child_bank' ) ) ) {
return $this->getPermissionObject()->PermissionDenied();
}
if ( $validate_only == true ) {
Debug::Text( 'Validating Only!', __FILE__, __LINE__, __METHOD__, 10 );
$permission_children_ids = false;
} else {
//Get Permission Hierarchy Children first, as this can be used for viewing, or editing.
$permission_children_ids = $this->getPermissionChildren();
}
[ $data, $total_records ] = $this->convertToMultipleRecords( $data );
Debug::Text( 'Received data for: ' . $total_records . ' BankAccounts', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$validator_stats = [ 'total_records' => $total_records, 'valid_records' => 0 ];
$validator = $save_result = []; $key = false;
if ( is_array( $data ) && $total_records > 0 ) {
$this->getProgressBarObject()->start( $this->getAPIMessageID(), $total_records );
foreach ( $data as $key => $row ) {
$primary_validator = new Validator();
$lf = TTnew( 'BankAccountListFactory' ); /** @var BankAccountListFactory $lf */
$lf->StartTransaction();
if ( isset( $row['id'] ) && $row['id'] != '' ) {
//Modifying existing object.
//Get wage object, so we can only modify just changed data for specific records if needed.
$lf->getByIdAndCompanyId( $row['id'], $this->getCurrentCompanyObject()->getId() );
if ( $lf->getRecordCount() == 1 ) {
//Object exists, check edit permissions
if (
$validate_only == true
||
(
$this->getPermissionObject()->Check( 'user', 'edit_bank' )
|| ( $this->getPermissionObject()->Check( 'user', 'edit_own_bank' ) && $this->getPermissionObject()->isOwner( $lf->getCurrent()->getCreatedBy(), $lf->getCurrent()->getUser() ) === true )
|| ( $this->getPermissionObject()->Check( 'user', 'edit_child_bank' ) && $this->getPermissionObject()->isChild( $lf->getCurrent()->getUser(), $permission_children_ids ) === true )
) ) {
Debug::Text( 'Row Exists, getting current data for ID: ' . $row['id'], __FILE__, __LINE__, __METHOD__, 10 );
$lf = $lf->getCurrent();
$row = array_merge( $lf->getObjectAsArray(), $row );
} else {
$primary_validator->isTrue( 'permission', false, TTi18n::gettext( 'Edit permission denied' ) );
}
} else {
//Object doesn't exist.
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Edit permission denied, record does not exist' ) );
}
} //else {
//Adding new object, check ADD permissions.
//$primary_validator->isTrue( 'permission', $this->getPermissionObject()->Check('user', 'add'), TTi18n::gettext('Add permission denied') );
//}
//Debug::Arr($row, 'Data: ', __FILE__, __LINE__, __METHOD__, 10);
$is_valid = $primary_validator->isValid();
if ( $is_valid == true ) { //Check to see if all permission checks passed before trying to save data.
Debug::Text( 'Setting object data...', __FILE__, __LINE__, __METHOD__, 10 );
if ( ( $row['user_id'] != '' && $row['user_id'] == $this->getCurrentUserObject()->getId() ) && $row['company_id'] == '' && $this->getPermissionObject()->Check( 'user', 'edit_own_bank' ) ) {
Debug::Text( 'Current User/Company', __FILE__, __LINE__, __METHOD__, 10 );
//Current user
$row['company_id'] = $this->getCurrentCompanyObject()->getId();
$row['user_id'] = $this->getCurrentUserObject()->getId();
} else if ( $row['user_id'] != '' && ( $row['company_id'] == '' || $row['company_id'] == $this->getCurrentCompanyObject()->getId() ) && $this->getPermissionObject()->Check( 'user', 'edit_child_bank' ) ) {
Debug::Text( 'Specified Child User', __FILE__, __LINE__, __METHOD__, 10 );
//Specified User
$row['company_id'] = $this->getCurrentCompanyObject()->getId();
} else if ( $row['user_id'] != '' && ( $row['company_id'] == '' || $row['company_id'] == $this->getCurrentCompanyObject()->getId() ) && $this->getPermissionObject()->Check( 'user', 'edit_bank' ) ) {
Debug::Text( 'Specified User', __FILE__, __LINE__, __METHOD__, 10 );
//Specified User
$row['company_id'] = $this->getCurrentCompanyObject()->getId();
} else if ( $row['company_id'] != '' && $row['user_id'] == '' && $this->getPermissionObject()->Check( 'company', 'edit_own_bank' ) ) {
Debug::Text( 'Specified Company', __FILE__, __LINE__, __METHOD__, 10 );
//Company bank.
$row['company_id'] = $this->getCurrentCompanyObject()->getId();
} else {
Debug::Text( 'No Company or User ID specified...', __FILE__, __LINE__, __METHOD__, 10 );
//Assume its always the currently logged in users account.
$row['company_id'] = $this->getCurrentCompanyObject()->getId();
if ( !isset( $row['user_id'] ) || $this->getPermissionObject()->Check( 'company', 'edit_bank' ) == false ) {
$row['user_id'] = $this->getCurrentUserObject()->getId();
}
}
$lf->setObjectFromArray( $row );
$is_valid = $lf->isValid( $ignore_warning );
if ( $is_valid == true ) {
Debug::Text( 'Saving data...', __FILE__, __LINE__, __METHOD__, 10 );
if ( $validate_only == true ) {
$save_result[$key] = true;
} else {
$save_result[$key] = $lf->Save();
}
$validator_stats['valid_records']++;
}
}
if ( $is_valid == false ) {
Debug::Text( 'Data is Invalid...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->FailTransaction(); //Just rollback this single record, continue on to the rest.
$validator[$key] = $this->setValidationArray( [ $primary_validator, $lf ] );
} else if ( $validate_only == true ) {
$lf->FailTransaction();
}
$lf->CommitTransaction();
$this->getProgressBarObject()->set( $this->getAPIMessageID(), $key );
}
$this->getProgressBarObject()->stop( $this->getAPIMessageID() );
return $this->handleRecordValidationResults( $validator, $validator_stats, $key, $save_result );
}
return $this->returnHandler( false );
}
/**
* Delete one or more bank accounts.
* @param array $data bank account data
* @return array|bool
*/
function deleteBankAccount( $data ) {
if ( !is_array( $data ) ) {
$data = [ $data ];
}
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
if ( !$this->getPermissionObject()->Check( 'user', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'user', 'edit_bank' ) || $this->getPermissionObject()->Check( 'user', 'edit_own_bank' ) || $this->getPermissionObject()->Check( 'user', 'edit_child_bank' ) ) ) {
return $this->getPermissionObject()->PermissionDenied();
}
//Get Permission Hierarchy Children first, as this can be used for viewing, or editing.
$permission_children_ids = $this->getPermissionChildren();
Debug::Text( 'Received data for: ' . count( $data ) . ' BankAccounts', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$total_records = count( $data );
$validator = $save_result = []; $key = false;
$validator_stats = [ 'total_records' => $total_records, 'valid_records' => 0 ];
if ( is_array( $data ) && $total_records > 0 ) {
$this->getProgressBarObject()->start( $this->getAPIMessageID(), $total_records );
foreach ( $data as $key => $id ) {
$primary_validator = new Validator();
$lf = TTnew( 'BankAccountListFactory' ); /** @var BankAccountListFactory $lf */
$lf->StartTransaction();
if ( $id != '' ) {
//Modifying existing object.
//Get bank account object, so we can only modify just changed data for specific records if needed.
$lf->getByIdAndCompanyId( $id, $this->getCurrentCompanyObject()->getId() );
if ( $lf->getRecordCount() == 1 ) {
//Object exists, check edit permissions
if ( $this->getPermissionObject()->Check( 'user', 'edit_bank' )
|| ( $this->getPermissionObject()->Check( 'user', 'edit_own_bank' ) && $this->getPermissionObject()->isOwner( $lf->getCurrent()->getCreatedBy(), $lf->getCurrent()->getUser() ) === true )
|| ( $this->getPermissionObject()->Check( 'user', 'edit_child_bank' ) && $this->getPermissionObject()->isChild( $lf->getCurrent()->getId(), $permission_children_ids ) === true )
) {
Debug::Text( 'Record Exists, deleting record ID: ' . $id, __FILE__, __LINE__, __METHOD__, 10 );
$lf = $lf->getCurrent();
} else {
$primary_validator->isTrue( 'permission', false, TTi18n::gettext( 'Delete permission denied' ) );
}
} else {
//Object doesn't exist.
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Delete permission denied, record does not exist' ) );
}
} else {
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Delete permission denied, record does not exist' ) );
}
//Debug::Arr($lf, 'AData: ', __FILE__, __LINE__, __METHOD__, 10);
$is_valid = $primary_validator->isValid();
if ( $is_valid == true ) { //Check to see if all permission checks passed before trying to save data.
Debug::Text( 'Attempting to delete record...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->setDeleted( true );
$is_valid = $lf->isValid();
if ( $is_valid == true ) {
Debug::Text( 'Record Deleted...', __FILE__, __LINE__, __METHOD__, 10 );
$save_result[$key] = $lf->Save();
$validator_stats['valid_records']++;
}
}
if ( $is_valid == false ) {
Debug::Text( 'Data is Invalid...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->FailTransaction(); //Just rollback this single record, continue on to the rest.
$validator[$key] = $this->setValidationArray( [ $primary_validator, $lf ] );
}
$lf->CommitTransaction();
$this->getProgressBarObject()->set( $this->getAPIMessageID(), $key );
}
$this->getProgressBarObject()->stop( $this->getAPIMessageID() );
return $this->handleRecordValidationResults( $validator, $validator_stats, $key, $save_result );
}
return $this->returnHandler( false );
}
}
?>

View File

@@ -0,0 +1,378 @@
<?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 API\Users
*/
class APIEthnicGroup extends APIFactory {
protected $main_class = 'EthnicGroupFactory';
/**
* APIEthnicGroup constructor.
*/
public function __construct() {
parent::__construct(); //Make sure parent constructor is always called.
return true;
}
/**
* Get options for dropdown boxes.
* @param bool|string $name Name of options to return, ie: 'columns', 'type', 'status'
* @param mixed $parent Parent name/ID of options to return if data is in hierarchical format. (ie: Province)
* @return bool|array
*/
function getOptions( $name = false, $parent = null ) {
if ( $name == 'columns'
&& ( !$this->getPermissionObject()->Check( 'ethnic_group', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'ethnic_group', 'view' ) || $this->getPermissionObject()->Check( 'ethnic_group', 'view_own' ) || $this->getPermissionObject()->Check( 'ethnic_group', 'view_child' ) ) ) ) {
$name = 'list_columns';
}
return parent::getOptions( $name, $parent );
}
/**
* Get default ethnic group data for creating new ethnic groups.
* @return array
*/
function getEthnicGroupDefaultData() {
$company_obj = $this->getCurrentCompanyObject();
Debug::Text( 'Getting ethnic group default data...', __FILE__, __LINE__, __METHOD__, 10 );
$data = [
'company_id' => $company_obj->getId(),
];
return $this->returnHandler( $data );
}
/**
* Get ethnic group data for one or more.
* @param array $data filter data
* @param bool $disable_paging
* @return array
*/
function getEthnicGroup( $data = null, $disable_paging = false ) {
$data = $this->initializeFilterAndPager( $data, $disable_paging );
if ( !$this->getPermissionObject()->Check( 'ethnic_group', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'ethnic_group', 'view' ) || $this->getPermissionObject()->Check( 'ethnic_group', 'view_own' ) || $this->getPermissionObject()->Check( 'ethnic_group', 'view_child' ) ) ) {
//return $this->getPermissionObject()->PermissionDenied();
$data['filter_columns'] = $this->handlePermissionFilterColumns( ( isset( $data['filter_columns'] ) ) ? $data['filter_columns'] : null, Misc::trimSortPrefix( $this->getOptions( 'list_columns' ) ) );
}
//Allow supervisor (subordinates only) to see all ethnic groups.
//$data['filter_data']['permission_children_ids'] = $this->getPermissionObject()->getPermissionChildren( 'user', 'view' );
$eglf = TTnew( 'EthnicGroupListFactory' ); /** @var EthnicGroupListFactory $eglf */
$eglf->getAPISearchByCompanyIdAndArrayCriteria( $this->getCurrentCompanyObject()->getId(), $data['filter_data'], $data['filter_items_per_page'], $data['filter_page'], null, $data['filter_sort'] );
Debug::Text( 'Record Count: ' . $eglf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10 );
if ( $eglf->getRecordCount() > 0 ) {
$this->setPagerObject( $eglf );
$retarr = [];
foreach ( $eglf as $eg_obj ) {
$retarr[] = $eg_obj->getObjectAsArray( $data['filter_columns'] );
}
return $this->returnHandler( $retarr );
}
return $this->returnHandler( true ); //No records returned.
}
/**
* @param string $format
* @param array $data
* @param bool $disable_paging
* @return array|bool
*/
function exportEthnicGroup( $format = 'csv', $data = null, $disable_paging = true ) {
$result = $this->stripReturnHandler( $this->getEthnicGroup( $data, $disable_paging ) );
return $this->exportRecords( $format, 'export_ethnic_group', $result, ( ( isset( $data['filter_columns'] ) ) ? $data['filter_columns'] : null ) );
}
/**
* Get only the fields that are common across all records in the search criteria. Used for Mass Editing of records.
* @param array $data filter data
* @return array
*/
function getCommonEthnicGroupData( $data ) {
return Misc::arrayIntersectByRow( $this->stripReturnHandler( $this->getEthnicGroup( $data, true ) ) );
}
/**
* Validate ethnic group data for one or more.
* @param array $data ethnic group data
* @return array
*/
function validateEthnicGroup( $data ) {
return $this->setEthnicGroup( $data, true );
}
/**
* Set ethnic group data for one or more.
* @param array $data ethnic group data
* @param bool $validate_only
* @param bool $ignore_warning
* @return array|bool
*/
function setEthnicGroup( $data, $validate_only = false, $ignore_warning = true ) {
$validate_only = (bool)$validate_only;
$ignore_warning = (bool)$ignore_warning;
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
if ( !$this->getPermissionObject()->Check( 'ethnic_group', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'ethnic_group', 'edit' ) || $this->getPermissionObject()->Check( 'ethnic_group', 'edit_own' ) || $this->getPermissionObject()->Check( 'ethnic_group', 'edit_child' ) || $this->getPermissionObject()->Check( 'ethnic_group', 'add' ) ) ) {
return $this->getPermissionObject()->PermissionDenied();
}
if ( $validate_only == true ) {
Debug::Text( 'Validating Only!', __FILE__, __LINE__, __METHOD__, 10 );
}
[ $data, $total_records ] = $this->convertToMultipleRecords( $data );
Debug::Text( 'Received data for: ' . $total_records . ' Ethnic Groups', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$validator_stats = [ 'total_records' => $total_records, 'valid_records' => 0 ];
$validator = $save_result = []; $key = false;
if ( is_array( $data ) && $total_records > 0 ) {
foreach ( $data as $key => $row ) {
$primary_validator = new Validator();
$lf = TTnew( 'EthnicGroupListFactory' ); /** @var EthnicGroupListFactory $lf */
$lf->StartTransaction();
if ( isset( $row['id'] ) && $row['id'] != '' ) {
//Modifying existing object.
$lf->getByIdAndCompanyId( $row['id'], $this->getCurrentCompanyObject()->getId() );
if ( $lf->getRecordCount() == 1 ) {
//Object exists, check edit permissions
if (
$validate_only == true
||
(
$this->getPermissionObject()->Check( 'ethnic_group', 'edit' )
|| ( $this->getPermissionObject()->Check( 'ethnic_group', 'edit_own' ) && $this->getPermissionObject()->isOwner( $lf->getCurrent()->getCreatedBy(), $lf->getCurrent()->getID() ) === true )
) ) {
Debug::Text( 'Row Exists, getting current data for ID: ' . $row['id'], __FILE__, __LINE__, __METHOD__, 10 );
$lf = $lf->getCurrent();
$row = array_merge( $lf->getObjectAsArray(), $row );
} else {
$primary_validator->isTrue( 'permission', false, TTi18n::gettext( 'Edit permission denied' ) );
}
} else {
//Object doesn't exist.
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Edit permission denied, record does not exist' ) );
}
} else {
//Adding new object, check ADD permissions.
$primary_validator->isTrue( 'permission', $this->getPermissionObject()->Check( 'ethnic_group', 'add' ), TTi18n::gettext( 'Add permission denied' ) );
}
Debug::Arr( $row, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$is_valid = $primary_validator->isValid();
if ( $is_valid == true ) { //Check to see if all permission checks passed before trying to save data.
Debug::Text( 'Setting object data...', __FILE__, __LINE__, __METHOD__, 10 );
//Force Company ID to current company.
$row['company_id'] = $this->getCurrentCompanyObject()->getId();
$lf->setObjectFromArray( $row );
$is_valid = $lf->isValid( $ignore_warning );
if ( $is_valid == true ) {
Debug::Text( 'Saving data...', __FILE__, __LINE__, __METHOD__, 10 );
if ( $validate_only == true ) {
$save_result[$key] = true;
} else {
$save_result[$key] = $lf->Save();
}
$validator_stats['valid_records']++;
}
}
if ( $is_valid == false ) {
Debug::Text( 'Data is Invalid...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->FailTransaction(); //Just rollback this single record, continue on to the rest.
$validator[$key] = $this->setValidationArray( [ $primary_validator, $lf ] );
} else if ( $validate_only == true ) {
$lf->FailTransaction();
}
$lf->CommitTransaction();
}
return $this->handleRecordValidationResults( $validator, $validator_stats, $key, $save_result );
}
return $this->returnHandler( false );
}
/**
* Delete one or more.
* @param array $data ethnic group data
* @return array|bool
*/
function deleteEthnicGroup( $data ) {
if ( !is_array( $data ) ) {
$data = [ $data ];
}
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
if ( !$this->getPermissionObject()->Check( 'ethnic_group', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'ethnic_group', 'delete' ) || $this->getPermissionObject()->Check( 'ethnic_group', 'delete_own' ) || $this->getPermissionObject()->Check( 'ethnic_group', 'delete_child' ) ) ) {
return $this->getPermissionObject()->PermissionDenied();
}
Debug::Text( 'Received data for: ' . count( $data ) . ' Ethnic Groups', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$total_records = count( $data );
$validator = $save_result = []; $key = false;
$validator_stats = [ 'total_records' => $total_records, 'valid_records' => 0 ];
if ( is_array( $data ) && $total_records > 0 ) {
foreach ( $data as $key => $id ) {
$primary_validator = new Validator();
$lf = TTnew( 'EthnicGroupListFactory' ); /** @var EthnicGroupListFactory $lf */
$lf->StartTransaction();
if ( $id != '' ) {
//Modifying existing object.
$lf->getByIdAndCompanyId( $id, $this->getCurrentCompanyObject()->getId() );
if ( $lf->getRecordCount() == 1 ) {
//Object exists, check edit permissions
if ( $this->getPermissionObject()->Check( 'ethnic_group', 'delete' )
|| ( $this->getPermissionObject()->Check( 'ethnic_group', 'delete_own' ) && $this->getPermissionObject()->isOwner( $lf->getCurrent()->getCreatedBy(), $lf->getCurrent()->getID() ) === true ) ) {
Debug::Text( 'Record Exists, deleting record ID: ' . $id, __FILE__, __LINE__, __METHOD__, 10 );
$lf = $lf->getCurrent();
} else {
$primary_validator->isTrue( 'permission', false, TTi18n::gettext( 'Delete permission denied' ) );
}
} else {
//Object doesn't exist.
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Delete permission denied, record does not exist' ) );
}
} else {
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Delete permission denied, record does not exist' ) );
}
//Debug::Arr($lf, 'AData: ', __FILE__, __LINE__, __METHOD__, 10);
$is_valid = $primary_validator->isValid();
if ( $is_valid == true ) { //Check to see if all permission checks passed before trying to save data.
Debug::Text( 'Attempting to delete record...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->setDeleted( true );
$is_valid = $lf->isValid();
if ( $is_valid == true ) {
Debug::Text( 'Record Deleted...', __FILE__, __LINE__, __METHOD__, 10 );
$save_result[$key] = $lf->Save();
$validator_stats['valid_records']++;
}
}
if ( $is_valid == false ) {
Debug::Text( 'Data is Invalid...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->FailTransaction(); //Just rollback this single record, continue on to the rest.
$validator[$key] = $this->setValidationArray( [ $primary_validator, $lf ] );
}
$lf->CommitTransaction();
}
return $this->handleRecordValidationResults( $validator, $validator_stats, $key, $save_result );
}
return $this->returnHandler( false );
}
/**
* Copy one or more .
* @param array $data ethnic group IDs
* @return array
*/
function copyEthnicGroup( $data ) {
if ( !is_array( $data ) ) {
$data = [ $data ];
}
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
Debug::Text( 'Received data for: ' . count( $data ) . ' Ethnic Groups', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$src_rows = $this->stripReturnHandler( $this->getEthnicGroup( [ 'filter_data' => [ 'id' => $data ] ], true ) );
if ( is_array( $src_rows ) && count( $src_rows ) > 0 ) {
Debug::Arr( $src_rows, 'SRC Rows: ', __FILE__, __LINE__, __METHOD__, 10 );
foreach ( $src_rows as $key => $row ) {
unset( $src_rows[$key]['id'] ); //Clear fields that can't be copied
$src_rows[$key]['name'] = Misc::generateCopyName( $row['name'] ); //Generate unique name
}
//Debug::Arr($src_rows, 'bSRC Rows: ', __FILE__, __LINE__, __METHOD__, 10);
return $this->setEthnicGroup( $src_rows ); //Save copied rows
}
return $this->returnHandler( false );
}
}
?>

View File

@@ -0,0 +1,444 @@
<?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 API\Users
*/
class APIRemittanceDestinationAccount extends APIFactory {
protected $main_class = 'RemittanceDestinationAccountFactory';
/**
* APIRemittanceDestinationAccount constructor.
*/
public function __construct() {
parent::__construct(); //Make sure parent constructor is always called.
return true;
}
/**
* Get options for dropdown boxes.
* @param bool|string $name Name of options to return, ie: 'columns', 'type', 'status'
* @param mixed $parent Parent name/ID of options to return if data is in hierarchical format. (ie: Province)
* @return bool|array
*/
function getOptions( $name = false, $parent = null ) {
if ( $name == 'columns'
&& ( !$this->getPermissionObject()->Check( 'remittance_destination_account', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'remittance_destination_account', 'view' ) || $this->getPermissionObject()->Check( 'remittance_destination_account', 'view_own' ) || $this->getPermissionObject()->Check( 'remittance_destination_account', 'view_child' ) ) ) ) {
$name = 'list_columns';
}
return parent::getOptions( $name, $parent );
}
/**
* Get default remittance destination account data for creating new remittance destination accounts.
* @param null $ach_transaction_type
* @return array
*/
function getRemittanceDestinationAccountDefaultData( $ach_transaction_type = null ) {
$company_obj = $this->getCurrentCompanyObject();
Debug::Text( 'Getting remittance destination account default data...', __FILE__, __LINE__, __METHOD__, 10 );
if ( $ach_transaction_type == '' ) {
$ach_transaction_type = 22; //Checking
}
if ( $ach_transaction_type == 32 ) { //Savings
$name = TTi18n::getText( 'Savings Account' );
} else {
$name = TTi18n::getText( 'Checking Account' );
}
$data = [
'company_id' => $company_obj->getId(),
// 'user_id' => $user_obj->getId(), // As the employee edit tab sub view, the user_id should be the current editing employee, rather than the login one.
'status_id' => 10, //enabled
'name' => $name,
'priority' => 5,
'amount_type' => 10, //percent
'percent_amount' => 100,
];
//Get New Hire Defaults.
// Currency isn't used for destination accounts from here anymore. Plus there can be multiple new hire defaults now too.
// If anything we should try to get the currency from the user record instead?
//$udlf = TTnew( 'UserDefaultListFactory' ); /** @var UserDefaultListFactory $udlf */
//$udlf->getByCompanyId( $company_obj->getId() );
//if ( $udlf->getRecordCount() > 0 ) {
// Debug::Text( 'Using User Defaults, as they exist...', __FILE__, __LINE__, __METHOD__, 10 );
// $udf_obj = $udlf->getCurrent();
// $data['currency_id'] = $udf_obj->getCurrency();
//}
return $this->returnHandler( $data );
}
/**
* Export data to csv
* @param string $format file format (csv)
* @param array $data filter data
* @param bool $disable_paging
* @return array
*/
function exportRemittanceDestinationAccount( $format = 'csv', $data = null, $disable_paging = true ) {
$result = $this->stripReturnHandler( $this->getRemittanceDestinationAccount( $data, $disable_paging ) );
return $this->exportRecords( $format, 'export_payment_methods', $result, ( ( isset( $data['filter_columns'] ) ) ? $data['filter_columns'] : null ) );
}
/**
* Get remittance destination account data for one or more accounts.
* @param array $data filter data
* @param bool $disable_paging
* @return array|bool
*/
function getRemittanceDestinationAccount( $data = null, $disable_paging = false ) {
$data = $this->initializeFilterAndPager( $data, $disable_paging );
if ( $this->getPermissionObject()->checkAuthenticationType( 700 ) == false ) { //700=HTTP Auth with username/password
return $this->getPermissionObject()->AuthenticationTypeDenied();
}
if ( !$this->getPermissionObject()->Check( 'remittance_destination_account', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'remittance_destination_account', 'view' ) || $this->getPermissionObject()->Check( 'remittance_destination_account', 'view_own' ) || $this->getPermissionObject()->Check( 'remittance_destination_account', 'view_child' ) ) ) {
return $this->getPermissionObject()->PermissionDenied();
}
$data['filter_data']['permission_children_ids'] = $this->getPermissionObject()->getPermissionChildren( 'remittance_destination_account', 'view' );
//Allow getting users from other companies, so we can change admin contacts when using the master company.
if ( isset( $data['filter_data']['company_id'] )
&& TTUUID::isUUID( $data['filter_data']['company_id'] ) && $data['filter_data']['company_id'] != TTUUID::getZeroID() && $data['filter_data']['company_id'] != TTUUID::getNotExistID()
&& ( $this->getPermissionObject()->Check( 'company', 'enabled' ) && $this->getPermissionObject()->Check( 'company', 'view' ) ) ) {
$company_id = $data['filter_data']['company_id'];
} else {
$company_id = $this->getCurrentCompanyObject()->getId();
}
$rdalf = TTnew( 'RemittanceDestinationAccountListFactory' ); /** @var RemittanceDestinationAccountListFactory $rdalf */
$rdalf->getAPISearchByCompanyIdAndArrayCriteria( $company_id, $data['filter_data'], $data['filter_items_per_page'], $data['filter_page'], null, $data['filter_sort'] );
Debug::Text( 'Record Count: ' . $rdalf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10 );
if ( $rdalf->getRecordCount() > 0 ) {
$this->setPagerObject( $rdalf );
$retarr = [];
foreach ( $rdalf as $ut_obj ) {
$retarr[] = $ut_obj->getObjectAsArray( $data['filter_columns'], $data['filter_data']['permission_children_ids'] );
}
return $this->returnHandler( $retarr );
}
return $this->returnHandler( true ); //No records returned.
}
/**
* Get only the fields that are common across all records in the search criteria. Used for Mass Editing of records.
* @param array $data filter data
* @return array
*/
function getCommonRemittanceDestinationAccountData( $data ) {
return Misc::arrayIntersectByRow( $this->stripReturnHandler( $this->getRemittanceDestinationAccount( $data, true ) ) );
}
/**
* Validate remittance destination account data for one or more accounts.
* @param array $data remittance destination account data
* @return array
*/
function validateRemittanceDestinationAccount( $data ) {
return $this->setRemittanceDestinationAccount( $data, true );
}
/**
*
* Set remittance destination account data for one or more accounts.
* @param array $data remittance destination account data
* @param bool $validate_only
* @param bool $ignore_warning
* @return array|bool
*/
function setRemittanceDestinationAccount( $data, $validate_only = false, $ignore_warning = true ) {
$validate_only = (bool)$validate_only;
$ignore_warning = (bool)$ignore_warning;
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
if ( $this->getPermissionObject()->checkAuthenticationType( 700 ) == false ) { //700=HTTP Auth with username/password
return $this->getPermissionObject()->AuthenticationTypeDenied();
}
if ( !$this->getPermissionObject()->Check( 'remittance_destination_account', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'remittance_destination_account', 'edit' ) || $this->getPermissionObject()->Check( 'remittance_destination_account', 'edit_own' ) || $this->getPermissionObject()->Check( 'remittance_destination_account', 'edit_child' ) || $this->getPermissionObject()->Check( 'remittance_destination_account', 'add' ) ) ) {
return $this->getPermissionObject()->PermissionDenied();
}
if ( $validate_only == true ) {
Debug::Text( 'Validating Only!', __FILE__, __LINE__, __METHOD__, 10 );
$permission_children_ids = false;
} else {
//Get Permission Hierarchy Children first, as this can be used for viewing, or editing.
$permission_children_ids = $this->getPermissionChildren();
}
[ $data, $total_records ] = $this->convertToMultipleRecords( $data );
Debug::Text( 'Received data for: ' . $total_records . ' Remittance destination accounts', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$validator_stats = [ 'total_records' => $total_records, 'valid_records' => 0 ];
$validator = $save_result = []; $key = false;
if ( is_array( $data ) && $total_records > 0 ) {
foreach ( $data as $key => $row ) {
$primary_validator = new Validator();
$lf = TTnew( 'RemittanceDestinationAccountListFactory' ); /** @var RemittanceDestinationAccountListFactory $lf */
$lf->StartTransaction();
if ( isset( $row['id'] ) && $row['id'] != '' ) {
//Modifying existing object.
//Get remittance destination account object, so we can only modify just changed data for specific records if needed.
$lf->getByIdAndCompanyId( $row['id'], $this->getCurrentCompanyObject()->getId() );
if ( $lf->getRecordCount() == 1 ) {
//Object exists, check edit permissions
if (
$validate_only == true
||
(
$this->getPermissionObject()->Check( 'remittance_destination_account', 'edit' )
|| ( $this->getPermissionObject()->Check( 'remittance_destination_account', 'edit_own' ) && $this->getPermissionObject()->isOwner( $lf->getCurrent()->getCreatedBy(), $lf->getCurrent()->getUser() ) === true )
|| ( $this->getPermissionObject()->Check( 'remittance_destination_account', 'edit_child' ) && $this->getPermissionObject()->isChild( $lf->getCurrent()->getUser(), $permission_children_ids ) === true )
) ) {
Debug::Text( 'Row Exists, getting current data for ID: ' . $row['id'], __FILE__, __LINE__, __METHOD__, 10 );
$lf = $lf->getCurrent();
$row = array_merge( $lf->getObjectAsArray(), $row );
} else {
$primary_validator->isTrue( 'permission', false, TTi18n::gettext( 'Edit permission denied' ) );
}
} else {
//Object doesn't exist.
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Edit permission denied, record does not exist' ) );
}
} else {
//Adding new object, check ADD permissions.
$primary_validator->isTrue( 'permission', $this->getPermissionObject()->Check( 'remittance_destination_account', 'add' ), TTi18n::gettext( 'Add permission denied' ) );
}
Debug::Arr( $row, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$is_valid = $primary_validator->isValid();
if ( $is_valid == true ) { //Check to see if all permission checks passed before trying to save data.
Debug::Text( 'Setting object data...', __FILE__, __LINE__, __METHOD__, 10 );
unset( $lf->data['legal_entity_id'] );
$lf->setObjectFromArray( $row );
$lf->Validator->setValidateOnly( $validate_only );
$is_valid = $lf->isValid( $ignore_warning );
if ( $is_valid == true ) {
Debug::Text( 'Saving data...', __FILE__, __LINE__, __METHOD__, 10 );
if ( $validate_only == true ) {
$save_result[$key] = true;
} else {
$save_result[$key] = $lf->Save();
}
$validator_stats['valid_records']++;
}
}
if ( $is_valid == false ) {
Debug::Text( 'Data is Invalid...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->FailTransaction(); //Just rollback this single record, continue on to the rest.
$validator[$key] = $this->setValidationArray( [ $primary_validator, $lf ] );
} else if ( $validate_only == true ) {
$lf->FailTransaction();
}
$lf->CommitTransaction();
}
return $this->handleRecordValidationResults( $validator, $validator_stats, $key, $save_result );
}
return $this->returnHandler( false );
}
/**
* Delete one or more account.
* @param array $data remittance destination account data
* @return array|bool
*/
function deleteRemittanceDestinationAccount( $data ) {
if ( !is_array( $data ) ) {
$data = [ $data ];
}
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
if ( $this->getPermissionObject()->checkAuthenticationType( 700 ) == false ) { //700=HTTP Auth with username/password
return $this->getPermissionObject()->AuthenticationTypeDenied();
}
if ( !$this->getPermissionObject()->Check( 'remittance_destination_account', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'remittance_destination_account', 'delete' ) || $this->getPermissionObject()->Check( 'remittance_destination_account', 'delete_own' ) || $this->getPermissionObject()->Check( 'remittance_destination_account', 'delete_child' ) ) ) {
return $this->getPermissionObject()->PermissionDenied();
}
//Get Permission Hierarchy Children first, as this can be used for viewing, or editing.
$permission_children_ids = $this->getPermissionChildren();
Debug::Text( 'Received data for: ' . count( $data ) . ' Remittance destination accounts', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$total_records = count( $data );
$validator = $save_result = []; $key = false;
$validator_stats = [ 'total_records' => $total_records, 'valid_records' => 0 ];
if ( is_array( $data ) && $total_records > 0 ) {
foreach ( $data as $key => $id ) {
$primary_validator = new Validator();
$lf = TTnew( 'RemittanceDestinationAccountListFactory' ); /** @var RemittanceDestinationAccountListFactory $lf */
$lf->StartTransaction();
if ( $id != '' ) {
//Modifying existing object.
//Get remittance destination account object, so we can only modify just changed data for specific records if needed.
$lf->getByIdAndCompanyId( $id, $this->getCurrentCompanyObject()->getId() );
if ( $lf->getRecordCount() == 1 ) {
//Object exists, check edit permissions
if ( $this->getPermissionObject()->Check( 'remittance_destination_account', 'delete' )
|| ( $this->getPermissionObject()->Check( 'remittance_destination_account', 'delete_own' ) && $this->getPermissionObject()->isOwner( $lf->getCurrent()->getCreatedBy(), $lf->getCurrent()->getUser() ) === true )
|| ( $this->getPermissionObject()->Check( 'remittance_destination_account', 'delete_child' ) && $this->getPermissionObject()->isChild( $lf->getCurrent()->getUser(), $permission_children_ids ) === true ) ) {
Debug::Text( 'Record Exists, deleting record ID: ' . $id, __FILE__, __LINE__, __METHOD__, 10 );
$lf = $lf->getCurrent();
} else {
$primary_validator->isTrue( 'permission', false, TTi18n::gettext( 'Delete permission denied' ) );
}
} else {
//Object doesn't exist.
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Delete permission denied, record does not exist' ) );
}
} else {
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Delete permission denied, record does not exist' ) );
}
//Debug::Arr($lf, 'AData: ', __FILE__, __LINE__, __METHOD__, 10);
$is_valid = $primary_validator->isValid();
if ( $is_valid == true ) { //Check to see if all permission checks passed before trying to save data.
Debug::Text( 'Attempting to delete record...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->setDeleted( true );
$is_valid = $lf->isValid();
if ( $is_valid == true ) {
Debug::Text( 'Record Deleted...', __FILE__, __LINE__, __METHOD__, 10 );
$save_result[$key] = $lf->Save();
$validator_stats['valid_records']++;
}
}
if ( $is_valid == false ) {
Debug::Text( 'Data is Invalid...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->FailTransaction(); //Just rollback this single record, continue on to the rest.
$validator[$key] = $this->setValidationArray( [ $primary_validator, $lf ] );
}
$lf->CommitTransaction();
}
return $this->handleRecordValidationResults( $validator, $validator_stats, $key, $save_result );
}
return $this->returnHandler( false );
}
/**
* Copy one or more remittance destination accounts.
* @param array $data remittance destination account IDs
* @return array
*/
function copyRemittanceDestinationAccount( $data ) {
if ( !is_array( $data ) ) {
$data = [ $data ];
}
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
Debug::Text( 'Received data for: ' . count( $data ) . ' remittance destination accounts', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$src_rows = $this->stripReturnHandler( $this->getRemittanceDestinationAccount( [ 'filter_data' => [ 'id' => $data ] ], true ) );
if ( is_array( $src_rows ) && count( $src_rows ) > 0 ) {
Debug::Arr( $src_rows, 'SRC Rows: ', __FILE__, __LINE__, __METHOD__, 10 );
foreach ( $src_rows as $key => $row ) {
unset( $src_rows[$key]['id'] ); //Clear fields that can't be copied
$src_rows[$key]['name'] = Misc::generateCopyName( $row['name'] ); //Generate unique name
//Must get decrypted account number, otherwise it will fail trying to save the record.
// This can be helpful if the customer needs to do a mass edit of routing numbers because a bank was bought out or something.
//NOTE: This never gets returned to the end-user so its still secure.
$rdalf = TTnew('RemittanceDestinationAccountListFactory'); /** @var RemittanceDestinationAccountFactory $rdlf */
$rdalf->getByIdAndCompanyId( TTUUID::castUUID( $row['id'] ), $this->getCurrentUserObject()->getCompany() );
if ( $rdalf->getRecordCount() == 1 ) {
$src_rows[$key]['value3'] = $rdalf->getCurrent()->getValue3();
}
}
//Debug::Arr($src_rows, 'bSRC Rows: ', __FILE__, __LINE__, __METHOD__, 10);
return $this->setRemittanceDestinationAccount( $src_rows ); //Save copied rows
}
return $this->returnHandler( false );
}
}
?>

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,429 @@
<?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 API\Users
*/
class APIUserContact extends APIFactory {
protected $main_class = 'UserContactFactory';
/**
* APIUserContact constructor.
*/
public function __construct() {
parent::__construct(); //Make sure parent constructor is always called.
return true;
}
/**
* Get options for dropdown boxes.
* @param bool|string $name Name of options to return, ie: 'columns', 'type', 'status'
* @param mixed $parent Parent name/ID of options to return if data is in hierarchical format. (ie: Province)
* @return bool|array
*/
function getOptions( $name = false, $parent = null ) {
if ( $name == 'columns'
&& ( !$this->getPermissionObject()->Check( 'user_contact', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'user_contact', 'view' ) || $this->getPermissionObject()->Check( 'user_contact', 'view_own' ) || $this->getPermissionObject()->Check( 'user_contact', 'view_child' ) ) ) ) {
$name = 'list_columns';
}
return parent::getOptions( $name, $parent );
}
/**
* Get default user data for creating new users.
* @return array
*/
function getUserContactDefaultData() {
//Allow getting default data from other companies, so it makes it easier to create the first employee of a company.
$company_id = $this->getCurrentCompanyObject()->getId();
Debug::Text( 'Getting user contact default data for Company ID: ' . $company_id, __FILE__, __LINE__, __METHOD__, 10 );
//Get New Hire Defaults.
$udlf = TTnew( 'UserDefaultListFactory' ); /** @var UserDefaultListFactory $udlf */
$udlf->getByCompanyId( $company_id );
if ( $udlf->getRecordCount() > 0 ) {
Debug::Text( 'Using User Defaults, as they exist...', __FILE__, __LINE__, __METHOD__, 10 );
$udf_obj = $udlf->getCurrent();
$data = [
'country' => $udf_obj->getCountry(),
'province' => $udf_obj->getProvince(),
];
}
if ( !isset( $data['country'] ) ) {
$data['country'] = 'US';
}
return $this->returnHandler( $data );
}
/**
* Get user data for one or more users.
* @param array $data filter data
* @param boolean $disable_paging disables paging and returns all records.
* @return array|bool
*/
function getUserContact( $data = null, $disable_paging = false ) {
$data = $this->initializeFilterAndPager( $data, $disable_paging );
if ( !$this->getPermissionObject()->Check( 'user_contact', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'user_contact', 'view' ) || $this->getPermissionObject()->Check( 'user_contact', 'view_own' ) || $this->getPermissionObject()->Check( 'user_contact', 'view_child' ) ) ) {
return $this->getPermissionObject()->PermissionDenied();
}
//Get Permission Hierarchy Children first, as this can be used for viewing, or editing.
$data['filter_data']['permission_children_ids'] = $this->getPermissionObject()->getPermissionChildren( 'user_contact', 'view' );
$uclf = TTnew( 'UserContactListFactory' ); /** @var UserContactListFactory $uclf */
$uclf->getAPISearchByCompanyIdAndArrayCriteria( $this->getCurrentCompanyObject()->getId(), $data['filter_data'], $data['filter_items_per_page'], $data['filter_page'], null, $data['filter_sort'] );
Debug::Text( 'Record Count: ' . $uclf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10 );
if ( $uclf->getRecordCount() > 0 ) {
$this->getProgressBarObject()->start( $this->getAPIMessageID(), $uclf->getRecordCount() );
$this->setPagerObject( $uclf );
$retarr = [];
foreach ( $uclf as $uc_obj ) {
$retarr[] = $uc_obj->getObjectAsArray( $data['filter_columns'], $data['filter_data']['permission_children_ids'] );
$this->getProgressBarObject()->set( $this->getAPIMessageID(), $uclf->getCurrentRow() );
}
$this->getProgressBarObject()->stop( $this->getAPIMessageID() );
return $this->returnHandler( $retarr );
}
return $this->returnHandler( true ); //No records returned.
}
/**
* Export data to csv
* @param string $format file format (csv)
* @param array $data filter data
* @param bool $disable_paging
* @return array
*/
function exportUserContact( $format = 'csv', $data = null, $disable_paging = true ) {
$result = $this->stripReturnHandler( $this->getUserContact( $data, $disable_paging ) );
return $this->exportRecords( $format, 'export_employee_contacts', $result, ( ( isset( $data['filter_columns'] ) ) ? $data['filter_columns'] : null ) );
}
/**
* Get only the fields that are common across all records in the search criteria. Used for Mass Editing of records.
* @param array $data filter data
* @return array
*/
function getCommonUserContactData( $data ) {
return Misc::arrayIntersectByRow( $this->stripReturnHandler( $this->getUserContact( $data, true ) ) );
}
/**
* Validate user data for one or more users.
* @param array $data user data
* @return array
*/
function validateUserContact( $data ) {
return $this->setUserContact( $data, true );
}
/**
* Set user data for one or more users.
* @param array $data user data
* @param bool $validate_only
* @param bool $ignore_warning
* @return array|bool
*/
function setUserContact( $data, $validate_only = false, $ignore_warning = true ) {
$validate_only = (bool)$validate_only;
$ignore_warning = (bool)$ignore_warning;
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
if ( !$this->getPermissionObject()->Check( 'user_contact', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'user_contact', 'edit' ) || $this->getPermissionObject()->Check( 'user_contact', 'edit_own' ) || $this->getPermissionObject()->Check( 'user_contact', 'edit_child' ) || $this->getPermissionObject()->Check( 'user_contact', 'add' ) ) ) {
return $this->getPermissionObject()->PermissionDenied();
}
if ( $validate_only == true ) {
Debug::Text( 'Validating Only!', __FILE__, __LINE__, __METHOD__, 10 );
$permission_children_ids = false;
} else {
//Get Permission Hierarchy Children first, as this can be used for viewing, or editing.
$permission_children_ids = $this->getPermissionChildren();
}
[ $data, $total_records ] = $this->convertToMultipleRecords( $data );
Debug::Text( 'Received data for: ' . $total_records . ' Users', __FILE__, __LINE__, __METHOD__, 10 );
//Debug::Arr($data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10);
$validator_stats = [ 'total_records' => $total_records, 'valid_records' => 0 ];
$validator = $save_result = []; $key = false;
if ( is_array( $data ) && $total_records > 0 ) {
$this->getProgressBarObject()->start( $this->getAPIMessageID(), $total_records );
foreach ( $data as $key => $row ) {
$primary_validator = new Validator();
$lf = TTnew( 'UserContactListFactory' ); /** @var UserContactListFactory $lf */
$lf->StartTransaction();
if ( isset( $row['id'] ) && $row['id'] != '' ) {
//Modifying existing object.
//Get user object, so we can only modify just changed data for specific records if needed.
$lf->getByIdAndCompanyId( $row['id'], $this->getCurrentCompanyObject()->getId() );
if ( $lf->getRecordCount() == 1 ) {
//Object exists, check edit permissions
//Debug::Text('User ID: '. $row['id'] .' Created By: '. $lf->getCurrent()->getCreatedBy() .' Is Owner: '. (int)$this->getPermissionObject()->isOwner( $lf->getCurrent()->getCreatedBy(), $lf->getCurrent()->getID() ) .' Is Child: '. (int)$this->getPermissionObject()->isChild( $lf->getCurrent()->getId(), $permission_children_ids ), __FILE__, __LINE__, __METHOD__, 10);
if (
$validate_only == true
||
(
$this->getPermissionObject()->Check( 'user_contact', 'edit' )
|| ( $this->getPermissionObject()->Check( 'user_contact', 'edit_own' ) && $this->getPermissionObject()->isOwner( $lf->getCurrent()->getCreatedBy(), $lf->getCurrent()->getUser() ) === true )
|| ( $this->getPermissionObject()->Check( 'user_contact', 'edit_child' ) && $this->getPermissionObject()->isChild( $lf->getCurrent()->getUser(), $permission_children_ids ) === true )
) ) {
Debug::Text( 'Row Exists, getting current data for ID: ' . $row['id'], __FILE__, __LINE__, __METHOD__, 10 );
$lf = $lf->getCurrent(); //Make the current $lf variable the current object, so we can ignore some fields if needed.
$row = array_merge( $lf->getObjectAsArray(), $row );
} else {
$primary_validator->isTrue( 'permission', false, TTi18n::gettext( 'Edit permission denied' ) );
}
} else {
//Object doesn't exist.
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Edit permission denied, record does not exist' ) );
}
} else {
//Adding new object, check ADD permissions.
if ( !( $validate_only == true
||
( $this->getPermissionObject()->Check( 'user_contact', 'add' )
&&
(
$this->getPermissionObject()->Check( 'user_contact', 'edit' )
|| ( isset( $row['user_id'] ) && $this->getPermissionObject()->Check( 'user_contact', 'edit_own' ) && $this->getPermissionObject()->isOwner( false, $row['user_id'] ) === true ) //We don't know the created_by of the user at this point, but only check if the user is assigned to the logged in person.
|| ( isset( $row['user_id'] ) && $this->getPermissionObject()->Check( 'user_contact', 'edit_child' ) && $this->getPermissionObject()->isChild( $row['user_id'], $permission_children_ids ) === true )
)
)
) ) {
$primary_validator->isTrue( 'permission', false, TTi18n::gettext( 'Add permission denied' ) );
}
}
//Debug::Arr($row, 'Data: ', __FILE__, __LINE__, __METHOD__, 10);
$is_valid = $primary_validator->isValid();
if ( $is_valid == true ) { //Check to see if all permission checks passed before trying to save data.
Debug::Text( 'Attempting to save data... API Message ID: ' . $this->getAPIMessageID(), __FILE__, __LINE__, __METHOD__, 10 );
if ( DEMO_MODE == true && $lf->isNew() == false ) { //Allow changing these if DEMO is enabled, but they are adding new records.
Debug::Text( 'DEMO Mode ENABLED, disable modifying some data...', __FILE__, __LINE__, __METHOD__, 10 );
unset( $row['status_id'] );
}
//If the user doesn't have permissions to change the hierarchy_control, unset that data.
//Force Company ID to current company.
if ( !isset( $row['company_id'] ) || !$this->getPermissionObject()->Check( 'company', 'add' ) ) {
//$lf->setCompany( $this->getCurrentCompanyObject()->getId() );
$row['company_id'] = $this->getCurrentCompanyObject()->getId();
}
$lf->setObjectFromArray( $row );
//Force Company ID to current company.
//$lf->setCompany( $this->getCurrentCompanyObject()->getId() );
$lf->Validator->setValidateOnly( $validate_only );
$is_valid = $lf->isValid( $ignore_warning );
if ( $is_valid == true ) {
Debug::Text( 'Saving data...', __FILE__, __LINE__, __METHOD__, 10 );
if ( $validate_only == true ) {
$save_result[$key] = true;
} else {
$save_result[$key] = $lf->Save();
}
$validator_stats['valid_records']++;
}
}
if ( $is_valid == false ) {
Debug::Text( 'Data is Invalid...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->FailTransaction(); //Just rollback this single record, continue on to the rest.
$validator[$key] = $this->setValidationArray( [ $primary_validator, $lf ] );
} else if ( $validate_only == true ) {
//Always fail transaction when valididate only is used, as is saved to different tables immediately.
$lf->FailTransaction();
}
$lf->CommitTransaction();
$this->getProgressBarObject()->set( $this->getAPIMessageID(), $key );
}
$this->getProgressBarObject()->stop( $this->getAPIMessageID() );
return $this->handleRecordValidationResults( $validator, $validator_stats, $key, $save_result );
}
return $this->returnHandler( false );
}
/**
* Delete one or more users.
* @param array $data user data
* @return array|bool
*/
function deleteUserContact( $data ) {
if ( !is_array( $data ) ) {
$data = [ $data ];
}
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
if ( !$this->getPermissionObject()->Check( 'user_contact', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'user_contact', 'delete' ) || $this->getPermissionObject()->Check( 'user_contact', 'delete_own' ) || $this->getPermissionObject()->Check( 'user_contact', 'delete_child' ) ) ) {
return $this->getPermissionObject()->PermissionDenied();
}
//Get Permission Hierarchy Children first, as this can be used for viewing, or editing.
$permission_children_ids = $this->getPermissionChildren();
Debug::Text( 'Received data for: ' . count( $data ) . ' Users', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$total_records = count( $data );
$validator = $save_result = []; $key = false;
$validator_stats = [ 'total_records' => $total_records, 'valid_records' => 0 ];
if ( is_array( $data ) && $total_records > 0 ) {
$this->getProgressBarObject()->start( $this->getAPIMessageID(), $total_records );
foreach ( $data as $key => $id ) {
$primary_validator = new Validator();
$lf = TTnew( 'UserContactListFactory' ); /** @var UserContactListFactory $lf */
$lf->StartTransaction();
if ( $id != '' ) {
//Modifying existing object.
//Get user object, so we can only modify just changed data for specific records if needed.
$lf->getByIdAndCompanyId( $id, $this->getCurrentCompanyObject()->getId() );
if ( $lf->getRecordCount() == 1 ) {
//Object exists, check edit permissions
//Debug::Text('User ID: '. $user['id'] .' Created By: '. $lf->getCurrent()->getCreatedBy() .' Is Owner: '. (int)$this->getPermissionObject()->isOwner( $lf->getCurrent()->getCreatedBy(), $lf->getCurrent()->getID() ) .' Is Child: '. (int)$this->getPermissionObject()->isChild( $lf->getCurrent()->getId(), $permission_children_ids ), __FILE__, __LINE__, __METHOD__, 10);
if ( $this->getPermissionObject()->Check( 'user_contact', 'delete' )
|| ( $this->getPermissionObject()->Check( 'user_contact', 'delete_own' ) && $this->getPermissionObject()->isOwner( $lf->getCurrent()->getCreatedBy(), $lf->getCurrent()->getUser() ) === true )
|| ( $this->getPermissionObject()->Check( 'user_contact', 'delete_child' ) && $this->getPermissionObject()->isChild( $lf->getCurrent()->getUser(), $permission_children_ids ) === true ) ) {
Debug::Text( 'Record Exists, deleting record ID: ' . $id, __FILE__, __LINE__, __METHOD__, 10 );
$lf = $lf->getCurrent();
} else {
$primary_validator->isTrue( 'permission', false, TTi18n::gettext( 'Delete permission denied' ) );
}
} else {
//Object doesn't exist.
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Delete permission denied, record does not exist' ) );
}
} else {
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Delete permission denied, record does not exist' ) );
}
//Debug::Arr($lf, 'AData: ', __FILE__, __LINE__, __METHOD__, 10);
$is_valid = $primary_validator->isValid();
if ( $is_valid == true ) { //Check to see if all permission checks passed before trying to save data.
Debug::Text( 'Attempting to delete record...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->setDeleted( true );
$is_valid = $lf->isValid();
if ( $is_valid == true ) {
Debug::Text( 'Record Deleted...', __FILE__, __LINE__, __METHOD__, 10 );
$save_result[$key] = $lf->Save();
$validator_stats['valid_records']++;
}
}
if ( $is_valid == false ) {
Debug::Text( 'Data is Invalid...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->FailTransaction(); //Just rollback this single record, continue on to the rest.
$validator[$key] = $this->setValidationArray( [ $primary_validator, $lf ] );
}
$lf->CommitTransaction();
$this->getProgressBarObject()->set( $this->getAPIMessageID(), $key );
}
$this->getProgressBarObject()->stop( $this->getAPIMessageID() );
return $this->handleRecordValidationResults( $validator, $validator_stats, $key, $save_result );
}
return $this->returnHandler( false );
}
/**
* Copy one or more users.
* @param array $data user data
* @return array
*/
function copyUserContact( $data ) {
//Can only Copy as New, not just a regular copy, as too much data needs to be changed,
//such as username, password, employee_number, SIN, first/last name address...
return $this->returnHandler( false );
}
}
?>

View File

@@ -0,0 +1,365 @@
<?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 API\Users
*/
class APIUserDeduction extends APIFactory {
protected $main_class = 'UserDeductionFactory';
/**
* APIUserDeduction constructor.
*/
public function __construct() {
parent::__construct(); //Make sure parent constructor is always called.
return true;
}
/**
* Get default user_deduction data for creating new user_deductiones.
* @return array
*/
function getUserDeductionDefaultData() {
Debug::Text( 'Getting user_deduction default data...', __FILE__, __LINE__, __METHOD__, 10 );
$data = [];
return $this->returnHandler( $data );
}
/**
* Get user_deduction data for one or more user_deductiones.
* @param array $data filter data
* @param bool $disable_paging
* @return array|bool
*/
function getUserDeduction( $data = null, $disable_paging = false ) {
$data = $this->initializeFilterAndPager( $data, $disable_paging );
if ( $this->getPermissionObject()->checkAuthenticationType( 700 ) == false ) { //700=HTTP Auth with username/password
return $this->getPermissionObject()->AuthenticationTypeDenied();
}
if ( !$this->getPermissionObject()->Check( 'user_tax_deduction', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'user_tax_deduction', 'view' ) || $this->getPermissionObject()->Check( 'user_tax_deduction', 'view_own' ) || $this->getPermissionObject()->Check( 'user_tax_deduction', 'view_child' ) ) ) {
return $this->getPermissionObject()->PermissionDenied();
}
$data['filter_data']['permission_children_ids'] = $this->getPermissionObject()->getPermissionChildren( 'user_tax_deduction', 'view' );
$blf = TTnew( 'UserDeductionListFactory' ); /** @var UserDeductionListFactory $blf */
$blf->getAPISearchByCompanyIdAndArrayCriteria( $this->getCurrentCompanyObject()->getId(), $data['filter_data'], $data['filter_items_per_page'], $data['filter_page'], null, $data['filter_sort'] );
Debug::Text( 'Record Count: ' . $blf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10 );
if ( $blf->getRecordCount() > 0 ) {
$this->getProgressBarObject()->start( $this->getAPIMessageID(), $blf->getRecordCount() );
$this->setPagerObject( $blf );
$retarr = [];
foreach ( $blf as $b_obj ) {
$retarr[] = $b_obj->getObjectAsArray( $data['filter_columns'] );
$this->getProgressBarObject()->set( $this->getAPIMessageID(), $blf->getCurrentRow() );
}
$this->getProgressBarObject()->stop( $this->getAPIMessageID() );
return $this->returnHandler( $retarr );
}
return $this->returnHandler( true ); //No records returned.
}
/**
* Get only the fields that are common across all records in the search criteria. Used for Mass Editing of records.
* @param array $data filter data
* @return array
*/
function getCommonUserDeductionData( $data ) {
return Misc::arrayIntersectByRow( $this->stripReturnHandler( $this->getUserDeduction( $data, true ) ) );
}
/**
* Validate user_deduction data for one or more user_deductiones.
* @param array $data user_deduction data
* @return array
*/
function validateUserDeduction( $data ) {
return $this->setUserDeduction( $data, true );
}
/**
* Set user_deduction data for one or more user_deductiones.
* @param array $data user_deduction data
* @param bool $validate_only
* @param bool $ignore_warning
* @return array|bool
*/
function setUserDeduction( $data, $validate_only = false, $ignore_warning = true ) {
$validate_only = (bool)$validate_only;
$ignore_warning = (bool)$ignore_warning;
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
if ( $this->getPermissionObject()->checkAuthenticationType( 700 ) == false ) { //700=HTTP Auth with username/password
return $this->getPermissionObject()->AuthenticationTypeDenied();
}
if ( !$this->getPermissionObject()->Check( 'user_tax_deduction', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'user_tax_deduction', 'edit' ) || $this->getPermissionObject()->Check( 'user_tax_deduction', 'edit_own' ) || $this->getPermissionObject()->Check( 'user_tax_deduction', 'edit_child' ) || $this->getPermissionObject()->Check( 'user_tax_deduction', 'add' ) ) ) {
return $this->getPermissionObject()->PermissionDenied();
}
if ( $validate_only == true ) {
Debug::Text( 'Validating Only!', __FILE__, __LINE__, __METHOD__, 10 );
$permission_children_ids = false;
} else {
//Get Permission Hierarchy Children first, as this can be used for viewing, or editing.
$permission_children_ids = $this->getPermissionChildren();
}
[ $data, $total_records ] = $this->convertToMultipleRecords( $data );
Debug::Text( 'Received data for: ' . $total_records . ' UserDeductions', __FILE__, __LINE__, __METHOD__, 10 );
//Debug::Arr($data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10);
$validator_stats = [ 'total_records' => $total_records, 'valid_records' => 0 ];
$validator = $save_result = []; $key = false;
if ( is_array( $data ) && $total_records > 0 ) {
$this->getProgressBarObject()->start( $this->getAPIMessageID(), $total_records );
foreach ( $data as $key => $row ) {
$primary_validator = new Validator();
$lf = TTnew( 'UserDeductionListFactory' ); /** @var UserDeductionListFactory $lf */
$lf->StartTransaction();
if ( isset( $row['id'] ) && $row['id'] != '' ) {
//Modifying existing object.
//Get user_deduction object, so we can only modify just changed data for specific records if needed.
$lf->getByIdAndCompanyId( $row['id'], $this->getCurrentCompanyObject()->getId() );
if ( $lf->getRecordCount() == 1 ) {
//Object exists, check edit permissions
if (
$validate_only == true
||
(
$this->getPermissionObject()->Check( 'user_tax_deduction', 'edit' )
|| ( $this->getPermissionObject()->Check( 'user_tax_deduction', 'edit_own' ) && $this->getPermissionObject()->isOwner( $lf->getCurrent()->getCreatedBy(), $lf->getCurrent()->getUser() ) === true )
|| ( $this->getPermissionObject()->Check( 'user_tax_deduction', 'edit_child' ) && $this->getPermissionObject()->isChild( $lf->getCurrent()->getUser(), $permission_children_ids ) === true )
) ) {
Debug::Text( 'Row Exists, getting current data for ID: ' . $row['id'], __FILE__, __LINE__, __METHOD__, 10 );
$lf = $lf->getCurrent();
$row = array_merge( $lf->getObjectAsArray(), $row );
} else {
$primary_validator->isTrue( 'permission', false, TTi18n::gettext( 'Edit permission denied' ) );
}
} else {
//Object doesn't exist.
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Edit permission denied, record does not exist' ) );
}
} else {
//Adding new object, check ADD permissions.
if ( !( $validate_only == true
||
( $this->getPermissionObject()->Check( 'user_tax_deduction', 'add' )
&&
(
$this->getPermissionObject()->Check( 'user_tax_deduction', 'edit' )
|| ( isset( $row['user_id'] ) && $this->getPermissionObject()->Check( 'user_tax_deduction', 'edit_own' ) && $this->getPermissionObject()->isOwner( false, $row['user_id'] ) === true ) //We don't know the created_by of the user at this point, but only check if the user is assigned to the logged in person.
|| ( isset( $row['user_id'] ) && $this->getPermissionObject()->Check( 'user_tax_deduction', 'edit_child' ) && $this->getPermissionObject()->isChild( $row['user_id'], $permission_children_ids ) === true )
)
)
) ) {
$primary_validator->isTrue( 'permission', false, TTi18n::gettext( 'Add permission denied' ) );
}
}
//Debug::Arr($row, 'Data: ', __FILE__, __LINE__, __METHOD__, 10);
$is_valid = $primary_validator->isValid();
if ( $is_valid == true ) { //Check to see if all permission checks passed before trying to save data.
Debug::Text( 'Setting object data...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->setObjectFromArray( $row );
$is_valid = $lf->isValid( $ignore_warning ); //preValidate must be called before hasDataChanged() as length_of_service_date is always set and if its the employees hire date, then gets converted to NULL. This can change the hasDataChanged() result.
if ( $lf->hasDataChanged() == true ) { //This prevents audit log from getting poluted when nothing changed.
if ( $is_valid == true ) {
Debug::Text( 'Saving data...', __FILE__, __LINE__, __METHOD__, 10 );
if ( $validate_only == true ) {
$save_result[$key] = true;
} else {
$save_result[$key] = $lf->Save();
}
$validator_stats['valid_records']++;
}
} else {
Debug::Text( ' Data did not change, skipping saving...', __FILE__, __LINE__, __METHOD__, 10 );
$save_result[$key] = true;
$validator_stats['valid_records']++;
}
}
if ( $is_valid == false ) {
Debug::Text( 'Data is Invalid...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->FailTransaction(); //Just rollback this single record, continue on to the rest.
$validator[$key] = $this->setValidationArray( [ $primary_validator, $lf ] );
} else if ( $validate_only == true ) {
$lf->FailTransaction();
}
$lf->CommitTransaction();
$this->getProgressBarObject()->set( $this->getAPIMessageID(), $key );
}
$this->getProgressBarObject()->stop( $this->getAPIMessageID() );
return $this->handleRecordValidationResults( $validator, $validator_stats, $key, $save_result );
}
return $this->returnHandler( false );
}
/**
* Delete one or more user_deductions.
* @param array $data user_deduction data
* @return array|bool
*/
function deleteUserDeduction( $data ) {
if ( !is_array( $data ) ) {
$data = [ $data ];
}
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
if ( $this->getPermissionObject()->checkAuthenticationType( 700 ) == false ) { //700=HTTP Auth with username/password
return $this->getPermissionObject()->AuthenticationTypeDenied();
}
if ( !$this->getPermissionObject()->Check( 'user_tax_deduction', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'user_tax_deduction', 'delete' ) || $this->getPermissionObject()->Check( 'user_tax_deduction', 'delete_own' ) || $this->getPermissionObject()->Check( 'user_tax_deduction', 'delete_child' ) ) ) {
return $this->getPermissionObject()->PermissionDenied();
}
//Get Permission Hierarchy Children first, as this can be used for viewing, or editing.
$permission_children_ids = $this->getPermissionChildren();
Debug::Text( 'Received data for: ' . count( $data ) . ' UserDeductions', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$total_records = count( $data );
$validator = $save_result = []; $key = false;
$validator_stats = [ 'total_records' => $total_records, 'valid_records' => 0 ];
if ( is_array( $data ) && $total_records > 0 ) {
$this->getProgressBarObject()->start( $this->getAPIMessageID(), $total_records );
foreach ( $data as $key => $id ) {
$primary_validator = new Validator();
$lf = TTnew( 'UserDeductionListFactory' ); /** @var UserDeductionListFactory $lf */
$lf->StartTransaction();
if ( $id != '' ) {
//Modifying existing object.
//Get user_deduction object, so we can only modify just changed data for specific records if needed.
$lf->getByIdAndCompanyId( $id, $this->getCurrentCompanyObject()->getId() );
if ( $lf->getRecordCount() == 1 ) {
//Object exists, check edit permissions
if ( $this->getPermissionObject()->Check( 'user_tax_deduction', 'delete' )
|| ( $this->getPermissionObject()->Check( 'user_tax_deduction', 'delete_own' ) && $this->getPermissionObject()->isOwner( $lf->getCurrent()->getCreatedBy(), $lf->getCurrent()->getID() ) === true )
|| ( $this->getPermissionObject()->Check( 'user_tax_deduction', 'delete_child' ) && $this->getPermissionObject()->isChild( $lf->getCurrent()->getUser(), $permission_children_ids ) === true ) ) {
Debug::Text( 'Record Exists, deleting record ID: ' . $id, __FILE__, __LINE__, __METHOD__, 10 );
$lf = $lf->getCurrent();
} else {
$primary_validator->isTrue( 'permission', false, TTi18n::gettext( 'Delete permission denied' ) );
}
} else {
//Object doesn't exist.
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Delete permission denied, record does not exist' ) );
}
} else {
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Delete permission denied, record does not exist' ) );
}
//Debug::Arr($lf, 'AData: ', __FILE__, __LINE__, __METHOD__, 10);
$is_valid = $primary_validator->isValid();
if ( $is_valid == true ) { //Check to see if all permission checks passed before trying to save data.
Debug::Text( 'Attempting to delete record...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->setDeleted( true );
$is_valid = $lf->isValid();
if ( $is_valid == true ) {
Debug::Text( 'Record Deleted...', __FILE__, __LINE__, __METHOD__, 10 );
$save_result[$key] = $lf->Save();
$validator_stats['valid_records']++;
}
}
if ( $is_valid == false ) {
Debug::Text( 'Data is Invalid...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->FailTransaction(); //Just rollback this single record, continue on to the rest.
$validator[$key] = $this->setValidationArray( [ $primary_validator, $lf ] );
}
$lf->CommitTransaction();
$this->getProgressBarObject()->set( $this->getAPIMessageID(), $key );
}
$this->getProgressBarObject()->stop( $this->getAPIMessageID() );
return $this->handleRecordValidationResults( $validator, $validator_stats, $key, $save_result );
}
return $this->returnHandler( false );
}
}
?>

View File

@@ -0,0 +1,435 @@
<?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 API\Users
*/
class APIUserDefault extends APIFactory {
protected $main_class = 'UserDefaultFactory';
/**
* APIUserDefault constructor.
*/
public function __construct() {
parent::__construct(); //Make sure parent constructor is always called.
return true;
}
/**
* Get default UserDefault data for creating new UserDefaultes.
* @return array
*/
function getUserDefaultDefaultData() {
$company_obj = $this->getCurrentCompanyObject();
$user_obj = $this->getCurrentUserObject();
$user_preference_obj = $this->getCurrentUserPreferenceObject();
//Permissions
$pclf = TTnew( 'PermissionControlListFactory' ); /** @var PermissionControlListFactory $pclf */
$pclf->getByCompanyIdAndLevel( $company_obj->getID(), 10, 1, null, null, [ 'level' => 'desc' ] );
if ( $pclf->getRecordCount() > 0 ) {
$permission_control_id = $pclf->getCurrent()->getID();
} else {
$permission_control_id = TTUUID::getZeroID();
}
//Terminated Permissions
$pclf = TTnew( 'PermissionControlListFactory' ); /** @var PermissionControlListFactory $pclf */
$pclf->getByCompanyIdAndLevel( $company_obj->getID(), 5, 1, null, null, [ 'level' => 'desc' ] );
if ( $pclf->getRecordCount() > 0 ) {
$terminated_permission_control_id = $pclf->getCurrent()->getID();
} else {
$terminated_permission_control_id = TTUUID::getZeroID();
}
//Get Pay Period Schedule
$ppslf = TTNew( 'PayPeriodScheduleListFactory' ); /** @var PayPeriodScheduleListFactory $ppslf */
$ppslf->getByCompanyId( $company_obj->getID() );
if ( $ppslf->getRecordCount() == 1 ) {
$pay_period_schedule_id = $ppslf->getCurrent()->getID();
} else {
$pay_period_schedule_id = null;
}
//Get Policy Group
$pglf = TTNew( 'PolicyGroupListFactory' ); /** @var PolicyGroupListFactory $pglf */
$pglf->getByCompanyId( $company_obj->getID() );
if ( $pglf->getRecordCount() == 1 ) {
$policy_group_id = $pglf->getCurrent()->getID();
} else {
$policy_group_id = null;
}
Debug::Text( 'Getting UserDefault default data...', __FILE__, __LINE__, __METHOD__, 10 );
$data = [
'company_id' => $company_obj->getId(),
'legal_entity_id' => $user_obj->getLegalEntity(),
'display_order' => 100,
//Employee Identification
'currency_id' => $user_obj->getCurrency(),
'policy_group_id' => $policy_group_id,
'pay_period_schedule_id' => $pay_period_schedule_id,
'permission_control_id' => $permission_control_id,
'terminated_permission_control_id' => $terminated_permission_control_id,
//Preferences
'time_zone' => $user_preference_obj->getTimeZone(),
'language' => 'en',
'date_format' => $user_preference_obj->getDateFormat(),
'time_format' => $user_preference_obj->getTimeFormat(),
'time_unit_format' => $user_preference_obj->getTimeUnitFormat(),
'distance_format' => $user_preference_obj->getDistanceFormat(), //Kilometers
'items_per_page' => $user_preference_obj->getItemsPerPage(),
'start_week_day' => $user_preference_obj->getStartWeekDay(),
//Contact Info
'city' => $this->getCurrentCompanyObject()->getCity(),
'country' => $this->getCurrentCompanyObject()->getCountry(),
'province' => $this->getCurrentCompanyObject()->getProvince(),
'work_phone' => $this->getCurrentCompanyObject()->getWorkPhone(),
'created_by_id' => $this->getCurrentUserObject()->getId(),
];
return $this->returnHandler( $data );
}
/**
* Get UserDefault data for one or more UserDefaultes.
* @param array $data filter data
* @param bool $disable_paging
* @return array|bool
*/
function getUserDefault( $data = null, $disable_paging = false ) {
$data = $this->initializeFilterAndPager( $data, $disable_paging );
if ( !$this->getPermissionObject()->Check( 'user_default', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'user_default', 'view' ) || $this->getPermissionObject()->Check( 'user_default', 'view_own' ) || $this->getPermissionObject()->Check( 'user_default', 'view_child' ) ) ) {
$data['filter_columns'] = $this->handlePermissionFilterColumns( ( isset( $data['filter_columns'] ) ) ? $data['filter_columns'] : null, Misc::trimSortPrefix( $this->getOptions( 'list_columns' ) ) );
$data['filter_data']['permission_children_ids'] = null;
} else {
//Get Permission Hierarchy Children first, as this can be used for viewing, or editing.
$data['filter_data']['permission_children_ids'] = $this->getPermissionObject()->getPermissionChildren( 'user_default', 'view' );
}
$uplf = TTnew( 'UserDefaultListFactory' ); /** @var UserDefaultListFactory $uplf */
$uplf->getAPISearchByCompanyIdAndArrayCriteria( $this->getCurrentCompanyObject()->getId(), $data['filter_data'], $data['filter_items_per_page'], $data['filter_page'], null, $data['filter_sort'] );
Debug::Text( 'Record Count: ' . $uplf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10 );
if ( $uplf->getRecordCount() > 0 ) {
$this->getProgressBarObject()->start( $this->getAPIMessageID(), $uplf->getRecordCount() );
$this->setPagerObject( $uplf );
$retarr = [];
foreach ( $uplf as $ut_obj ) {
$retarr[] = $ut_obj->getObjectAsArray( $data['filter_columns'], $data['filter_data']['permission_children_ids'] );
$this->getProgressBarObject()->set( $this->getAPIMessageID(), $uplf->getCurrentRow() );
}
$this->getProgressBarObject()->stop( $this->getAPIMessageID() );
return $this->returnHandler( $retarr );
}
return $this->returnHandler( true ); //No records returned.
}
/**
* Validate UserDefault data for one or more UserDefaultes.
* @param array $data UserDefault data
* @return array
*/
function validateUserDefault( $data ) {
return $this->setUserDefault( $data, true );
}
/**
* Set UserDefault data for one or more UserDefaultes.
* @param array $data UserDefault data
* @param bool $validate_only
* @param bool $ignore_warning
* @return array|bool
*/
function setUserDefault( $data, $validate_only = false, $ignore_warning = true ) {
$validate_only = (bool)$validate_only;
$ignore_warning = (bool)$ignore_warning;
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
if ( !$this->getPermissionObject()->Check( 'user_default', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'user_default', 'edit' ) || $this->getPermissionObject()->Check( 'user_default', 'add' ) ) ) {
return $this->getPermissionObject()->PermissionDenied();
}
if ( $validate_only == true ) {
Debug::Text( 'Validating Only!', __FILE__, __LINE__, __METHOD__, 10 );
}
[ $data, $total_records ] = $this->convertToMultipleRecords( $data );
Debug::Text( 'Received data for: ' . $total_records . ' UserDefaults', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$validator_stats = [ 'total_records' => $total_records, 'valid_records' => 0 ];
$validator = $save_result = []; $key = false;
if ( is_array( $data ) && $total_records > 0 ) {
$this->getProgressBarObject()->start( $this->getAPIMessageID(), $total_records );
foreach ( $data as $key => $row ) {
$primary_validator = new Validator();
$lf = TTnew( 'UserDefaultListFactory' ); /** @var UserDefaultListFactory $lf */
$lf->StartTransaction();
if ( isset( $row['id'] ) && $row['id'] != '' ) {
//Modifying existing object.
//Get UserDefault object, so we can only modify just changed data for specific records if needed.
$lf->getByIdAndCompanyId( $row['id'], $this->getCurrentCompanyObject()->getId() );
if ( $lf->getRecordCount() == 1 ) {
//Object exists, check edit permissions
if (
$validate_only == true
||
(
$this->getPermissionObject()->Check( 'user_default', 'edit' )
) ) {
Debug::Text( 'Row Exists, getting current data for ID: ' . $row['id'], __FILE__, __LINE__, __METHOD__, 10 );
$lf = $lf->getCurrent();
$row = array_merge( $lf->getObjectAsArray(), $row );
} else {
$primary_validator->isTrue( 'permission', false, TTi18n::gettext( 'Edit permission denied' ) );
}
} else {
//Object doesn't exist.
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Edit permission denied, record does not exist' ) );
}
} else {
//Adding new object, check ADD permissions.
$primary_validator->isTrue( 'permission', $this->getPermissionObject()->Check( 'user_default', 'add' ), TTi18n::gettext( 'Add permission denied' ) );
//Because this class has sub-classes that depend on it, when adding a new record we need to make sure the ID is set first,
//so the sub-classes can depend on it. We also need to call Save( TRUE, TRUE ) to force a lookup on isNew()
$row['id'] = $lf->getNextInsertId();
}
Debug::Arr( $row, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$is_valid = $primary_validator->isValid();
if ( $is_valid == true ) { //Check to see if all permission checks passed before trying to save data.
Debug::Text( 'Setting object data...', __FILE__, __LINE__, __METHOD__, 10 );
//Force Company ID to current company.
$row['company_id'] = $this->getCurrentCompanyObject()->getId();
//If the user doesn't have permissions to change the hierarchy_control, unset that data.
if ( isset( $row['hierarchy_control'] ) && ( $this->getPermissionObject()->Check( 'hierarchy', 'edit' ) || $this->getPermissionObject()->Check( 'user', 'edit_hierarchy' ) ) ) {
Debug::Text( 'Allowing change of hierarchy...', __FILE__, __LINE__, __METHOD__, 10 );
} else {
Debug::Text( 'NOT allowing change of hierarchy...', __FILE__, __LINE__, __METHOD__, 10 );
unset( $row['hierarchy_control'] );
}
$lf->setObjectFromArray( $row );
$is_valid = $lf->isValid( $ignore_warning );
if ( $is_valid == true ) {
Debug::Text( 'Saving data...', __FILE__, __LINE__, __METHOD__, 10 );
if ( $validate_only == true ) {
$save_result[$key] = true;
} else {
$save_result[$key] = $lf->Save( true, true );
}
$validator_stats['valid_records']++;
}
}
if ( $is_valid == false ) {
Debug::Text( 'Data is Invalid...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->FailTransaction(); //Just rollback this single record, continue on to the rest.
$validator[$key] = $this->setValidationArray( [ $primary_validator, $lf ] );
} else if ( $validate_only == true ) {
$lf->FailTransaction();
}
$lf->CommitTransaction();
$this->getProgressBarObject()->set( $this->getAPIMessageID(), $key );
}
$this->getProgressBarObject()->stop( $this->getAPIMessageID() );
return $this->handleRecordValidationResults( $validator, $validator_stats, $key, $save_result );
}
return $this->returnHandler( false );
}
/**
* Delete one or more UserDefaults.
* @param array $data UserDefault data
* @return array|bool
*/
function deleteUserDefault( $data ) {
if ( !is_array( $data ) ) {
$data = [ $data ];
}
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
if ( !$this->getPermissionObject()->Check( 'user_default', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'user_default', 'delete' ) ) ) {
return $this->getPermissionObject()->PermissionDenied();
}
Debug::Text( 'Received data for: ' . count( $data ) . ' UserDefaults', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$total_records = count( $data );
$validator = $save_result = []; $key = false;
$validator_stats = [ 'total_records' => $total_records, 'valid_records' => 0 ];
if ( is_array( $data ) && $total_records > 0 ) {
$this->getProgressBarObject()->start( $this->getAPIMessageID(), $total_records );
foreach ( $data as $key => $id ) {
$primary_validator = new Validator();
$lf = TTnew( 'UserDefaultListFactory' ); /** @var UserDefaultListFactory $lf */
$lf->StartTransaction();
if ( $id != '' ) {
//Modifying existing object.
//Get UserDefault object, so we can only modify just changed data for specific records if needed.
$lf->getByIdAndCompanyId( $id, $this->getCurrentCompanyObject()->getId() );
if ( $lf->getRecordCount() == 1 ) {
//Object exists, check edit permissions
if ( $this->getPermissionObject()->Check( 'user_default', 'delete' ) ) {
Debug::Text( 'Record Exists, deleting record ID: ' . $id, __FILE__, __LINE__, __METHOD__, 10 );
$lf = $lf->getCurrent();
} else {
$primary_validator->isTrue( 'permission', false, TTi18n::gettext( 'Delete permission denied' ) );
}
} else {
//Object doesn't exist.
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Delete permission denied, record does not exist' ) );
}
} else {
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Delete permission denied, record does not exist' ) );
}
//Debug::Arr($lf, 'AData: ', __FILE__, __LINE__, __METHOD__, 10);
$is_valid = $primary_validator->isValid();
if ( $is_valid == true ) { //Check to see if all permission checks passed before trying to save data.
Debug::Text( 'Attempting to delete record...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->setDeleted( true );
$is_valid = $lf->isValid();
if ( $is_valid == true ) {
Debug::Text( 'Record Deleted...', __FILE__, __LINE__, __METHOD__, 10 );
$save_result[$key] = $lf->Save();
$validator_stats['valid_records']++;
}
}
if ( $is_valid == false ) {
Debug::Text( 'Data is Invalid...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->FailTransaction(); //Just rollback this single record, continue on to the rest.
$validator[$key] = $this->setValidationArray( [ $primary_validator, $lf ] );
}
$lf->CommitTransaction();
$this->getProgressBarObject()->set( $this->getAPIMessageID(), $key );
}
$this->getProgressBarObject()->stop( $this->getAPIMessageID() );
return $this->handleRecordValidationResults( $validator, $validator_stats, $key, $save_result );
}
return $this->returnHandler( false );
}
/**
* Copy one or more UserDefaultes.
* @param array $data UserDefault IDs
* @return array
*/
function copyUserDefault( $data ) {
if ( !is_array( $data ) ) {
$data = [ $data ];
}
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
Debug::Text( 'Received data for: ' . count( $data ) . ' UserDefaults', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$src_rows = $this->stripReturnHandler( $this->getUserDefault( [ 'filter_data' => [ 'id' => $data ] ], true ) );
if ( is_array( $src_rows ) && count( $src_rows ) > 0 ) {
Debug::Arr( $src_rows, 'SRC Rows: ', __FILE__, __LINE__, __METHOD__, 10 );
foreach ( $src_rows as $key => $row ) {
unset( $src_rows[$key]['id'], $src_rows[$key]['manual_id'] ); //Clear fields that can't be copied
$src_rows[$key]['name'] = Misc::generateCopyName( $row['name'] ); //Generate unique name
}
//Debug::Arr($src_rows, 'bSRC Rows: ', __FILE__, __LINE__, __METHOD__, 10);
return $this->setUserDefault( $src_rows ); //Save copied rows
}
return $this->returnHandler( false );
}
}
?>

View File

@@ -0,0 +1,240 @@
<?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 API\Users
*/
class APIUserDefaultPreferenceNotification extends APIFactory {
protected $main_class = 'UserDefaultPreferenceNotificationFactory';
/**
* APIUserDefaultPreferenceNotification constructor.
*/
public function __construct() {
parent::__construct(); //Make sure parent constructor is always called.
return true;
}
/**
* Get default UserDefaultPreferenceNotification data for creating new UserDefaultPreferenceNotificationes.
* @return array
*/
function getUserDefaultPreferenceNotificationDefaultData() {
Debug::Text( 'Getting DefaultPreferenceNotification default data...', __FILE__, __LINE__, __METHOD__, 10 );
$upnf = TTnew( 'UserPreferenceNotificationFactory' ); /** @var UserPreferenceNotificationFactory $upnf */
$data = $upnf->getUserPreferenceNotificationTypeDefaultValues( [ 'system' ] );
return $this->returnHandler( $data );
}
/**
* Get UserDefaultPreferenceNotification data for one or more UserDefaultPreferenceNotificationes.
* @param array $data filter data
* @param bool $disable_paging
* @return array|bool
*/
function getUserDefaultPreferenceNotification( $data = null, $disable_paging = false ) {
$data = $this->initializeFilterAndPager( $data, $disable_paging );
if ( !$this->getPermissionObject()->Check( 'user', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'user', 'edit' ) ) ) {
return $this->getPermissionObject()->PermissionDenied();
}
if ( isset( $data['filter_data']['user_default_id'] ) && TTUUID::isUUID( $data['filter_data']['user_default_id'] ) ) {
$udpnlf = TTnew( 'UserDefaultPreferenceNotificationListFactory' ); /** @var UserDefaultPreferenceNotificationListFactory $udpnlf */
$udpnlf->getByUserDefaultId( $data['filter_data']['user_default_id'] );
Debug::Text( 'Record Count: ' . $udpnlf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10 );
if ( $udpnlf->getRecordCount() > 0 ) {
$this->getProgressBarObject()->start( $this->getAPIMessageID(), $udpnlf->getRecordCount() );
$this->setPagerObject( $udpnlf );
$retarr = [];
foreach ( $udpnlf as $udpn ) {
$retarr[] = $udpn->getObjectAsArray( $data['filter_columns'] );
$this->getProgressBarObject()->set( $this->getAPIMessageID(), $udpnlf->getCurrentRow() );
}
//If there is no record for the preference notification type, add with default values set.
$all_notification_preferences = $this->stripReturnHandler( $this->getUserDefaultPreferenceNotificationDefaultData() );
foreach ( $all_notification_preferences as $preference ) {
if ( !in_array( $preference['type_id'], array_column( $retarr, 'type_id' ) ) ) {
$retarr[] = $preference;
};
}
$this->getProgressBarObject()->stop( $this->getAPIMessageID() );
return $this->returnHandler( $retarr );
}
}
// No user default notification preferences found, returning default values.
return $this->getUserDefaultPreferenceNotificationDefaultData();
}
/**
* Validate UserDefaultPreferenceNotification data for one or more UserDefaultPreferenceNotificationes.
* @param array $data UserDefaultPreferenceNotification data
* @return array
*/
function validateUserDefaultPreferenceNotification( $data ) {
return $this->setUserDefaultPreferenceNotification( $data, true );
}
/**
* Set UserDefaultPreferenceNotification data for one or more UserDefaultPreferenceNotificationes.
* @param array $data UserDefaultPreferenceNotification data
* @param bool $validate_only
* @param bool $ignore_warning
* @return array|bool
*/
function setUserDefaultPreferenceNotification( $data, $validate_only = false, $ignore_warning = true ) {
$validate_only = (bool)$validate_only;
$ignore_warning = (bool)$ignore_warning;
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
if ( !$this->getPermissionObject()->Check( 'user', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'user', 'edit' ) || $this->getPermissionObject()->Check( 'user', 'add' ) ) ) {
return $this->getPermissionObject()->PermissionDenied();
}
if ( $validate_only == true ) {
Debug::Text( 'Validating Only!', __FILE__, __LINE__, __METHOD__, 10 );
}
[ $data, $total_records ] = $this->convertToMultipleRecords( $data );
Debug::Text( 'Received data for: ' . $total_records . ' UserDefaultPreferenceNotifications', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$validator_stats = [ 'total_records' => $total_records, 'valid_records' => 0 ];
$validator = $save_result = []; $key = false;
if ( is_array( $data ) && $total_records > 0 ) {
$this->getProgressBarObject()->start( $this->getAPIMessageID(), $total_records );
foreach ( $data as $key => $row ) {
$primary_validator = new Validator();
$lf = TTnew( 'UserDefaultPreferenceNotificationListFactory' ); /** @var UserDefaultPreferenceNotificationListFactory $lf */
$lf->StartTransaction();
if ( isset( $row['id'] ) && $row['id'] != '' && $row['id'] != -1 && $row['id'] != TTUUID::getNotExistID() ) {
//Modifying existing object.
//Get UserDefaultPreferenceNotification object, so we can only modify just changed data for specific records if needed.
$lf->getByIdAndCompanyId( $row['id'], $this->getCurrentCompanyObject()->getId() );
if ( $lf->getRecordCount() == 1 ) {
//Object exists, check edit permissions
if (
$validate_only == true
||
(
$this->getPermissionObject()->Check( 'user', 'edit' )
) ) {
Debug::Text( 'Row Exists, getting current data for ID: ' . $row['id'], __FILE__, __LINE__, __METHOD__, 10 );
$lf = $lf->getCurrent();
$row = array_merge( $lf->getObjectAsArray(), $row );
} else {
$primary_validator->isTrue( 'permission', false, TTi18n::gettext( 'Edit permission denied' ) );
}
} else {
//Object doesn't exist.
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Edit permission denied, record does not exist' ) );
}
} else {
unset( $row['id'] ); //ID could be '-1', so simply unset it so it doesn't try to update a non-existing record.
//Adding new object, check ADD permissions.
$primary_validator->isTrue( 'permission', $this->getPermissionObject()->Check( 'user', 'add' ), TTi18n::gettext( 'Add permission denied' ) );
}
Debug::Arr( $row, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$is_valid = $primary_validator->isValid( $ignore_warning );
if ( $is_valid == true ) { //Check to see if all permission checks passed before trying to save data.
Debug::Text( 'Setting object data...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->setObjectFromArray( $row );
$is_valid = $lf->isValid( $ignore_warning );
if ( $is_valid == true ) {
Debug::Text( 'Saving data...', __FILE__, __LINE__, __METHOD__, 10 );
if ( $validate_only == true ) {
$save_result[$key] = true;
} else {
$save_result[$key] = $lf->Save();
}
$validator_stats['valid_records']++;
}
}
if ( $is_valid == false ) {
Debug::Text( 'Data is Invalid...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->FailTransaction(); //Just rollback this single record, continue on to the rest.
$validator[$key] = $this->setValidationArray( [ $primary_validator, $lf ] );
} else if ( $validate_only == true ) {
$lf->FailTransaction();
}
$lf->CommitTransaction();
$this->getProgressBarObject()->set( $this->getAPIMessageID(), $key );
}
$this->getProgressBarObject()->stop( $this->getAPIMessageID() );
return $this->handleRecordValidationResults( $validator, $validator_stats, $key, $save_result );
}
return $this->returnHandler( false );
}
}
?>

View File

@@ -0,0 +1,253 @@
<?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 API\Users
*/
class APIUserGenericData extends APIFactory {
protected $main_class = 'UserGenericDataFactory';
/**
* APIUserGenericData constructor.
*/
public function __construct() {
parent::__construct(); //Make sure parent constructor is always called.
return true;
}
/**
* Get user data for one or more users.
* Default to disable paging as it would rarely be used for this.
* @param array $data filter data
* @param bool $disable_paging
* @return array
*/
function getUserGenericData( $data = null, $disable_paging = true ) {
$data = $this->initializeFilterAndPager( $data, $disable_paging );
//Only allow getting generic data for currently logged in user unless user_id = 0, then get company wide data.
//$data['filter_data']['user_id'] = $this->getCurrentUserObject()->getId();
if ( !isset( $data['filter_data']['user_id'] ) || ( isset( $data['filter_data']['user_id'] ) && $data['filter_data']['user_id'] != TTUUID::getZeroID() ) ) {
Debug::Text( 'Forcing User ID to current user: ' . $this->getCurrentUserObject()->getId(), __FILE__, __LINE__, __METHOD__, 10 );
$data['filter_data']['user_id'] = $this->getCurrentUserObject()->getId();
} else {
Debug::Text( 'Company wide data...', __FILE__, __LINE__, __METHOD__, 10 );
$data['filter_data']['user_id'] = TTUUID::getZeroID(); //Company wide data.
}
Debug::Arr( $data, 'Getting User Generic Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$ugdlf = TTnew( 'UserGenericDataListFactory' ); /** @var UserGenericDataListFactory $ugdlf */
$ugdlf->getAPISearchByCompanyIdAndArrayCriteria( $this->getCurrentCompanyObject()->getId(), $data['filter_data'], $data['filter_items_per_page'], $data['filter_page'], null, $data['filter_sort'] );
Debug::Text( 'Record Count: ' . $ugdlf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10 );
if ( $ugdlf->getRecordCount() > 0 ) {
$this->setPagerObject( $ugdlf );
$retarr = [];
foreach ( $ugdlf as $ugd_obj ) {
$retarr[] = $ugd_obj->getObjectAsArray( $data['filter_columns'] );
}
return $this->returnHandler( $retarr );
}
return $this->returnHandler( true );
}
/**
* Set user data for one or more users.
* @param array $data user data
* @param bool $ignore_warning
* @return array
*/
function setUserGenericData( $data, $ignore_warning = true ) {
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
[ $data, $total_records ] = $this->convertToMultipleRecords( $data );
Debug::Text( 'Received data for: ' . $total_records . ' Users', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$validator_stats = [ 'total_records' => $total_records, 'valid_records' => 0 ];
$validator = $save_result = []; $key = false;
if ( is_array( $data ) && $total_records > 0 ) {
foreach ( $data as $key => $row ) {
$row['company_id'] = $this->getCurrentUserObject()->getCompany();
if ( !isset( $row['user_id'] ) || ( isset( $row['user_id'] ) && $row['user_id'] != '' && $row['user_id'] != TTUUID::getZeroId() ) ) {
Debug::Text( 'Forcing User ID to current user: ' . $this->getCurrentUserObject()->getId(), __FILE__, __LINE__, __METHOD__, 10 );
$row['user_id'] = $this->getCurrentUserObject()->getId();
} else {
Debug::Text( 'Company wide data...', __FILE__, __LINE__, __METHOD__, 10 );
$row['user_id'] = TTUUID::getZeroId(); //Company wide data.
}
$primary_validator = new Validator();
$lf = TTnew( 'UserGenericDataListFactory' ); /** @var UserGenericDataListFactory $lf */
$lf->StartTransaction();
if ( isset( $row['id'] ) ) {
//Modifying existing object.
//Get object, so we can only modify just changed data for specific records if needed.
//$lf->getByUserIdAndId( $row['user_id'], $row['id'] );
$lf->getByCompanyIdAndUserIdAndId( $row['company_id'], $row['user_id'], $row['id'] );
if ( $lf->getRecordCount() == 1 ) {
//Object exists, check edit permissions
$lf = $lf->getCurrent(); //Make the current $lf variable the current object, otherwise getDataDifferences() fails to function.
$row = array_merge( $lf->getObjectAsArray(), $row );
} else {
//Object doesn't exist.
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Edit permission denied, employee does not exist' ) );
}
} //else {
//Adding new object, check ADD permissions.
//$primary_validator->isTrue( 'permission', $this->getPermissionObject()->Check('user', 'add'), TTi18n::gettext('Add permission denied') );
//}
Debug::Arr( $row, 'User Generic Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$is_valid = $primary_validator->isValid();
if ( $is_valid == true ) { //Check to see if all permission checks passed before trying to save data.
Debug::Text( 'Attempting to save User Data...', __FILE__, __LINE__, __METHOD__, 10 );
//Force Company ID to current company.
$row['company_id'] = $this->getCurrentCompanyObject()->getId();
$lf->setObjectFromArray( $row );
$is_valid = $lf->isValid( $ignore_warning );
if ( $is_valid == true ) {
Debug::Text( 'Saving User Data...', __FILE__, __LINE__, __METHOD__, 10 );
$save_result[$key] = $lf->Save();
$validator_stats['valid_records']++;
}
}
if ( $is_valid == false ) {
Debug::Text( 'User Data is Invalid...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->FailTransaction(); //Just rollback this single record, continue on to the rest.
$validator[$key] = $this->setValidationArray( [ $primary_validator, $lf ] );
}
$lf->CommitTransaction();
}
return $this->handleRecordValidationResults( $validator, $validator_stats, $key, $save_result );
}
return $this->returnHandler( false );
}
/**
* Delete one or more users.
* @param array $data user data
* @return array
*/
function deleteUserGenericData( $data ) {
if ( !is_array( $data ) ) {
$data = [ $data ];
}
Debug::Text( 'Received data for: ' . count( $data ) . ' Users', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$total_records = count( $data );
$validator = $save_result = []; $key = false;
$validator_stats = [ 'total_records' => $total_records, 'valid_records' => 0 ];
if ( is_array( $data ) && $total_records > 0 ) {
foreach ( $data as $key => $id ) {
$primary_validator = new Validator();
$lf = TTnew( 'UserGenericDataListFactory' ); /** @var UserGenericDataListFactory $lf */
$lf->StartTransaction();
if ( $id != '' ) {
//Modifying existing object.
//Get user object, so we can only modify just changed data for specific records if needed.
$lf->getByUserIdAndId( $this->getCurrentUserObject()->getId(), $id );
if ( $lf->getRecordCount() == 1 ) {
//Object exists
Debug::Text( 'User Generic Data Exists, getting current data for ID: ' . $id, __FILE__, __LINE__, __METHOD__, 10 );
$lf = $lf->getCurrent();
} else {
//Object doesn't exist.
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Delete permission denied, generic data does not exist' ) );
}
} else {
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Delete permission denied, generic data does not exist' ) );
}
//Debug::Arr($lf, 'AData: ', __FILE__, __LINE__, __METHOD__, 10);
$is_valid = $primary_validator->isValid();
if ( $is_valid == true ) { //Check to see if all permission checks passed before trying to save data.
Debug::Text( 'Attempting to delete user generic data...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->setDeleted( true );
$is_valid = $lf->isValid();
if ( $is_valid == true ) {
Debug::Text( 'User Deleted...', __FILE__, __LINE__, __METHOD__, 10 );
$save_result[$key] = $lf->Save();
$validator_stats['valid_records']++;
}
}
if ( $is_valid == false ) {
Debug::Text( 'User Generic Data is Invalid...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->FailTransaction(); //Just rollback this single record, continue on to the rest.
$validator[$key] = $this->setValidationArray( [ $primary_validator, $lf ] );
}
$lf->CommitTransaction();
}
return $this->handleRecordValidationResults( $validator, $validator_stats, $key, $save_result );
}
return $this->returnHandler( false );
}
}
?>

View File

@@ -0,0 +1,224 @@
<?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 API\Users
*/
class APIUserGenericStatus extends APIFactory {
protected $main_class = 'UserGenericStatusFactory';
/**
* APIUserGenericStatus constructor.
*/
public function __construct() {
parent::__construct(); //Make sure parent constructor is always called.
return true;
}
/**
* Get user generic status data for one or more .
* @param array $data filter data
* @param bool $disable_paging
* @return array
*/
function getUserGenericStatus( $data = null, $disable_paging = false ) {
$data = $this->initializeFilterAndPager( $data, $disable_paging );
$user_id = $this->getCurrentUserObject()->getId();
if ( $data['filter_data']['batch_id'] != '' ) {
$batch_id = $data['filter_data']['batch_id'];
$ugslf = TTnew( 'UserGenericStatusListFactory' ); /** @var UserGenericStatusListFactory $ugslf */
$ugslf->getByUserIdAndBatchId( $user_id, $batch_id, $data['filter_items_per_page'], $data['filter_page'], null, $data['filter_sort'] );
Debug::Text( 'Record Count: ' . $ugslf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10 );
if ( $ugslf->getRecordCount() > 0 ) {
$this->getProgressBarObject()->start( $this->getAPIMessageID(), $ugslf->getRecordCount() );
$this->setPagerObject( $ugslf );
$rows = [];
foreach ( $ugslf as $ugs_obj ) {
$rows[] = [
'id' => $ugs_obj->getId(),
'user_id' => $ugs_obj->getUser(),
'batch_id' => $ugs_obj->getBatchId(),
'status_id' => $ugs_obj->getStatus(),
'status' => Option::getByKey( $ugs_obj->getStatus(), $ugs_obj->getOptions( 'status' ) ),
'label' => $ugs_obj->getLabel(),
'description' => $ugs_obj->getDescription(),
'link' => $ugs_obj->getLink(),
'deleted' => $ugs_obj->getDeleted(),
];
$this->getProgressBarObject()->set( $this->getAPIMessageID(), $ugslf->getCurrentRow() );
}
$this->getProgressBarObject()->stop( $this->getAPIMessageID() );
return $this->returnHandler( $rows );
} else {
return $this->returnHandler( true ); //No records returned.
}
} else {
return $this->returnHandler( true ); //No records returned.
}
}
/**
* Delete one or more user generic status data.
* @param array $data
* @return array
*/
function deleteUserGenericStatus( $data ) {
if ( !is_array( $data ) ) {
$data = [ $data ];
}
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
Debug::Text( 'Received data for: ' . count( $data ) . ' User Generic Status', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$total_records = count( $data );
$validator = $save_result = []; $key = false;
$validator_stats = [ 'total_records' => $total_records, 'valid_records' => 0 ];
if ( is_array( $data ) && $total_records > 0 ) {
$this->getProgressBarObject()->start( $this->getAPIMessageID(), $total_records );
foreach ( $data as $key => $id ) {
$primary_validator = new Validator();
$lf = TTnew( 'UserGenericStatusListFactory' ); /** @var UserGenericStatusListFactory $lf */
$lf->StartTransaction();
if ( $id != '' ) {
//Modifying existing object.
//Get branch object, so we can only modify just changed data for specific records if needed.
$lf->getByIdAndCompanyId( $id, $this->getCurrentCompanyObject()->getId() );
if ( $lf->getRecordCount() == 1 ) {
//Object exists, check edit permissions
if ( $this->getPermissionObject()->Check( 'user', 'delete' )
|| ( $this->getPermissionObject()->Check( 'user', 'delete_own' ) && $this->getPermissionObject()->isOwner( $lf->getCurrent()->getCreatedBy(), $lf->getCurrent()->getID() ) === true ) ) {
Debug::Text( 'Record Exists, deleting record ID: ' . $id, __FILE__, __LINE__, __METHOD__, 10 );
$lf = $lf->getCurrent();
} else {
$primary_validator->isTrue( 'permission', false, TTi18n::gettext( 'Delete permission denied' ) );
}
} else {
//Object doesn't exist.
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Delete permission denied, record does not exist' ) );
}
} else {
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Delete permission denied, record does not exist' ) );
}
//Debug::Arr($lf, 'AData: ', __FILE__, __LINE__, __METHOD__, 10);
$is_valid = $primary_validator->isValid();
if ( $is_valid == true ) { //Check to see if all permission checks passed before trying to save data.
Debug::Text( 'Attempting to delete record...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->setDeleted( true );
$is_valid = $lf->isValid();
if ( $is_valid == true ) {
Debug::Text( 'Record Deleted...', __FILE__, __LINE__, __METHOD__, 10 );
$save_result[$key] = $lf->Save();
$validator_stats['valid_records']++;
}
}
if ( $is_valid == false ) {
Debug::Text( 'Data is Invalid...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->FailTransaction(); //Just rollback this single record, continue on to the rest.
$validator[$key] = $this->setValidationArray( [ $primary_validator, $lf ] );
}
$lf->CommitTransaction();
$this->getProgressBarObject()->set( $this->getAPIMessageID(), $key );
}
$this->getProgressBarObject()->stop( $this->getAPIMessageID() );
return $this->handleRecordValidationResults( $validator, $validator_stats, $key, $save_result );
}
return $this->returnHandler( false );
}
/**
* @param string $user_id UUID
* @param string $batch_id UUID
* @return array|bool
*/
function getUserGenericStatusCountArray( $user_id, $batch_id ) {
$user_id = $this->getCurrentUserObject()->getId();
if ( $batch_id != '' ) {
$ugslf = TTnew( 'UserGenericStatusListFactory' ); /** @var UserGenericStatusListFactory $ugslf */
$status_count_arr = $ugslf->getStatusCountArrayByUserIdAndBatchId( $user_id, $batch_id );
return $this->returnHandler( $status_count_arr );
}
return $this->returnHandler( false );
}
/**
* Export data to csv
* @param string $format file format (csv)
* @param array $data filter data
* @param bool $disable_paging
* @return array
*/
function exportUserGenericStatus( $format = 'csv', $data = null, $disable_paging = true ) {
$result = $this->stripReturnHandler( $this->getUserGenericStatus( $data, $disable_paging ) );
return $this->exportRecords( $format, 'export_status_report', $result, ( ( isset( $data['filter_columns'] ) ) ? $data['filter_columns'] : null ) );
}
}
?>

View File

@@ -0,0 +1,398 @@
<?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 API\Users
*/
class APIUserGroup extends APIFactory {
protected $main_class = 'UserGroupFactory';
/**
* APIUserGroup constructor.
*/
public function __construct() {
parent::__construct(); //Make sure parent constructor is always called.
return true;
}
/**
* Get options for dropdown boxes.
* @param bool|string $name Name of options to return, ie: 'columns', 'type', 'status'
* @param mixed $parent Parent name/ID of options to return if data is in hierarchical format. (ie: Province)
* @return bool|array
*/
function getOptions( $name = false, $parent = null ) {
if ( $name == 'columns'
&& ( !$this->getPermissionObject()->Check( 'user_group', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'user_group', 'view' ) || $this->getPermissionObject()->Check( 'user_group', 'view_own' ) || $this->getPermissionObject()->Check( 'user_group', 'view_child' ) ) ) ) {
$name = 'list_columns';
}
return parent::getOptions( $name, $parent );
}
/**
* Get default UserGroup data for creating new UserGroupes.
* @return array
*/
function getUserGroupDefaultData() {
$company_obj = $this->getCurrentCompanyObject();
Debug::Text( 'Getting UserGroup default data...', __FILE__, __LINE__, __METHOD__, 10 );
$data = [
'company_id' => $company_obj->getId(),
'parent_id' => 0,
'name' => null,
];
return $this->returnHandler( $data );
}
/**
* Get UserGroup data for one or more UserGroupes.
* @param array $data filter data
* @param bool $disable_paging
* @param string $mode
* @return array
*/
function getUserGroup( $data = null, $disable_paging = false, $mode = 'flat' ) {
$data = $this->initializeFilterAndPager( $data, $disable_paging );
if ( !$this->getPermissionObject()->Check( 'user_group', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'user_group', 'view' ) || $this->getPermissionObject()->Check( 'user_group', 'view_own' ) || $this->getPermissionObject()->Check( 'user_group', 'view_child' ) ) ) {
//return $this->getPermissionObject()->PermissionDenied();
$data['filter_columns'] = $this->handlePermissionFilterColumns( ( isset( $data['filter_columns'] ) ) ? $data['filter_columns'] : null, Misc::trimSortPrefix( $this->getOptions( 'list_columns' ) ) );
}
//Allow supervisor (subordinates only) to see all groups.
//$data['filter_data']['permission_children_ids'] = $this->getPermissionObject()->getPermissionChildren( 'user', 'view' );
//Allow getting users from other companies, so we can change admin contacts when using the master company.
if ( isset( $data['filter_data']['company_id'] )
&& TTUUID::isUUID( $data['filter_data']['company_id'] ) && $data['filter_data']['company_id'] != TTUUID::getZeroID() && $data['filter_data']['company_id'] != TTUUID::getNotExistID()
&& ( $this->getPermissionObject()->Check( 'company', 'enabled' ) && $this->getPermissionObject()->Check( 'company', 'view' ) ) ) {
$company_id = $data['filter_data']['company_id'];
} else {
$company_id = $this->getCurrentCompanyObject()->getId();
}
$uglf = TTnew( 'UserGroupListFactory' ); /** @var UserGroupListFactory $uglf */
if ( $mode == 'flat' ) {
$uglf->getAPISearchByCompanyIdAndArrayCriteria( $company_id, $data['filter_data'], $data['filter_items_per_page'], $data['filter_page'], null, $data['filter_sort'] );
Debug::Text( 'Record Count: ' . $uglf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10 );
if ( $uglf->getRecordCount() > 0 ) {
$this->getProgressBarObject()->start( $this->getAPIMessageID(), $uglf->getRecordCount() );
$this->setPagerObject( $uglf );
$retarr = [];
foreach ( $uglf as $ug_obj ) {
$retarr[] = $ug_obj->getObjectAsArray( $data['filter_columns'] );
$this->getProgressBarObject()->set( $this->getAPIMessageID(), $uglf->getCurrentRow() );
}
$this->getProgressBarObject()->stop( $this->getAPIMessageID() );
return $this->returnHandler( $retarr );
}
} else {
$nodes = $uglf->getByCompanyIdArray( $company_id );
//Debug::Arr($nodes, ' Nodes: ', __FILE__, __LINE__, __METHOD__, 10);
//Debug::Text('Record Count: '. count($nodes), __FILE__, __LINE__, __METHOD__, 10);
if ( isset( $nodes ) ) {
$retarr = TTTree::FormatArray( $nodes );
//Debug::Arr($retarr, ' Data: ', __FILE__, __LINE__, __METHOD__, 10);
return $this->returnHandler( $retarr );
}
}
return $this->returnHandler( true ); //No records returned.
}
/**
* Get only the fields that are common across all records in the search criteria. Used for Mass Editing of records.
* @param array $data filter data
* @return array
*/
function getCommonUserGroupData( $data ) {
return Misc::arrayIntersectByRow( $this->stripReturnHandler( $this->getUserGroup( $data, true ) ) );
}
/**
* Validate UserGroup data for one or more UserGroupes.
* @param array $data UserGroup data
* @return array
*/
function validateUserGroup( $data ) {
return $this->setUserGroup( $data, true );
}
/**
* Set UserGroup data for one or more UserGroupes.
* @param array $data UserGroup data
* @param bool $validate_only
* @param bool $ignore_warning
* @return array|bool
*/
function setUserGroup( $data, $validate_only = false, $ignore_warning = true ) {
$validate_only = (bool)$validate_only;
$ignore_warning = (bool)$ignore_warning;
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
if ( !$this->getPermissionObject()->Check( 'user_group', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'user_group', 'edit' ) || $this->getPermissionObject()->Check( 'user_group', 'edit_own' ) || $this->getPermissionObject()->Check( 'user_group', 'edit_child' ) || $this->getPermissionObject()->Check( 'user_group', 'add' ) ) ) {
return $this->getPermissionObject()->PermissionDenied();
}
if ( $validate_only == true ) {
Debug::Text( 'Validating Only!', __FILE__, __LINE__, __METHOD__, 10 );
}
[ $data, $total_records ] = $this->convertToMultipleRecords( $data );
Debug::Text( 'Received data for: ' . $total_records . ' UserGroups', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$validator_stats = [ 'total_records' => $total_records, 'valid_records' => 0 ];
$validator = $save_result = []; $key = false;
if ( is_array( $data ) && $total_records > 0 ) {
foreach ( $data as $key => $row ) {
$primary_validator = new Validator();
$lf = TTnew( 'UserGroupListFactory' ); /** @var UserGroupListFactory $lf */
$lf->StartTransaction();
if ( isset( $row['id'] ) && $row['id'] != '' ) {
//Modifying existing object.
//Get UserGroup object, so we can only modify just changed data for specific records if needed.
$lf->getByIdAndCompanyId( $row['id'], $this->getCurrentCompanyObject()->getId() );
if ( $lf->getRecordCount() == 1 ) {
//Object exists, check edit permissions
if (
$validate_only == true
||
(
$this->getPermissionObject()->Check( 'user_group', 'edit' )
|| ( $this->getPermissionObject()->Check( 'user_group', 'edit_own' ) && $this->getPermissionObject()->isOwner( $lf->getCurrent()->getCreatedBy(), $lf->getCurrent()->getID() ) === true )
) ) {
Debug::Text( 'Row Exists, getting current data for ID: ' . $row['id'], __FILE__, __LINE__, __METHOD__, 10 );
$lf = $lf->getCurrent();
$row = array_merge( $lf->getObjectAsArray(), $row );
} else {
$primary_validator->isTrue( 'permission', false, TTi18n::gettext( 'Edit permission denied' ) );
}
} else {
//Object doesn't exist.
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Edit permission denied, record does not exist' ) );
}
} else {
//Adding new object, check ADD permissions.
$primary_validator->isTrue( 'permission', $this->getPermissionObject()->Check( 'user_group', 'add' ), TTi18n::gettext( 'Add permission denied' ) );
}
Debug::Arr( $row, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$is_valid = $primary_validator->isValid();
if ( $is_valid == true ) { //Check to see if all permission checks passed before trying to save data.
Debug::Text( 'Setting object data...', __FILE__, __LINE__, __METHOD__, 10 );
//Force Company ID to current company.
$row['company_id'] = $this->getCurrentCompanyObject()->getId();
$lf->setObjectFromArray( $row );
$is_valid = $lf->isValid( $ignore_warning );
if ( $is_valid == true ) {
Debug::Text( 'Saving data...', __FILE__, __LINE__, __METHOD__, 10 );
if ( $validate_only == true ) {
$save_result[$key] = true;
} else {
$save_result[$key] = $lf->Save();
}
$validator_stats['valid_records']++;
}
}
if ( $is_valid == false ) {
Debug::Text( 'Data is Invalid...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->FailTransaction(); //Just rollback this single record, continue on to the rest.
$validator[$key] = $this->setValidationArray( [ $primary_validator, $lf ] );
} else if ( $validate_only == true ) {
$lf->FailTransaction();
}
$lf->CommitTransaction();
}
return $this->handleRecordValidationResults( $validator, $validator_stats, $key, $save_result );
}
return $this->returnHandler( false );
}
/**
* Delete one or more UserGroups.
* @param array $data UserGroup data
* @return array|bool
*/
function deleteUserGroup( $data ) {
if ( !is_array( $data ) ) {
$data = [ $data ];
}
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
if ( !$this->getPermissionObject()->Check( 'user_group', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'user_group', 'delete' ) || $this->getPermissionObject()->Check( 'user_group', 'delete_own' ) || $this->getPermissionObject()->Check( 'user_group', 'delete_child' ) ) ) {
return $this->getPermissionObject()->PermissionDenied();
}
Debug::Text( 'Received data for: ' . count( $data ) . ' UserGroups', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$total_records = count( $data );
$validator = $save_result = []; $key = false;
$validator_stats = [ 'total_records' => $total_records, 'valid_records' => 0 ];
if ( is_array( $data ) && $total_records > 0 ) {
foreach ( $data as $key => $id ) {
$primary_validator = new Validator();
$lf = TTnew( 'UserGroupListFactory' ); /** @var UserGroupListFactory $lf */
$lf->StartTransaction();
if ( $id != '' ) {
//Modifying existing object.
//Get UserGroup object, so we can only modify just changed data for specific records if needed.
$lf->getByIdAndCompanyId( $id, $this->getCurrentCompanyObject()->getId() );
if ( $lf->getRecordCount() == 1 ) {
//Object exists, check edit permissions
if ( $this->getPermissionObject()->Check( 'user_group', 'delete' )
|| ( $this->getPermissionObject()->Check( 'user_group', 'delete_own' ) && $this->getPermissionObject()->isOwner( $lf->getCurrent()->getCreatedBy(), $lf->getCurrent()->getID() ) === true ) ) {
Debug::Text( 'Record Exists, deleting record ID: ' . $id, __FILE__, __LINE__, __METHOD__, 10 );
$lf = $lf->getCurrent();
} else {
$primary_validator->isTrue( 'permission', false, TTi18n::gettext( 'Delete permission denied' ) );
}
} else {
//Object doesn't exist.
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Delete permission denied, record does not exist' ) );
}
} else {
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Delete permission denied, record does not exist' ) );
}
//Debug::Arr($lf, 'AData: ', __FILE__, __LINE__, __METHOD__, 10);
$is_valid = $primary_validator->isValid();
if ( $is_valid == true ) { //Check to see if all permission checks passed before trying to save data.
Debug::Text( 'Attempting to delete record...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->setDeleted( true );
$is_valid = $lf->isValid();
if ( $is_valid == true ) {
Debug::Text( 'Record Deleted...', __FILE__, __LINE__, __METHOD__, 10 );
$save_result[$key] = $lf->Save();
$validator_stats['valid_records']++;
}
}
if ( $is_valid == false ) {
Debug::Text( 'Data is Invalid...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->FailTransaction(); //Just rollback this single record, continue on to the rest.
$validator[$key] = $this->setValidationArray( [ $primary_validator, $lf ] );
}
$lf->CommitTransaction();
}
return $this->handleRecordValidationResults( $validator, $validator_stats, $key, $save_result );
}
return $this->returnHandler( false );
}
/**
* Change parent of one or more groups to another group.
* @param array $src_id source Group ID
* @param int $dst_id destination Group ID
* @return array
*/
function dragNdropUserGroup( $src_id, $dst_id ) {
if ( !is_array( $src_id ) ) {
$src_id = [ $src_id ];
}
if ( is_array( $dst_id ) ) {
return $this->returnHandler( false );
}
Debug::Arr( $src_id, 'Src ID: Data: ', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $dst_id, 'Dst ID: Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$src_rows = $this->stripReturnHandler( $this->getUserGroup( [ 'filter_data' => [ 'id' => $src_id ] ], true, 'flat' ) );
if ( is_array( $src_rows ) && count( $src_rows ) > 0 ) {
Debug::Arr( $src_rows, 'SRC Rows: ', __FILE__, __LINE__, __METHOD__, 10 );
foreach ( $src_rows as $key => $row ) {
$src_rows[$key]['parent_id'] = $dst_id;
}
unset( $row ); //code standards
Debug::Arr( $src_rows, 'bSRC Rows: ', __FILE__, __LINE__, __METHOD__, 10 );
return $this->setUserGroup( $src_rows ); //Save copied rows
}
return $this->returnHandler( false );
}
}
?>

View File

@@ -0,0 +1,439 @@
<?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 API\Users
*/
class APIUserPreference extends APIFactory {
protected $main_class = 'UserPreferenceFactory';
/**
* APIUserPreference constructor.
*/
public function __construct() {
parent::__construct(); //Make sure parent constructor is always called.
return true;
}
/**
* Get default UserPreference data for creating new UserPreferencees.
* @return array
*/
function getUserPreferenceDefaultData() {
$company_id = $this->getCurrentCompanyObject()->getId();
Debug::Text( 'Getting UserPreference default data...', __FILE__, __LINE__, __METHOD__, 10 );
//Get New Hire Defaults.
$udlf = TTnew( 'UserDefaultListFactory' ); /** @var UserDefaultListFactory $udlf */
$udlf->getByCompanyId( $company_id );
if ( $udlf->getRecordCount() > 0 ) {
Debug::Text( 'Using User Defaults, as they exist...', __FILE__, __LINE__, __METHOD__, 10 );
$udf_obj = $udlf->getCurrent();
$data = [
'company_id' => $company_id,
'language' => $udf_obj->getLanguage(),
'date_format' => $udf_obj->getDateFormat(),
'time_format' => $udf_obj->getTimeFormat(),
'time_zone' => $udf_obj->getTimeZone(),
'time_unit_format' => $udf_obj->getTimeUnitFormat(),
'distance_format' => $udf_obj->getDistanceFormat(),
'items_per_page' => $udf_obj->getItemsPerPage(),
'start_week_day' => $udf_obj->getStartWeekDay(),
//'enable_email_notification_exception' => $udf_obj->getEnableEmailNotificationException(),
//'enable_email_notification_message' => $udf_obj->getEnableEmailNotificationMessage(),
//'enable_email_notification_home' => $udf_obj->getEnableEmailNotificationHome(),
//'enable_email_notification_pay_stub' => $udf_obj->getEnableEmailNotificationPayStub(),
//'enable_auto_context_menu' => true,
'enable_save_timesheet_state' => true,
'notification_duration' => 120,
'notification_status_id' => 0,
];
} else {
$data = [
'company_id' => $company_id,
'language' => 'en',
'time_unit_format' => 20, //Hours
'distance_format' => 10, // Kilometers
'items_per_page' => 25,
//'enable_email_notification_exception' => true,
//'enable_email_notification_message' => true,
//'enable_email_notification_home' => false,
//'enable_email_notification_pay_stub' => true,
//'enable_auto_context_menu' => true,
'enable_save_timesheet_state' => true,
'notification_duration' => 120,
'notification_status_id' => 0,
];
}
return $this->returnHandler( $data );
}
/**
* Get UserPreference data for one or more UserPreferencees.
* @param array $data filter data
* @param bool $disable_paging
* @return array|bool
*/
function getUserPreference( $data = null, $disable_paging = false ) {
$data = $this->initializeFilterAndPager( $data, $disable_paging );
if ( !$this->getPermissionObject()->Check( 'user_preference', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'user_preference', 'view' ) || $this->getPermissionObject()->Check( 'user_preference', 'view_own' ) || $this->getPermissionObject()->Check( 'user_preference', 'view_child' ) ) ) {
return $this->getPermissionObject()->PermissionDenied();
}
//Get Permission Hierarchy Children first, as this can be used for viewing, or editing.
$data['filter_data']['permission_children_ids'] = $this->getPermissionObject()->getPermissionChildren( 'user_preference', 'view' );
$uplf = TTnew( 'UserPreferenceListFactory' ); /** @var UserPreferenceListFactory $uplf */
$uplf->getAPISearchByCompanyIdAndArrayCriteria( $this->getCurrentCompanyObject()->getId(), $data['filter_data'], $data['filter_items_per_page'], $data['filter_page'], null, $data['filter_sort'] );
Debug::Text( 'Record Count: ' . $uplf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10 );
if ( $uplf->getRecordCount() > 0 ) {
$this->getProgressBarObject()->start( $this->getAPIMessageID(), $uplf->getRecordCount() );
$this->setPagerObject( $uplf );
$retarr = [];
foreach ( $uplf as $ut_obj ) {
$retarr[] = $ut_obj->getObjectAsArray( $data['filter_columns'], $data['filter_data']['permission_children_ids'] );
$this->getProgressBarObject()->set( $this->getAPIMessageID(), $uplf->getCurrentRow() );
}
$this->getProgressBarObject()->stop( $this->getAPIMessageID() );
return $this->returnHandler( $retarr );
}
return $this->returnHandler( true ); //No records returned.
}
/**
* Export data to csv
* @param string $format file format (csv)
* @param array $data filter data
* @param bool $disable_paging
* @return array
*/
function exportUserPreference( $format = 'csv', $data = null, $disable_paging = true ) {
$result = $this->stripReturnHandler( $this->getUserPreference( $data, $disable_paging ) );
return $this->exportRecords( $format, 'export_employee_preference', $result, ( ( isset( $data['filter_columns'] ) ) ? $data['filter_columns'] : null ) );
}
/**
* Get only the fields that are common across all records in the search criteria. Used for Mass Editing of records.
* @param array $data filter data
* @return array
*/
function getCommonUserPreferenceData( $data ) {
return Misc::arrayIntersectByRow( $this->stripReturnHandler( $this->getUserPreference( $data, true ) ) );
}
/**
* Validate UserPreference data for one or more UserPreferencees.
* @param array $data UserPreference data
* @return array
*/
function validateUserPreference( $data ) {
return $this->setUserPreference( $data, true );
}
/**
* Set UserPreference data for one or more UserPreferencees.
* @param array $data UserPreference data
* @param bool $validate_only
* @param bool $ignore_warning
* @return array|bool
*/
function setUserPreference( $data, $validate_only = false, $ignore_warning = true ) {
$validate_only = (bool)$validate_only;
$ignore_warning = (bool)$ignore_warning;
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
if ( !$this->getPermissionObject()->Check( 'user_preference', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'user_preference', 'edit' ) || $this->getPermissionObject()->Check( 'user_preference', 'edit_own' ) || $this->getPermissionObject()->Check( 'user_preference', 'edit_child' ) || $this->getPermissionObject()->Check( 'user_preference', 'add' ) ) ) {
return $this->getPermissionObject()->PermissionDenied();
}
if ( $validate_only == true ) {
Debug::Text( 'Validating Only!', __FILE__, __LINE__, __METHOD__, 10 );
}
[ $data, $total_records ] = $this->convertToMultipleRecords( $data );
Debug::Text( 'Received data for: ' . $total_records . ' UserPreferences', __FILE__, __LINE__, __METHOD__, 10 );
//Debug::Arr($data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10);
$validator_stats = [ 'total_records' => $total_records, 'valid_records' => 0 ];
$validator = $save_result = []; $key = false;
if ( is_array( $data ) && $total_records > 0 ) {
$this->getProgressBarObject()->start( $this->getAPIMessageID(), $total_records );
foreach ( $data as $key => $row ) {
$primary_validator = new Validator();
$lf = TTnew( 'UserPreferenceListFactory' ); /** @var UserPreferenceListFactory $lf */
$lf->StartTransaction();
if ( isset( $row['id'] ) && $row['id'] != '' ) {
//Modifying existing object.
//Get UserPreference object, so we can only modify just changed data for specific records if needed.
$lf->getByIdAndCompanyId( $row['id'], $this->getCurrentCompanyObject()->getId() );
if ( $lf->getRecordCount() == 1 ) {
//Object exists, check edit permissions
if (
$validate_only == true
||
(
$this->getPermissionObject()->Check( 'user_preference', 'edit' )
|| ( $this->getPermissionObject()->Check( 'user_preference', 'edit_own' ) && $this->getPermissionObject()->isOwner( $lf->getCurrent()->getCreatedBy(), $lf->getCurrent()->getUser() ) === true )
) ) {
Debug::Text( 'Row Exists, getting current data for ID: ' . $row['id'], __FILE__, __LINE__, __METHOD__, 10 );
$lf = $lf->getCurrent();
$row = array_merge( $lf->getObjectAsArray(), $row );
} else {
$primary_validator->isTrue( 'permission', false, TTi18n::gettext( 'Edit permission denied' ) );
}
} else {
//Object doesn't exist.
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Edit permission denied, record does not exist' ) );
}
} else {
//Always allow the currently logged in user to create preferences in case the record isn't there.
if ( !( isset( $row['user_id'] ) && $row['user_id'] == $this->getCurrentUserObject()->getId() ) ) {
//Adding new object, check ADD permissions.
$primary_validator->isTrue( 'permission', $this->getPermissionObject()->Check( 'user', 'add' ), TTi18n::gettext( 'Add permission denied' ) );
}
}
Debug::Arr( $row, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$is_valid = $primary_validator->isValid();
if ( $is_valid == true ) { //Check to see if all permission checks passed before trying to save data.
Debug::Text( 'Setting object data...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->setObjectFromArray( $row );
//Force Company ID to current company.
//$lf->setCompany( $this->getCurrentCompanyObject()->getId() );
$is_valid = $lf->isValid( $ignore_warning );
if ( $is_valid == true ) {
Debug::Text( 'Saving data...', __FILE__, __LINE__, __METHOD__, 10 );
if ( $validate_only == true ) {
$save_result[$key] = true;
} else {
$save_result[$key] = $lf->Save();
}
$validator_stats['valid_records']++;
}
}
if ( $is_valid == false ) {
Debug::Text( 'Data is Invalid...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->FailTransaction(); //Just rollback this single record, continue on to the rest.
$validator[$key] = $this->setValidationArray( [ $primary_validator, $lf ] );
} else if ( $validate_only == true ) {
$lf->FailTransaction();
}
$lf->CommitTransaction();
$this->getProgressBarObject()->set( $this->getAPIMessageID(), $key );
}
$this->getProgressBarObject()->stop( $this->getAPIMessageID() );
return $this->handleRecordValidationResults( $validator, $validator_stats, $key, $save_result );
}
return $this->returnHandler( false );
}
/**
* Delete one or more UserPreferences.
* @param array $data UserPreference data
* @return array|bool
*/
function deleteUserPreference( $data ) {
if ( !is_array( $data ) ) {
$data = [ $data ];
}
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
if ( !$this->getPermissionObject()->Check( 'user_preference', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'user_preference', 'delete' ) || $this->getPermissionObject()->Check( 'user_preference', 'delete_own' ) || $this->getPermissionObject()->Check( 'user_preference', 'delete_child' ) ) ) {
return $this->getPermissionObject()->PermissionDenied();
}
Debug::Text( 'Received data for: ' . count( $data ) . ' UserPreferences', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$total_records = count( $data );
$validator = $save_result = []; $key = false;
$validator_stats = [ 'total_records' => $total_records, 'valid_records' => 0 ];
if ( is_array( $data ) && $total_records > 0 ) {
$this->getProgressBarObject()->start( $this->getAPIMessageID(), $total_records );
foreach ( $data as $key => $id ) {
$primary_validator = new Validator();
$lf = TTnew( 'UserPreferenceListFactory' ); /** @var UserPreferenceListFactory $lf */
$lf->StartTransaction();
if ( $id != '' ) {
//Modifying existing object.
//Get UserPreference object, so we can only modify just changed data for specific records if needed.
$lf->getByIdAndCompanyId( $id, $this->getCurrentCompanyObject()->getId() );
if ( $lf->getRecordCount() == 1 ) {
//Object exists, check edit permissions
if ( $this->getPermissionObject()->Check( 'user_preference', 'delete' )
|| ( $this->getPermissionObject()->Check( 'user_preference', 'delete_own' ) && $this->getPermissionObject()->isOwner( $lf->getCurrent()->getCreatedBy(), $lf->getCurrent()->getID() ) === true ) ) {
Debug::Text( 'Record Exists, deleting record ID: ' . $id, __FILE__, __LINE__, __METHOD__, 10 );
$lf = $lf->getCurrent();
} else {
$primary_validator->isTrue( 'permission', false, TTi18n::gettext( 'Delete permission denied' ) );
}
} else {
//Object doesn't exist.
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Delete permission denied, record does not exist' ) );
}
} else {
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Delete permission denied, record does not exist' ) );
}
//Debug::Arr($lf, 'AData: ', __FILE__, __LINE__, __METHOD__, 10);
$is_valid = $primary_validator->isValid();
if ( $is_valid == true ) { //Check to see if all permission checks passed before trying to save data.
Debug::Text( 'Attempting to delete record...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->setDeleted( true );
$is_valid = $lf->isValid();
if ( $is_valid == true ) {
Debug::Text( 'Record Deleted...', __FILE__, __LINE__, __METHOD__, 10 );
$save_result[$key] = $lf->Save();
$validator_stats['valid_records']++;
}
}
if ( $is_valid == false ) {
Debug::Text( 'Data is Invalid...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->FailTransaction(); //Just rollback this single record, continue on to the rest.
$validator[$key] = $this->setValidationArray( [ $primary_validator, $lf ] );
}
$lf->CommitTransaction();
$this->getProgressBarObject()->set( $this->getAPIMessageID(), $key );
}
$this->getProgressBarObject()->stop( $this->getAPIMessageID() );
return $this->handleRecordValidationResults( $validator, $validator_stats, $key, $save_result );
}
return $this->returnHandler( false );
}
/**
* Copy one or more UserPreferencees.
* @param array $data UserPreference IDs
* @return array
*/
function copyUserPreference( $data ) {
if ( !is_array( $data ) ) {
$data = [ $data ];
}
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
Debug::Text( 'Received data for: ' . count( $data ) . ' UserPreferences', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$src_rows = $this->stripReturnHandler( $this->getUserPreference( [ 'filter_data' => [ 'id' => $data ] ], true ) );
if ( is_array( $src_rows ) && count( $src_rows ) > 0 ) {
Debug::Arr( $src_rows, 'SRC Rows: ', __FILE__, __LINE__, __METHOD__, 10 );
foreach ( $src_rows as $key => $row ) {
unset( $src_rows[$key]['id'], $src_rows[$key]['manual_id'] ); //Clear fields that can't be copied
$src_rows[$key]['name'] = Misc::generateCopyName( $row['name'] ); //Generate unique name
}
//Debug::Arr($src_rows, 'bSRC Rows: ', __FILE__, __LINE__, __METHOD__, 10);
return $this->setUserPreference( $src_rows ); //Save copied rows
}
return $this->returnHandler( false );
}
/**
* @param null $user_name
* @param int $type_id ID
* @param null $selected_schedule
* @return array|bool
*/
function getScheduleIcalendarURL( $user_name = null, $type_id = null, $selected_schedule = null ) {
$current_user_prefs = $this->getCurrentUserObject()->getUserPreferenceObject();
if ( is_object( $current_user_prefs ) ) {
return $this->returnHandler( $current_user_prefs->getScheduleIcalendarURL( $user_name, $type_id, $selected_schedule ) );
}
return false;
}
}
?>

View File

@@ -0,0 +1,373 @@
<?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 API\Users
*/
class APIUserPreferenceNotification extends APIFactory {
protected $main_class = 'UserPreferenceNotificationFactory';
/**
* APIUserPreferenceNotification constructor.
*/
public function __construct() {
parent::__construct(); //Make sure parent constructor is always called.
return true;
}
/**
* Get default preference notificatiin data for creating new preference notifications.
* Filters the return data by users permissions so only relevant data is returned.
* @return array|bool
*/
function getUserPreferenceNotificationDefaultData( $data ) {
Debug::Text( 'Getting user preference notification default data...', __FILE__, __LINE__, __METHOD__, 10 );
if ( isset( $data['filter_data']['user_id'] ) && $data['filter_data']['user_id'] !== $this->current_user->getId() ) {
// get permission children to verify user can view another users notification preferences
$permission_children_ids = $this->getPermissionChildren();
if ( $this->getPermissionObject()->Check( 'user_preference', 'enabled' )
&& ( $this->getPermissionObject()->Check( 'user_preference', 'view_own' ) && $this->getPermissionObject()->isOwner( false, $data['filter_data']['user_id'] ) === true )
|| $this->getPermissionObject()->Check( 'user_preference', 'view_child' ) ) {
// if getting preference for a different user, we need to load their user and preference data
$ulf = TTnew( 'UserListFactory' ); /** @var UserListFactory $ulf */
$ulf->getById( $data['filter_data']['user_id'] );
if ( $ulf->getRecordCount() == 1 ) {
$u_obj = $ulf->getCurrent(); /** @var UserFactory $u_obj */
}
} else {
return $this->getPermissionObject()->PermissionDenied();
}
} else {
$u_obj = $this->current_user;
}
$upnf = TTnew( 'UserPreferenceNotificationFactory' ); /** @var UserPreferenceNotificationFactory $upnf */
$notification_preference_data_array = $upnf->getUserPreferenceNotificationTypeDefaultValues( null );
$retarr = UserPreferenceNotificationFactory::filterUserNotificationPreferencesByPermissions( $notification_preference_data_array, $u_obj );
// if user uses these defaults to set notification preferences, the user ID is required
foreach ( $retarr as $key => $data ) {
$retarr[$key]['user_id'] = $u_obj->getId();
}
return $this->returnHandler( $retarr );
}
/**
* Get preference notification data for one or more preference notifications.
* @param array $data filter data
* @param bool $disable_paging
* @return array|bool
*/
function getUserPreferenceNotification( $data = null, $disable_paging = false ) {
$data = $this->initializeFilterAndPager( $data, $disable_paging );
if ( !$this->getPermissionObject()->Check( 'user_preference', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'user_preference', 'view' ) || $this->getPermissionObject()->Check( 'user_preference', 'view_own' ) || $this->getPermissionObject()->Check( 'user_preference', 'view_child' ) ) ) {
return $this->getPermissionObject()->PermissionDenied();
}
//Get Permission Hierarchy Children first, as this can be used for viewing, or editing.
$data['filter_data']['permission_children_ids'] = $this->getPermissionObject()->getPermissionChildren( 'user_preference', 'view' );
$upnlf = TTnew( 'UserPreferenceNotificationListFactory' ); /** @var UserPreferenceNotificationListFactory $upnlf */
$upnlf->getAPISearchByCompanyIdAndArrayCriteria( $this->getCurrentCompanyObject()->getId(), $data['filter_data'], $data['filter_items_per_page'], $data['filter_page'], null, $data['filter_sort'] );
Debug::Text( 'Record Count: ' . $upnlf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10 );
if ( $upnlf->getRecordCount() > 0 ) {
$this->setPagerObject( $upnlf );
if ( isset( $data['filter_data']['user_id'] ) && $data['filter_data']['user_id'] !== $this->current_user->getId() ) {
// if getting preference for a different user, we need to load their user and preference data
$ulf = TTnew( 'UserListFactory' ); /** @var UserListFactory $ulf */
$ulf->getById( $data['filter_data']['user_id'] );
if ( $ulf->getRecordCount() == 1 ) {
$u_obj = $ulf->getCurrent(); /** @var UserFactory $u_obj */
}
} else {
$u_obj = $this->current_user;
}
$notification_preference_data_array = [];
foreach ( $upnlf as $upn_obj ) { /** @var UserPreferenceNotificationFactory $upn_obj */
$notification_preference_data_array[] = $upn_obj->getObjectAsArray();
}
//If user does not have a record for the preference notification type, add it so the user can see it with default values set.
$all_notification_preferences = $this->stripReturnHandler( $this->getUserPreferenceNotificationDefaultData( $data ) );
foreach ( $all_notification_preferences as $preference ) {
if ( !in_array( $preference['type_id'], array_column( $notification_preference_data_array, 'type_id' ) ) ) {
$notification_preference_data_array[] = $preference;
};
}
$retarr = UserPreferenceNotificationFactory::filterUserNotificationPreferencesByPermissions( $notification_preference_data_array, $u_obj );
return $this->returnHandler( $retarr );
}
// no notification preferences found for user, return default values so that notification preferences are not empty in my account preferences
return $this->getUserPreferenceNotificationDefaultData( $data );
}
/**
* Get only the fields that are common across all records in the search criteria. Used for Mass Editing of records.
* @param array $data filter data
* @return array
*/
function getCommonUserPreferenceNotificationData( $data ) {
return Misc::arrayIntersectByRow( $this->stripReturnHandler( $this->getUserPreferenceNotification( $data, true ) ) );
}
/**
* Validate preference notification data for one or more preference notifications.
* @param array $data preference notification data
* @return array
*/
function validateUserPreferenceNotification( $data ) {
return $this->setUserPreferenceNotification( $data, true );
}
/**
* Set preference notification data for one or more preference notifications.
* @param array $data preference notification data
* @param bool $validate_only
* @param bool $ignore_warning
* @return array|bool
*/
function setUserPreferenceNotification( $data, $validate_only = false, $ignore_warning = true ) {
$validate_only = (bool)$validate_only;
$ignore_warning = (bool)$ignore_warning;
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
if ( !$this->getPermissionObject()->Check( 'user_preference', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'user_preference', 'edit' ) || $this->getPermissionObject()->Check( 'user_preference', 'edit_own' ) || $this->getPermissionObject()->Check( 'user_preference', 'edit_child' ) || $this->getPermissionObject()->Check( 'user_preference', 'edit_own' ) ) ) {
return $this->getPermissionObject()->PermissionDenied();
}
if ( $validate_only == true ) {
Debug::Text( 'Validating Only!', __FILE__, __LINE__, __METHOD__, 10 );
$permission_children_ids = false;
} else {
//Get Permission Hierarchy Children first, as this can be used for viewing, or editing.
$permission_children_ids = $this->getPermissionChildren();
}
[ $data, $total_records ] = $this->convertToMultipleRecords( $data );
Debug::Text( 'Received data for: ' . $total_records . ' UserPreferenceNotifications', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$validator_stats = [ 'total_records' => $total_records, 'valid_records' => 0 ];
$validator = $save_result = []; $key = false;
if ( is_array( $data ) && $total_records > 0 ) {
foreach ( $data as $key => $row ) {
$primary_validator = new Validator();
$lf = TTnew( 'UserPreferenceNotificationListFactory' ); /** @var UserPreferenceNotificationListFactory $lf */
$lf->StartTransaction();
if ( isset( $row['id'] ) && $row['id'] != '' && $row['id'] != -1 && $row['id'] != TTUUID::getNotExistID() ) {
//Modifying existing object.
//Get preference notification object, so we can only modify just changed data for specific records if needed.
$lf->getByIdAndCompanyId( $row['id'], $this->getCurrentCompanyObject()->getId() );
if ( $lf->getRecordCount() == 1 ) {
//Object exists, check edit permissions
if (
$validate_only == true
||
(
$this->getPermissionObject()->Check( 'user_preference', 'edit' )
|| ( $this->getPermissionObject()->Check( 'user_preference', 'edit_own' ) && $this->getPermissionObject()->isOwner( $lf->getCurrent()->getCreatedBy(), $lf->getCurrent()->getUser() ) === true )
|| ( $this->getPermissionObject()->Check( 'user_preference', 'edit_child' ) && $this->getPermissionObject()->isChild( $lf->getCurrent()->getUser(), $permission_children_ids ) === true )
) ) {
Debug::Text( 'Row Exists, getting current data for ID: ' . $row['id'], __FILE__, __LINE__, __METHOD__, 10 );
$lf = $lf->getCurrent();
$row = array_merge( $lf->getObjectAsArray(), $row );
} else {
$primary_validator->isTrue( 'permission', false, TTi18n::gettext( 'Edit permission denied' ) );
}
} else {
//Object doesn't exist.
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Edit permission denied, record does not exist' ) );
}
} else {
unset( $row['id'] ); //ID could be '-1', so simply unset it so it doesn't try to update a non-existing record.
//Always allow the currently logged in user to create preferences in case the record isn't there.
if ( !( isset( $row['user_id'] ) && $row['user_id'] == $this->getCurrentUserObject()->getId() ) ) {
//Adding new object, check ADD permissions.
$primary_validator->isTrue( 'permission', $this->getPermissionObject()->Check( 'user', 'add' ), TTi18n::gettext( 'Add permission denied' ) );
}
}
Debug::Arr( $row, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$is_valid = $primary_validator->isValid( $ignore_warning );
if ( $is_valid == true ) { //Check to see if all permission checks passed before trying to save data.
Debug::Text( 'Setting object data...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->setObjectFromArray( $row );
$lf->Validator->setValidateOnly( $validate_only );
$is_valid = $lf->isValid( $ignore_warning );
if ( $is_valid == true ) {
Debug::Text( 'Saving data...', __FILE__, __LINE__, __METHOD__, 10 );
if ( $validate_only == true ) {
$save_result[$key] = true;
} else {
$save_result[$key] = $lf->Save();
}
$validator_stats['valid_records']++;
}
}
if ( $is_valid == false ) {
Debug::Text( 'Data is Invalid...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->FailTransaction(); //Just rollback this single record, continue on to the rest.
$validator[$key] = $this->setValidationArray( [ $primary_validator, $lf ] );
} else if ( $validate_only == true ) {
$lf->FailTransaction();
}
$lf->CommitTransaction();
}
return $this->handleRecordValidationResults( $validator, $validator_stats, $key, $save_result );
}
return $this->returnHandler( false );
}
/**
* Delete one or more preference notifications.
* @param array $data preference notification data
* @return array|bool
*/
function deleteUserPreferenceNotification( $data ) {
if ( !is_array( $data ) ) {
$data = [ $data ];
}
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
if ( !$this->getPermissionObject()->Check( 'user_preference', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'user_preference', 'delete' ) || $this->getPermissionObject()->Check( 'user_preference', 'delete_own' ) || $this->getPermissionObject()->Check( 'user_preference', 'delete_child' ) ) ) {
return $this->getPermissionObject()->PermissionDenied();
}
Debug::Text( 'Received data for: ' . count( $data ) . ' UserPreferenceNotifications', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$total_records = count( $data );
$validator = $save_result = []; $key = false;
$validator_stats = [ 'total_records' => $total_records, 'valid_records' => 0 ];
if ( is_array( $data ) && $total_records > 0 ) {
foreach ( $data as $key => $id ) {
$primary_validator = new Validator();
$lf = TTnew( 'UserPreferenceNotificationListFactory' ); /** @var UserPreferenceNotificationListFactory $lf */
$lf->StartTransaction();
if ( $id != '' ) {
//Modifying existing object.
//Get preference notification object, so we can only modify just changed data for specific records if needed.
$lf->getByIdAndCompanyId( $id, $this->getCurrentCompanyObject()->getId() );
if ( $lf->getRecordCount() == 1 ) {
//Object exists, check edit permissions
if ( $this->getPermissionObject()->Check( 'user_preference', 'delete' )
|| ( $this->getPermissionObject()->Check( 'user_preference', 'delete_own' ) && $this->getPermissionObject()->isOwner( $lf->getCurrent()->getCreatedBy(), $lf->getCurrent()->getID() ) === true ) ) {
Debug::Text( 'Record Exists, deleting record ID: ' . $id, __FILE__, __LINE__, __METHOD__, 10 );
$lf = $lf->getCurrent();
} else {
$primary_validator->isTrue( 'permission', false, TTi18n::gettext( 'Delete permission denied' ) );
}
} else {
//Object doesn't exist.
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Delete permission denied, record does not exist' ) );
}
} else {
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Delete permission denied, record does not exist' ) );
}
//Debug::Arr($lf, 'AData: ', __FILE__, __LINE__, __METHOD__, 10);
$is_valid = $primary_validator->isValid();
if ( $is_valid == true ) { //Check to see if all permission checks passed before trying to save data.
Debug::Text( 'Attempting to delete record...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->setDeleted( true );
$is_valid = $lf->isValid();
if ( $is_valid == true ) {
Debug::Text( 'Record Deleted...', __FILE__, __LINE__, __METHOD__, 10 );
$save_result[$key] = $lf->Save();
$validator_stats['valid_records']++;
}
}
if ( $is_valid == false ) {
Debug::Text( 'Data is Invalid...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->FailTransaction(); //Just rollback this single record, continue on to the rest.
$validator[$key] = $this->setValidationArray( [ $primary_validator, $lf ] );
}
$lf->CommitTransaction();
}
return $this->handleRecordValidationResults( $validator, $validator_stats, $key, $save_result );
}
return $this->returnHandler( false );
}
}
?>

View File

@@ -0,0 +1,361 @@
<?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 API\Users
*/
class APIUserReportData extends APIFactory {
protected $main_class = 'UserReportDataFactory';
/**
* APIUserReportData constructor.
*/
public function __construct() {
parent::__construct(); //Make sure parent constructor is always called.
return true;
}
/**
* Get user data for one or more users.
* @param array $data filter data
* @param bool $disable_paging
* @return array
*/
function getUserReportData( $data = null, $disable_paging = false ) {
$data = $this->initializeFilterAndPager( $data, $disable_paging );
//Only allow getting report data for currently logged in user.
$data['filter_data']['user_id'] = $this->getCurrentUserObject()->getId();
//This is done so that users can set a saved report on payroll remittance agency event and then other users can run the tax wizard with that same saved report.
if ( isset( $data['filter_data']['id'] ) && $this->getPermissionObject()->Check( 'pay_stub', 'add' ) && $this->getPermissionObject()->Check( 'pay_stub', 'edit' ) ) {
//FIXME: perhaps add additional checks that the id is specified in a payroll remittance agency event.
unset( $data['filter_data']['user_id'] );
}
Debug::Arr( $data, 'Getting User Report Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$ugdlf = TTnew( 'UserReportDataListFactory' ); /** @var UserReportDataListFactory $ugdlf */
$ugdlf->getAPISearchByCompanyIdAndArrayCriteria( $this->getCurrentCompanyObject()->getId(), $data['filter_data'], $data['filter_items_per_page'], $data['filter_page'], null, $data['filter_sort'] );
Debug::Text( 'Record Count: ' . $ugdlf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10 );
if ( $ugdlf->getRecordCount() > 0 ) {
$this->setPagerObject( $ugdlf );
$retarr = [];
foreach ( $ugdlf as $ugd_obj ) {
$retarr[] = $ugd_obj->getObjectAsArray( $data['filter_columns'] );
}
return $this->returnHandler( $retarr );
}
return $this->returnHandler( true );
}
/**
* Set user data for one or more users.
* @param array $data user data
* @param bool $validate_only
* @param bool $ignore_warning
* @return array
*/
function setUserReportData( $data, $validate_only = false, $ignore_warning = true ) {
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
$validate_only = (bool)$validate_only;
$ignore_warning = (bool)$ignore_warning;
[ $data, $total_records ] = $this->convertToMultipleRecords( $data );
Debug::Text( 'Received data for: ' . $total_records . ' Users', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
if ( $validate_only == true ) {
Debug::Text( 'Validating Only!', __FILE__, __LINE__, __METHOD__, 10 );
$permission_children_ids = false;
} else {
//Get Permission Hierarchy Children first, as this can be used for viewing, or editing.
$permission_children_ids = $this->getPermissionChildren();
}
$validator_stats = [ 'total_records' => $total_records, 'valid_records' => 0 ];
$validator = $save_result = []; $key = false;
if ( is_array( $data ) && $total_records > 0 ) {
foreach ( $data as $key => $row ) {
$row['company_id'] = $this->getCurrentUserObject()->getCompany();
if ( !isset( $row['user_id'] )
|| !( $this->getPermissionObject()->Check( 'user', 'view' ) || ( $this->getPermissionObject()->Check( 'user', 'view_child' ) && $this->getPermissionObject()->isChild( $row['user_id'], $permission_children_ids ) === true ) ) ) {
//Force user_id to currently logged in user.
Debug::Text( 'Forcing user_id...', __FILE__, __LINE__, __METHOD__, 10 );
$row['user_id'] = $this->getCurrentUserObject()->getId();
}
$primary_validator = new Validator();
// The value of auto_refresh is directly placed into a <script> block and must be sanitized to an int. Note htmlspecialchars would not offer protection here.
if ( isset( $row['data']['config']['other']['auto_refresh'] ) ) {
$row['data']['config']['other']['auto_refresh'] = (int)$primary_validator->stripNonNumeric( trim( $row['data']['config']['other']['auto_refresh'] ) );
}
$lf = TTnew( 'UserReportDataListFactory' ); /** @var UserReportDataListFactory $lf */
$lf->StartTransaction();
if ( isset( $row['id'] ) && $row['id'] != '' && $row['id'] != TTUUID::getZeroID() ) {
//Modifying existing object.
//Get user object, so we can only modify just changed data for specific records if needed.
$lf->getByUserIdAndId( $row['user_id'], $row['id'] );
if ( $lf->getRecordCount() == 1 ) {
//Object exists, check edit permissions
$lf = $lf->getCurrent(); //Make the current $lf variable the current object, otherwise getDataDifferences() fails to function.
$row = array_merge( $lf->getObjectAsArray(), $row );
} else {
//Object doesn't exist.
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Edit permission denied, employee does not exist' ) );
}
} else {
//Adding new object, check ADD permissions.
//$primary_validator->isTrue( 'permission', $this->getPermissionObject()->Check('user', 'add'), TTi18n::gettext('Add permission denied') );
//If the user sets the Saved Report navigation dropdown box to "-- NONE --" a zero UUID will come through as the 'id', so clear that out so it saves properly.
unset( $row['id'] );
}
Debug::Arr( $row, 'User Report Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$is_valid = $primary_validator->isValid();
if ( $is_valid == true ) { //Check to see if all permission checks passed before trying to save data.
Debug::Text( 'Attempting to save User Report Data...', __FILE__, __LINE__, __METHOD__, 10 );
//Force Company ID to current company.
$row['company_id'] = $this->getCurrentCompanyObject()->getId();
$lf->setObjectFromArray( $row );
//$lf->setUser( $this->getCurrentUserObject()->getId() ); //Need to be able support copying reports to other users.
$lf->Validator->setValidateOnly( $validate_only );
$is_valid = $lf->isValid( $ignore_warning );
if ( $is_valid == true ) {
Debug::Text( 'Saving User Data...', __FILE__, __LINE__, __METHOD__, 10 );
if ( $validate_only == true ) {
$save_result[$key] = true;
} else {
$save_result[$key] = $lf->Save();
}
$validator_stats['valid_records']++;
}
}
if ( $is_valid == false ) {
Debug::Text( 'Data is Invalid...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->FailTransaction(); //Just rollback this single record, continue on to the rest.
$validator[$key] = $this->setValidationArray( [ $primary_validator, $lf ] );
} else if ( $validate_only == true ) {
//Always fail transaction when valididate only is used, as is saved to different tables immediately.
$lf->FailTransaction();
}
$lf->CommitTransaction();
}
return $this->handleRecordValidationResults( $validator, $validator_stats, $key, $save_result );
}
return $this->returnHandler( false );
}
/**
* Validate user data for one or more users.
* @param array $data user data
* @return array
*/
function validateUserReportData( $data ) {
return $this->setUserReportData( $data, true );
}
/**
* Delete one or more users.
* @param array $data user data
* @return array
*/
function deleteUserReportData( $data ) {
Debug::Arr( $data, 'DataA: ', __FILE__, __LINE__, __METHOD__, 10 );
if ( !is_array( $data ) ) {
$data = [ $data ];
}
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
Debug::Text( 'Received data for: ' . count( $data ) . ' Users', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$total_records = count( $data );
$validator = $save_result = []; $key = false;
$validator_stats = [ 'total_records' => $total_records, 'valid_records' => 0 ];
if ( is_array( $data ) && $total_records > 0 ) {
foreach ( $data as $key => $id ) {
$primary_validator = new Validator();
$lf = TTnew( 'UserReportDataListFactory' ); /** @var UserReportDataListFactory $lf */
$lf->StartTransaction();
if ( $id != '' ) {
//Modifying existing object.
//Get user object, so we can only modify just changed data for specific records if needed.
$lf->getByUserIdAndId( $this->getCurrentUserObject()->getId(), $id );
if ( $lf->getRecordCount() == 1 ) {
//Object exists
Debug::Text( 'User Report Data Exists, deleting record: ' . $id, __FILE__, __LINE__, __METHOD__, 10 );
$lf = $lf->getCurrent();
} else {
//Object doesn't exist.
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Delete permission denied, report data does not exist' ) );
}
} else {
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Delete permission denied, report data does not exist' ) );
}
//Debug::Arr($lf, 'AData: ', __FILE__, __LINE__, __METHOD__, 10);
$is_valid = $primary_validator->isValid();
if ( $is_valid == true ) { //Check to see if all permission checks passed before trying to save data.
Debug::Text( 'Attempting to delete user report data...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->setDeleted( true );
$is_valid = $lf->isValid();
if ( $is_valid == true ) {
Debug::Text( 'User Deleted...', __FILE__, __LINE__, __METHOD__, 10 );
$save_result[$key] = $lf->Save();
$validator_stats['valid_records']++;
}
}
if ( $is_valid == false ) {
Debug::Text( 'User Report Data is Invalid...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->FailTransaction(); //Just rollback this single record, continue on to the rest.
$validator[$key] = $this->setValidationArray( [ $primary_validator, $lf ] );
}
$lf->CommitTransaction();
}
return $this->handleRecordValidationResults( $validator, $validator_stats, $key, $save_result );
}
return $this->returnHandler( false );
}
/**
* Share or copy report to other users.
* @param array $user_report_data_ids User Report Data row IDs
* @param array $destination_user_ids User IDs to copy reports to
* @return array|bool
*/
function shareUserReportData( $user_report_data_ids, $destination_user_ids ) {
if ( !is_array( $user_report_data_ids ) ) {
$user_report_data_ids = [ $user_report_data_ids ];
}
if ( !is_array( $destination_user_ids ) ) {
$destination_user_ids = [ $destination_user_ids ];
}
Debug::Arr( $user_report_data_ids, 'User Report Data IDs: ', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $destination_user_ids, 'Destination User IDs: ', __FILE__, __LINE__, __METHOD__, 10 );
$src_rows = $this->stripReturnHandler( $this->getUserReportData( [ 'filter_data' => [ 'id' => $user_report_data_ids ] ] ) );
if ( is_array( $src_rows ) && count( $src_rows ) > 0 ) {
Debug::Arr( $src_rows, 'SRC Rows: ', __FILE__, __LINE__, __METHOD__, 10 );
$dst_rows = [];
$x = 0;
foreach ( $src_rows as $key => $row ) {
unset( $src_rows[$key]['id'], $src_rows[$key]['created_date'], $src_rows[$key]['created_by'] ); //Clear fields that can't be copied
$src_rows[$key]['name'] = Misc::generateShareName( $this->getCurrentUserObject()->getFullName(), $row['name'] ); //Generate unique name
$description = null;
if ( isset( $row['description'] ) && $row['description'] != '' ) {
$description = $row['description'] . "\n";
}
$src_rows[$key]['description'] = $description . TTi18n::getText( 'Report shared by' ) . ' ' . $this->getCurrentUserObject()->getFullName() . ' ' . TTi18n::getText( 'on' ) . ' ' . TTDate::getDate( 'DATE+TIME', time() );
//Do not copy over is_default value so that users cannot set default for other users.
$src_rows[$key]['is_default'] = false;
//Should we copy any schedules that go along with each saved report? This could cause a lot of issues with mass emails being sent out without intention.
$urdf = TTnew( 'UserReportDataFactory' ); /** @var UserReportDataFactory $urdf */
//Copy to destination users.
if ( is_array( $destination_user_ids ) ) {
foreach ( $destination_user_ids as $destination_user_id ) {
$dst_rows[$x] = $src_rows[$key];
$dst_rows[$x]['user_id'] = $destination_user_id;
$ulf = TTnew( 'UserListFactory' ); /** @var UserListFactory $ulf */
$ulf->getByIdAndCompanyId( $destination_user_id, $this->getCurrentUserObject()->getCompany() );
if ( $ulf->getRecordCount() == 1 ) {
TTLog::addEntry( TTUUID::castUUID( $row['id'] ), 500, TTi18n::getText( 'Shared report with' ) . ': ' . $ulf->getCurrent()->getFullName( false, true ), null, $urdf->getTable() );
}
$x++;
}
}
}
unset( $src_rows );
Debug::Arr( $dst_rows, 'DST Rows: ', __FILE__, __LINE__, __METHOD__, 10 );
return $this->setUserReportData( $dst_rows ); //Save copied rows
}
return $this->returnHandler( false );
}
}
?>

View File

@@ -0,0 +1,96 @@
<?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 API\Company
*/
class APIUserSetting extends APIFactory {
protected $main_class = 'UserSettingFactory';
/**
* APIUserSetting constructor.
*/
public function __construct() {
parent::__construct(); //Make sure parent constructor is always called.
return true;
}
/**
* @param $name
* @return array|bool
*/
function getUserSetting( $name ) {
$retarr = UserSettingFactory::getUserSetting( $this->getCurrentUserObject()->getId(), $name );
if ( $retarr == true ) {
return $this->returnHandler( $retarr );
}
return $this->returnHandler( true );
}
/**
* @param $name
* @param $value
* @param int $type_id
* @return array|bool
*/
function setUserSetting( $name, $value, $type_id = 10 ) {
$retval = UserSettingFactory::setUserSetting( $this->getCurrentUserObject()->getId(), $name, $value, $type_id );
return $this->returnHandler( $retval );
}
/**
* @param $name
* @return array|bool
*/
function deleteUserSetting( $name ) {
$retval = UserSettingFactory::deleteUserSetting( $this->getCurrentUserObject()->getId(), $name );
return $this->returnHandler( $retval );
}
}
?>

View File

@@ -0,0 +1,391 @@
<?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 API\Users
*/
class APIUserTitle extends APIFactory {
protected $main_class = 'UserTitleFactory';
/**
* APIUserTitle constructor.
*/
public function __construct() {
parent::__construct(); //Make sure parent constructor is always called.
return true;
}
/**
* Get options for dropdown boxes.
* @param bool|string $name Name of options to return, ie: 'columns', 'type', 'status'
* @param mixed $parent Parent name/ID of options to return if data is in hierarchical format. (ie: Province)
* @return bool|array
*/
function getOptions( $name = false, $parent = null ) {
if ( $name == 'columns'
&& ( !$this->getPermissionObject()->Check( 'user_title', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'user_title', 'view' ) || $this->getPermissionObject()->Check( 'user_title', 'view_own' ) || $this->getPermissionObject()->Check( 'user_title', 'view_child' ) ) ) ) {
$name = 'list_columns';
}
return parent::getOptions( $name, $parent );
}
/**
* Get default UserTitle data for creating new UserTitles.
* @return array
*/
function getUserTitleDefaultData() {
$company_obj = $this->getCurrentCompanyObject();
Debug::Text( 'Getting UserTitle default data...', __FILE__, __LINE__, __METHOD__, 10 );
$data = [
'company_id' => $company_obj->getId(),
];
$utf = TTnew( 'UserTitleFactory' ); /** @var UserTitleFactory $utf */
$data = $utf->getCustomFieldsDefaultData( $company_obj->getId(), $data );
return $this->returnHandler( $data );
}
/**
* Get UserTitle data for one or more UserTitles.
* @param array $data filter data
* @param bool $disable_paging
* @return array
*/
function getUserTitle( $data = null, $disable_paging = false ) {
$data = $this->initializeFilterAndPager( $data, $disable_paging );
if ( !$this->getPermissionObject()->Check( 'user_title', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'user_title', 'view' ) || $this->getPermissionObject()->Check( 'user_title', 'view_own' ) || $this->getPermissionObject()->Check( 'user_title', 'view_child' ) ) ) {
$data['filter_columns'] = $this->handlePermissionFilterColumns( ( isset( $data['filter_columns'] ) ) ? $data['filter_columns'] : null, Misc::trimSortPrefix( $this->getOptions( 'list_columns' ) ) );
}
//Allow supervisor (subordinates only) to see all job titles.
//$data['filter_data']['permission_children_ids'] = $this->getPermissionObject()->getPermissionChildren( 'user', 'view' );
//Allow getting users from other companies, so we can change admin contacts when using the master company.
if ( isset( $data['filter_data']['company_id'] )
&& TTUUID::isUUID( $data['filter_data']['company_id'] ) && $data['filter_data']['company_id'] != TTUUID::getZeroID() && $data['filter_data']['company_id'] != TTUUID::getNotExistID()
&& ( $this->getPermissionObject()->Check( 'company', 'enabled' ) && $this->getPermissionObject()->Check( 'company', 'view' ) ) ) {
$company_id = $data['filter_data']['company_id'];
} else {
$company_id = $this->getCurrentCompanyObject()->getId();
}
$utlf = TTnew( 'UserTitleListFactory' ); /** @var UserTitleListFactory $utlf */
$utlf->getAPISearchByCompanyIdAndArrayCriteria( $company_id, $data['filter_data'], $data['filter_items_per_page'], $data['filter_page'], null, $data['filter_sort'] );
Debug::Text( 'Record Count: ' . $utlf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10 );
if ( $utlf->getRecordCount() > 0 ) {
$this->setPagerObject( $utlf );
$retarr = [];
foreach ( $utlf as $ut_obj ) {
$retarr[] = $ut_obj->getObjectAsArray( $data['filter_columns'] );
}
return $this->returnHandler( $retarr );
}
return $this->returnHandler( true ); //No records returned.
}
/**
* Export data to csv
* @param string $format file format (csv)
* @param array $data filter data
* @param bool $disable_paging
* @return array
*/
function exportUserTitle( $format = 'csv', $data = null, $disable_paging = true ) {
$result = $this->stripReturnHandler( $this->getUserTitle( $data, $disable_paging ) );
return $this->exportRecords( $format, 'export_employee_title', $result, ( ( isset( $data['filter_columns'] ) ) ? $data['filter_columns'] : null ) );
}
/**
* Get only the fields that are common across all records in the search criteria. Used for Mass Editing of records.
* @param array $data filter data
* @return array
*/
function getCommonUserTitleData( $data ) {
return Misc::arrayIntersectByRow( $this->stripReturnHandler( $this->getUserTitle( $data, true ) ) );
}
/**
* Validate UserTitle data for one or more UserTitles.
* @param array $data UserTitle data
* @return array
*/
function validateUserTitle( $data ) {
return $this->setUserTitle( $data, true );
}
/**
* Set UserTitle data for one or more UserTitles.
* @param array $data UserTitle data
* @param bool $validate_only
* @param bool $ignore_warning
* @return array|bool
*/
function setUserTitle( $data, $validate_only = false, $ignore_warning = true ) {
$validate_only = (bool)$validate_only;
$ignore_warning = (bool)$ignore_warning;
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
if ( !$this->getPermissionObject()->Check( 'user_title', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'user_title', 'edit' ) || $this->getPermissionObject()->Check( 'user_title', 'edit_own' ) || $this->getPermissionObject()->Check( 'user_title', 'edit_child' ) || $this->getPermissionObject()->Check( 'user_title', 'add' ) ) ) {
return $this->getPermissionObject()->PermissionDenied();
}
if ( $validate_only == true ) {
Debug::Text( 'Validating Only!', __FILE__, __LINE__, __METHOD__, 10 );
}
[ $data, $total_records ] = $this->convertToMultipleRecords( $data );
Debug::Text( 'Received data for: ' . $total_records . ' UserTitles', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$validator_stats = [ 'total_records' => $total_records, 'valid_records' => 0 ];
$validator = $save_result = []; $key = false;
if ( is_array( $data ) && $total_records > 0 ) {
foreach ( $data as $key => $row ) {
$primary_validator = new Validator();
$lf = TTnew( 'UserTitleListFactory' ); /** @var UserTitleListFactory $lf */
$lf->StartTransaction();
if ( isset( $row['id'] ) && $row['id'] != '' ) {
//Modifying existing object.
//Get UserTitle object, so we can only modify just changed data for specific records if needed.
$lf->getByIdAndCompanyId( $row['id'], $this->getCurrentCompanyObject()->getId() );
if ( $lf->getRecordCount() == 1 ) {
//Object exists, check edit permissions
if (
$validate_only == true
||
(
$this->getPermissionObject()->Check( 'user_title', 'edit' )
|| ( $this->getPermissionObject()->Check( 'user_title', 'edit_own' ) && $this->getPermissionObject()->isOwner( $lf->getCurrent()->getCreatedBy(), $lf->getCurrent()->getID() ) === true )
) ) {
Debug::Text( 'Row Exists, getting current data for ID: ' . $row['id'], __FILE__, __LINE__, __METHOD__, 10 );
$lf = $lf->getCurrent();
$row = array_merge( $lf->getObjectAsArray(), $row );
} else {
$primary_validator->isTrue( 'permission', false, TTi18n::gettext( 'Edit permission denied' ) );
}
} else {
//Object doesn't exist.
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Edit permission denied, record does not exist' ) );
}
} else {
//Adding new object, check ADD permissions.
$primary_validator->isTrue( 'permission', $this->getPermissionObject()->Check( 'user_title', 'add' ), TTi18n::gettext( 'Add permission denied' ) );
}
Debug::Arr( $row, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$is_valid = $primary_validator->isValid();
if ( $is_valid == true ) { //Check to see if all permission checks passed before trying to save data.
Debug::Text( 'Setting object data...', __FILE__, __LINE__, __METHOD__, 10 );
//Force Company ID to current company.
$row['company_id'] = $this->getCurrentCompanyObject()->getId();
$lf->setObjectFromArray( $row );
$is_valid = $lf->isValid( $ignore_warning );
if ( $is_valid == true ) {
Debug::Text( 'Saving data...', __FILE__, __LINE__, __METHOD__, 10 );
if ( $validate_only == true ) {
$save_result[$key] = true;
} else {
$save_result[$key] = $lf->Save();
}
$validator_stats['valid_records']++;
}
}
if ( $is_valid == false ) {
Debug::Text( 'Data is Invalid...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->FailTransaction(); //Just rollback this single record, continue on to the rest.
$validator[$key] = $this->setValidationArray( [ $primary_validator, $lf ] );
} else if ( $validate_only == true ) {
$lf->FailTransaction();
}
$lf->CommitTransaction();
}
return $this->handleRecordValidationResults( $validator, $validator_stats, $key, $save_result );
}
return $this->returnHandler( false );
}
/**
* Delete one or more UserTitles.
* @param array $data UserTitle data
* @return array|bool
*/
function deleteUserTitle( $data ) {
if ( !is_array( $data ) ) {
$data = [ $data ];
}
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
if ( !$this->getPermissionObject()->Check( 'user_title', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'user_title', 'delete' ) || $this->getPermissionObject()->Check( 'user_title', 'delete_own' ) || $this->getPermissionObject()->Check( 'user_title', 'delete_child' ) ) ) {
return $this->getPermissionObject()->PermissionDenied();
}
Debug::Text( 'Received data for: ' . count( $data ) . ' UserTitles', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$total_records = count( $data );
$validator = $save_result = []; $key = false;
$validator_stats = [ 'total_records' => $total_records, 'valid_records' => 0 ];
if ( is_array( $data ) && $total_records > 0 ) {
foreach ( $data as $key => $id ) {
$primary_validator = new Validator();
$lf = TTnew( 'UserTitleListFactory' ); /** @var UserTitleListFactory $lf */
$lf->StartTransaction();
if ( TTUUID::isUUID( $id ) ) {
//Modifying existing object.
//Get UserTitle object, so we can only modify just changed data for specific records if needed.
$lf->getByIdAndCompanyId( $id, $this->getCurrentCompanyObject()->getId() );
if ( $lf->getRecordCount() == 1 ) {
//Object exists, check edit permissions
if ( $this->getPermissionObject()->Check( 'user_title', 'delete' )
|| ( $this->getPermissionObject()->Check( 'user_title', 'delete_own' ) && $this->getPermissionObject()->isOwner( $lf->getCurrent()->getCreatedBy(), $lf->getCurrent()->getID() ) === true ) ) {
Debug::Text( 'Record Exists, deleting record ID: ' . $id, __FILE__, __LINE__, __METHOD__, 10 );
$lf = $lf->getCurrent();
} else {
$primary_validator->isTrue( 'permission', false, TTi18n::gettext( 'Delete permission denied' ) );
}
} else {
//Object doesn't exist.
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Delete permission denied, record does not exist' ) );
}
} else {
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Delete permission denied, record does not exist' ) );
}
//Debug::Arr($lf, 'AData: ', __FILE__, __LINE__, __METHOD__, 10);
$is_valid = $primary_validator->isValid();
if ( $is_valid == true ) { //Check to see if all permission checks passed before trying to save data.
Debug::Text( 'Attempting to delete record...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->setDeleted( true );
$is_valid = $lf->isValid();
if ( $is_valid == true ) {
Debug::Text( 'Record Deleted...', __FILE__, __LINE__, __METHOD__, 10 );
$save_result[$key] = $lf->Save();
$validator_stats['valid_records']++;
}
}
if ( $is_valid == false ) {
Debug::Text( 'Data is Invalid...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->FailTransaction(); //Just rollback this single record, continue on to the rest.
$validator[$key] = $this->setValidationArray( [ $primary_validator, $lf ] );
}
$lf->CommitTransaction();
}
return $this->handleRecordValidationResults( $validator, $validator_stats, $key, $save_result );
}
return $this->returnHandler( false );
}
/**
* Copy one or more UserTitles.
* @param array $data UserTitle IDs
* @return array
*/
function copyUserTitle( $data ) {
if ( !is_array( $data ) ) {
$data = [ $data ];
}
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
Debug::Text( 'Received data for: ' . count( $data ) . ' UserTitles', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$src_rows = $this->stripReturnHandler( $this->getUserTitle( [ 'filter_data' => [ 'id' => $data ] ], true ) );
if ( is_array( $src_rows ) && count( $src_rows ) > 0 ) {
Debug::Arr( $src_rows, 'SRC Rows: ', __FILE__, __LINE__, __METHOD__, 10 );
foreach ( $src_rows as $key => $row ) {
unset( $src_rows[$key]['id'], $src_rows[$key]['manual_id'] ); //Clear fields that can't be copied
$src_rows[$key]['name'] = Misc::generateCopyName( $row['name'] ); //Generate unique name
}
//Debug::Arr($src_rows, 'bSRC Rows: ', __FILE__, __LINE__, __METHOD__, 10);
return $this->setUserTitle( $src_rows ); //Save copied rows
}
return $this->returnHandler( false );
}
}
?>

View File

@@ -0,0 +1,478 @@
<?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 API\Users
*/
class APIUserWage extends APIFactory {
protected $main_class = 'UserWageFactory';
/**
* APIUserWage constructor.
*/
public function __construct() {
parent::__construct(); //Make sure parent constructor is always called.
return true;
}
/**
* Get default wage data for creating new wagees.
* @param string $user_id UUID
* @return array
*/
function getUserWageDefaultData( $user_id = null ) {
$company_obj = $this->getCurrentCompanyObject();
Debug::Text( 'Getting wage default data...', __FILE__, __LINE__, __METHOD__, 10 );
//If user_id is passed, check for other wage entries, if none, default to the employees hire date.
if ( $user_id != '' ) {
//Check for existing wage entries.
$uwlf = TTnew( 'UserWageListFactory' ); /** @var UserWageListFactory $uwlf */
$uwlf->getLastWageByUserId( $user_id );
if ( $uwlf->getRecordCount() == 1 ) {
Debug::Text( 'Previous wage entry already exists...', __FILE__, __LINE__, __METHOD__, 10 );
$effective_date = time();
} else {
Debug::Text( 'Trying to use hire date...', __FILE__, __LINE__, __METHOD__, 10 );
$ulf = TTnew( 'UserListFactory' ); /** @var UserListFactory $ulf */
$ulf->getByIdAndCompanyId( $user_id, $this->getCurrentCompanyObject()->getId() );
if ( $ulf->getRecordCount() > 0 ) {
$effective_date = $ulf->getCurrent()->getHireDate();
}
}
} else {
Debug::Text( 'No user specified...', __FILE__, __LINE__, __METHOD__, 10 );
$effective_date = time();
}
$data = [
'company_id' => $company_obj->getId(),
'type_id' => 10,
'wage' => '0.00',
'hourly_rate' => '0.00',
'effective_date' => TTDate::getAPIDate( 'DATE', $effective_date ),
'labor_burden_percent' => 0,
'weekly_time' => ( 3600 * 40 ), //40hrs/week
];
return $this->returnHandler( $data );
}
/**
* Get wage data for one or more wagees.
* @param array $data filter data
* @param bool $disable_paging
* @param bool $last_user_wage_only
* @return array|bool
*/
function getUserWage( $data = null, $disable_paging = false, $last_user_wage_only = false ) {
$data = $this->initializeFilterAndPager( $data, $disable_paging );
if ( $this->getPermissionObject()->checkAuthenticationType( 700 ) == false ) { //700=HTTP Auth with username/password
return $this->getPermissionObject()->AuthenticationTypeDenied();
}
if ( !$this->getPermissionObject()->Check( 'wage', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'wage', 'view' ) || $this->getPermissionObject()->Check( 'wage', 'view_own' ) || $this->getPermissionObject()->Check( 'wage', 'view_child' ) ) ) {
return $this->getPermissionObject()->PermissionDenied();
}
//Get Permission Hierarchy Children first, as this can be used for viewing, or editing.
$data['filter_data']['permission_children_ids'] = $this->getPermissionObject()->getPermissionChildren( 'wage', 'view' );
$blf = TTnew( 'UserWageListFactory' ); /** @var UserWageListFactory $blf */
if ( $last_user_wage_only == true ) {
Debug::Text( 'Using APILastWageSearch...', __FILE__, __LINE__, __METHOD__, 10 );
$blf->getAPILastWageSearchByCompanyIdAndArrayCriteria( $this->getCurrentCompanyObject()->getId(), $data['filter_data'], $data['filter_items_per_page'], $data['filter_page'], null, $data['filter_sort'] );
} else {
$blf->getAPISearchByCompanyIdAndArrayCriteria( $this->getCurrentCompanyObject()->getId(), $data['filter_data'], $data['filter_items_per_page'], $data['filter_page'], null, $data['filter_sort'] );
}
Debug::Text( 'Record Count: ' . $blf->getRecordCount(), __FILE__, __LINE__, __METHOD__, 10 );
if ( $blf->getRecordCount() > 0 ) {
$this->getProgressBarObject()->start( $this->getAPIMessageID(), $blf->getRecordCount() );
$this->setPagerObject( $blf );
$retarr = [];
foreach ( $blf as $b_obj ) {
$retarr[] = $b_obj->getObjectAsArray( $data['filter_columns'], $data['filter_data']['permission_children_ids'] );
$this->getProgressBarObject()->set( $this->getAPIMessageID(), $blf->getCurrentRow() );
}
$this->getProgressBarObject()->stop( $this->getAPIMessageID() );
return $this->returnHandler( $retarr );
}
return $this->returnHandler( true ); //No records returned.
}
/**
* Export wage data to csv
* @param string $format file format (csv)
* @param array $data filter data
* @param bool $disable_paging
* @return array
*/
function exportUserWage( $format = 'csv', $data = null, $disable_paging = true ) {
$result = $this->stripReturnHandler( $this->getUserWage( $data, $disable_paging ) );
return $this->exportRecords( $format, 'export_wage', $result, ( ( isset( $data['filter_columns'] ) ) ? $data['filter_columns'] : null ) );
}
/**
* Get only the fields that are common across all records in the search criteria. Used for Mass Editing of records.
* @param array $data filter data
* @return array
*/
function getCommonUserWageData( $data ) {
return Misc::arrayIntersectByRow( $this->stripReturnHandler( $this->getUserWage( $data, true ) ) );
}
/**
* Validate wage data for one or more wagees.
* @param array $data wage data
* @return array
*/
function validateUserWage( $data ) {
return $this->setUserWage( $data, true, false );
}
/**
* Set wage data for one or more wagees.
* @param array $data wage data
* @param bool $validate_only
* @param bool $ignore_warning
* @return array|bool
*/
function setUserWage( $data, $validate_only = false, $ignore_warning = true ) {
$validate_only = (bool)$validate_only;
$ignore_warning = (bool)$ignore_warning;
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
if ( $this->getPermissionObject()->checkAuthenticationType( 700 ) == false ) { //700=HTTP Auth with username/password
return $this->getPermissionObject()->AuthenticationTypeDenied();
}
if ( !$this->getPermissionObject()->Check( 'wage', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'wage', 'edit' ) || $this->getPermissionObject()->Check( 'wage', 'edit_own' ) || $this->getPermissionObject()->Check( 'wage', 'edit_child' ) || $this->getPermissionObject()->Check( 'wage', 'add' ) ) ) {
return $this->getPermissionObject()->PermissionDenied();
}
if ( $validate_only == true ) {
Debug::Text( 'Validating Only!', __FILE__, __LINE__, __METHOD__, 10 );
$permission_children_ids = false;
} else {
//Get Permission Hierarchy Children first, as this can be used for viewing, or editing.
$permission_children_ids = $this->getPermissionChildren();
}
[ $data, $total_records ] = $this->convertToMultipleRecords( $data );
Debug::Text( 'Received data for: ' . $total_records . ' UserWages', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$validator_stats = [ 'total_records' => $total_records, 'valid_records' => 0 ];
$validator = $save_result = []; $key = false;
if ( is_array( $data ) && $total_records > 0 ) {
$this->getProgressBarObject()->start( $this->getAPIMessageID(), $total_records );
foreach ( $data as $key => $row ) {
$primary_validator = new Validator();
$lf = TTnew( 'UserWageListFactory' ); /** @var UserWageListFactory $lf */
$lf->StartTransaction();
if ( isset( $row['id'] ) && $row['id'] != '' ) {
//Modifying existing object.
//Get wage object, so we can only modify just changed data for specific records if needed.
$lf->getByIdAndCompanyId( $row['id'], $this->getCurrentCompanyObject()->getId() );
if ( $lf->getRecordCount() == 1 ) {
//Object exists, check edit permissions
if (
$validate_only == true
||
(
$this->getPermissionObject()->Check( 'wage', 'edit' )
|| ( $this->getPermissionObject()->Check( 'wage', 'edit_own' ) && $this->getPermissionObject()->isOwner( $lf->getCurrent()->getCreatedBy(), $lf->getCurrent()->getUser() ) === true )
|| ( $this->getPermissionObject()->Check( 'wage', 'edit_child' ) && $this->getPermissionObject()->isChild( $lf->getCurrent()->getUser(), $permission_children_ids ) === true )
) ) {
Debug::Text( 'Row Exists, getting current data for ID: ' . $row['id'], __FILE__, __LINE__, __METHOD__, 10 );
$lf = $lf->getCurrent();
$row = array_merge( $lf->getObjectAsArray(), $row );
} else {
$primary_validator->isTrue( 'permission', false, TTi18n::gettext( 'Edit permission denied' ) );
}
} else {
//Object doesn't exist.
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Edit permission denied, record does not exist' ) );
}
} else {
//Adding new object, check ADD permissions.
if ( !( $validate_only == true
||
( $this->getPermissionObject()->Check( 'wage', 'add' )
&&
(
$this->getPermissionObject()->Check( 'wage', 'edit' )
|| ( isset( $row['user_id'] ) && $this->getPermissionObject()->Check( 'wage', 'edit_own' ) && $this->getPermissionObject()->isOwner( false, $row['user_id'] ) === true ) //We don't know the created_by of the user at this point, but only check if the user is assigned to the logged in person.
|| ( isset( $row['user_id'] ) && $this->getPermissionObject()->Check( 'wage', 'edit_child' ) && $this->getPermissionObject()->isChild( $row['user_id'], $permission_children_ids ) === true )
)
)
) ) {
$primary_validator->isTrue( 'permission', false, TTi18n::gettext( 'Add permission denied' ) );
}
}
//Debug::Arr($row, 'Data: ', __FILE__, __LINE__, __METHOD__, 10);
$is_valid = $primary_validator->isValid();
if ( $is_valid == true ) { //Check to see if all permission checks passed before trying to save data.
Debug::Text( 'Setting object data...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->setObjectFromArray( $row );
$lf->Validator->setValidateOnly( $validate_only );
$is_valid = $lf->isValid( $ignore_warning );
if ( $is_valid == true ) {
Debug::Text( 'Saving data...', __FILE__, __LINE__, __METHOD__, 10 );
if ( $validate_only == true ) {
$save_result[$key] = true;
} else {
$save_result[$key] = $lf->Save();
}
$validator_stats['valid_records']++;
}
}
if ( $is_valid == false ) {
Debug::Text( 'Data is Invalid...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->FailTransaction(); //Just rollback this single record, continue on to the rest.
$validator[$key] = $this->setValidationArray( [ $primary_validator, $lf ] );
} else if ( $validate_only == true ) {
$lf->FailTransaction();
}
$lf->CommitTransaction();
$this->getProgressBarObject()->set( $this->getAPIMessageID(), $key );
}
$this->getProgressBarObject()->stop( $this->getAPIMessageID() );
return $this->handleRecordValidationResults( $validator, $validator_stats, $key, $save_result );
}
return $this->returnHandler( false );
}
/**
* Delete one or more wages.
* @param array $data wage data
* @return array|bool
*/
function deleteUserWage( $data ) {
if ( !is_array( $data ) ) {
$data = [ $data ];
}
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
if ( $this->getPermissionObject()->checkAuthenticationType( 700 ) == false ) { //700=HTTP Auth with username/password
return $this->getPermissionObject()->AuthenticationTypeDenied();
}
if ( !$this->getPermissionObject()->Check( 'wage', 'enabled' )
|| !( $this->getPermissionObject()->Check( 'wage', 'delete' ) || $this->getPermissionObject()->Check( 'wage', 'delete_own' ) || $this->getPermissionObject()->Check( 'wage', 'delete_child' ) ) ) {
return $this->getPermissionObject()->PermissionDenied();
}
//Get Permission Hierarchy Children first, as this can be used for viewing, or editing.
$permission_children_ids = $this->getPermissionChildren();
Debug::Text( 'Received data for: ' . count( $data ) . ' UserWages', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$total_records = count( $data );
$validator = $save_result = []; $key = false;
$validator_stats = [ 'total_records' => $total_records, 'valid_records' => 0 ];
if ( is_array( $data ) && $total_records > 0 ) {
$this->getProgressBarObject()->start( $this->getAPIMessageID(), $total_records );
foreach ( $data as $key => $id ) {
$primary_validator = new Validator();
$lf = TTnew( 'UserWageListFactory' ); /** @var UserWageListFactory $lf */
$lf->StartTransaction();
if ( $id != '' ) {
//Modifying existing object.
//Get wage object, so we can only modify just changed data for specific records if needed.
$lf->getByIdAndCompanyId( $id, $this->getCurrentCompanyObject()->getId() );
if ( $lf->getRecordCount() == 1 ) {
//Object exists, check edit permissions
if ( $this->getPermissionObject()->Check( 'wage', 'delete' )
|| ( $this->getPermissionObject()->Check( 'wage', 'delete_own' ) && $this->getPermissionObject()->isOwner( $lf->getCurrent()->getCreatedBy(), $lf->getCurrent()->getUser() ) === true )
|| ( $this->getPermissionObject()->Check( 'wage', 'delete_child' ) && $this->getPermissionObject()->isChild( $lf->getCurrent()->getUser(), $permission_children_ids ) === true ) ) {
Debug::Text( 'Record Exists, deleting record ID: ' . $id, __FILE__, __LINE__, __METHOD__, 10 );
$lf = $lf->getCurrent();
} else {
$primary_validator->isTrue( 'permission', false, TTi18n::gettext( 'Delete permission denied' ) );
}
} else {
//Object doesn't exist.
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Delete permission denied, record does not exist' ) );
}
} else {
$primary_validator->isTrue( 'id', false, TTi18n::gettext( 'Delete permission denied, record does not exist' ) );
}
//Debug::Arr($lf, 'AData: ', __FILE__, __LINE__, __METHOD__, 10);
$is_valid = $primary_validator->isValid();
if ( $is_valid == true ) { //Check to see if all permission checks passed before trying to save data.
Debug::Text( 'Attempting to delete record...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->setDeleted( true );
$is_valid = $lf->isValid();
if ( $is_valid == true ) {
Debug::Text( 'Record Deleted...', __FILE__, __LINE__, __METHOD__, 10 );
$save_result[$key] = $lf->Save();
$validator_stats['valid_records']++;
}
}
if ( $is_valid == false ) {
Debug::Text( 'Data is Invalid...', __FILE__, __LINE__, __METHOD__, 10 );
$lf->FailTransaction(); //Just rollback this single record, continue on to the rest.
$validator[$key] = $this->setValidationArray( [ $primary_validator, $lf ] );
}
$lf->CommitTransaction();
$this->getProgressBarObject()->set( $this->getAPIMessageID(), $key );
}
$this->getProgressBarObject()->stop( $this->getAPIMessageID() );
return $this->handleRecordValidationResults( $validator, $validator_stats, $key, $save_result );
}
return $this->returnHandler( false );
}
/**
* Copy one or more wagees.
* @param array $data wage IDs
* @return array
*/
function copyUserWage( $data ) {
if ( !is_array( $data ) ) {
$data = [ $data ];
}
if ( !is_array( $data ) ) {
return $this->returnHandler( false );
}
Debug::Text( 'Received data for: ' . count( $data ) . ' UserWages', __FILE__, __LINE__, __METHOD__, 10 );
Debug::Arr( $data, 'Data: ', __FILE__, __LINE__, __METHOD__, 10 );
$src_rows = $this->stripReturnHandler( $this->getUserWage( [ 'filter_data' => [ 'id' => $data ] ], true ) );
if ( is_array( $src_rows ) && count( $src_rows ) > 0 ) {
Debug::Arr( $src_rows, 'SRC Rows: ', __FILE__, __LINE__, __METHOD__, 10 );
foreach ( $src_rows as $key => $row ) {
unset( $src_rows[$key]['id'] ); //Clear fields that can't be copied
}
unset( $row ); //code standards
//Debug::Arr($src_rows, 'bSRC Rows: ', __FILE__, __LINE__, __METHOD__, 10);
return $this->setUserWage( $src_rows ); //Save copied rows
}
return $this->returnHandler( false );
}
/**
* Calculate salaried employees hourly rate based on wage and weekly hours.
* @param float $wage Wage
* @param float $weekly_hours Weekly Hours
* @param int $wage_type_id Wage Type ID
* @return array|float
*/
function getHourlyRate( $wage, $weekly_hours, $wage_type_id = 10 ) {
if ( $wage == '' ) {
return '0.00';
}
if ( $weekly_hours == '' ) {
return '0.00';
}
if ( $wage_type_id == '' ) {
return '0.00';
}
$data = [
'type_id' => $wage_type_id,
'wage' => $wage,
'weekly_time' => TTDate::parseTimeUnit( $weekly_hours ),
];
//FIXME: Pass user_id and/or currency_id so we can properly round to the right number of decimals.
$uwf = TTnew( 'UserWageFactory' ); /** @var UserWageFactory $uwf */
$uwf->setObjectFromArray( $data );
//$hourly_rate = TTi18n::formatNumber( $uwf->calcHourlyRate(), true, 2, 4 );
//calcHourlyRate() returns a float() so we have to re-format the value here again.
$hourly_rate = Misc::MoneyRound( $uwf->calcHourlyRate(), 2, ( ( is_object( $uwf->getUserObject() ) && is_object( $uwf->getUserObject()->getCurrencyObject() ) ) ? $uwf->getUserObject()->getCurrencyObject() : null ) );
return $this->returnHandler( $hourly_rate );
}
}
?>