added person attributes MVC structure

This commit is contained in:
Damiano Chintala
2021-04-15 16:17:04 +02:00
committed by Jeroen Peelaerts
parent 4e51a13eb7
commit 8c080930c9
8 changed files with 1290 additions and 1 deletions

View File

@@ -159,5 +159,6 @@ $autoload['model'] = array(
'Tax',
'Tax_category',
'Tax_code',
'Tax_jurisdiction'
'Tax_jurisdiction',
'Person_attribute'
);

View File

@@ -0,0 +1,184 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once("Secure_Controller.php");
class Person_attributes extends Secure_Controller
{
public function __construct()
{
parent::__construct('person_attributes');
}
public function index()
{
$data['table_headers'] = $this->xss_clean(get_person_attribute_definition_manage_table_headers());
$this->load->view('person_attributes/manage', $data);
}
/**
* Returns person table data rows. This will be called with AJAX.
*/
public function search()
{
$search = $this->input->get('search');
$limit = $this->input->get('limit');
$offset = $this->input->get('offset');
$sort = $this->input->get('sort');
$order = $this->input->get('order');
$person_attributes = $this->Person_attribute->search($search, $limit, $offset, $sort, $order);
$total_rows = $this->Person_attribute->get_found_rows($search);
$data_rows = array();
foreach($person_attributes->result() as $person_attribute)
{
$person_attribute->definition_flags = $this->_get_person_attributes($person_attribute->definition_flags);
$data_rows[] = get_person_attribute_definition_data_row($person_attribute, $this);
}
$data_rows = $this->xss_clean($data_rows);
echo json_encode(array('total' => $total_rows, 'rows' => $data_rows));
}
public function save_person_attribute_value($person_attribute_value)
{
$success = $this->Person_attribute->save_value(urldecode($person_attribute_value), $this->input->post('definition_id'), $this->input->post('person_id'), $this->input->post('person_attribute_id'));
echo json_encode(array('success' => $success != 0));
}
public function delete_person_attribute_value($person_attribute_value)
{
$success = $this->Person_attribute->delete_value($person_attribute_value, $this->input->post('definition_id'));
echo json_encode(array('success' => $success));
}
public function save_definition($definition_id = NO_DEFINITION_ID)
{
$definition_flags = 0;
$flags = (empty($this->input->post('definition_flags'))) ? array() : $this->input->post('definition_flags');
foreach($flags as $flag)
{
$definition_flags |= $flag;
}
//Save definition data
$definition_data = array(
'definition_name' => $this->input->post('definition_name'),
'definition_unit' => $this->input->post('definition_unit') != '' ? $this->input->post('definition_unit') : NULL,
'definition_flags' => $definition_flags,
'definition_fk' => $this->input->post('definition_group') != '' ? $this->input->post('definition_group') : NULL
);
if ($this->input->post('definition_type') != null)
{
$definition_data['definition_type'] = DEFINITION_TYPES[$this->input->post('definition_type')];
}
$definition_name = $this->xss_clean($definition_data['definition_name']);
if($this->Person_attribute->save_definition($definition_data, $definition_id))
{
//New definition
if($definition_id == 0)
{
$definition_values = json_decode($this->input->post('definition_values'));
foreach($definition_values as $definition_value)
{
$this->Person_attribute->save_value($definition_value, $definition_data['definition_id']);
}
echo json_encode(array('success' => TRUE, 'message' => $this->lang->line('person_attributes_definition_successful_adding').' '.
$definition_name, 'id' => $definition_data['definition_id']));
}
//Existing definition
else
{
echo json_encode(array('success' => TRUE, 'message' => $this->lang->line('person_attributes_definition_successful_updating').' '.
$definition_name, 'id' => $definition_id));
}
}
//Failure
else
{
echo json_encode(array('success' => FALSE, 'message' => $this->lang->line('person_attributes_definition_error_adding_updating', $definition_name), 'id' => -1));
}
}
public function suggest_person_attribute($definition_id)
{
$suggestions = $this->xss_clean($this->Person_attribute->get_suggestions($definition_id, $this->input->get('term')));
echo json_encode($suggestions);
}
public function get_row($row_id)
{
$person_attribute_definition_info = $this->Person_attribute->get_info($row_id);
$person_attribute_definition_info->definition_flags = $this->_get_person_attributes($person_attribute_definition_info->definition_flags);
$data_row = $this->xss_clean(get_person_attribute_definition_data_row($person_attribute_definition_info));
echo json_encode($data_row);
}
private function _get_person_attributes($definition_flags = 0)
{
$definition_flag_names = array();
foreach (Person_attribute::get_definition_flags() as $id => $term)
{
if ($id & $definition_flags)
{
$definition_flag_names[$id] = $this->lang->line('person_attributes_' . strtolower($term) . '_visibility');
}
}
return $definition_flag_names;
}
public function view($definition_id = NO_DEFINITION_ID)
{
$info = $this->Person_attribute->get_info($definition_id);
foreach(get_object_vars($info) as $property => $value)
{
$info->$property = $this->xss_clean($value);
}
$data['definition_id'] = $definition_id;
$data['definition_values'] = $this->Person_attribute->get_definition_values($definition_id);
$data['definition_group'] = $this->Person_attribute->get_definitions_by_type(GROUP, $definition_id);
$data['definition_group'][''] = $this->lang->line('common_none_selected_text');
$data['definition_info'] = $info;
$show_all = Person_attribute::SHOW_IN_CUSTOMERS | Person_attribute::SHOW_IN_EMPLOYEES | Person_attribute::SHOW_IN_SUPPLIERS;
$data['definition_flags'] = $this->_get_person_attributes($show_all);
$selected_flags = $info->definition_flags === '' ? $show_all : $info->definition_flags;
$data['selected_definition_flags'] = $this->_get_person_attributes($selected_flags);
$this->load->view("person_attributes/form", $data);
}
public function delete_value($person_attribute_id)
{
return $this->Person_attribute->delete_value($person_attribute_id);
}
public function delete()
{
$person_attributes_to_delete = $this->input->post('ids');
if($this->Person_attribute->delete_definition_list($person_attributes_to_delete))
{
$message = $this->lang->line('person_attributes_definition_successful_deleted') . ' ' . count($person_attributes_to_delete) . ' ' . $this->lang->line('person_attributes_definition_one_or_multiple');
echo json_encode(array('success' => TRUE, 'message' => $message));
}
else
{
echo json_encode(array('success' => FALSE, 'message' => $this->lang->line('person_attributes_definition_cannot_be_deleted')));
}
}
}

