-
-
Save slav123/90c84ed2890e98d852f0 to your computer and use it in GitHub Desktop.
GroceryCrud parent category
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function _navigation() | |
{ | |
$this->crud->set_crud_url_path(site_url('admin/index/index/navigation')); | |
$this->crud->set_table('navigation'); | |
$this->crud->columns('name', 'parent_id'); | |
$this->crud->unset_delete(); | |
$this->crud->unset_add(); | |
$this->crud->fields('name', 'parent_id'); | |
/* Add this customisation for parent_relation */ | |
$primary_id_field = 'id'; | |
$table_name = 'navigation'; | |
$relation_field_name = 'parent_id'; | |
$parent_field = 'parent_id'; | |
$title_field = 'name_pl'; | |
$title_display_as = 'Chose parent category'; | |
$same_table = TRUE; //not required, the default is false | |
$this->set_parent_relation($primary_id_field, $table_name, $parent_field, $title_field, $this->crud, $title_display_as, $relation_field_name, NULL, $same_table); | |
$this->data['crud'] = $this->crud->render(); | |
} | |
/** | |
* @param $primary_id_field | |
* @param $table_name | |
* @param $parent_field | |
* @param $title_field | |
* @param $crud | |
* @param $title_display_as | |
* @param $relation_field_name | |
* @param null $where | |
* @param bool $same_table | |
*/ | |
function set_parent_relation($primary_id_field, $table_name, $parent_field, $title_field, $crud, $title_display_as, $relation_field_name, $where = NULL, $same_table = FALSE) | |
{ | |
$this->gc_id = $primary_id_field; | |
$this->gc_table = $table_name; | |
$this->gc_field_name = $relation_field_name; | |
$this->gc_parent = $parent_field; | |
$this->gc_title = $title_field; | |
$this->gc_where = $where; | |
$this->gc_title_display_as = $title_display_as; | |
$this->gc_same_table = $same_table; | |
$crud->set_css('assets/grocery_crud/css/jquery_plugins/chosen/chosen.css'); | |
$crud->set_js('assets/grocery_crud/js/jquery-1.10.2.min.js'); | |
$crud->set_js('assets/grocery_crud/js/jquery_plugins/jquery.chosen.min.js'); | |
$crud->set_js('assets/grocery_crud/js/jquery_plugins/ajax-chosen.js'); | |
$crud->set_js('assets/grocery_crud/js/jquery_plugins/config/jquery.chosen.config.js'); | |
$crud->set_relation($this->gc_field_name, $this->gc_table, $this->gc_title); | |
$crud->callback_field($this->gc_field_name, array($this, 'parent_relation_callback')); | |
} | |
/** | |
* @param string $value | |
* @param null $primary | |
* | |
* @return string | |
*/ | |
function parent_relation_callback($value = '', $primary = NULL) | |
{ | |
if (! empty($value)) | |
{ | |
$this->gc_history = $this->get_parent_history($value); | |
} | |
$return_string = '<script type="text/javascript"> var ajax_relation_url = ""; </script>'; | |
$return_string .= '<select name="' . $this->gc_field_name . '" class="chosen-select" data-placeholder="' . $this->gc_title_display_as . '"><option value=""></option>'; | |
$return_string .= $this->parent_repeat(NULL, - 1, $value); | |
$return_string .= '</select>'; | |
return $return_string; | |
} | |
/** | |
* @param null $parent | |
* @param $tree_level | |
* @param string $value | |
* | |
* @return string | |
*/ | |
function parent_repeat($parent = NULL, $tree_level = -1, $value = '') | |
{ | |
$return_string = ''; | |
if ($this->gc_where !== NULL) | |
{ | |
$this->db->where($this->gc_where); | |
} | |
if ($parent === NULL) | |
{ | |
$this->db->where($this->gc_parent . ' IS NULL'); | |
} | |
else | |
{ | |
$this->db->where($this->gc_parent, $parent); | |
} | |
$db_result = $this->db->get($this->gc_table); | |
if ($db_result->num_rows() > 0) | |
{ | |
$tree_level ++; | |
foreach ($db_result->result() as $result) | |
{ | |
$tree_string = $tree_level != 0 ? '|' . str_repeat('-', $tree_level * 4) : ''; | |
$selected = $value !== '' && $value == $result->{$this->gc_id} ? 'selected = "selected"' : ''; | |
$disabled = $this->gc_same_table && ! empty($value) && in_array($result->{$this->gc_id}, $this->gc_history) ? 'disabled="disabled"' : ''; | |
$return_string .= "<option value='" . $result->{$this->gc_id} . "' {$selected} {$disabled} >{$tree_string} " . $result->{$this->gc_title} . "</option>"; | |
$return_string .= $this->parent_repeat($result->{$this->gc_id}, $tree_level, $value); | |
} | |
} | |
return $return_string; | |
} | |
/** | |
* @param $id | |
* | |
* @return array | |
*/ | |
function get_parent_history($id) | |
{ | |
$history = array(); | |
$this->db->where($this->gc_id, $id); | |
$result = $this->db->get($this->gc_table)->row(); | |
$history[] = $result->{$this->gc_id}; | |
while (! empty($result->{$this->gc_parent})) | |
{ | |
$this->db->where($this->gc_id, $result->{$this->gc_parent}); | |
$result = $this->db->get($this->gc_table)->row(); | |
$history[] = $result->{$this->gc_id}; | |
} | |
return $history; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment