File "PhpcgUsersProfiles.php"

Full path: /home/humancap/cl.humancap.com.my/admin/class/crud/PhpcgUsersProfiles.php
File size: 20.2 KB B
MIME-type: text/x-php
Charset: utf-8

Download   Open   Edit   Advanced Editor   Back

<?php
namespace crud;

use common\Utils;
use phpformbuilder\database\DB;
use phpformbuilder\database\Pagination;
use secure\Secure;

class PhpcgUsersProfiles extends Elements
{

    // item name passed in url
    public $item;

    // item name displayed
    public $item_label;

    // associative array : field => field displayed name
    public $fields;

    // primary key passed to create|edit|delete
    public $primary_keys; // primary keys fieldnames

    // CREATE rights
    public $can_create = false;

    public $pks = array(); // primary key values for each row
    public $pk_concat_values = array(); // concatenated values of primary key(s) for each row
    public $pk_url_params = array(); // primary key(s) sent to the edit/delete forms URL for each row
    public $update_record_authorized = array();
    public $id = array();
    public $profile_name = array();
    public $r_assess_bi = array();
    public $u_assess_bi = array();
    public $cd_assess_bi = array();
    public $cq_assess_bi = array();
    public $r_assessment = array();
    public $u_assessment = array();
    public $cd_assessment = array();
    public $cq_assessment = array();
    public $r_assessment_by_assessor = array();
    public $u_assessment_by_assessor = array();
    public $cd_assessment_by_assessor = array();
    public $cq_assessment_by_assessor = array();
    public $r_behavioral_indicator = array();
    public $u_behavioral_indicator = array();
    public $cd_behavioral_indicator = array();
    public $cq_behavioral_indicator = array();
    public $r_company = array();
    public $u_company = array();
    public $cd_company = array();
    public $cq_company = array();
    public $r_competency = array();
    public $u_competency = array();
    public $cd_competency = array();
    public $cq_competency = array();
    public $r_competency_type = array();
    public $u_competency_type = array();
    public $cd_competency_type = array();
    public $cq_competency_type = array();
    public $r_field = array();
    public $u_field = array();
    public $cd_field = array();
    public $cq_field = array();
    public $r_industry = array();
    public $u_industry = array();
    public $cd_industry = array();
    public $cq_industry = array();
    public $r_position = array();
    public $u_position = array();
    public $cd_position = array();
    public $cq_position = array();
    public $r_position_competency = array();
    public $u_position_competency = array();
    public $cd_position_competency = array();
    public $cq_position_competency = array();
    public $r_project = array();
    public $u_project = array();
    public $cd_project = array();
    public $cq_project = array();
    public $r_phpcg_users = array();
    public $u_phpcg_users = array();
    public $cd_phpcg_users = array();
    public $cq_phpcg_users = array();
    public $r_phpcg_users_profiles = array();
    public $u_phpcg_users_profiles = array();
    public $cd_phpcg_users_profiles = array();
    public $cq_phpcg_users_profiles = array();

    public $active_filtered_fields = array();
    public $debug_content = '';
    public $export_data_button;
    public $filters_form;
    public $is_single_view = false;
    public $item_url;
    public $join_query = '';
    public $main_pdo_settings = array();
    public $pagination_html;

    // Array of primary fieldnames => values to select a single record for view
    public $params;

    public $records_count;
    public $select_number_per_page;
    public $sorting;

