From 0ea8f5e1a8221abcc3d014d2ae276a415fa99c2c Mon Sep 17 00:00:00 2001 From: GoFrendi Gunawan Date: Sun, 16 Jun 2013 22:14:29 +0700 Subject: [PATCH] add generic_example and generic_model --- application/controllers/generic_examples.php | 256 ++++++++ .../generic_grocery_crud_model.php | 617 ++++++++++++++++++ 2 files changed, 873 insertions(+) create mode 100644 application/controllers/generic_examples.php create mode 100755 application/controllers/generic_grocery_crud_model.php diff --git a/application/controllers/generic_examples.php b/application/controllers/generic_examples.php new file mode 100644 index 00000000..18cc0d37 --- /dev/null +++ b/application/controllers/generic_examples.php @@ -0,0 +1,256 @@ +load->database(); + $this->load->helper('url'); + + $this->load->library('grocery_CRUD'); + } + + public function new_crud(){ + $crud = new grocery_CRUD(); + $crud->set_model('generic_grocery_crud_model'); + return $crud; + } + + public function _example_output($output = null) + { + $this->load->view('example.php',$output); + } + + public function offices() + { + $crud = $this->new_crud(); + $output = $crud->render(); + + $this->_example_output($output); + } + + public function index() + { + $this->_example_output((object)array('output' => '' , 'js_files' => array() , 'css_files' => array())); + } + + public function offices_management() + { + try{ + $crud = $this->new_crud(); + + $crud->set_theme('datatables'); + $crud->set_table('offices'); + $crud->set_subject('Office'); + $crud->required_fields('city'); + $crud->columns('city','country','phone','addressLine1','postalCode'); + + $output = $crud->render(); + + $this->_example_output($output); + + }catch(Exception $e){ + show_error($e->getMessage().' --- '.$e->getTraceAsString()); + } + } + + public function employees_management() + { + $crud = $this->new_crud(); + + $crud->set_theme('datatables'); + $crud->set_table('employees'); + $crud->set_relation('officeCode','offices','city'); + $crud->display_as('officeCode','Office City'); + $crud->set_subject('Employee'); + + $crud->required_fields('lastName'); + + $crud->set_field_upload('file_url','assets/uploads/files'); + + $output = $crud->render(); + + $this->_example_output($output); + } + + public function customers_management() + { + $crud = $this->new_crud(); + + $crud->set_table('customers'); + $crud->columns('customerName','contactLastName','phone','city','country','salesRepEmployeeNumber','creditLimit'); + $crud->display_as('salesRepEmployeeNumber','from Employeer') + ->display_as('customerName','Name') + ->display_as('contactLastName','Last Name'); + $crud->set_subject('Customer'); + $crud->set_relation('salesRepEmployeeNumber','employees','lastName'); + + $output = $crud->render(); + + $this->_example_output($output); + } + + public function orders_management() + { + $crud = $this->new_crud(); + + $crud->set_relation('customerNumber','customers','{contactLastName} {contactFirstName}'); + $crud->display_as('customerNumber','Customer'); + $crud->set_table('orders'); + $crud->set_subject('Order'); + $crud->unset_add(); + $crud->unset_delete(); + + $output = $crud->render(); + + $this->_example_output($output); + } + + public function products_management() + { + $crud = $this->new_crud(); + + $crud->set_table('products'); + $crud->set_subject('Product'); + $crud->unset_columns('productDescription'); + $crud->callback_column('buyPrice',array($this,'valueToEuro')); + + $output = $crud->render(); + + $this->_example_output($output); + } + + public function valueToEuro($value, $row) + { + return $value.' €'; + } + + public function film_management() + { + $crud = $this->new_crud(); + + $crud->set_table('film'); + $crud->set_relation_n_n('actors', 'film_actor', 'actor', 'film_id', 'actor_id', 'fullname','priority'); + $crud->set_relation_n_n('category', 'film_category', 'category', 'film_id', 'category_id', 'name'); + $crud->unset_columns('special_features','description','actors'); + + $crud->fields('title', 'description', 'actors' , 'category' ,'release_year', 'rental_duration', 'rental_rate', 'length', 'replacement_cost', 'rating', 'special_features'); + + $output = $crud->render(); + + $this->_example_output($output); + } + + public function film_management_twitter_bootstrap() + { + try{ + $crud = $this->new_crud(); + + $crud->set_theme('twitter-bootstrap'); + $crud->set_table('film'); + $crud->set_relation_n_n('actors', 'film_actor', 'actor', 'film_id', 'actor_id', 'fullname','priority'); + $crud->set_relation_n_n('category', 'film_category', 'category', 'film_id', 'category_id', 'name'); + $crud->unset_columns('special_features','description','actors'); + + $crud->fields('title', 'description', 'actors' , 'category' ,'release_year', 'rental_duration', 'rental_rate', 'length', 'replacement_cost', 'rating', 'special_features'); + + $output = $crud->render(); + $this->_example_output($output); + + }catch(Exception $e){ + show_error($e->getMessage().' --- '.$e->getTraceAsString()); + } + } + + function multigrids() + { + $this->config->load('grocery_crud'); + $this->config->set_item('grocery_crud_dialog_forms',true); + $this->config->set_item('grocery_crud_default_per_page',10); + + $output1 = $this->offices_management2(); + + $output2 = $this->employees_management2(); + + $output3 = $this->customers_management2(); + + $js_files = $output1->js_files + $output2->js_files + $output3->js_files; + $css_files = $output1->css_files + $output2->css_files + $output3->css_files; + $output = "

List 1

".$output1->output."

List 2

".$output2->output."

List 3

".$output3->output; + + $this->_example_output((object)array( + 'js_files' => $js_files, + 'css_files' => $css_files, + 'output' => $output + )); + } + + public function offices_management2() + { + $crud = new grocery_CRUD(); + $crud->set_table('offices'); + $crud->set_subject('Office'); + + $crud->set_crud_url_path(site_url(strtolower(__CLASS__."/".__FUNCTION__)),site_url(strtolower(__CLASS__."/multigrids"))); + + $output = $crud->render(); + + if($crud->getState() != 'list') { + $this->_example_output($output); + } else { + return $output; + } + } + + public function employees_management2() + { + $crud = $this->new_crud(); + + $crud->set_theme('datatables'); + $crud->set_table('employees'); + $crud->set_relation('officeCode','offices','city'); + $crud->display_as('officeCode','Office City'); + $crud->set_subject('Employee'); + + $crud->required_fields('lastName'); + + $crud->set_field_upload('file_url','assets/uploads/files'); + + $crud->set_crud_url_path(site_url(strtolower(__CLASS__."/".__FUNCTION__)),site_url(strtolower(__CLASS__."/multigrids"))); + + $output = $crud->render(); + + if($crud->getState() != 'list') { + $this->_example_output($output); + } else { + return $output; + } + } + + public function customers_management2() + { + + $crud = $this->new_crud(); + + $crud->set_table('customers'); + $crud->columns('customerName','contactLastName','phone','city','country','salesRepEmployeeNumber','creditLimit'); + $crud->display_as('salesRepEmployeeNumber','from Employeer') + ->display_as('customerName','Name') + ->display_as('contactLastName','Last Name'); + $crud->set_subject('Customer'); + $crud->set_relation('salesRepEmployeeNumber','employees','lastName'); + + $crud->set_crud_url_path(site_url(strtolower(__CLASS__."/".__FUNCTION__)),site_url(strtolower(__CLASS__."/multigrids"))); + + $output = $crud->render(); + + if($crud->getState() != 'list') { + $this->_example_output($output); + } else { + return $output; + } + } + +} diff --git a/application/controllers/generic_grocery_crud_model.php b/application/controllers/generic_grocery_crud_model.php new file mode 100755 index 00000000..a1cb0672 --- /dev/null +++ b/application/controllers/generic_grocery_crud_model.php @@ -0,0 +1,617 @@ + + * @version test + * @link http://www.grocerycrud.com/documentation + */ +class generic_grocery_CRUD_Model extends CI_Model { + + protected $primary_key = null; + protected $table_name = null; + protected $relation = array(); + protected $relation_n_n = array(); + protected $primary_keys = array(); + + function __construct() + { + parent::__construct(); + } + + function db_table_exists($table_name = null) + { + return $this->db->table_exists($table_name); + } + + function get_list() + { + if($this->table_name === null) + return false; + + $select = $this->db->protect_identifiers("{$this->table_name}").".*"; + + //set_relation special queries + if(!empty($this->relation)) + { + foreach($this->relation as $relation) + { + list($field_name , $related_table , $related_field_title) = $relation; + $unique_join_name = $this->_unique_join_name($field_name); + $unique_field_name = $this->_unique_field_name($field_name); + + if(strstr($related_field_title,'{')) + { + $related_field_title = str_replace(" "," ",$related_field_title); + $select .= ", CONCAT('".str_replace(array('{','}'),array("',COALESCE({$unique_join_name}.",", ''),'"),str_replace("'","\\'",$related_field_title))."') as $unique_field_name"; + } + else + { + $select .= ', ' . $this->db->protect_identifiers($unique_join_name).'.'. $this->db->protect_identifiers($related_field_title).' AS '. $this->db->protect_identifiers($unique_field_name); + } + + if($this->field_exists($related_field_title)) + $select .= ", `{$this->table_name}`.$related_field_title AS '{$this->table_name}.$related_field_title'"; + } + } + + //set_relation_n_n special queries. We prefer sub queries from a simple join for the relation_n_n as it is faster and more stable on big tables. + if(!empty($this->relation_n_n)) + { + $select = $this->relation_n_n_queries($select); + } + + $this->db->select($select, false); + + $results = $this->db->get($this->table_name)->result(); + + return $results; + } + + public function get_row($table_name = null) + { + $table_name = $table_name === null ? $this->table_name : $table_name; + + return $this->db->get($table_name)->row(); + } + + public function set_primary_key($field_name, $table_name = null) + { + $table_name = $table_name === null ? $this->table_name : $table_name; + + $this->primary_keys[$table_name] = $field_name; + } + + protected function relation_n_n_queries($select) + { + $this_table_primary_key = $this->get_primary_key(); + foreach($this->relation_n_n as $relation_n_n) + { + list($field_name, $relation_table, $selection_table, $primary_key_alias_to_this_table, + $primary_key_alias_to_selection_table, $title_field_selection_table, $priority_field_relation_table) = array_values((array)$relation_n_n); + + $primary_key_selection_table = $this->get_primary_key($selection_table); + + $field = ""; + $use_template = strpos($title_field_selection_table,'{') !== false; + $field_name_hash = $this->_unique_field_name($title_field_selection_table); + if($use_template) + { + $title_field_selection_table = str_replace(" ", " ", $title_field_selection_table); + $field .= "CONCAT('".str_replace(array('{','}'),array("',COALESCE(",", ''),'"),str_replace("'","\\'",$title_field_selection_table))."')"; + } + else + { + $field .= "$selection_table.$title_field_selection_table"; + } + + //Sorry Codeigniter but you cannot help me with the subquery! + $select .= ", (SELECT GROUP_CONCAT(DISTINCT $field) FROM $selection_table " + ."LEFT JOIN $relation_table ON $relation_table.$primary_key_alias_to_selection_table = $selection_table.$primary_key_selection_table " + ."WHERE $relation_table.$primary_key_alias_to_this_table = `{$this->table_name}`.$this_table_primary_key GROUP BY $relation_table.$primary_key_alias_to_this_table) AS $field_name"; + } + + return $select; + } + + function order_by($order_by , $direction) + { + $this->db->order_by( $order_by , $direction ); + } + + function where($key, $value = NULL, $escape = TRUE) + { + $this->db->where( $key, $value, $escape); + } + + function or_where($key, $value = NULL, $escape = TRUE) + { + $this->db->or_where( $key, $value, $escape); + } + + function having($key, $value = NULL, $escape = TRUE) + { + $this->db->having( $key, $value, $escape); + } + + function or_having($key, $value = NULL, $escape = TRUE) + { + $this->db->or_having( $key, $value, $escape); + } + + function like($field, $match = '', $side = 'both') + { + $this->db->like($field, $match, $side); + } + + function or_like($field, $match = '', $side = 'both') + { + $this->db->or_like($field, $match, $side); + } + + function limit($value, $offset = '') + { + $this->db->limit( $value , $offset ); + } + + function get_total_results() + { + //set_relation_n_n special queries. We prefer sub queries from a simple join for the relation_n_n as it is faster and more stable on big tables. + if(!empty($this->relation_n_n)) + { + $select = "{$this->table_name}.*"; + $select = $this->relation_n_n_queries($select); + + $this->db->select($select,false); + + return $this->db->get($this->table_name)->num_rows(); + } + + return $this->db->count_all_results($this->table_name); + + } + + function set_basic_table($table_name = null) + { + if( !($this->db->table_exists($table_name)) ) + return false; + + $this->table_name = $table_name; + + return true; + } + + function get_edit_values($primary_key_value) + { + $primary_key_field = $this->get_primary_key(); + $this->db->where($primary_key_field,$primary_key_value); + $result = $this->db->get($this->table_name)->row(); + return $result; + } + + function join_relation($field_name , $related_table , $related_field_title) + { + $related_primary_key = $this->get_primary_key($related_table); + + if($related_primary_key !== false) + { + $unique_name = $this->_unique_join_name($field_name); + $this->db->join( $related_table.' as '.$unique_name , "$unique_name.$related_primary_key = {$this->table_name}.$field_name",'left'); + + $this->relation[$field_name] = array($field_name , $related_table , $related_field_title); + + return true; + } + + return false; + } + + function set_relation_n_n_field($field_info) + { + $this->relation_n_n[$field_info->field_name] = $field_info; + } + + protected function _unique_join_name($field_name) + { + return 'j'.substr(md5($field_name),0,8); //This j is because is better for a string to begin with a letter and not with a number + } + + protected function _unique_field_name($field_name) + { + return 's'.substr(md5($field_name),0,8); //This s is because is better for a string to begin with a letter and not with a number + } + + function get_relation_array($field_name , $related_table , $related_field_title, $where_clause, $order_by, $limit = null, $search_like = null) + { + $relation_array = array(); + $field_name_hash = $this->_unique_field_name($field_name); + + $related_primary_key = $this->get_primary_key($related_table); + + $select = "$related_table.$related_primary_key, "; + + if(strstr($related_field_title,'{')) + { + $related_field_title = str_replace(" ", " ", $related_field_title); + $select .= "CONCAT('".str_replace(array('{','}'),array("',COALESCE(",", ''),'"),str_replace("'","\\'",$related_field_title))."') as $field_name_hash"; + } + else + { + $select .= "$related_table.$related_field_title as $field_name_hash"; + } + + $this->db->select($select,false); + if($where_clause !== null) + $this->db->where($where_clause); + + if($where_clause !== null) + $this->db->where($where_clause); + + if($limit !== null) + $this->db->limit($limit); + + if($search_like !== null) + $this->db->having("$field_name_hash LIKE '%".$this->db->escape_like_str($search_like)."%'"); + + $order_by !== null + ? $this->db->order_by($order_by) + : $this->db->order_by($field_name_hash); + + $results = $this->db->get($related_table)->result(); + + foreach($results as $row) + { + $relation_array[$row->$related_primary_key] = $row->$field_name_hash; + } + + return $relation_array; + } + + function get_ajax_relation_array($search, $field_name , $related_table , $related_field_title, $where_clause, $order_by) + { + return $this->get_relation_array($field_name , $related_table , $related_field_title, $where_clause, $order_by, 10 , $search); + } + + function get_relation_total_rows($field_name , $related_table , $related_field_title, $where_clause) + { + if($where_clause !== null) + $this->db->where($where_clause); + + return $this->db->count_all_results($related_table); + } + + function get_relation_n_n_selection_array($primary_key_value, $field_info) + { + $select = ""; + $related_field_title = $field_info->title_field_selection_table; + $use_template = strpos($related_field_title,'{') !== false;; + $field_name_hash = $this->_unique_field_name($related_field_title); + if($use_template) + { + $related_field_title = str_replace(" ", " ", $related_field_title); + $select .= "CONCAT('".str_replace(array('{','}'),array("',COALESCE(",", ''),'"),str_replace("'","\\'",$related_field_title))."') as $field_name_hash"; + } + else + { + $select .= "$related_field_title as $field_name_hash"; + } + $this->db->select('*, '.$select,false); + + $selection_primary_key = $this->get_primary_key($field_info->selection_table); + + if(empty($field_info->priority_field_relation_table)) + { + if(!$use_template){ + $this->db->order_by("{$field_info->selection_table}.{$field_info->title_field_selection_table}"); + } + } + else + { + $this->db->order_by("{$field_info->relation_table}.{$field_info->priority_field_relation_table}"); + } + $this->db->where($field_info->primary_key_alias_to_this_table, $primary_key_value); + $this->db->join( + $field_info->selection_table, + "{$field_info->relation_table}.{$field_info->primary_key_alias_to_selection_table} = {$field_info->selection_table}.{$selection_primary_key}" + ); + $results = $this->db->get($field_info->relation_table)->result(); + + $results_array = array(); + foreach($results as $row) + { + $results_array[$row->{$field_info->primary_key_alias_to_selection_table}] = $row->{$field_name_hash}; + } + + return $results_array; + } + + function get_relation_n_n_unselected_array($field_info, $selected_values) + { + $use_where_clause = !empty($field_info->where_clause); + + $select = ""; + $related_field_title = $field_info->title_field_selection_table; + $use_template = strpos($related_field_title,'{') !== false; + $field_name_hash = $this->_unique_field_name($related_field_title); + + if($use_template) + { + $related_field_title = str_replace(" ", " ", $related_field_title); + $select .= "CONCAT('".str_replace(array('{','}'),array("',COALESCE(",", ''),'"),str_replace("'","\\'",$related_field_title))."') as $field_name_hash"; + } + else + { + $select .= "$related_field_title as $field_name_hash"; + } + $this->db->select('*, '.$select,false); + + if($use_where_clause){ + $this->db->where($field_info->where_clause); + } + + $selection_primary_key = $this->get_primary_key($field_info->selection_table); + if(!$use_template) + $this->db->order_by("{$field_info->selection_table}.{$field_info->title_field_selection_table}"); + $results = $this->db->get($field_info->selection_table)->result(); + + $results_array = array(); + foreach($results as $row) + { + if(!isset($selected_values[$row->$selection_primary_key])) + $results_array[$row->$selection_primary_key] = $row->{$field_name_hash}; + } + + return $results_array; + } + + function db_relation_n_n_update($field_info, $post_data ,$main_primary_key) + { + $this->db->where($field_info->primary_key_alias_to_this_table, $main_primary_key); + if(!empty($post_data)) + $this->db->where_not_in($field_info->primary_key_alias_to_selection_table , $post_data); + $this->db->delete($field_info->relation_table); + + $counter = 0; + if(!empty($post_data)) + { + foreach($post_data as $primary_key_value) + { + $where_array = array( + $field_info->primary_key_alias_to_this_table => $main_primary_key, + $field_info->primary_key_alias_to_selection_table => $primary_key_value, + ); + + $this->db->where($where_array); + $count = $this->db->from($field_info->relation_table)->count_all_results(); + + if($count == 0) + { + if(!empty($field_info->priority_field_relation_table)) + $where_array[$field_info->priority_field_relation_table] = $counter; + + $this->db->insert($field_info->relation_table, $where_array); + + }elseif($count >= 1 && !empty($field_info->priority_field_relation_table)) + { + $this->db->update( $field_info->relation_table, array($field_info->priority_field_relation_table => $counter) , $where_array); + } + + $counter++; + } + } + } + + function db_relation_n_n_delete($field_info, $main_primary_key) + { + $this->db->where($field_info->primary_key_alias_to_this_table, $main_primary_key); + $this->db->delete($field_info->relation_table); + } + + function get_field_types_basic_table() + { + $db_field_types = array(); + /*ORIGINAL SCRIPT : + foreach($this->db->query("SHOW COLUMNS FROM `{$this->table_name}`")->result() as $db_field_type) + */ + foreach($this->db->field_data($this->table_name) as $db_field_type) + { + $db_type = $db_field_type->type; + $length = $db_field_type->max_length; + $db_field_types[$db_field_type->name]['db_max_length'] = $length; + $db_field_types[$db_field_type->name]['db_type'] = $db_type; + $db_field_types[$db_field_type->name]['db_null'] = true; + $db_field_types[$db_field_type->name]['db_extra'] = ''; + /*ORIGINAL SCRIPT : + $type = explode("(",$db_field_type->Type); + $db_type = $type[0]; + + + if(isset($type[1])) + { + if(substr($type[1],-1) == ')') + { + $length = substr($type[1],0,-1); + } + else + { + list($length) = explode(" ",$type[1]); + $length = substr($length,0,-1); + } + } + else + { + $length = ''; + } + $db_field_types[$db_field_type->Field]['db_max_length'] = $length; + $db_field_types[$db_field_type->Field]['db_type'] = $db_type; + $db_field_types[$db_field_type->Field]['db_null'] = $db_field_type->Null == 'YES' ? true : false; + $db_field_types[$db_field_type->Field]['db_extra'] = $db_field_type->Extra; + */ + } + + $results = $this->db->field_data($this->table_name); + foreach($results as $num => $row) + { + $row = (array)$row; + $results[$num] = (object)( array_merge($row, $db_field_types[$row['name']]) ); + } + } + + function get_field_types($table_name) + { + $results = $this->db->field_data($table_name); + return $results; + } + + function db_update($post_array, $primary_key_value) + { + $primary_key_field = $this->get_primary_key(); + return $this->db->update($this->table_name,$post_array, array( $primary_key_field => $primary_key_value)); + } + + function db_insert($post_array) + { + $insert = $this->db->insert($this->table_name,$post_array); + if($insert) + { + return $this->db->insert_id(); + } + return false; + } + + function db_delete($primary_key_value) + { + $primary_key_field = $this->get_primary_key(); + + if($primary_key_field === false) + return false; + + //IN SQLITE this produce error + //$this->db->limit(1); + $this->db->delete($this->table_name,array( $primary_key_field => $primary_key_value)); + if( $this->db->affected_rows() != 1) + return false; + else + return true; + } + + function db_file_delete($field_name, $filename) + { + if( $this->db->update($this->table_name,array($field_name => ''),array($field_name => $filename)) ) + { + return true; + } + else + { + return false; + } + } + + function field_exists($field,$table_name = null) + { + if(empty($table_name)) + { + $table_name = $this->table_name; + } + /*ORIGINAL SCRIPT + return $this->db->field_exists($field,$table_name); + */ + // sqlite doesn't support this + $field_data_list = $this->db->field_data($table_name); + foreach($field_data_list as $field_data){ + if($field_data->name == $field) return TRUE; + } + return FALSE; + } + + function get_primary_key($table_name = null) + { + if($table_name == null) + { + if(isset($this->primary_keys[$this->table_name])) + { + return $this->primary_keys[$this->table_name]; + } + + if(empty($this->primary_key)) + { + $fields = $this->get_field_types_basic_table(); + + /*ORIGINAL SCRIPT + foreach($fields as $field) + { + if($field->primary_key == 1) + { + return $field->name; + } + } + + return false; + */ + $primary_key = FALSE; + foreach($fields as $field) + { + if(isset($field->primary_key) && $field->primary_key == 1) + { + $primary_key = $field->name; + break; + } + } + // pqsql doesn't provide primary key information. In this case, assume the first field as primary + if($primary_key === FALSE){ + $primary_key = $fields[0]->name; + } + + return $primary_key; + } + else + { + return $this->primary_key; + } + } + else + { + if(isset($this->primary_keys[$table_name])) + { + return $this->primary_keys[$table_name]; + } + + $fields = $this->get_field_types($table_name); + + /*ORIGINAL SCRIPT + foreach($fields as $field) + { + if($field->primary_key == 1) + { + return $field->name; + } + } + return FALSE; + */ + $primary_key = FALSE; + foreach($fields as $field) + { + if(isset($field->primary_key) && $field->primary_key == 1) + { + $primary_key = $field->name; + break; + } + } + // pqsql doesn't provide primary key information. In this case, assume the first field as primary + if($primary_key === FALSE){ + $primary_key = $fields[0]->name; + } + + return $primary_key; + } + + } + + function escape_str($value) + { + return $this->db->escape_str($value); + } + +}