middleware('auth'); $this->_service = 'contact'; $this->_module = 'contactattribute'; $this->_viewname = 'contactattribute'; $this->_title = 'Contact Attributes'; $this->_base_url = '/contactattribute'; } public function form($type, $id = null) { $values = []; if ($type == 'edit') { $response = LambdaRequest::signedrequest('get', $this->_service, $this->_module.'/get_one', ['id' => $id]); if (isset($response->statusCode) && $response->statusCode == 401) { return redirect('/unauthorised'); } $values = $response->data->item ?? []; if (is_object($values)) { if (isset($values->exclusions) && is_string($values->exclusions) && $values->exclusions !== '') { $values->exclusions = array_values(array_filter(array_map('trim', explode(',', $values->exclusions)))); } else { $values->exclusions = []; } if (isset($values->inclusions) && is_string($values->inclusions) && $values->inclusions !== '') { $values->inclusions = array_values(array_filter(array_map('trim', explode(',', $values->inclusions)))); } else { $values->inclusions = []; } if (!isset($values->max_count)) { $values->max_count = 0; } if (!isset($values->is_tab)) { $values->is_tab = 'no'; } if (!isset($values->tab)) { $values->tab = ''; } if (!isset($values->tag_instructions)) { $values->tag_instructions = ''; } if (!isset($values->workflow)) { $values->workflow = 'no'; } if (!isset($values->send_approval_to_manager_if_approver_vacant)) { $values->send_approval_to_manager_if_approver_vacant = 'no'; } if (!isset($values->workflow_type)) { $values->workflow_type = 'single'; } if (!isset($values->workflow_approvers) || !is_array($values->workflow_approvers)) { $values->workflow_approvers = $values->workflow === 'yes' ? ['l1manager'] : []; } if (!isset($values->sort_order) || $values->sort_order === null || $values->sort_order === '') { $values->sort_order = 0; } } } else { $values = (object) [ 'max_count' => 0, 'exclusions' => [], 'inclusions' => [], 'is_tab' => 'no', 'tab' => '', 'tag_instructions' => '', 'workflow' => 'no', 'send_approval_to_manager_if_approver_vacant' => 'no', 'workflow_type' => 'single', 'workflow_approvers' => ['l1manager'], 'sort_order' => 0, ]; } $this->_beforeform($type, $values); $fields = $this->get_form_fields($type, $values); return $this->view('form')->with([ 'title' => $this->_title, 'mode' => $type, 'fields' => $fields, 'base_url' => $this->_base_url, 'item' => $values, ]); } public function list(Request $request) { $response = LambdaRequest::signedrequest('get', $this->_service, $this->_module.'/get_all'); //echo '
'; print_r($response); exit;
        $fields = $this->get_list_fields();
        return $this->view('list')->with([
            'title' => $this->_title,
            'fields' => $fields,
            'base_url' => $this->_base_url,
            'items' => $response->data->items,
            'buttons' => $this->get_list_buttons()
        ]);
    }

    protected function get_list_fields(){
        return [
            'actions' => ['title' => 'Actions', 'buttons' => [ 'edit', 'delete' ] ],
            //'media_thumb' => ['title' => 'Media Thumb', 'type' => 'image'],
            'attribute' => ['title' => 'Attribute', 'type' => 'text'],
            'sort_order' => ['title' => 'Sort Order', 'type' => 'number'],
            'workflow' => ['title' => 'Workflow', 'type' => 'text'],
            'status' => ['title' => 'Status', 'type' => 'select'],
        ];
    }

    protected function get_form_fields($type, $values){
        $exclusionOptions = [];
        $tabOptions = [];
        $listResponse = LambdaRequest::signedrequest('get', $this->_service, $this->_module.'/get_all');
        if (isset($listResponse->data->items) && is_array($listResponse->data->items)) {
            foreach ($listResponse->data->items as $attr) {
                if (isset($attr->tab) && is_string($attr->tab)) {
                    $tabName = trim($attr->tab);
                    if ($tabName !== '') {
                        $tabOptions[$tabName] = $tabName;
                    }
                }
                if (!isset($attr->code) || (string) $attr->code === '') {
                    continue;
                }
                if ($type === 'edit' && is_object($values) && isset($values->code) && $attr->code === $values->code) {
                    continue;
                }
                $label = isset($attr->attribute) ? $attr->attribute : $attr->code;
                $exclusionOptions[$attr->code] = $label;
            }
        }
        if ($type === 'edit' && is_object($values) && isset($values->tab) && is_string($values->tab)) {
            $currentTab = trim($values->tab);
            if ($currentTab !== '' && !array_key_exists($currentTab, $tabOptions)) {
                $tabOptions[$currentTab] = $currentTab;
            }
        }
        ksort($tabOptions);

        $users_dd = ['l1manager' => 'L1 Manager', 'l2manager' => 'L2 Manager', 'l3manager' => 'L3 Manager', 'l4manager' => 'L4 Manager'];
        $users = $this->getlambda('get', 'ums', 'users', 'dropdown');
        if (isset($users->data->items) && is_array($users->data->items)) {
            foreach ($users->data->items as $user) {
                $users_dd[$user->id] = $user->firstname . ' ' . $user->lastname . ' (' . $user->employee_code . ')';
            }
        }

        return [
            'attribute' => ['title' => 'Attribute', 'type' => 'text', 'required' => true],
            'code' => ['title' => 'Code (small caps without spaces and special chars except "_")', 'type' => 'text', 'required' => true],
            'required' => ['title' => 'Is Required', 'type' => 'yesno', 'required' => true],
            'editable' => ['title' => 'Is Editable', 'type' => 'yesno', 'required' => true],
            'is_tag' => ['title' => 'Is Tag', 'type' => 'yesno', 'required' => true, 'default' => 'no'],
            'tag_instructions' => ['title' => 'Tag Instructions', 'type' => 'textarea', 'required' => false, 'fullwidth' => true],
            'tab' => ['title' => 'Tab', 'type' => 'datalist', 'required' => false, 'values' => $tabOptions, 'placeholder' => 'Choose an existing tab or type a new name'],
            'type' => ['title' => 'Attribute Type', 'type' => 'select', 'required' => true, 'values' => ['text' => 'Text', 'number' => 'Number', 'date' => 'Date', 'email' => 'Email', 'url' => 'Url', 'select' => 'Dropdown', 'multiselect' => 'Multi Select', 'checkbox' => 'Checkbox',  'radio' => 'Radio', 'image' => 'Image']  ],
            
            'sort_order' => ['title' => 'Sort Order', 'type' => 'number', 'required' => true],
            'max_count' => ['title' => 'Max no of Contacts per position', 'type' => 'number', 'required' => false, 'default' => 0],
            'min_count' => ['title' => 'Min no of Contacts per position', 'type' => 'number', 'required' => false, 'default' => 0],
            'exclusions' => ['title' => 'Exclusions', 'type' => 'multiselect', 'required' => false, 'values' => $exclusionOptions],
            'inclusions' => ['title' => 'Inclusions', 'type' => 'multiselect', 'required' => false, 'values' => $exclusionOptions],
            'status' => ['title' => 'Status', 'type' => 'select', 'required' => true, 'values' => ['active' => 'Active', 'disabled' => 'Disabled'] ],
            'workflow' => ['title' => 'Enable Workflow', 'type' => 'yesno', 'required' => true, 'default' => 'no'],
            'send_approval_to_manager_if_approver_vacant' => [
                'title' => 'Send Approval Notification to Manager if Approver is Vacant',
                'type' => 'yesno',
                'required' => true,
                'default' => 'no',
                'workflow_only' => true,
            ],
            'workflow_type' => [
                'title' => 'Workflow Type',
                'type' => 'select',
                'required' => true,
                'default' => 'single',
                'workflow_only' => true,
                'values' => ['single' => 'Single', 'multiple' => 'Multiple', 'sequencial' => 'Sequential'],
            ],
            'workflow_approvers' => [
                'title' => 'Workflow Approvers',
                'type' => 'mapping',
                'required' => false,
                'approvers' => $users_dd,
                'fullwidth' => true,
                'workflow_only' => true,
            ],
            'options' => ['title' => 'Options', 'type' => 'mapping', 'fullwidth' => true ],
        ];
    }

    protected function _beforeform($type, $values) {
        if($type == 'edit') {
            $this->_request->session()->put('entity_id', $values->id);
        } else {
            $this->_request->session()->forget('entity_id');
        }
        return $values;
    }

    protected function _beforesave($type, $request) {
        $data = $request->except('_token');
        $data['id'] = $request->session()->get('entity_id');
        if(!$data['id']) {
            unset($data['id']);
        }
        $options = [];
        if(isset($data['option_value'])) {
            foreach($data['option_value'] as $k => $ov) {
                $options[] = ['value' => $ov, 'display' => $data['option_name'][$k] ];
            }
            $data['options'] = $options;
            unset($data['option_value']);
            unset($data['option_name']);
        }
        if (isset($data['exclusions']) && is_array($data['exclusions'])) {
            $data['exclusions'] = implode(',', array_filter(array_map('strval', $data['exclusions'])));
        } else {
            $data['exclusions'] = '';
        }
        if (isset($data['inclusions']) && is_array($data['inclusions'])) {
            $data['inclusions'] = implode(',', array_filter(array_map('strval', $data['inclusions'])));
        } else {
            $data['inclusions'] = '';
        }
        if (!isset($data['max_count']) || $data['max_count'] === '' || $data['max_count'] === null) {
            $data['max_count'] = 0;
        } else {
            $data['max_count'] = (int) $data['max_count'];
        }
        if (!isset($data['sort_order']) || $data['sort_order'] === '' || $data['sort_order'] === null) {
            $data['sort_order'] = 0;
        } else {
            $data['sort_order'] = (int) $data['sort_order'];
        }
        if (!isset($data['is_tab']) || !in_array($data['is_tab'], ['yes', 'no'], true)) {
            $data['is_tab'] = 'no';
        }
        if (isset($data['tab']) && is_string($data['tab'])) {
            $data['tab'] = trim($data['tab']);
        } else {
            $data['tab'] = '';
        }
        if (!isset($data['workflow']) || !in_array($data['workflow'], ['yes', 'no'], true)) {
            $data['workflow'] = 'no';
        }
        if (!isset($data['send_approval_to_manager_if_approver_vacant']) || !in_array($data['send_approval_to_manager_if_approver_vacant'], ['yes', 'no'], true)) {
            $data['send_approval_to_manager_if_approver_vacant'] = 'no';
        }
        if (!isset($data['workflow_type']) || !in_array($data['workflow_type'], ['single', 'sequencial'], true)) {
            $data['workflow_type'] = 'single';
        }
        if (isset($data['workflow_approvers']) && is_array($data['workflow_approvers'])) {
            $data['workflow_approvers'] = array_values(array_filter($data['workflow_approvers']));
        } else {
            $data['workflow_approvers'] = [];
        }
        if ($data['workflow'] !== 'yes') {
            $data['workflow_type'] = 'single';
            $data['workflow_approvers'] = [];
            $data['send_approval_to_manager_if_approver_vacant'] = 'no';
        } elseif (empty($data['workflow_approvers'])) {
            $data['workflow_approvers'] = ['l1manager'];
        }
        return $data;
    }
}