    public function __construct($element, $params = array())
    {
        $this->table         = $element->table;
        $this->item          = $element->item;
        $this->item_label    = $element->item_label;
        $this->primary_keys  = $element->primary_keys;
        $this->select_data   = $element->select_data;
        $this->fields        = $element->fields;

        $table = $this->table;

        $this->params = $params;

        if (!empty($params)) {
            $this->is_single_view = true;
        }

        $json = file_get_contents(ADMIN_DIR . 'crud-data/' . $this->item . '-filter-data.json');
        $filters_array = json_decode($json, true);
        $this->item_url = $_SERVER['REQUEST_URI'];

        // connect to the database
        $db = new Pagination(DEBUG);
        $db->setDebugMode('register');


        $columns = 'phpcg_users_profiles.id, phpcg_users_profiles.profile_name, phpcg_users_profiles.r_assess_bi, phpcg_users_profiles.u_assess_bi, phpcg_users_profiles.cd_assess_bi, phpcg_users_profiles.cq_assess_bi, phpcg_users_profiles.r_assessment, phpcg_users_profiles.u_assessment, phpcg_users_profiles.cd_assessment, phpcg_users_profiles.cq_assessment, phpcg_users_profiles.r_assessment_by_assessor, phpcg_users_profiles.u_assessment_by_assessor, phpcg_users_profiles.cd_assessment_by_assessor, phpcg_users_profiles.cq_assessment_by_assessor, phpcg_users_profiles.r_behavioral_indicator, phpcg_users_profiles.u_behavioral_indicator, phpcg_users_profiles.cd_behavioral_indicator, phpcg_users_profiles.cq_behavioral_indicator, phpcg_users_profiles.r_company, phpcg_users_profiles.u_company, phpcg_users_profiles.cd_company, phpcg_users_profiles.cq_company, phpcg_users_profiles.r_competency, phpcg_users_profiles.u_competency, phpcg_users_profiles.cd_competency, phpcg_users_profiles.cq_competency, phpcg_users_profiles.r_competency_type, phpcg_users_profiles.u_competency_type, phpcg_users_profiles.cd_competency_type, phpcg_users_profiles.cq_competency_type, phpcg_users_profiles.r_field, phpcg_users_profiles.u_field, phpcg_users_profiles.cd_field, phpcg_users_profiles.cq_field, phpcg_users_profiles.r_industry, phpcg_users_profiles.u_industry, phpcg_users_profiles.cd_industry, phpcg_users_profiles.cq_industry, phpcg_users_profiles.r_position, phpcg_users_profiles.u_position, phpcg_users_profiles.cd_position, phpcg_users_profiles.cq_position, phpcg_users_profiles.r_position_competency, phpcg_users_profiles.u_position_competency, phpcg_users_profiles.cd_position_competency, phpcg_users_profiles.cq_position_competency, phpcg_users_profiles.r_project, phpcg_users_profiles.u_project, phpcg_users_profiles.cd_project, phpcg_users_profiles.cq_project, phpcg_users_profiles.r_phpcg_users, phpcg_users_profiles.u_phpcg_users, phpcg_users_profiles.cd_phpcg_users, phpcg_users_profiles.cq_phpcg_users, phpcg_users_profiles.r_phpcg_users_profiles, phpcg_users_profiles.u_phpcg_users_profiles, phpcg_users_profiles.cd_phpcg_users_profiles, phpcg_users_profiles.cq_phpcg_users_profiles';
        $where = array();

        // restricted rights query
        if (Secure::canReadRestricted($table)) {
            $where = array_merge($where, Secure::getRestrictionQuery($table));
        }

        // filters
        $filters = new ElementsFilters($table, $filters_array, $this->join_query);
        $this->active_filtered_fields = $filters->getActiveFilteredFields();
        $where_filters = $filters->getWhere();
        $where = array_merge($where, $where_filters);

        // search
        $where_search = array();
        if (isset($_POST['search_field']) && isset($_POST['search_string'])) {
            $searchVals = explode(' + ', $_POST['search_string']);
            $search_string = $searchVals[0];
            $_SESSION['rp_search_field'][$table] = $_POST['search_field'];
            $_SESSION['rp_search_string'][$table] = $search_string;
            if (sizeof($searchVals) > 1) {
                $_SESSION['rp_search_string_2'][$table] = $searchVals[1];
            } else {
                unset($_SESSION['rp_search_string_2'][$table]);
            }
        }

        if (isset($_SESSION['rp_search_string'][$table]) && !empty($_SESSION['rp_search_string'][$table])) {
            $sf = $_SESSION['rp_search_field'][$table];
            $search_field = $table . '.' . $sf;
            $search_field2 = '';
            $search_string_sqlvalue = $db->safe('%' . $_SESSION['rp_search_string'][$table] . '%');
            if (isset($_SESSION['rp_search_string_2'][$table])) {
                $search_string_2_sqlvalue = $db->safe('%' . $_SESSION['rp_search_string_2'][$table] . '%');
            }
            if (file_exists(ADMIN_DIR . 'crud-data/' . $this->item . '-select-data.json')) {
                $json = file_get_contents(ADMIN_DIR . 'crud-data/' . $this->item . '-select-data.json');
                $selects_array = json_decode($json, true);
                if (isset($selects_array[$sf]) && $selects_array[$sf]['from'] == 'from_table') {
                    $search_field = $selects_array[$sf]['from_table'] . '.' . $selects_array[$sf]['from_field_1'];
                    if (!empty($selects_array[$sf]['from_field_2'])) {
                        $search_field2 = $selects_array[$sf]['from_table'] . '.' . $selects_array[$sf]['from_field_2'];
                    }
                }
            }
            $where_search[] = 'LOWER(' . $search_field . ') LIKE LOWER(' . $search_string_sqlvalue . ')';
            if (!empty($search_field2) && isset($search_string_2_sqlvalue) && ($search_string_2_sqlvalue != "'%%'")) {
                $where_search[] = 'LOWER(' . $search_field2 . ') LIKE LOWER(' . $search_string_2_sqlvalue . ')';
            }
            $where = array_merge($where, $where_search);
        }

        $this->filters_form = $filters->returnForm($this->item_url);

        // Get join queries from active filters
        $active_filters_join_queries = $filters->buildElementJoinQuery();

        if (isset($_POST['search_field'])) {
            $pagination_url = str_replace(ADMIN_URL . 'search/', ADMIN_URL, $_SERVER['REQUEST_URI']);
        } else {
            $pagination_url = $_SERVER['REQUEST_URI'];
        }
        if (isset($_POST['npp']) && is_numeric($_POST['npp'])) {
            $_SESSION['npp'] = $_POST['npp'];
        } elseif (!isset($_SESSION['npp'])) {
            $_SESSION['npp'] = 20;
        }
        if ($this->is_single_view) {
            // if single record view
            $active_filters_join_queries = $filters->buildElementJoinQuery();
            $pagination_url = '';
            // replace 'fieldname' with 'table.fieldname' to avoid ambigous query
            $where_params = array_combine(
                array_map(function ($k) {
                    return $this->table . '.' . $k;
                }, array_keys($this->params)),
                $this->params
            );
            $where = array_merge($where, $where_params);
        }

        // order query
        $this->sorting = ElementsUtilities::getSorting($table, 'id', 'ASC');

        $npp = $_SESSION['npp'];
        if (!empty($where_search) && PAGINE_SEARCH_RESULTS === false) {
            $npp = 1000000;
        }

        if (empty($where)) {
            $where = null;
        }

        // $this->main_pdo_settings are the PDO settings without the pagination LIMIT.
        $this->main_pdo_settings = array(
            'function' => 'select',
            'from'    => 'phpcg_users_profiles' . $active_filters_join_queries,
            'values'   => $columns,
            'where'    => $where,
            'extras'   => array('order_by' => $this->sorting),
            'debug'    => DEBUG_DB_QUERIES
        );

        $this->pagination_html = $db->pagine($this->main_pdo_settings, $npp, 'p', $pagination_url, 5, true, '/', '');

        if (DEBUG_DB_QUERIES) {
            $this->debug_content .= '<p class="debug-title text-bg-info">"' . $this->table . '" queries</p>' . $db->getDebugContent();
        }

        $update_authorized = false;
        if (Secure::canUpdate($this->table)) {
            // user can update ALL the records
            $update_authorized = true;
        }

        $this->records_count = $db->rowCount();
        if (!empty($this->records_count)) {
            while ($row = $db->fetch()) {
                $primary_keys_array = array(
                    'id' => $row->id
                );
                $this->pks[] = $primary_keys_array;
                $pk_concatenated_values = $row->id;
                $this->pk_concat_values[] = $pk_concatenated_values;
                $this->update_record_authorized[$pk_concatenated_values] = $update_authorized;
                $this->pk_url_params[] = http_build_query($primary_keys_array, '', '/');
                $this->id[] = ElementsUtilities::getUserProfileValue($row->id);
                $this->profile_name[] = ElementsUtilities::getUserProfileValue($row->profile_name);
                $this->r_assess_bi[] = ElementsUtilities::getUserProfileValue($row->r_assess_bi);
                $this->u_assess_bi[] = ElementsUtilities::getUserProfileValue($row->u_assess_bi);
                $this->cd_assess_bi[] = ElementsUtilities::getUserProfileValue($row->cd_assess_bi);
                $this->cq_assess_bi[] = ElementsUtilities::getUserProfileValue($row->cq_assess_bi);
                $this->r_assessment[] = ElementsUtilities::getUserProfileValue($row->r_assessment);
                $this->u_assessment[] = ElementsUtilities::getUserProfileValue($row->u_assessment);
                $this->cd_assessment[] = ElementsUtilities::getUserProfileValue($row->cd_assessment);
                $this->cq_assessment[] = ElementsUtilities::getUserProfileValue($row->cq_assessment);
                $this->r_assessment_by_assessor[] = ElementsUtilities::getUserProfileValue($row->r_assessment_by_assessor);
                $this->u_assessment_by_assessor[] = ElementsUtilities::getUserProfileValue($row->u_assessment_by_assessor);
                $this->cd_assessment_by_assessor[] = ElementsUtilities::getUserProfileValue($row->cd_assessment_by_assessor);
                $this->cq_assessment_by_assessor[] = ElementsUtilities::getUserProfileValue($row->cq_assessment_by_assessor);
                $this->r_behavioral_indicator[] = ElementsUtilities::getUserProfileValue($row->r_behavioral_indicator);
                $this->u_behavioral_indicator[] = ElementsUtilities::getUserProfileValue($row->u_behavioral_indicator);
                $this->cd_behavioral_indicator[] = ElementsUtilities::getUserProfileValue($row->cd_behavioral_indicator);
                $this->cq_behavioral_indicator[] = ElementsUtilities::getUserProfileValue($row->cq_behavioral_indicator);
                $this->r_company[] = ElementsUtilities::getUserProfileValue($row->r_company);
                $this->u_company[] = ElementsUtilities::getUserProfileValue($row->u_company);
                $this->cd_company[] = ElementsUtilities::getUserProfileValue($row->cd_company);
                $this->cq_company[] = ElementsUtilities::getUserProfileValue($row->cq_company);
                $this->r_competency[] = ElementsUtilities::getUserProfileValue($row->r_competency);
                $this->u_competency[] = ElementsUtilities::getUserProfileValue($row->u_competency);
                $this->cd_competency[] = ElementsUtilities::getUserProfileValue($row->cd_competency);
                $this->cq_competency[] = ElementsUtilities::getUserProfileValue($row->cq_competency);
                $this->r_competency_type[] = ElementsUtilities::getUserProfileValue($row->r_competency_type);
                $this->u_competency_type[] = ElementsUtilities::getUserProfileValue($row->u_competency_type);
                $this->cd_competency_type[] = ElementsUtilities::getUserProfileValue($row->cd_competency_type);
                $this->cq_competency_type[] = ElementsUtilities::getUserProfileValue($row->cq_competency_type);
                $this->r_field[] = ElementsUtilities::getUserProfileValue($row->r_field);
                $this->u_field[] = ElementsUtilities::getUserProfileValue($row->u_field);
                $this->cd_field[] = ElementsUtilities::getUserProfileValue($row->cd_field);
                $this->cq_field[] = ElementsUtilities::getUserProfileValue($row->cq_field);
                $this->r_industry[] = ElementsUtilities::getUserProfileValue($row->r_industry);
                $this->u_industry[] = ElementsUtilities::getUserProfileValue($row->u_industry);
                $this->cd_industry[] = ElementsUtilities::getUserProfileValue($row->cd_industry);
                $this->cq_industry[] = ElementsUtilities::getUserProfileValue($row->cq_industry);
                $this->r_position[] = ElementsUtilities::getUserProfileValue($row->r_position);
                $this->u_position[] = ElementsUtilities::getUserProfileValue($row->u_position);
                $this->cd_position[] = ElementsUtilities::getUserProfileValue($row->cd_position);
                $this->cq_position[] = ElementsUtilities::getUserProfileValue($row->cq_position);
                $this->r_position_competency[] = ElementsUtilities::getUserProfileValue($row->r_position_competency);
                $this->u_position_competency[] = ElementsUtilities::getUserProfileValue($row->u_position_competency);
                $this->cd_position_competency[] = ElementsUtilities::getUserProfileValue($row->cd_position_competency);
                $this->cq_position_competency[] = ElementsUtilities::getUserProfileValue($row->cq_position_competency);
                $this->r_project[] = ElementsUtilities::getUserProfileValue($row->r_project);
                $this->u_project[] = ElementsUtilities::getUserProfileValue($row->u_project);
                $this->cd_project[] = ElementsUtilities::getUserProfileValue($row->cd_project);
                $this->cq_project[] = ElementsUtilities::getUserProfileValue($row->cq_project);
                $this->r_phpcg_users[] = ElementsUtilities::getUserProfileValue($row->r_phpcg_users);
                $this->u_phpcg_users[] = ElementsUtilities::getUserProfileValue($row->u_phpcg_users);
                $this->cd_phpcg_users[] = ElementsUtilities::getUserProfileValue($row->cd_phpcg_users);
                $this->cq_phpcg_users[] = ElementsUtilities::getUserProfileValue($row->cq_phpcg_users);
                $this->r_phpcg_users_profiles[] = ElementsUtilities::getUserProfileValue($row->r_phpcg_users_profiles);
                $this->u_phpcg_users_profiles[] = ElementsUtilities::getUserProfileValue($row->u_phpcg_users_profiles);
                $this->cd_phpcg_users_profiles[] = ElementsUtilities::getUserProfileValue($row->cd_phpcg_users_profiles);
                $this->cq_phpcg_users_profiles[] = ElementsUtilities::getUserProfileValue($row->cq_phpcg_users_profiles);
            }
        }

        // Autocomplete doesn't need the followings settings
        if (!isset($_POST['is_autocomplete'])) {
            if (!$this->is_single_view) {
                // CREATE/DELETE rights
                if (Secure::canCreate($table) || Secure::canCreateRestricted($table)) {
                    $this->can_create = true;
                }

                // restricted UPDATE rights
                if (Secure::canUpdateRestricted($table)) {
                    $where = array_merge(
                        Secure::getRestrictionQuery($table),
                        $where_filters,
                        $where_search
                    );

                    $pdo_settings = array(
                        'function' => 'select',
                        'from'    => 'phpcg_users_profiles' . $active_filters_join_queries,
                        'values'   => $columns,
                        'where'    => $where,
                        'extras'   => array('order_by' => $this->sorting),
                        'debug'    => DEBUG_DB_QUERIES
                    );

                    // get authorized update primary keys
                    $db->pagine($pdo_settings, $npp, 'p', $pagination_url, 5, true, '/', '');
                    if (DEBUG_DB_QUERIES) {
                        $this->debug_content .= '<p class="debug-title text-bg-info">"' . $this->table . '" - get authorized update primary keys</p>' . $db->getDebugContent();
                    }
                    $records_count = $db->rowCount();
                    if (!empty($records_count)) {
                        while ($row = $db->fetch()) {
                            $this->update_record_authorized[$row->id] = true;
                        }
                    }
                }
            }
        } // end if

        if (!$this->is_single_view) {
            // Export data button
            $this->export_data_button = ElementsUtilities::exportDataButtons($table, $this->main_pdo_settings);

            // number/page
            $numbers_array = array(5, 10, 20, 50, 100, 200, 10000);
            $this->select_number_per_page = ElementsUtilities::selectNumberPerPage($numbers_array, $_SESSION['npp'], $this->item_url);
        }
    }
}