View File

@@ -0,0 +1,30 @@
<?php
$lang["person_attributes_person_attribute_value_invalid_chars"] = "Person Attribute value cannot contain '_' or '|'";
$lang["person_attributes_confirm_delete"] = "Are you sure you want to delete the selected person_attribute(s)?";
$lang["person_attributes_confirm_restore"] = "Are you sure you want to restore the selected person attribute(s)?";
$lang["person_attributes_definition_cannot_be_deleted"] = "Could not delete selected person attribute(s)";
$lang["person_attributes_definition_error_adding_updating"] = "Person Attribute %1 could not be added or updated. Please check the error log.";
$lang["person_attributes_definition_flags"] = "Person Attribute Visibility";
$lang["person_attributes_definition_group"] = "Group";
$lang["person_attributes_definition_id"] = "Id";
$lang["person_attributes_definition_name"] = "Add Person Attribute";
$lang["person_attributes_definition_name_required"] = "Person Attribute name is a required field";
$lang["person_attributes_definition_one_or_multiple"] = "person_attribute(s)";
$lang["person_attributes_definition_successful_adding"] = "You have successfully added item";
$lang["person_attributes_definition_successful_deleted"] = "You have successfully deleted";
$lang["person_attributes_definition_successful_updating"] = "You have successfully updated person attribute";
$lang["person_attributes_definition_type"] = "Person Attribute Type";
$lang["person_attributes_definition_type_required"] = "Person Attribute type is a required field";
$lang["person_attributes_definition_unit"] = "Measurement Unit";
$lang["person_attributes_definition_values"] = "Person Attribute Values";
$lang["person_attributes_new"] = "New Person Attribute";
$lang["person_attributes_no_person_attributes_to_display"] = "No Items to display";
$lang["person_attributes_receipt_visibility"] = "Receipt";
$lang["person_attributes_show_in_customers"] = "Show in customers";
$lang["person_attributes_show_in_customers_visibility"] = "Customers";
$lang["person_attributes_show_in_employees"] = "Show in employees";
$lang["person_attributes_show_in_employees_visibility"] = "Employees";
$lang["person_attributes_show_in_suppliers"] = "Show in suppliers";
$lang["person_attributes_show_in_suppliers_visibility"] = "Suppliers";
$lang["person_attributes_update"] = "Update Person Attribute";

View File

