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 ); } } ?>