Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 20
CRAP
0.00% covered (danger)
0.00%
0 / 214
AdminService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 20
2256.00
0.00% covered (danger)
0.00%
0 / 214
 __construct
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 3
 __clone
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 2
 getInstance
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 5
 enableStaffLogin
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 12
 addStaffDepartments
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 14
 deleteStaffDepartments
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 8
 getTimetableAdminDeptPrivilleges
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 11
 updateTimeTableAdminDeptPrivilleges
0.00% covered (danger)
0.00%
0 / 1
42.00
0.00% covered (danger)
0.00%
0 / 25
 deleteAllTimetableAdminDeptPrivilleges
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 9
 createAdminType
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 9
 toggleAllowProfileEditById
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 9
 toggleAllowProfileEditALL
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 14
 getAdminDetailsById
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 10
 getAdminListByMenuItem
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 13
 verifyAdminAccountAndFetchDetails
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 17
 getAllSupportAdmins
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 11
 resetPassord
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 13
 blockAdmin
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 9
 enableStudentFieldReset
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 11
 softDeleteField
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 9
<?php
namespace com\linways\core\ams\professional\service;
use com\linways\core\ams\professional\dto\Admin;
use com\linways\core\ams\professional\dto\SettingsConstents;
use com\linways\core\ams\professional\exception\ProfessionalException;
use com\linways\core\ams\professional\dto\LibraryStaffDepartment;
use com\linways\core\ams\professional\mapper\AdminServiceMapper;
use com\linways\core\ams\professional\request\AdminLoginRequest;
class AdminService extends BaseService
{
    // /Condition 1 - Presence of a static member variable
    private static $_instance = null;
    private $mapper = [];
    // /Condition 2 - Locked down the constructor
    private function __construct()
    {
        $this->mapper = AdminServiceMapper::getInstance()->getMapper();
    }
    // Prevent any oustide instantiation of this class
    // /Condition 3 - Prevent any object or instance of that class to be cloned
    private function __clone()
    {
    }
    // Prevent any copy of this object
    // /Condition 4 - Have a single globally accessible static method
    public static function getInstance()
    {
        if (!is_object(self::$_instance)) // or if( is_null(self::$_instance) ) or if( self::$_instance == null )
            self::$_instance = new self ();
        return self::$_instance;
    }
    /**
     * method for checking enableStaffLogin from admin side
     *
     * @param int $adminTypeId
     * @return boolean
     * @throws ProfessionalException
     */
    public function enableStaffLogin($adminTypeId)
    {
        $menuItem = NULL;
        $enableStaffLogin = false;
        try {
            $authGroupId = $adminTypeId;
            $permission[] = SettingsConstents::ENABLE_STAFF_LOGIN_FROM_ADMIN;
            $enableStaffLogin = PermissionService::getInstance()->checkAuthGroupPermission ( $authGroupId, $permission);
        } catch (\Exception $e) {
            throw new ProfessionalException ($e->getCode(), $e->getMessage());
        }
        return $enableStaffLogin;
    }   
    /**
     * add staff associated departments
     *
     * @param LibraryStaffDepartment $libraryStaffDepartment
     * @return \com\linways\base\dto\MySqlResult
     * @throws ProfessionalException
     */
    public function addStaffDepartments($libraryStaffDepartment)
    {
        $sql = "INSERT INTO libraryStaffDepartment(staffId,deptId,createdBy,updatedBy,createdDate,updatedDate) VALUES ";
        $values = [];
        foreach ($libraryStaffDepartment->deptIds as $deptId) {
            $values [] = "($libraryStaffDepartment->staffId,$deptId,$libraryStaffDepartment->createdBy,$libraryStaffDepartment->updatedBy,utc_timestamp(),utc_timestamp())";
        }
        $sql .= implode(',', $values);
        try {
            $this->deleteStaffDepartments($libraryStaffDepartment->staffId);
            return $this->executeQuery($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException ($e->getCode(), $e->getMessage());
        }
    }
    /**
     * delete staff associated departments
     *
     * @param int $staffId
     * @return \com\linways\base\dto\MySqlResult
     * @throws ProfessionalException
     */
    public function deleteStaffDepartments($staffId)
    {
        $sql = "DELETE FROM libraryStaffDepartment WHERE staffId=$staffId";
        try {
            return $this->executeQuery($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException ($e->getCode(), $e->getMessage());
        }
    }
    /**
     * get timetable admin dept privilleges
     *
     * @param int $adminTypeId
     * @return array $deptPrivilleges
     */
    public function getTimetableAdminDeptPrivilleges($adminTypeId)
    {
        $deptPrivilleges = [];
        $adminTypeId = $this->realEscapeString($adminTypeId);
        $sql = "select * FROM timtable_admin_dept_privilleges WHERE adminTypeId=$adminTypeId";
        try {
            $deptPrivilleges = $this->executeQueryForList($sql, $this->mapper [AdminServiceMapper::GETTIMTABLEADMINDEPTPRIVILLEGES]);
        } catch (\Exception $e) {
            throw new ProfessionalException ($e->getCode(), $e->getMessage());
        }
        return $deptPrivilleges;
    }
    /**
     * Method for updating admin dept privilleges
     *
     * @param array $deptIds
     * @param int $adminTypeId
     * @param int $uId
     *            - logged in user id
     */
    public function updateTimeTableAdminDeptPrivilleges($deptIds, $adminTypeId, $uId)
    {
        $adminTypeId = $this->realEscapeString($adminTypeId);
        $uId = $this->realEscapeString($uId);
        //Delete all existing privilleges
        $this->deleteAllTimetableAdminDeptPrivilleges($adminTypeId);
        if (!empty ($deptIds)) {
            $sql = "INSERT INTO timtable_admin_dept_privilleges(deptId,adminTypeId,createdBy,updatedBy,createdDate,updatedDate)  VALUES ";
            $values = null;
            foreach ($deptIds as $deptId) {
                $deptId = $this->realEscapeString($deptId);
                if (empty ($values)) {
                    $values .= " ($deptId,$adminTypeId,$uId,$uId,utc_timestamp(),utc_timestamp() )";
                } else {
                    $values .= " , ($deptId,$adminTypeId,$uId,$uId,utc_timestamp(),utc_timestamp() )";
                }
            }
            if (!empty ($values)) {
                $sql .= $values;
                try {
                    $this->executeQuery($sql);
                } catch (\Exception $e) {
                    throw new ProfessionalException ($e->getCode(), $e->getMessage());
                }
            }
        }
    }
    /**
     * Delete all timetable admin dept privilleges
     * @param int $adminTypeId
     * @throws ProfessionalException
     */
    public function deleteAllTimetableAdminDeptPrivilleges($adminTypeId)
    {
        $adminTypeId = $this->realEscapeString($adminTypeId);
        $sql = "DELETE FROM timtable_admin_dept_privilleges WHERE adminTypeId = $adminTypeId";
        try {
            $this->executeQuery($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException ($e->getCode(), $e->getMessage());
        }
    }
    /**
     * Create Admin Type
     * @param string $admintypeName
     * @return id
     * @throws ProfessionalException
     */
    public function createAdminType($admintypeName)
    {
        $sql = "INSERT INTO admin_privilege_type(admintypeName) VALUES ( '$admintypeName')";
        try {
            return $this->executeQueryForObject($sql, TRUE);
        } catch (\Exception $e) {
            throw new ProfessionalException ($e->getCode(), $e->getMessage());
        }
        return null;
    }
    /**
     * Allow staff to edit their profile
     * @param int $admintypeName
     * @return id
     * @throws ProfessionalException
     * @author Vishnu
     */
    public function toggleAllowProfileEditById($staffId)
    {
        $sql = "UPDATE staffaccounts SET allowProfileEdit = IF (allowProfileEdit = 0, 1, 0) WHERE staffID = " . $staffId . "";
        try {
            return $this->executeQuery($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException ($e->getCode(), $e->getMessage());
        }
        return null;
    }
    /**
     * Allow staff to edit their profile
     * @param int $admintypeName
     * @return id
     * @throws ProfessionalException
     * @author Vishnu
     */
    public function toggleAllowProfileEditALL($staffId, $ischecked)
    {
        $staffIds = implode(',', $staffId);
        if ($ischecked == 1) {
            $sql = "UPDATE staffaccounts SET allowProfileEdit = 1 WHERE staffID in ($staffIds)";
        } else {
            $sql = "UPDATE staffaccounts SET allowProfileEdit = 0 WHERE staffID in ($staffIds)";
        }
        try {
            return $this->executeQuery($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException ($e->getCode(), $e->getMessage());
        }
        return null;
    }
    /**
     * Undocumented function
     *
     * @param [type] $id
     * @return void
     */
    public function getAdminDetailsById($id)
    {
        $id = $this->realEscapeString($id);
        $sql = "SELECT adminID as id, adminName as name, adminEmail as email,isSupportAdmin FROM adminaccount WHERE adminID=$id";
        try {
            $adminDetails = $this->executeQueryForObject($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException ($e->getCode(), $e->getMessage());
        }
        return $adminDetails;
    }
    /**
     * Undocumented function
     *
     * @return void
     * @throws ProfessionalException
     */
    public function getAdminListByMenuItem($menuItem)
    {
        $menuItem = $this->realEscapeString($menuItem);
        $sql = null;
        try {
            $sql = "SELECT aa.adminId as id,aa.adminName as name,aa.adminEmail as email  from adminaccount aa
                    inner join admin_privileges ap on aa.admintypeID = ap.admintypeID
                    where ap.menuItems = '$menuItem' and aa.isSupportAdmin=0";
            $adminDetails = $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $adminDetails;
    }
    /**
     * @param AdminLoginRequest $request
     * @return Object|null|Admin
     * @throws ProfessionalException
     */
    public function verifyAdminAccountAndFetchDetails(AdminLoginRequest $request)
    {
        $request = $this->realEscapeObject($request);
        $response = null;
        $sql = "SELECT adminID,adminAccount,adminPassword,adminName,adminEmail,admintypeID 
                FROM adminaccount WHERE adminPassword='$request->password'";
        if (!empty($request->adminAccount)) {
            $sql .= " AND adminAccount='$request->adminAccount";
        } elseif (!empty($request->email)) {
            $sql .= " AND adminEmail='$request->email";
        }
        try {
            $response = $this->executeQueryForObject($sql, false, $this->mapper[AdminServiceMapper::GET_ADMIN_DETAILS_MAPPER]);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $response;
    }
    /**
     * @param AdminLoginRequest $request
     * @return Object|null|Admin
     * @throws ProfessionalException
     */
    public function getAllSupportAdmins()
    {
        $response = null;
        $sql = "SELECT adminID,adminAccount,adminName,admintypeID 
                FROM adminaccount WHERE isSupportAdmin = 1 ";
        try {
            $response = $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $response;
    }
    /**
     * Reset admin password
     * @param $adminId
     * @param $adminAccount
     * @return $new password
     * @throws ProfessionalException
     */
    public function resetPassord($adminId,$adminAccount){
        $adminId = $this->realEscapeString($adminId);
        $adminAccount = $this->realEscapeString($adminAccount);
        $date = date('dmy');
        $newPassword = $date."-".$adminAccount; 
        $sql = "UPDATE adminaccount SET adminPassword = md5('$newPassword') WHERE adminID = $adminId";
        try {
            if($this->executeQuery($sql)){
                return $newPassword;
            }
        } catch (\Exception $e) {
            throw new ProfessionalException ($e->getCode(), $e->getMessage());
        }
    }
    /**
     * Block admin login
     * @param $adminId
     * @param $status
     * @return null
     * @throws ProfessionalException
     */
    public function blockAdmin($adminId,$status){
        $adminId = $this->realEscapeString($adminId);
        $status = $this->realEscapeString($status);
        $sql = "UPDATE adminaccount SET is_blocked = $status WHERE adminID = $adminId";
        try {
            return $this->executeQuery($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException ($e->getCode(), $e->getMessage());
        }
    }
    /**
     * Checking enableStudentFieldReset from admin side
     * @param int $adminTypeId
     * @return boolean
     * @throws ProfessionalException
     */
    public function enableStudentFieldReset($adminTypeId)
    {
        $enableStudentFieldReset = false;
        try {
            $authGroupId = $adminTypeId;
            $permission[] = SettingsConstents::ENABLE_STUDENT_FIELDS_RESET_FROM_ADMIN;
            $enableStudentFieldReset = PermissionService::getInstance()->checkAuthGroupPermission ( $authGroupId, $permission);
        } catch (\Exception $e) {
            throw new ProfessionalException ($e->getCode(), $e->getMessage());
        }
        return $enableStudentFieldReset;
    }
    function softDeleteField($tableName, $fieldRef, $fieldValue)
    {
        $sql        = "UPDATE " . $tableName . " SET isDeleted = 1 WHERE " . $fieldRef . "='" . $fieldValue . "'";
        try {
            $this->executeQuery($sql);
            return true;
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
}