@@ -0,0 +1,665 @@
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Person_attribute class
*/
class Person_attribute extends CI_Model
{
const SHOW_IN_CUSTOMERS = 1;
const SHOW_IN_EMPLOYEES = 2;
const SHOW_IN_SUPPLIERS = 4;
public static function get_definition_flags()
{
$class = new ReflectionClass(__CLASS__);
return array_flip($class->getConstants());
}
/*
Determines if a given definition_id is an person_attribute
*/
public function exists($definition_id, $deleted = FALSE)
{
$this->db->from('person_attribute_definitions');
$this->db->where('definition_id', $definition_id);
$this->db->where('deleted', $deleted);
return ($this->db->get()->num_rows() == 1);
}
public function link_exists($person_id, $definition_id = FALSE)
{
$this->db->from('person_attribute_links');
if(empty($definition_id))
{
$this->db->where('definition_id <>');
$this->db->where('person_attribute_id');
}
else
{
$this->db->where('definition_id', $definition_id);
}
$this->db->where('person_id', $person_id);
return ($this->db->get()->num_rows() > 0);
}
/*
Determines if a given person_attribute_value exists in the person_attribute_values table and returns the person_attribute_id if it does
*/
public function value_exists($person_attribute_value)
{
$this->db->distinct('person_attribute_id');
$this->db->from('person_attribute_values');
$this->db->where('person_attribute_value', $person_attribute_value);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
return $query->row()->person_attribute_id;
}
return FALSE;
}
/*
Gets information about a particular person_attribute definition
*/
public function get_info($definition_id)
{
$this->db->select('parent_definition.definition_name AS definition_group, definition.*');
$this->db->from('person_attribute_definitions AS definition');
$this->db->join('person_attribute_definitions AS parent_definition', 'parent_definition.definition_id = definition.definition_fk', 'left');
$this->db->where('definition.definition_id', $definition_id);
$query = $this->db->get();
if($query->num_rows() == 1)
{
return $query->row();
}
else
{
//Get empty base parent object, as $person_id is NOT a customer
$item_obj = new stdClass();
//Get all the fields from customers table
foreach($this->db->list_fields('person_attribute_definitions') as $field)
{
$item_obj->$field = '';
}
return $item_obj;
}
}
/*
Performs a search on person_attribute definitions
*/
public function search($search, $rows = 0, $limit_from = 0, $sort = 'definition.definition_name', $order = 'asc')
{
$this->db->select('parent_definition.definition_name AS definition_group, definition.*');
$this->db->from('person_attribute_definitions AS definition');
$this->db->join('person_attribute_definitions AS parent_definition', 'parent_definition.definition_id = definition.definition_fk', 'left');
$this->db->group_start();
$this->db->like('definition.definition_name', $search);
$this->db->or_like('definition.definition_type', $search);
$this->db->group_end();
$this->db->where('definition.deleted', 0);
$this->db->order_by($sort, $order);
if($rows > 0)
{
$this->db->limit($rows, $limit_from);
}
return $this->db->get();
}
public function get_person_attributes_by_person($person_id)
{
$this->db->from('person_attribute_definitions');
$this->db->join('person_attribute_links', 'person_attribute_links.definition_id = person_attribute_definitions.definition_id');
$this->db->where('person_id', $person_id);
$this->db->where('deleted', 0);
$this->db->order_by('definition_name','ASC');
$results = $this->db->get()->result_array();
return $this->_to_array($results, 'definition_id');
}
public function get_values_by_definitions($definition_ids)
{
if(count($definition_ids ? : []))
{
$this->db->from('person_attribute_definitions');
$this->db->group_start();
$this->db->where_in('definition_fk', array_keys($definition_ids));
$this->db->or_where_in('definition_id', array_keys($definition_ids));
$this->db->where('definition_type !=', GROUP);
$this->db->group_end();
$this->db->where('deleted', 0);
$results = $this->db->get()->result_array();
return $this->_to_array($results, 'definition_id');
}
return array();
}
public function get_definitions_by_type($person_attribute_type, $definition_id = NO_DEFINITION_ID)
{
$this->db->from('person_attribute_definitions');
$this->db->where('definition_type', $person_attribute_type);
$this->db->where('deleted', 0);
if($definition_id != CATEGORY_DEFINITION_ID)
{
$this->db->where('definition_id != ', $definition_id);
}
$this->db->where('definition_fk');
$results = $this->db->get()->result_array();
return $this->_to_array($results, 'definition_id', 'definition_name');
}
public function get_definitions_by_flags($definition_flags)
{
$this->db->from('person_attribute_definitions');
$this->db->where('definition_flags &', $definition_flags);
$this->db->where('deleted', 0);
$this->db->where('definition_type <>', GROUP);
$this->db->order_by('definition_id');
$results = $this->db->get()->result_array();
return $this->_to_array($results, 'definition_id', 'definition_name');
}
/**
* Returns an array of person_attribute definition names and IDs
*
* @param boolean $groups If FALSE does not return GROUP type person_attributes in the array
* @return array Array containing definition IDs, person_attribute names and -1 index with the local language '[SELECT]' line.
*/
public function get_definition_names($groups = TRUE)
{
$this->db->from('person_attribute_definitions');
$this->db->where('deleted', 0);
$this->db->order_by('definition_name','ASC');
if($groups === FALSE)
{
$this->db->where_not_in('definition_type',GROUP);
}
$results = $this->db->get()->result_array();
$definition_name = array(-1 => $this->lang->line('common_none_selected_text'));
return $definition_name + $this->_to_array($results, 'definition_id', 'definition_name');
}
public function get_definition_values($definition_id)
{
$person_attribute_values = [];
if($definition_id > 0 || $definition_id == CATEGORY_DEFINITION_ID)
{
$this->db->from('person_attribute_links');
$this->db->join('person_attribute_values', 'person_attribute_values.person_attribute_id = person_attribute_links.person_attribute_id');
$this->db->where('definition_id', $definition_id);
$this->db->where('person_id');
$this->db->order_by('person_attribute_value','ASC');
$results = $this->db->get()->result_array();
return $this->_to_array($results, 'person_attribute_id', 'person_attribute_value');
}
return $person_attribute_values;
}
private function _to_array($results, $key, $value = '')
{
return array_column(array_map(function($result) use ($key, $value) {
return [$result[$key], empty($value) ? $result : $result[$value]];
}, $results), 1, 0);
}
/*
Gets total of rows
*/
public function get_total_rows()
{
$this->db->from('person_attribute_definitions');
$this->db->where('deleted', 0);
return $this->db->count_all_results();
}
/*
Get number of rows
*/
public function get_found_rows($search)
{
return $this->search($search)->num_rows();
}
private function check_data_validity($definition, $from, $to)
{
$success = FALSE;
if($from === TEXT)
{
$this->db->select('person_id,person_attribute_value');
$this->db->from('person_attribute_values');
$this->db->join('person_attribute_links', 'person_attribute_values.person_attribute_id = person_attribute_links.person_attribute_id');
$this->db->where('definition_id',$definition);
$success = TRUE;
if($to === DATE)
{
foreach($this->db->get()->result_array() as $row)
{
if(valid_date($row['person_attribute_value']) === FALSE)
{
log_message('ERROR', 'person_id: ' . $row['person_id'] . ' with person_attribute_value: ' . $row['person_attribute_value'] . ' cannot be converted to datetime');
$success = FALSE;
}
}
}
else if($to === DECIMAL)
{
foreach($this->db->get()->result_array() as $row)
{
if(valid_decimal($row['person_attribute_value']) === FALSE)
{
log_message('ERROR', 'person_id: ' . $row['person_id'] . ' with person_attribute_value: ' . $row['person_attribute_value'] . ' cannot be converted to decimal');
$success = FALSE;
}
}
}
}
return $success;
}
private function convert_definition_type($definition_id, $from_type, $to_type)
{
$success = FALSE;
//From TEXT
if($from_type === TEXT)
{
//To DATETIME or DECIMAL
if(in_array($to_type, [DATE, DECIMAL], TRUE))
{
$field = ($to_type === DATE ? 'person_attribute_date' : 'person_attribute_decimal');
if($this->check_data_validity($definition_id, $from_type, $to_type))
{
$this->db->trans_start();
$query = 'UPDATE ospos_person_attribute_values ';
$query .= 'INNER JOIN ospos_person_attribute_links ';
$query .= 'ON ospos_person_attribute_values.person_attribute_id = ospos_person_attribute_links.person_attribute_id ';
$query .= 'SET '. $field .'= person_attribute_value, ';
$query .= 'person_attribute_value = NULL ';
$query .= 'WHERE definition_id = ' . $this->db->escape($definition_id);
$success = $this->db->query($query);
$this->db->trans_complete();
}
}
//To DROPDOWN or CHECKBOX
else if($to_type === DROPDOWN)
{
$success = TRUE;
}
else if($to_type === CHECKBOX)
{
$checkbox_person_attribute_values = $this->checkbox_person_attribute_values($definition_id);
$this->db->trans_start();
$query = 'UPDATE ospos_person_attribute_values values ';
$query .= 'INNER JOIN ospos_person_attribute_links links ';
$query .= 'ON values.person_attribute_id = links.person_attribute_id ';
$query .= "SET links.person_attribute_id = IF((values.person_attribute_value IN('FALSE','0','') OR (values.person_attribute_value IS NULL)), $checkbox_person_attribute_values[0], $checkbox_person_attribute_values[1]) ";
$query .= 'WHERE definition_id = ' . $this->db->escape($definition_id);
$success = $this->db->query($query);
$this->db->trans_complete();
}
}
//From DROPDOWN
else if($from_type === DROPDOWN)
{
//To TEXT
if(in_array($to_type, [TEXT, CHECKBOX], TRUE))
{
$this->db->trans_start();
$this->db->from('ospos_person_attribute_links');
$this->db->where('definition_id',$definition_id);
$this->db->where('person_id', NULL);
$success = $this->db->delete();
$this->db->trans_complete();
//To CHECKBOX
if($to_type === CHECKBOX)
{
$checkbox_person_attribute_values = $this->checkbox_person_attribute_values($definition_id);
$this->db->trans_start();
$query = 'UPDATE ospos_person_attribute_values vals ';
$query .= 'INNER JOIN ospos_person_attribute_links links ';
$query .= 'ON vals.person_attribute_id = links.person_attribute_id ';
$query .= "SET links.person_attribute_id = IF((vals.person_attribute_value IN('FALSE','0','') OR (vals.person_attribute_value IS NULL)), $checkbox_person_attribute_values[0], $checkbox_person_attribute_values[1]) ";
$query .= 'WHERE links.definition_id = ' . $this->db->escape($definition_id);
$success = $this->db->query($query);
$this->db->trans_complete();
}
}
}
//From any other type
else
{
$success = TRUE;
}
return $success;
}
private function checkbox_person_attribute_values($definition_id)
{
$zero_person_attribute_id = $this->value_exists('0');
$one_person_attribute_id = $this->value_exists('1');
if($zero_person_attribute_id === FALSE)
{
$zero_person_attribute_id = $this->save_value('0', $definition_id, FALSE, FALSE, CHECKBOX);
}
if($one_person_attribute_id === FALSE)
{
$one_person_attribute_id = $this->save_value('1', $definition_id, FALSE, FALSE, CHECKBOX);
}
return array($zero_person_attribute_id, $one_person_attribute_id);
}
/*
Inserts or updates a definition
*/
public function save_definition(&$definition_data, $definition_id = NO_DEFINITION_ID)
{
//Run these queries as a transaction, we want to make sure we do all or nothing
$this->db->trans_start();
//Definition doesn't exist
if($definition_id === NO_DEFINITION_ID || !$this->exists($definition_id))
{
if($this->exists($definition_id,TRUE))
{
$success = $this->undelete($definition_id);
}
else
{
$success = $this->db->insert('person_attribute_definitions', $definition_data);
$definition_data['definition_id'] = $this->db->insert_id();
}
}
//Definition already exists
else
{
$this->db->select('definition_type, definition_name');
$this->db->from('person_attribute_definitions');
$this->db->where('definition_id', $definition_id);
$row = $this->db->get()->row();
$from_definition_type = $row->definition_type;
$from_definition_name = $row->definition_name;
$to_definition_type = $definition_data['definition_type'];
if($from_definition_type !== $to_definition_type)
{
if(!$this->convert_definition_type($definition_id,$from_definition_type,$to_definition_type))
{
return FALSE;
}
}
$this->db->where('definition_id', $definition_id);
$success = $this->db->update('person_attribute_definitions', $definition_data);
$definition_data['definition_id'] = $definition_id;
}
$this->db->trans_complete();
$success &= $this->db->trans_status();
return $success;
}
public function get_definition_by_name($definition_name, $definition_type = FALSE)
{
$this->db->from('person_attribute_definitions');
$this->db->where('definition_name', $definition_name);
if($definition_type != FALSE)
{
$this->db->where('definition_type', $definition_type);
}
return $this->db->get()->result_array();
}
public function save_link($person_id, $definition_id, $person_attribute_id)
{
$this->db->trans_start();
if($this->link_exists($person_id, $definition_id))
{
$this->db->where('definition_id', $definition_id);
$this->db->where('person_id', $person_id);
$this->db->update('person_attribute_links', array('person_attribute_id' => $person_attribute_id));
}
else
{
$this->db->insert('person_attribute_links', array('person_attribute_id' => $person_attribute_id, 'person_id' => $person_id, 'definition_id' => $definition_id));
}
$this->db->trans_complete();
return $this->db->trans_status();
}
public function delete_link($person_id)
{
return $this->db->delete('person_attribute_links', array('person_id' => $person_id));
}
public function get_link_value($person_id, $definition_id)
{
$this->db->where('person_id', $person_id);
$this->db->where('definition_id', $definition_id);
return $this->db->get('person_attribute_links')->row_object();
}
public function get_link_values($person_id, $definition_flags)
{
$format = $this->db->escape(dateformat_mysql());
$this->db->select("GROUP_CONCAT(person_attribute_value SEPARATOR ', ') AS person_attribute_values");
$this->db->select("GROUP_CONCAT(DATE_FORMAT(person_attribute_date, $format) SEPARATOR ', ') AS person_attribute_dtvalues");
$this->db->from('person_attribute_links');
$this->db->join('person_attribute_values', 'person_attribute_values.person_attribute_id = person_attribute_links.person_attribute_id');
$this->db->join('person_attribute_definitions', 'person_attribute_definitions.definition_id = person_attribute_links.definition_id');
$this->db->where('definition_type <>', GROUP);
$this->db->where('deleted', 0);
// if(!empty($id))
// {
// $this->db->where($sale_receiving_fk, $id);
// }
// else
// {
// $this->db->where('sale_id');
// $this->db->where('receiving_id');
// }
$this->db->where('person_id', (int) $person_id);
$this->db->where('definition_flags & ', $definition_flags);
return $this->db->get();
}
public function get_person_attribute_value($person_id, $definition_id)
{
$this->db->from('person_attribute_values');
$this->db->join('person_attribute_links', 'person_attribute_links.person_attribute_id = person_attribute_values.person_attribute_id');
$this->db->where('definition_id', $definition_id);
$this->db->where('person_id', (int) $person_id);
return $this->db->get()->row_object();
}
public function copy_person_attribute_links($person_id, $id)
{
$this->db->query(
'INSERT INTO ospos_person_attribute_links (person_id, definition_id, person_attribute_id)
SELECT ' . $this->db->escape($person_id) . ', definition_id, person_attribute_id, ' . $this->db->escape($id) . '
FROM ' . $this->db->dbprefix('person_attribute_links') . '
WHERE person_id = ' . $this->db->escape($person_id)
);
}
public function get_suggestions($definition_id, $term)
{
$suggestions = array();
$this->db->distinct();
$this->db->select('person_attribute_value, person_attribute_values.person_attribute_id');
$this->db->from('person_attribute_definitions AS definition');
$this->db->join('person_attribute_links', 'person_attribute_links.definition_id = definition.definition_id');
$this->db->join('person_attribute_values', 'person_attribute_values.person_attribute_id = person_attribute_links.person_attribute_id');
$this->db->like('person_attribute_value', $term);
$this->db->where('deleted', 0);
$this->db->where('definition.definition_id', $definition_id);
$this->db->order_by('person_attribute_value','ASC');
foreach($this->db->get()->result() as $row)
{
$row_array = (array) $row;
$suggestions[] = array('value' => $row_array['person_attribute_id'], 'label' => $row_array['person_attribute_value']);
}
return $suggestions;
}
public function save_value($person_attribute_value, $definition_id, $person_id = FALSE, $person_attribute_id = FALSE, $definition_type = DROPDOWN)
{
$this->db->trans_start();
//New Person_attribute
if(empty($person_attribute_id) || empty($person_id))
{
if(in_array($definition_type, [TEXT, DROPDOWN, CHECKBOX], TRUE))
{
$person_attribute_id = $this->value_exists($person_attribute_value);
if(empty($person_attribute_id))
{
$this->db->insert('person_attribute_values', array('person_attribute_value' => $person_attribute_value));
}
}
else if($definition_type == DECIMAL)
{
$this->db->insert('person_attribute_values', array('person_attribute_decimal' => $person_attribute_value));
}
else
{
$this->db->insert('person_attribute_values', array('person_attribute_date' => date('Y-m-d', strtotime($person_attribute_value))));
}
$person_attribute_id = $person_attribute_id ? $person_attribute_id : $this->db->insert_id();
$this->db->insert('person_attribute_links', array(
'person_attribute_id' => empty($person_attribute_id) ? NULL : $person_attribute_id,
'person_id' => empty($person_id) ? NULL : $person_id,
'definition_id' => $definition_id));
}
//Existing Person_attribute
else
{
$this->db->where('person_attribute_id', $person_attribute_id);
if(in_array($definition_type, [TEXT, DROPDOWN], TRUE))
{
$this->db->update('person_attribute_values', array('person_attribute_value' => $person_attribute_value));
}
else if($definition_type == DECIMAL)
{
$this->db->update('person_attribute_values', array('person_attribute_decimal' => $person_attribute_value));
}
else
{
$this->db->update('person_attribute_values', array('person_attribute_date' => date('Y-m-d', strtotime($person_attribute_value))));
}
}
$this->db->trans_complete();
return $person_attribute_id;
}
public function delete_value($person_attribute_value, $definition_id)
{
return $this->db->query("DELETE atrv, atrl FROM " . $this->db->dbprefix('person_attribute_values') . " atrv, " . $this->db->dbprefix('person_attribute_links') . " atrl " .
"WHERE atrl.person_attribute_id = atrv.person_attribute_id AND atrv.person_attribute_value = " . $this->db->escape($person_attribute_value) . " AND atrl.definition_id = " . $this->db->escape($definition_id));
}
/**
* Deletes an Person_attribute definition from the database and associated column in the customer_import.csv
*
* @param unknown $definition_id Person_attribute definition ID to remove.
* @return boolean TRUE if successful and FALSE if there is a failure
*/
public function delete_definition($definition_id)
{
$this->db->where('definition_id', $definition_id);
return $this->db->update('person_attribute_definitions', array('deleted' => 1));
}
public function delete_definition_list($definition_ids)
{
$this->db->where_in('definition_id', $definition_ids);
return $this->db->update('person_attribute_definitions', array('deleted' => 1));
}
/*
Undeletes one person_attribute definition
*/
public function undelete($definition_id)
{
$this->db->where('definition_id', $definition_id);
return $this->db->update('person_attribute_definitions', array('deleted'=>0));
}
}

