Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // form validation greater than example http://stackoverflow.com/questions/1996197/codeigniter-validation-how-to-limit-numerical-value
- require_once("MY_Ezbuild.php");
- if (!defined('BASEPATH'))
- exit('No direct script access allowed');
- class Destination extends MY_Ezbuild {
- function __construct() {
- parent::__construct();
- //$this->load->database();
- $this->load->helper('url');
- /* ------------------ */
- $this->load->library('grocery_CRUD');
- $this->delimiter = "- ";
- $this->lu_delimiter = " - ";
- $this->form_validation->set_message('required', '* required');
- $this->session->set_userdata('language', 'EN');
- $this->lan = $this->session->userdata('language');
- $this->lan = "NL";
- }
- private function _subject_from_meta($language, $table) {
- $default_subject = ucfirst($table->name);
- $default_subject = str_replace("_", " ", $default_subject);
- $sql = "select `name` from `m_table_subject` `mts`
- where `mts`.`language` = ? and `mts`.`m_tableID` = ?";
- $query = $this->defaultdb->query($sql, Array($language, $table->m_tableID));
- foreach ($query->result() as $row) {
- $subject = $row->name;
- }
- if (!isset($subject)) {
- $subject = $default_subject;
- }
- return ($subject);
- }
- private function _create_hyperlinks($sql, $primary_key, $table, $fields) {
- $query = $this->defaultdb->query($sql, array($primary_key));
- $str = "";
- $keyfield_hyper = $table . "ID";
- foreach ($query->result() as $row) {
- $url = site_url("$table/index/read/" . $row->$keyfield_hyper);
- $hyper = "";
- foreach ($fields as $field) {
- if (isset($row->$field)) {
- if ($hyper != "") {
- $hyper .= " - " . $row->$field;
- } else {
- $hyper .= $row->$field;
- }
- }
- }
- if ($hyper != "") {
- $str .= "<a href='" . $url . "'>" . $hyper . "</a><br/>";
- }
- }
- $str = $this->rtrim($str, "<br/>");
- return $str;
- }
- private function _get_table_subject($tableID) {
- if (is_numeric($tableID)) {
- $table = $this->_findtablebyID($tableID);
- } else {
- $table = $this->_findtable($tableID);
- }
- $subject = "";
- if (isset($table)) {
- if ($this->lan == "NL") {
- $subject = $this->_subject_from_meta("NL", $table);
- } else if ($this->lan == "EN") {
- $subject = $this->_subject_from_meta("EN", $table);
- } else if ($this->lan == "FR") {
- $subject = $this->_subject_from_meta("FR", $table);
- } else if ($this->lan == "DE") {
- $subject = $this->_subject_from_meta("DE", $table);
- }
- }
- return ($subject);
- }
- private function _lookup_fields($tableID, $prefix) {
- $sql = "select `name` from m_field where m_tableID = $tableID and lookup_sequence <> 0 and is_AIfield = 0 order by lookup_sequence";
- $string = "";
- $query = $this->defaultdb->query($sql, array($tableID));
- foreach ($query->result() as $row) {
- $string .= $prefix . "." . $row->name . ",";
- }
- if ($string == "") {
- $sql = "select `name` from m_field where m_tableID = ? and (`name` = 'name' or `mysql_type` like 'varchar%')";
- $query = $this->defaultdb->query($sql, array($tableID));
- foreach ($query->result() as $row) {
- if ($row->name == "name") { // preference for name field
- $string = $prefix . "." . $row->name . ",";
- break;
- } else {
- $string = $prefix . "." . $row->name . ",";
- }
- }
- }
- if ($string != "") {
- $string = $this->rtrim($string, ",");
- } else {
- $this->log("No lookup fields found tableID = $tableID");
- }
- return ($string);
- }
- function _init_stack($table1ID, $table2ID) {
- $this->joinstack = array();
- $sql = "select mr.m_relationID, mr.table1ID, mr.table2ID, mr.cardinality "
- . " from m_relation mr where (mr.table1ID = ?) or (mr.table2ID = ?)";
- $query = $this->defaultdb->query($sql, array($table1ID, $table1ID));
- // what tables are linked to $table1ID
- $finished = false;
- foreach ($query->result() as $row) {
- if ((($row->table2ID == $table2ID) && ($row->table1ID == $table1ID)) ||
- (($row->table2ID == $table1ID) && ($row->table1ID == $table2ID))) {
- $entry = new stdClass();
- $entry->table1ID = $table1ID;
- $entry->table2ID = $table2ID;
- $entry->valid = true;
- $finished = true;
- } else {
- $entry = new stdClass();
- $entry->table1ID = $row->table1ID;
- $entry->table2ID = $row->table2ID;
- $entry->valid = false;
- }
- array_push($this->joinstack, $entry);
- }
- return ($finished);
- }
- function _add_2_stack($table1ID, $table2ID) {
- $found = false;
- foreach ($this->joinstack as $entry) {
- if (( ($entry->table1ID == $table1ID ) && ($entry->table2ID == $table2ID ) ) ||
- ( ($entry->table2ID == $table1ID ) && ($entry->table1ID == $table2ID ) )) {
- $found = true;
- }
- }
- if (!$found) {
- $entry = new stdClass();
- $entry->table1ID = $table1ID;
- $entry->table2ID = $table2ID;
- $entry->valid = false;
- array_push($this->joinstack, $entry);
- return true;
- } else {
- return false;
- }
- }
- function _topstack() {
- $count = count($this->joinstack);
- if ($count > 0) {
- return ( $this->joinstack[$count - 1]);
- }
- }
- /*
- * Recursive routine to create the joins
- */
- function _process_stack() {
- $top_stack = $this->_topstack();
- if (isset($top_stack)) {
- $sql = "select mr.m_relationID, mr.table1ID, mr.table2ID, mr.cardinality "
- . " from m_relation mr where (mr.table1ID = ?) or (mr.table2ID = ?)";
- $debug = str_replace("?", $top_stack->table2ID, $sql);
- $query = $this->defaultdb->query($sql, array($top_stack->table2ID, $top_stack->table2ID));
- $ladd = false;
- $finished = false;
- foreach ($query->result() as $row) {
- if (($row->table1ID == $this->endtable->m_tableID) || ($row->table2ID == $this->endtable->m_tableID)) {
- $finished = true;
- $top_stack->valid = true;
- if ($row->table1ID == $top_stack->table2ID) {
- $ladd = $this->_add_2_stack($row->table2ID, $top_stack->table2ID);
- } else if ($row->table2ID == $top_stack->table2ID) {
- $ladd = $this->_add_2_stack($row->table1ID, $top_stack->table2ID);
- }
- if ($ladd) {
- $top_stack = $this->_topstack();
- $top_stack->valid = true;
- }
- break;
- } else if ($row->table1ID == $top_stack->table2ID) {
- $ladd = $this->_add_2_stack($row->table1ID, $row->table2ID);
- }
- }
- if ($finished) {
- return (true);
- } else {
- if (!$ladd) {
- array_pop($this->joinstack);
- }
- $finished = $this->_process_stack();
- if ($finished) {
- return (true);
- }
- }
- } else {
- return (true);
- }
- }
- /*
- * Process the stack and build the join
- */
- function _makejoin() {
- $size = count($this->joinstack);
- $sqljoin = "";
- $stop = false;
- $couple_table_info = null;
- $couple_table = null;
- while (!$stop) {
- $top_stack = $this->_topstack();
- if (isset($top_stack)) {
- if ($this->endtable->m_tableID == $top_stack->table1ID) {
- $couple_table = $top_stack->table2ID;
- } else if ($this->endtable->m_tableID == $top_stack->table2ID) {
- $couple_table = $top_stack->table1ID;
- } else if ($couple_table == $top_stack->table1ID) {
- $couple_table = $top_stack->table2ID;
- } else if ($couple_table == $top_stack->table2ID) {
- $couple_table = $top_stack->table1ID;
- } else {
- $couple_table = $top_stack->table1ID;
- }
- $couple_table_prev = $couple_table_info;
- $couple_table_info = $this->_findtablebyID($couple_table);
- if ($sqljoin == "") {
- $sqljoin .= " inner join " . $couple_table_info->name . " on " . $couple_table_info->name . "." .
- $this->endtable->name . "ID = " . $this->endtable->name . "." . $this->endtable->name . "ID";
- } else if (isset($couple_table_prev)) {
- $sqljoin .= " inner join " . $couple_table_info->name . " on " . $couple_table_info->name . "." .
- $couple_table_info->name . "ID = " . $couple_table_prev->name . "." . $couple_table_info->name . "ID";
- }
- array_pop($this->joinstack);
- $stop = ($couple_table == $this->begintable->m_tableID);
- } else {
- $stop = true; // stack is empty
- }
- }
- if ($sqljoin != "") {
- $fields = $this->_lookup_fields($this->endtable->m_tableID, $this->endtable->name);
- if ($fields != "") {
- $fields = $this->endtable->name . "." . $this->endtable->name . "ID," . $fields;
- $sqljoin = "select distinct $fields from " . $this->endtable->name . $sqljoin .
- " and " . $this->begintable->name . "." . $this->begintable->name . "ID = ?";
- }
- }
- return ($sqljoin);
- }
- function _create_join_extra($source_table, $lookup_table, $couple_table, $keyfield) {
- $table1 = $this->_findtable($source_table);
- $table2 = $this->_findtable($lookup_table);
- $ct = $this->_findtablebyID($couple_table);
- $this->begintable = $table1;
- $this->endtable = $table2;
- $this->keyfield = $keyfield;
- $sqljoin = "";
- $sqljoin .= " left outer join " . $ct->name . " on " . $ct->name . "." . $source_table . "ID =" . $source_table . "." . $source_table . "ID";
- $sqljoin .= " left outer join " . $lookup_table . " on " . $ct->name . "." . $lookup_table . "ID =" . $lookup_table . "." . $lookup_table . "ID";
- if ($sqljoin != "") {
- $fields = $this->_lookup_fields($this->endtable->m_tableID, $this->endtable->name);
- if ($fields != "") {
- $fields = $this->endtable->name . "." . $this->endtable->name . "ID," . $fields;
- $sqljoin = "select distinct $fields from " . $table1->name . $sqljoin .
- " and " . $this->begintable->name . "." . $this->begintable->name . "ID = ?";
- }
- }
- return ($sqljoin);
- }
- function _create_join_extra_old(
- $source_table, $lookup_table, $keyfield) {
- $table1 = $this->_findtable($source_table);
- $table2 = $this->_findtable($lookup_table);
- $this->begintable = $table1;
- $this->endtable = $table2;
- $this->keyfield = $keyfield;
- $finished = $this->_init_stack($table1->m_tableID, $table2->m_tableID);
- if (!$finished) {
- $finished = $this->_process_stack();
- if ($finished) {
- $sql = $this->_makejoin();
- }
- } else {
- $sql = $this->_makejoin(); // simple join
- }
- return ($sql);
- }
- private function _create_join($source_table, $field_name, $lookup_table, $lookup_tableID) {
- if ($lookup_tableID != -1) {
- $table1 = $this->rtrim($field_name, "ID"); // scrum_master
- $table1ID = $this->_findtable($table1);
- if (isset($table1ID)) {
- $sql = "select cardinality from m_relation where (table1ID = ? and table2ID = ?) or (table1ID = ? and table2ID = ?) ";
- $fields = $this->_lookup_fields($lookup_tableID, $lookup_table);
- $query = $this->defaultdb->query($sql, array($table1ID, $lookup_tableID, $lookup_tableID, $table1ID));
- foreach ($query->result() as $row) {
- $sql_join = " inner join $table1 on $table1.$field_name = $source_table.$field_name
- inner join $lookup_table on $lookup_table.$lookup_table" . "ID" . " = $table1.$lookup_table" . "ID" .
- " and $source_table.$field_name = ?";
- $sql = "select distinct $fields from $source_table $sql_join";
- $this->cb_lines[] = "//$sql";
- return ($sql);
- }
- } else {
- return;
- }
- } else {
- // so a directlink must be an ID field of a table
- $table1 = $this->rtrim($field_name, "ID"); // scrum_master
- $table1ID = $this->_findtable($table1);
- $fields = $this->_lookup_fields($table1ID);
- $sql_join = " inner join $table1 on $table1.$field_name = $source_table.$fieldname
- inner join $lookup_table on $lookup_table.$field_name" . "ID" . " = $source_table.$lookup_table" . "ID" .
- " and $source_table.$fieldname = ?";
- $sql = "select distinct $fields from $source_table $sql_join";
- $this->cb_lines[] = "//$sql";
- return ($sql);
- }
- }
- public function _generate_output($output = null) {
- $this->load->view('standard_view.php', $output);
- }
- public function index() {
- $crud = new grocery_CRUD();
- $crud->set_theme('datatables');
- if ($this->lan == "NL") {
- $subject = "bestemming";
- } else if ($this->lan == "EN") {
- $subject = "Destination";
- } else if ($this->lan == "FR") {
- $subject = "Destination";
- } else if ($this->lan == "DE") {
- $subject = "Destination";
- }
- $crud->set_subject($subject);
- $crud->set_table('destination');
- $crud->columns('name', 'cluster', 'segment', 'class', 'premium_building', 'premium_inventory_goods', 'premium_company_risk', 'checked', 'updated', 'CEA_codeID', 'destination_codeID');
- $crud->callback_column('CEA_codeID', array($this, '_callback_destination_CEA_codeID'));
- $crud->callback_column('destination_codeID', array($this, '_callback_destination_destination_codeID'));
- $crud->callback_before_update(array($this, '_update_record'));
- $crud->fields('name', 'cluster', 'segment', 'class', 'premium_building', 'premium_inventory_goods', 'premium_company_risk', 'checked', 'updated', 'CEA_codeID', 'destination_codeID');
- if ($this->lan == "NL") {
- $crud->display_as('destinationID', 'Destination');
- $crud->display_as('name', 'Bestemming');
- $crud->display_as('cluster', 'Cluster');
- $crud->display_as('segment', 'Segment');
- $crud->display_as('class', 'Klasse');
- $crud->display_as('premium_building', 'Premie gebouw');
- $crud->display_as('premium_inventory_goods', 'Premie inventaris goederen');
- $crud->display_as('premium_company_risk', 'Premie bedrijfsschade');
- $crud->display_as('checked', 'Gecontroleerd');
- $crud->display_as('updated', 'Datum wijziging');
- $crud->display_as('CEA_codeID', 'CEA code');
- $crud->display_as('destination_codeID', 'Destination code');
- } else if ($this->lan == "EN") {
- $crud->display_as('destinationID', 'Destination');
- $crud->display_as('name', 'Name');
- $crud->display_as('cluster', 'Cluster');
- $crud->display_as('segment', 'Segment');
- $crud->display_as('class', 'Class');
- $crud->display_as('premium_building', 'Premium building');
- $crud->display_as('premium_inventory_goods', 'Premium inventory goods');
- $crud->display_as('premium_company_risk', 'Premium company risk');
- $crud->display_as('checked', 'Checked');
- $crud->display_as('updated', 'Updated');
- $crud->display_as('CEA_codeID', 'CEA code');
- $crud->display_as('destination_codeID', 'Destination code');
- } else if ($this->lan == "FR") {
- $crud->display_as('destinationID', 'Destination');
- $crud->display_as('name', 'Name');
- $crud->display_as('cluster', 'Cluster');
- $crud->display_as('segment', 'Segment');
- $crud->display_as('class', 'Class');
- $crud->display_as('premium_building', 'Premium building');
- $crud->display_as('premium_inventory_goods', 'Premium inventory goods');
- $crud->display_as('premium_company_risk', 'Premium company risk');
- $crud->display_as('checked', 'Checked');
- $crud->display_as('updated', 'Updated');
- $crud->display_as('CEA_codeID', 'CEA code');
- $crud->display_as('destination_codeID', 'Destination code');
- } else if ($this->lan == "DE") {
- $crud->display_as('destinationID', 'Destination');
- $crud->display_as('name', 'Name');
- $crud->display_as('cluster', 'Cluster');
- $crud->display_as('segment', 'Segment');
- $crud->display_as('class', 'Class');
- $crud->display_as('premium_building', 'Premium building');
- $crud->display_as('premium_inventory_goods', 'Premium inventory goods');
- $crud->display_as('premium_company_risk', 'Premium company risk');
- $crud->display_as('checked', 'Checked');
- $crud->display_as('updated', 'Updated');
- $crud->display_as('CEA_codeID', 'CEA code');
- $crud->display_as('destination_codeID', 'Destination code');
- }
- if ($this->lan == "NL") {
- ;
- } else if ($this->lan == "EN") {
- ;
- } else if ($this->lan == "FR") {
- ;
- } else if ($this->lan == "DE") {
- ;
- }
- if ($this->lan == "NL") {
- ;
- } else if ($this->lan == "EN") {
- ;
- } else if ($this->lan == "FR") {
- ;
- } else if ($this->lan == "DE") {
- ;
- }
- if ($this->lan == "NL") {
- ;
- } else if ($this->lan == "EN") {
- ;
- } else if ($this->lan == "FR") {
- ;
- } else if ($this->lan == "DE") {
- ;
- }
- /* so only 1 item can be filled in */
- $sql = 'SELECT CEA_code.CEA_codeID,CEA_code.CEA_code FROM `CEA_code`';
- $query = $this->db->query($sql);
- $choose = array();
- foreach ($query->result() as $row) {
- $lu_value = '';
- foreach (get_object_vars($row) as $column => $value) {
- $lu_value .= $value . $this->lu_delimiter;
- }
- $lu_value = rtrim($lu_value, $this->lu_delimiter);
- $entry = $lu_value;
- $choose[$row->CEA_codeID] = $entry;
- }
- $crud->field_type('CEA_codeID', 'dropdown', $choose);
- /* so only 1 item can be filled in */
- $sql = 'SELECT destination_code.destination_codeID,destination_code.destination_code FROM `destination_code`';
- $query = $this->db->query($sql);
- $choose = array();
- foreach ($query->result() as $row) {
- $lu_value = '';
- foreach (get_object_vars($row) as $column => $value) {
- $lu_value .= $value . $this->lu_delimiter;
- }
- $lu_value = rtrim($lu_value, $this->lu_delimiter);
- $entry = $lu_value;
- $choose[$row->destination_codeID] = $entry;
- }
- $crud->field_type('destination_codeID', 'dropdown', $choose);
- if ($this->lan == "NL") {
- $crud->field_type('checked', 'enum', array('Nee', 'Ja'));
- } else if ($this->lan == "EN") {
- ;
- } else if ($this->lan == "FR") {
- ;
- } else if ($this->lan == "DE") {
- ;
- }
- $crud->field_type('name', 'string');
- $crud->field_type('cluster', 'string');
- $crud->field_type('segment', 'string');
- $crud->field_type('class', 'string');
- $crud->field_type('premium_building', 'string');
- $crud->field_type('premium_inventory_goods', 'string');
- $crud->field_type('premium_company_risk', 'string');
- $crud->required_fields('updated');
- $output = $crud->render();
- $state = $crud->getState();
- $state_info = $crud->getStateInfo();
- if (($state == "edit") || ($state == "read")) {
- $primary_key = $state_info->primary_key;
- $sql = 'select CEA_code.CEA_codeID,CEA_code.CEA_code from destination left outer join _Z_REL_CEA_code_destination on _Z_REL_CEA_code_destination.destinationID = destination.destinationID inner join CEA_code on _Z_REL_CEA_code_destination.CEA_codeID = CEA_code.CEA_codeID and destination.destinationID = ?';
- $CEA_codeID = null;
- $query = $this->defaultdb->query($sql, array($primary_key));
- foreach ($query->result() as $row) {
- $CEA_codeID = $row->CEA_codeID;
- }
- $primary_key = $state_info->primary_key;
- $sql = 'select destination_code.destination_codeID,destination_code.destination_code from destination left outer join _Z_REL_destination_code_destination on _Z_REL_destination_code_destination.destinationID = destination.destinationID inner join destination_code on _Z_REL_destination_code_destination.destination_codeID = destination_code.destination_codeID and destination.destinationID = ?';
- $destination_codeID = null;
- $query = $this->defaultdb->query($sql, array($primary_key));
- foreach ($query->result() as $row) {
- $destination_codeID = $row->destination_codeID;
- }
- $view_arr = array(
- 'CEA_codeID' => $CEA_codeID,
- 'destination_codeID' => $destination_codeID,
- 'output' => $output
- );
- $this->load->view('destination.php', $view_arr);
- } else {
- $view_arr = array(
- 'output' => $output
- );
- $this->load->view('destination.php', $view_arr);
- }
- }
- function _update_record($post_array, $primary_key) {
- $this->log("cb update");
- foreach ($post_array as $post) {
- echo ($post);
- }
- }
- public function _callback_destination_CEA_codeID($value, $row) {
- $field = 'destinationID';
- $keyfield = $row->$field;
- $speed = 'destination_CEA_code';
- if (isset($this->$speed)) {
- $sql = $this->$speed;
- } else {
- $sql = $this->_create_join_extra('destination', 'CEA_code', 2, $keyfield);
- $this->$speed = $sql;
- }
- if ((isset($sql) && ($sql != ""))) {
- $query = $this->defaultdb->query($sql, array($keyfield));
- $hyperlinks = '';
- $hyper_text = '';
- foreach ($query->result() as $row) {
- $hyperlink = null;
- foreach ($row as $key => $value) {
- if ((!$this->endswith($key, 'ID')) && (isset($value))) {
- $hyper_text .= $value . $this->delimiter;
- } else if (isset($value)) {
- $hyperlink = $value;
- }
- }
- if (isset($hyperlink)) {
- $hyper_text = $this->rtrim($hyper_text, $this->delimiter);
- $hyperlinks .= '<a href="' . site_url('CEA_code/index/read/' . $hyperlink) . '">' . $hyper_text . '</a>, ';
- }
- }
- if ($hyperlinks != '') {
- $hyperlinks = $this->rtrim($hyperlinks, ', ');
- return $hyperlinks;
- }
- }
- }
- public function _callback_destination_destination_codeID($value, $row) {
- $field = 'destinationID';
- $keyfield = $row->$field;
- $speed = 'destination_destination_code';
- if (isset($this->$speed)) {
- $sql = $this->$speed;
- } else {
- $sql = $this->_create_join_extra('destination', 'destination_code', 3, $keyfield);
- $this->$speed = $sql;
- }
- if ((isset($sql) && ($sql != ""))) {
- $query = $this->defaultdb->query($sql, array($keyfield));
- $hyperlinks = '';
- $hyper_text = '';
- foreach ($query->result() as $row) {
- $hyperlink = null;
- foreach ($row as $key => $value) {
- if ((!$this->endswith($key, 'ID')) && (isset($value))) {
- $hyper_text .= $value . $this->delimiter;
- } else if (isset($value)) {
- $hyperlink = $value;
- }
- }
- if (isset($hyperlink)) {
- $hyper_text = $this->rtrim($hyper_text, $this->delimiter);
- $hyperlinks .= '<a href="' . site_url('destination_code/index/read/' . $hyperlink) . '">' . $hyper_text . '</a>, ';
- }
- }
- if ($hyperlinks != '') {
- $hyperlinks = $this->rtrim($hyperlinks, ', ');
- return $hyperlinks;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement