mirror of
https://github.com/opensourcepos/opensourcepos.git
synced 2026-05-14 02:33:56 -04:00
Add attributes to items (#68)
This commit is contained in:
@@ -46,7 +46,7 @@ class Attributes extends Secure_Controller
|
||||
{
|
||||
$success = $this->Attribute->save_value(urldecode($attribute_value), $this->input->post('definition_id'), $this->input->post('item_id'), $this->input->post('attribute_id'));
|
||||
|
||||
echo json_encode(array('success' => $success));
|
||||
echo json_encode(array('success' => $success != 0));
|
||||
}
|
||||
|
||||
public function delete_attribute_value($attribute_value)
|
||||
|
||||
@@ -192,9 +192,12 @@ class Items extends Secure_Controller
|
||||
$data['default_tax_2_rate'] = '';
|
||||
$data['item_kits_enabled'] = $this->Employee->has_grant('item_kits', $this->Employee->get_logged_in_employee_info()->person_id);
|
||||
$data['definition_values'] = $this->Attribute->get_attributes_by_item($item_id);
|
||||
$definition_names = array(-1 => $this->lang->line('common_none_selected_text'));
|
||||
$data['definition_names'] = $this->Attribute->get_definition_names();
|
||||
|
||||
$data['definition_names'] = $definition_names + $this->Attribute->get_definition_names();
|
||||
foreach($data['definition_values'] as $definition_id => $definition)
|
||||
{
|
||||
unset($data['definition_names'][$definition_id]);
|
||||
}
|
||||
|
||||
$item_info = $this->Item->get_info($item_id);
|
||||
foreach(get_object_vars($item_info) as $property => $value)
|
||||
@@ -391,9 +394,9 @@ class Items extends Secure_Controller
|
||||
$data['definition_values'] = $this->Attribute->get_attributes_by_item($item_id) + $this->Attribute->get_values_by_definitions($definition_ids);
|
||||
$data['definition_names'] = $this->Attribute->get_definition_names();
|
||||
|
||||
foreach($data['definition_values'] as $definition_value)
|
||||
foreach($data['definition_values'] as $definition_id => $attribute_value)
|
||||
{
|
||||
unset($data['definition_names'][$definition_value['definition_id']]);
|
||||
unset($data['definition_names'][$definition_id]);
|
||||
}
|
||||
|
||||
$this->load->view('attributes/item', $data);
|
||||
@@ -543,10 +546,16 @@ class Items extends Secure_Controller
|
||||
}
|
||||
|
||||
// Save item attributes
|
||||
$definition_values = json_decode($this->input->post('definition_values'), TRUE);
|
||||
$attribute_links = $this->input->post('attribute_links');
|
||||
$attribute_ids = $this->input->post('attribute_ids');
|
||||
$this->Attribute->delete_link($item_id);
|
||||
foreach ($definition_values as $definition_id => $attribute_id) {
|
||||
$success &= $this->Attribute->save_link($item_id, $definition_id, $attribute_id);
|
||||
foreach ($attribute_links as $definition_id => $attribute_id) {
|
||||
$definition_type = $this->Attribute->get_info($definition_id)->definition_type;
|
||||
if ($definition_type != DROPDOWN)
|
||||
{
|
||||
$attribute_id = $this->Attribute->save_value($attribute_id, $definition_id, $item_id, $attribute_ids[$definition_id]);
|
||||
}
|
||||
$this->Attribute->save_link($item_id, $definition_id, $attribute_id);
|
||||
}
|
||||
|
||||
if($success && $upload_success)
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
|
||||
|
||||
const DEFAULT_LANGUAGE = 'english';
|
||||
const DEFAULT_LANGUAGE_CODE = 'en-US';
|
||||
|
||||
/**
|
||||
* Currency locale helper
|
||||
*/
|
||||
@@ -19,7 +22,8 @@ function current_language_code($load_system_language = FALSE)
|
||||
}
|
||||
}
|
||||
|
||||
return get_instance()->config->item('language_code');
|
||||
$language_code = get_instance()->config->item('language_code');
|
||||
return empty($language_code) ? DEFAULT_LANGUAGE_CODE : $language_code;
|
||||
}
|
||||
|
||||
function current_language($load_system_language = FALSE)
|
||||
@@ -36,7 +40,8 @@ function current_language($load_system_language = FALSE)
|
||||
}
|
||||
}
|
||||
|
||||
return get_instance()->config->item('language');
|
||||
$language = get_instance()->config->item('language');
|
||||
return empty($language) ? DEFAULT_LANGUAGE : $language;
|
||||
}
|
||||
|
||||
function get_languages()
|
||||
@@ -207,6 +212,7 @@ function get_timeformats()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Gets the payment options
|
||||
*/
|
||||
@@ -217,6 +223,7 @@ function get_payment_options()
|
||||
|
||||
$payments = array();
|
||||
|
||||
|
||||
if($config->item('payment_options_order') == 'debitcreditcash')
|
||||
{
|
||||
$payments[$lang->line('sales_debit')] = $lang->line('sales_debit');
|
||||
|
||||
@@ -112,7 +112,9 @@ class Attribute extends CI_Model
|
||||
$this->db->where('item_id', $item_id);
|
||||
$this->db->where('deleted', 0);
|
||||
|
||||
return $this->db->get()->result_array();
|
||||
$results = $this->db->get()->result_array();
|
||||
|
||||
return $this->_to_array($results, 'definition_id');
|
||||
}
|
||||
|
||||
public function get_values_by_definitions($definition_ids)
|
||||
@@ -128,7 +130,9 @@ class Attribute extends CI_Model
|
||||
|
||||
$this->db->where('deleted', 0);
|
||||
|
||||
return $this->db->get()->result_array();
|
||||
$results = $this->db->get()->result_array();
|
||||
|
||||
return $this->_to_array($results, 'definition_id');
|
||||
}
|
||||
|
||||
return array();
|
||||
@@ -148,12 +152,7 @@ class Attribute extends CI_Model
|
||||
$this->db->where('definition_fk');
|
||||
$results = $this->db->get()->result_array();
|
||||
|
||||
$attribute_definitions = array();
|
||||
foreach($results as $result)
|
||||
{
|
||||
$attribute_definitions[$result['definition_id']] = $result['definition_name'];
|
||||
}
|
||||
return $attribute_definitions;
|
||||
return $this->_to_array($results, 'definition_id', 'definition_name');
|
||||
}
|
||||
|
||||
public function get_definition_names()
|
||||
@@ -161,12 +160,8 @@ class Attribute extends CI_Model
|
||||
$this->db->from('attribute_definitions');
|
||||
$results = $this->db->get()->result_array();
|
||||
|
||||
$attribute_definitions = array();
|
||||
foreach($results as $result)
|
||||
{
|
||||
$attribute_definitions[$result['definition_id']] = $result['definition_name'];
|
||||
}
|
||||
return $attribute_definitions;
|
||||
$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)
|
||||
@@ -182,16 +177,18 @@ class Attribute extends CI_Model
|
||||
|
||||
$results = $this->db->get()->result_array();
|
||||
|
||||
$attribute_definitions = array();
|
||||
foreach($results as $result)
|
||||
{
|
||||
$attribute_definitions[$result['attribute_id']] = $result['attribute_value'];
|
||||
}
|
||||
return $attribute_definitions;
|
||||
return $this->_to_array($results, 'attribute_id', 'attribute_value');
|
||||
}
|
||||
return $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
|
||||
*/
|
||||
@@ -328,7 +325,7 @@ class Attribute extends CI_Model
|
||||
{
|
||||
$suggestions = array();
|
||||
$this->db->distinct();
|
||||
$this->db->select('attribute_value');
|
||||
$this->db->select('attribute_value, attribute_values.attribute_id');
|
||||
$this->db->from('attribute_definitions AS definition');
|
||||
$this->db->join('attribute_links', 'attribute_links.definition_id = definition.definition_id');
|
||||
$this->db->join('attribute_values', 'attribute_values.attribute_id = attribute_links.attribute_id');
|
||||
@@ -339,7 +336,7 @@ class Attribute extends CI_Model
|
||||
foreach($this->db->get()->result() as $row)
|
||||
{
|
||||
$row_array = (array) $row;
|
||||
$suggestions[] = array('label' => $row_array['attribute_value']);
|
||||
$suggestions[] = array('value' => $row_array['attribute_id'], 'label' => $row_array['attribute_value']);
|
||||
}
|
||||
|
||||
return $suggestions;
|
||||
@@ -358,6 +355,7 @@ class Attribute extends CI_Model
|
||||
'attribute_id' => empty($attribute_id) ? NULL : $attribute_id,
|
||||
'item_id' => empty($item_id) ? NULL : $item_id,
|
||||
'definition_id' => $definition_id));
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -367,7 +365,7 @@ class Attribute extends CI_Model
|
||||
|
||||
$this->db->trans_complete();
|
||||
|
||||
return $this->db->trans_status();
|
||||
return $attribute_id;
|
||||
}
|
||||
|
||||
public function delete_value($attribute_value, $definition_id)
|
||||
|
||||
@@ -44,8 +44,8 @@
|
||||
<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 class="input-group-btn">
|
||||
<button id="definition_value_add" class="btn input-sm" type="button"><span class="glyphicon glyphicon-plus-sign"></span></button>
|
||||
<span id="definition_value_add" class="input-group-addon input-sm btn btn-default">
|
||||
<span class="glyphicon glyphicon-plus-sign"></span>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
@@ -147,6 +147,7 @@
|
||||
{
|
||||
$(form).ajaxSubmit({
|
||||
beforeSerialize: function($form, options) {
|
||||
|
||||
is_new && $('<input>').attr({
|
||||
id: 'definition_values',
|
||||
type: 'hidden',
|
||||
|
||||
@@ -1,5 +1,15 @@
|
||||
|
||||
<div class="form-group form-group-sm">
|
||||
<?php echo form_label($this->lang->line("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_value)
|
||||
|
||||
foreach($definition_values as $definition_id => $definition_value)
|
||||
{
|
||||
?>
|
||||
|
||||
@@ -7,33 +17,34 @@ foreach($definition_values as $definition_value)
|
||||
<?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">
|
||||
<span class="input-group-addon input-sm"><span class="glyphicon glyphicon-tag"></span></span>
|
||||
|
||||
<?php
|
||||
$definition_id = $definition_value['definition_id'];
|
||||
$attribute_value = $this->Attribute->get_attribute_value($item_id, $definition_id);
|
||||
$attribute_id = (empty($attribute_value) || empty($attribute_value->attribute_id)) ? NULL : $attribute_value->attribute_id;
|
||||
echo form_hidden("attribute_ids[$definition_id]", $attribute_id);
|
||||
|
||||
if ($definition_value['definition_type'] == DATE)
|
||||
{
|
||||
echo form_input(array(
|
||||
'name' => 'definition_name[]',
|
||||
'name' => 'attribute_links[$definition_id]',
|
||||
'value' => date($this->config->item('dateformat') . ' ' . $this->config->item('timeformat'), strtotime($definition_value['attribute_value'])),
|
||||
'class' => 'form-control input-sm',
|
||||
'data-definition-id' => $definition_value['definition_id'],
|
||||
'data-definition-id' => $definition_id,
|
||||
'readonly' => 'true'));
|
||||
}
|
||||
else if ($definition_value['definition_type'] == DROPDOWN)
|
||||
{
|
||||
$values = $this->Attribute->get_definition_values($definition_id);
|
||||
$selected_value = $this->Attribute->get_link_value($item_id, $definition_id);
|
||||
echo form_dropdown('definition_name[]', $values, (empty($selected_value) ? NULL : $selected_value->attribute_id), "class='form-control' data-definition-id='$definition_id'");
|
||||
echo form_dropdown("attribute_links[$definition_id]", $values, (empty($selected_value) ? NULL : $selected_value->attribute_id), "class='form-control' data-definition-id='$definition_id'");
|
||||
}
|
||||
else if ($definition_value['definition_type'] == TEXT)
|
||||
{
|
||||
$attribute_value = $this->Attribute->get_attribute_value($item_id, $definition_id);
|
||||
$value = (empty($attribute_value) || empty($attribute_value->attribute_value)) ? NULL : $attribute_value->attribute_value;
|
||||
$id = (empty($attribute_value) || empty($attribute_value->attribute_id)) ? NULL : $attribute_value->attribute_id;
|
||||
echo form_input('definition_name[]', $value, "class='form-control' data-attribute-id='$id'");
|
||||
echo form_input("attribute_links[$definition_id]", $value, "class='form-control' data-definition-id='$definition_id'");
|
||||
}
|
||||
?>
|
||||
<span id="remove" class="input-group-addon input-sm btn btn-default"><span class="glyphicon glyphicon-trash"></span></span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -42,56 +53,44 @@ foreach($definition_values as $definition_value)
|
||||
}
|
||||
?>
|
||||
|
||||
|
||||
<div class="form-group form-group-sm">
|
||||
<?php echo form_label($this->lang->line("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>
|
||||
|
||||
<script type="text/javascript">
|
||||
(function() {
|
||||
var definition_values = <?php echo json_encode($definition_values, JSON_FORCE_OBJECT); ?>;
|
||||
|
||||
$('input[name*="definition"]').autocomplete({
|
||||
source: '<?php echo site_url("attributes/suggest_attribute/");?>' + $(this).data('definition-id'),
|
||||
appendTo: '.modal-content',
|
||||
select: function (a, ui) {
|
||||
$(this).data('attribute_id', ui.item.value);
|
||||
$(this).val(ui.item.label);
|
||||
},
|
||||
delay:10
|
||||
}).change(function() {
|
||||
$("input[name*='attribute_links']").change(function() {
|
||||
var definition_id = $(this).data('definition-id');
|
||||
definition_values[definition_id] = $(this).data('attribute-id');
|
||||
$("input[name='attribute_ids[" + definition_id + "]").val("");
|
||||
}).autocomplete({
|
||||
source: function(request, response) {
|
||||
$.get('<?php echo site_url('attributes/suggest_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 = {};
|
||||
$("input[name*='attribute_links'").each(function(index, element) {
|
||||
var definition_id = $(this).data('definition-id');
|
||||
result[definition_id] = $(element).val();
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
$("#definition_name").change(function() {
|
||||
var definition_id = $(this).val();
|
||||
definition_values[definition_id] = {};
|
||||
var attribute_values = definition_values();
|
||||
var definition_id = $("option:selected", this).val();
|
||||
attribute_values[definition_id] = "";
|
||||
$("#attributes").load('<?php echo site_url("items/attributes/$item_id");?>', {
|
||||
'definition_ids': JSON.stringify(definition_values)
|
||||
'definition_ids': JSON.stringify(attribute_values)
|
||||
});
|
||||
});
|
||||
|
||||
$('#item_form').validate({
|
||||
submitHandler: function (form, event) {
|
||||
$(form).ajaxSubmit({
|
||||
beforeSerialize: function ($form, options) {
|
||||
$("<input>").attr({
|
||||
id: 'definition_values',
|
||||
type: 'hidden',
|
||||
name: 'definition_values',
|
||||
value: JSON.stringify(definition_values || [])
|
||||
}).appendTo($form);
|
||||
}
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
})();
|
||||
</script>
|
||||
|
||||
|
||||
@@ -16,8 +16,10 @@
|
||||
from: "<?php echo $this->config->item('notify_vertical_position'); ?>"
|
||||
}});
|
||||
|
||||
var cookie_name = "<?php echo $this->config->item('csrf_cookie_name'); ?>";
|
||||
|
||||
var csrf_token = function() {
|
||||
return Cookies.get("<?php echo $this->config->item('csrf_cookie_name'); ?>");
|
||||
return Cookies.get(cookie_name);
|
||||
};
|
||||
|
||||
var csrf_form_base = function() {
|
||||
@@ -32,7 +34,6 @@
|
||||
|
||||
$.ajax = function() {
|
||||
var args = arguments[0];
|
||||
|
||||
if (args['type'] && args['type'].toLowerCase() == 'post' && csrf_token()) {
|
||||
if (typeof args['data'] === 'string')
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user