View File

@@ -0,0 +1,243 @@
<div id="required_fields_message"><?php echo $this->lang->line('common_fields_required_message'); ?></div>
<ul id="error_message_box" class="error_message_box"></ul>
<?php echo form_open('person_attributes/save_definition/'.$definition_id, array('id'=>'person_attribute_form', 'class'=>'form-horizontal')); ?>
<fieldset id="person_attribute_basic_info">
<div class="form-group form-group-sm">
<?php echo form_label($this->lang->line('person_attributes_definition_name'), 'definition_name', array('class'=>'required control-label col-xs-3')); ?>
<div class='col-xs-8'>
<?php echo form_input(array(
'name'=>'definition_name',
'id' => 'definition_name',
'class'=>'form-control input-sm',
'value'=>$definition_info->definition_name)
);?>
</div>
</div>
<div class="form-group form-group-sm">
<?php echo form_label($this->lang->line('person_attributes_definition_type'), 'definition_type', array('class'=>'required control-label col-xs-3')); ?>
<div class='col-xs-8'>
<?php echo form_dropdown('definition_type', DEFINITION_TYPES, array_search($definition_info->definition_type, DEFINITION_TYPES), 'id="definition_type" class="form-control"');?>
</div>
</div>
<div class="form-group form-group-sm">
<?php echo form_label($this->lang->line('person_attributes_definition_group'), 'definition_group', array('class' => 'control-label col-xs-3')); ?>
<div class='col-xs-8'>
<?php echo form_dropdown('definition_group', $definition_group, $definition_info->definition_fk, 'id="definition_group" class="form-control" ' . (empty($definition_group) ? 'disabled="disabled"' : ''));?>
</div>
</div>
<div class="form-group form-group-sm hidden">
<?php echo form_label($this->lang->line('person_attributes_definition_flags'), 'definition_flags', array('class' => 'control-label col-xs-3')); ?>
<div class='col-xs-8'>
<div class="input-group">
<?php echo form_multiselect('definition_flags[]', $definition_flags, array_keys($selected_definition_flags), array('id'=>'definition_flags', 'class'=>'selectpicker show-menu-arrow', 'data-none-selected-text'=>$this->lang->line('common_none_selected_text'), 'data-selected-text-format'=>'count > 1', 'data-style'=>'btn-default btn-sm', 'data-width'=>'fit')); ?>
</div>
</div>
</div>
<div class="form-group form-group-sm hidden">
<?php echo form_label($this->lang->line('person_attributes_definition_unit'), 'definition_units', array('class' => 'control-label col-xs-3')); ?>
<div class='col-xs-8'>
<div class="input-group">
<?php echo form_input(array('name'=>'definition_unit', 'value'=>$definition_info->definition_unit,'class'=>'form-control input-sm', 'id' => 'definition_unit'));?>
</div>
</div>
</div>
<div class="form-group form-group-sm hidden">
<?php echo form_label($this->lang->line('person_attributes_definition_values'), 'definition_value', array('class' => 'control-label col-xs-3')); ?>
<div class='col-xs-8'>
<div class="input-group">
<?php echo form_input(array('name'=>'definition_value', 'class'=>'form-control input-sm', 'id' => 'definition_value'));?>
<span id="add_person_attribute_value" class="input-group-addon input-sm btn btn-default">
<span class="glyphicon glyphicon-plus-sign"></span>
</span>
</div>
</div>
</div>
<div class="form-group form-group-sm hidden">
<?php echo form_label('&nbsp', 'definition_list_group', array('class' => 'control-label col-xs-3')); ?>
<div class='col-xs-8'>
<ul id="definition_list_group" class="list-group"></ul>
</div>
</div>
</fieldset>
<?php echo form_close(); ?>
<script type="text/javascript">
//validation and submit handling
$(document).ready(function()
{
var values = [];
var definition_id = <?php echo $definition_id; ?>;
var is_new = definition_id == 0;
var disable_definition_types = function()
{
var definition_type = $("#definition_type option:selected").text();
if(definition_type == "DATE" || (definition_type == "GROUP" && !is_new) || definition_type == "DECIMAL")
{
$('#definition_type').prop("disabled",true);
}
else if(definition_type == "DROPDOWN" || definition_type == "CHECKBOX")
{
$("#definition_type option:contains('GROUP')").hide();
$("#definition_type option:contains('DATE')").hide();
$("#definition_type option:contains('DECIMAL')").hide();
}
else
{
$("#definition_type option:contains('GROUP')").hide();
}
}
disable_definition_types();
var disable_category_dropdown = function()
{
if(definition_id == -1)
{
$('#definition_name').prop("disabled",true);
$('#definition_type').prop("disabled",true);
$('#definition_group').parents('.form-group').toggleClass("hidden", true);
$('#definition_flags').parents('.form-group').toggleClass('hidden', true);
}
}
disable_category_dropdown();
var show_hide_fields = function(event)
{
var is_dropdown = $('#definition_type').val() !== '1';
var is_decimal = $('#definition_type').val() !== '2';
var is_no_group = $('#definition_type').val() !== '0';
var is_category_dropdown = definition_id == -1;
$('#definition_value, #definition_list_group').parents('.form-group').toggleClass('hidden', is_dropdown);
$('#definition_unit').parents('.form-group').toggleClass('hidden', is_decimal);
//Appropriately show definition flags if not category_dropdown
if(definition_id != -1)
{
$('#definition_flags').parents('.form-group').toggleClass('hidden', !is_no_group);
}
};
$('#definition_type').change(show_hide_fields);
show_hide_fields();
$('.selectpicker').each(function () {
var $selectpicker = $(this);
$.fn.selectpicker.call($selectpicker, $selectpicker.data());
});
var remove_person_attribute_value = function()
{
var value = $(this).parents("li").text();
if (is_new)
{
values.splice($.inArray(value, values), 1);
}
else
{
$.post('<?php echo site_url($controller_name . "/delete_person_attribute_value/");?>' + escape(value), {definition_id: definition_id});
}
$(this).parents("li").remove();
};
var add_person_attribute_value = function(value)
{
var is_event = typeof(value) !== 'string';
if ($("#definition_value").val().match(/(\||_)/g) != null)
{
return;
}
if (is_event)
{
value = $('#definition_value').val();
if (!value)
{
return;
}
if (is_new)
{
values.push(value);
}
else
{
$.post('<?php echo site_url("person_attributes/save_person_attribute_value/");?>' + escape(value), {definition_id: definition_id});
}
}
$('#definition_list_group').append("<li class='list-group-item'>" + value + "<a href='javascript:void(0);'><span class='glyphicon glyphicon-trash pull-right'></span></a></li>")
.find(':last-child a').click(remove_person_attribute_value);
$('#definition_value').val('');
};
$('#add_person_attribute_value').click(add_person_attribute_value);
$('#definition_value').keypress(function (e) {
if (e.which == 13) {
add_person_attribute_value();
return false;
}
});
var definition_values = <?php echo json_encode(array_values($definition_values)) ?>;
$.each(definition_values, function(index, element) {
add_person_attribute_value(element);
});
$.validator.addMethod('valid_chars', function(value, element) {
return value.match(/(\||_)/g) == null;
}, "<?php echo $this->lang->line('person_attributes_person_attribute_value_invalid_chars'); ?>");
$('form').bind('submit', function () {
$(this).find(':input').prop('disabled', false);
});
$('#person_attribute_form').validate($.extend({
submitHandler: function(form)
{
$(form).ajaxSubmit({
beforeSerialize: function($form, options) {
is_new && $('<input>').attr({
id: 'definition_values',
type: 'hidden',
name: 'definition_values',
value: JSON.stringify(values)
}).appendTo($form);
},
success: function(response)
{
dialog_support.hide();
table_support.handle_submit('<?php echo site_url($controller_name); ?>', response);
},
dataType: 'json'
});
},
rules:
{
definition_name: 'required',
definition_value: 'valid_chars',
definition_type: 'required'
},
messages:
{
definition_name: "<?php echo $this->lang->line('person_attributes_definition_name_required'); ?>",
definition_type: "<?php echo $this->lang->line('person_attributes_definition_type_required'); ?>"
}
}, form_support.error));
});
</script>

View File

@@ -0,0 +1,37 @@
<?php $this->load->view("partial/header"); ?>
<script type="text/javascript">
$(document).ready(function()
{
<?php $this->load->view('partial/bootstrap_tables_locale'); ?>
table_support.init({
resource: '<?php echo site_url($controller_name);?>',
headers: <?php echo $table_headers; ?>,
pageSize: <?php echo $this->config->item('lines_per_page'); ?>,
uniqueId: 'definition_id'
});
});
</script>
<div id="title_bar" class="btn-toolbar print_hide">
<button class='btn btn-info btn-sm pull-right modal-dlg' data-btn-submit='<?php echo $this->lang->line('common_submit') ?>' data-href='<?php echo site_url($controller_name."/view"); ?>'
title='<?php echo $this->lang->line($controller_name . '_new'); ?>'>
<span class="glyphicon glyphicon-star">&nbsp</span><?php echo $this->lang->line($controller_name. '_new'); ?>
</button>
</div>
<div id="toolbar">
<div class="pull-left form-inline" role="toolbar">
<button id="delete" class="btn btn-default btn-sm print_hide">
<span class="glyphicon glyphicon-trash">&nbsp</span><?php echo $this->lang->line("common_delete"); ?>
</button>
</div>
</div>
<div id="table_holder">
<table id="table"></table>
</div>
<?php $this->load->view("partial/footer"); ?>

View File

@@ -0,0 +1,129 @@
<div class="form-group form-group-sm">
<?php echo form_label($this->lang->line("person_attributes_definition_name"), "definition_name_label", array('class' => 'control-label col-xs-3')); ?>
<div class='col-xs-8'>
<?php echo form_dropdown('definition_name', $definition_names, -1, array('id' => 'definition_name', 'class' => 'form-control')); ?>
</div>
</div>
<?php
foreach($definition_values as $definition_id => $definition_value)
{
?>
<div class="form-group form-group-sm">
<?php echo form_label($definition_value['definition_name'], $definition_value['definition_name'], array('class' => 'control-label col-xs-3')); ?>
<div class='col-xs-8'>
<div class="input-group">
<?php
echo form_hidden("person_attribute_ids[$definition_id]", $definition_value['person_attribute_id']);
$person_attribute_value = $definition_value['person_attribute_value'];
if ($definition_value['definition_type'] == DATE)
{
$value = (empty($person_attribute_value) || empty($person_attribute_value->person_attribute_date)) ? NOW : strtotime($person_attribute_value->person_attribute_date);
echo form_input(array(
'name' => "person_attribute_links[$definition_id]",
'value' => to_date($value),
'class' => 'form-control input-sm datetime',
'data-definition-id' => $definition_id,
'readonly' => 'true'));
}
else if ($definition_value['definition_type'] == DROPDOWN)
{
$selected_value = $definition_value['selected_value'];
echo form_dropdown("person_attribute_links[$definition_id]", $definition_value['values'], $selected_value, "class='form-control' data-definition-id='$definition_id'");
}
else if ($definition_value['definition_type'] == TEXT)
{
$value = (empty($person_attribute_value) || empty($person_attribute_value->person_attribute_value)) ? $definition_value['selected_value'] : $person_attribute_value->person_attribute_value;
echo form_input("person_attribute_links[$definition_id]", $value, "class='form-control valid_chars' data-definition-id='$definition_id'");
}
else if ($definition_value['definition_type'] == DECIMAL)
{
$value = (empty($person_attribute_value) || empty($person_attribute_value->person_attribute_decimal)) ? $definition_value['selected_value'] : $person_attribute_value->person_attribute_decimal;
echo form_input("person_attribute_links[$definition_id]", $value, "class='form-control valid_chars' data-definition-id='$definition_id'");
}
else if ($definition_value['definition_type'] == CHECKBOX)
{
$value = (empty($person_attribute_value) || empty($person_attribute_value->person_attribute_value)) ? $definition_value['selected_value'] : $person_attribute_value->person_attribute_value;
//Sends 0 if the box is unchecked instead of not sending anything.
echo form_input(array(
'type' => 'hidden',
'name' => "person_attribute_links[$definition_id]",
'id' => "person_attribute_links[$definition_id]",
'value' => 0,
'data-definition-id' => $definition_id
));
echo form_checkbox(array(
'name' => "person_attribute_links[$definition_id]",
'id' => "person_attribute_links[$definition_id]",
'value' => 1,
'checked' => ($value ? 1 : 0),
'class' => 'checkbox-inline',
'data-definition-id' => $definition_id
));
}
?>
<span class="input-group-addon input-sm btn btn-default remove_person_attribute_btn"><span class="glyphicon glyphicon-trash"></span></span>
</div>
</div>
</div>
<?php
}
?>
<script type="text/javascript">
(function() {
<?php $this->load->view('partial/datepicker_locale', array('config' => '{ minView: 2, format: "'.dateformat_bootstrap($this->config->item('dateformat') . '"}'))); ?>
var enable_delete = function() {
$('.remove_person_attribute_btn').click(function() {
$(this).parents('.form-group').remove();
});
};
enable_delete();
$("input[name*='person_attribute_links']").change(function() {
var definition_id = $(this).data('definition-id');
$("input[name='person_attribute_ids[" + definition_id + "]']").val('');
}).autocomplete({
source: function(request, response) {
$.get('<?php echo site_url('person_attributes/suggest_person_attribute/');?>' + this.element.data('definition-id') + '?term=' + request.term, function(data) {
return response(data);
}, 'json');
},
appendTo: '.modal-content',
select: function (event, ui) {
event.preventDefault();
$(this).val(ui.item.label);
},
delay: 10
});
var definition_values = function() {
var result = {};
$("[name*='person_attribute_links'").each(function() {
var definition_id = $(this).data('definition-id');
result[definition_id] = $(this).val();
});
return result;
};
var refresh = function() {
var definition_id = $("#definition_name option:selected").val();
var person_attribute_values = definition_values();
person_attribute_values[definition_id] = '';
$('#person_attributes').load('<?php echo site_url($controller_name . "/person_attributes/$person_id");?>', {
'definition_ids': JSON.stringify(person_attribute_values)
}, enable_delete);
};
$('#definition_name').change(function() {
refresh();
});
})();
</script>

View File

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB