Advertisement
Guest User

Untitled

a guest
Jun 19th, 2014
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 61.53 KB | None | 0 0
  1. <?php defined('BASEPATH') or exit('No direct script access allowed');
  2. class MY_Controller extends CI_Controller {
  3.  
  4.     public function __construct()
  5.     {
  6.         parent::__construct();
  7.         if ($this->input->is_ajax_request()) {
  8.             $this->output->enable_profiler(false);
  9.         } else {
  10.             $this->output->enable_profiler(true);
  11.         }
  12.  
  13.       $this->lang->load('account', 'russian');
  14.       $this->lang->load('admin', 'russian');
  15.     }
  16. }
  17.  
  18.  
  19. class Admin_Controller extends MY_Controller
  20. {
  21.  
  22.     public $data; //Данные вида
  23.     public $current_section; //Текущая страница
  24.     public $breadcrumbs = array(); //Хлебный крошки
  25.     public $catalog_menu; //Меню
  26.     public $filter_fields = array();
  27.     public $js_files = array();
  28.     public $css_files = array();
  29.     static $sdata;
  30.     public $table_bd;
  31.     //дополнительные данные в GC вид
  32.     public $extra_data;
  33.     public $fields = array();
  34.     public $display_as = array();
  35.  
  36.     public $current_object = null;
  37.     public $current_location = null;
  38.  
  39.     //USER  DATA
  40.     //group id
  41.     public $group_id;
  42.     //email
  43.     public $user_email;
  44.     //access rights
  45.     public $access = null;
  46.  
  47.  
  48.     //для поиска
  49.     public $mydata;
  50.  
  51.     public $post_srch = null;
  52.  
  53.  
  54.  
  55.  
  56.     //dependent select
  57.     public $dependent_select = null;
  58.  
  59.     function __construct()
  60.     {
  61.  
  62.  
  63.         parent::__construct();
  64.  
  65.         if ($this->ion_auth->logged_in()==false) {
  66.             redirect('auth');
  67.        }
  68.  
  69.         $this->group_id = $this->session->userdata('group_id');
  70.         $this->user_email = $this->session->userdata('email');
  71.         $this->access = $this->initAccess();
  72.  
  73.  
  74.         //если не ajax то проверяем права
  75.         if(!$this->input->is_ajax_request()){
  76.             switch ($this->ckeck_access()){
  77.  
  78.                 case 'denied': redirect('main/access_denied');
  79.                 break;
  80.  
  81.                 case 'restriction': redirect('main/access_restricted');
  82.                 break;
  83.  
  84.                 default : '';
  85.             }
  86.         }
  87.  
  88.  
  89.  
  90.         $this->mydata = new stdClass();
  91.         $this->extra_data = new stdClass();
  92.  
  93.  
  94.         $this->load->model('mod_admin');
  95.         $this->data =  new stdClass();
  96.         $this->wccache->delete_all();
  97.         $this->load->library('grocery_CRUD');
  98.         //$this->load->library('image_CRUD');
  99.         $this->crud = new grocery_CRUD(); //Подключаю CRUD
  100.  
  101.  
  102.  
  103.         $this->load->model('mod_admin', 'mod_admin', true);
  104.         $this->crud->set_theme('twitter-bootstrap');
  105.  
  106.  
  107.        $url = $this->uri->segment_array();
  108.  
  109.  
  110.        if(in_array('objects',$url)){
  111.             $this->current_object = 'objects';
  112.         }else if(in_array('realtors',$url)){
  113.             $this->current_object = 'realtors';
  114.         }else if(in_array('clients',$url)){
  115.             $this->current_object = 'clients';
  116.         }
  117.  
  118.  
  119.         $url_string = $this->uri->uri_string();
  120.  
  121.         if(startsWith($url_string,'objects')){
  122.             $this->current_location = 'objects';
  123.         }else if(startsWith($url_string,'clients')){
  124.             $this->current_location = 'clients';
  125.         } else if(startsWith($url_string,'realtors')){
  126.             $this->current_location = 'realtors';
  127.         } else if(startsWith($url_string,'exportdata')){
  128.             if(startsWith($url_string,'exportdata/objects')){
  129.                 $this->current_location = 'exportdata/objects';
  130.             }else if(startsWith($url_string,'exportdata/realtors')){
  131.                 $this->current_location = 'exportdata/realtors';
  132.             }
  133.         } else if(startsWith($url_string,'deleted/objects')){
  134.             $this->current_location = 'deleted/objects';
  135.         } else if(startsWith($url_string,'deleted/realtors')){
  136.             $this->current_location = 'deleted/realtors';
  137.         } else if(startsWith($url_string,'deleted/clients')){
  138.             $this->current_location = 'deleted/clients';
  139.         }
  140.  
  141.  
  142.  
  143.        $this->load->helper('database');
  144.  
  145.        $this->mydata->custom_data = array();
  146.  
  147.        $this->mydata->current_location = $this->current_location;
  148.  
  149.        $this->mydata->object_types = $this->db->get('object_types')->result();
  150.        $this->mydata->regions = $this->db->get('regions')->result();
  151.        $this->mydata->object_views = $this->db->get('object_views')->result();
  152.        $this->mydata->furnitures = $this->db->get('furnitures')->result();
  153.        $this->mydata->repairs = $this->db->get('repairs')->result();
  154.        $this->mydata->sources = $this->db->get('sources')->result();
  155.        $this->mydata->materials = $this->db->get('materials')->result();
  156.        $this->mydata->status =  field_enums('objects', 'status');
  157.        $this->mydata->sms_status =  field_enums('contractors', 'sms_status');
  158.        $this->mydata->call_status =  field_enums('contractors', 'call_status');
  159.        $this->mydata->realtor_status =  field_enums('contractors', 'realtor_status');
  160.  
  161.     }
  162.  
  163.  
  164.  
  165.  
  166.     function _example_output()
  167.     {
  168.  
  169.  
  170.         //если есть дополнительные данные в GC вид
  171.         if(!empty($this->extra_data)){
  172.             $this->crud->set_extra($this->extra_data);
  173.         }
  174.  
  175.  
  176.         //если не пустой массив с fields
  177.         if(!empty($this->fields)){
  178.             $this->crud->fields($this->fields);
  179.         }
  180.  
  181.         //название колонок и полей
  182.         if(!empty($this->display_as)){
  183.             foreach($this->display_as as $field => $title){
  184.                 $this->crud->display_as($field,$title);
  185.             }
  186.         }
  187.  
  188.  
  189.         $this->crud->set_table($this->table_bd);
  190.  
  191.         $this->data = $this->crud->render();
  192.  
  193.         //dependent select
  194.         if($this->dependent_select){
  195.             $this->data->output .= $this->dependent_select;
  196.         }
  197.  
  198.  
  199.         //тут выбираем какой поиск показывать соответственно страницам Объекты,Клиенты,Риэлторы
  200.  
  201.         switch ($this->current_object){
  202.             case 'objects':
  203.                 $this->data->search = $this->load->view('object_search',$this->mydata,true);
  204.             break;
  205.  
  206.             case 'realtors':
  207.                 $this->data->search = $this->load->view('realtor_search',$this->mydata,true);
  208.             break;
  209.  
  210.             case 'clients':
  211.                 $this->data->search = $this->load->view('client_search',$this->mydata,true);
  212.             break;
  213.  
  214.             default : $this->data->search = '';
  215.         }
  216.  
  217.  
  218.         $this->data->custom_data = $this->mydata->custom_data;
  219.  
  220.         $this->data->user_email = $this->user_email;
  221.         $this->data->access = $this->access;
  222.         $this->data->group_id = $this->group_id;
  223.  
  224.         $this->data->sdata = Admin_Controller::$sdata;
  225.         $this->data->old_action = $this->crud->getState();
  226.         $this->data->breadcrumbs = $this->breadcrumbs;
  227.         $this->data->current_section = $this->current_section;
  228.         $this->data->catalog_menu = $this->catalog_menu;
  229.         $this->data->js_files = $this->data->js_files + $this->js_files;
  230.         $this->data->css_files = $this->data->css_files + $this->css_files;
  231.         $this->load->view('main', $this->data);
  232.     }
  233.  
  234.  
  235.     //инициализируем права доступа
  236.     private function initAccess(){
  237.         $access_temp = array();
  238.         //текущий пользователь
  239.         $online_user = $this->db
  240.                             ->select('expires_date')
  241.                             ->where('id',$this->session->userdata('user_id'))
  242.                             ->get('users')
  243.                             ->row();
  244.         //если срок действия прав истек
  245.         if(date('Y-m-d H:i:s') > $online_user->expires_date){
  246.            /*$this->db
  247.                 ->where('user_id',$this->session->userdata('user_id'))
  248.                 ->delete('access_users');*/
  249.             return $access_temp;
  250.         } else {
  251.         // извлекаем список доступов
  252.             $access_temp = $this->db->select('a.name')
  253.                                                ->where('user_id',$this->session->userdata('user_id'))
  254.                                                ->join('access a','a.id = a_u.access_id')
  255.                                                ->get('access_users a_u')
  256.                                                ->result();
  257.             // если не пустой то создаем список доступов
  258.             if(!empty($access_temp)){
  259.                 foreach($access_temp as $key => $val){
  260.                     $access_temp[$key]=$val->name;
  261.                 }
  262.             }
  263.         }
  264.  
  265.         return $access_temp;
  266.     }
  267.  
  268.  
  269.  
  270.     /***********************    ПРОВЕРКА ДОСТУПОВ  ********************************/
  271.     private function ckeck_access(){
  272.         $uri_segments =  explode('/',$this->uri->uri_string());
  273.  
  274.         if($uri_segments[0] != 'main'){
  275.  
  276.             if(empty($this->access)){
  277.  
  278.                 return 'denied';
  279.  
  280.             } else {
  281.  
  282.                 //если нет никаких вообще доступов или не main страница и нет доступа к текущей странице - ошибка
  283.                 if((!in_array($uri_segments[0],$this->access))){
  284.  
  285.                     //$this->firephp->log('Пустые доступы или нет доступа к текущей странице');
  286.                     return 'restriction';
  287.                 }
  288.             }
  289.         }
  290.  
  291.  
  292.         return 'ok';
  293.         /***********************    ПРОВЕРКА ДОСТУПОВ *конец*  ********************************/
  294.     }
  295.  
  296.  
  297.     public function _callback_filter($data, $id = 'null')
  298.     {
  299.  
  300.  
  301.         if (isset($data['uri'])) {
  302.  
  303.             $data['uri'] = $this->mod_admin->get_is_uri($data['name'], $data['uri'], $this->
  304.                 table_bd, $id);
  305.         }
  306.  
  307.         foreach ($data as $k => &$v) {
  308.  
  309.  
  310.             if (array_key_exists($k, $this->filter_fields)) {
  311.                 $filters = $this->filter_fields[$k];
  312.  
  313.                 foreach ($filters as $key => $val) {
  314.                     if (function_exists($val) and $val != 'htmlspecialchars') {
  315.                         eval("\$data[\$k]  = \$val(\$v);");
  316.  
  317.  
  318.                     } elseif (function_exists($val) and $val == 'htmlspecialchars') {
  319.  
  320.                         $data[$k] = htmlspecialchars($v, ENT_QUOTES);
  321.  
  322.                     }
  323.  
  324.                 }
  325.  
  326.  
  327.             }
  328.  
  329.        if(!is_array($v))
  330.        {
  331.          $data[$k] = $this->encode_php_tags(trim($v));
  332.        }
  333.        else
  334.        {
  335.         foreach ($v as $ks=>$vs) {
  336.           $data[$k][$ks] = $this->encode_php_tags(trim($vs));
  337.         }
  338.        }
  339.  
  340.  
  341.  
  342.         }
  343.  
  344.  
  345.         return $data;
  346.     }
  347.     public function encode_php_tags($str)
  348.     {
  349.         return str_replace(array('<?php', '<?PHP', '<?', '?>'),  array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
  350.     }
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.      public function _unique_field_name($field_name) {
  358.         return 's'.substr(md5($field_name),0,8);
  359.     }
  360.  
  361.     public function _unique_join_name($field_name){
  362.          return 'j'.substr(md5($field_name),0,8);
  363.     }
  364.  
  365. }
  366.  
  367.  
  368.  
  369.  
  370.  class Common_contractor_controller extends Admin_Controller {
  371.  
  372.  
  373.     public $state = null;
  374.     public $state_info = null;
  375.  
  376.     function __construct(){
  377.         parent::__construct();
  378.  
  379.         if($this->input->get('refresh')){
  380.             $this->session->unset_userdata('srch');
  381.         }
  382.  
  383.  
  384.  
  385.  
  386.         //реализация поиска - записываем пост данные в сессию.
  387.         //удаление из сесси - реализовать кнопку для отчистки поиска с удалением из сессии.
  388.         if($this->input->post('srch')){
  389.  
  390.             $this->post_srch = $this->input->post();
  391.             $this->session->set_userdata('srch',$this->input->post());
  392.         } else if($this->session->userdata('srch')){
  393.             $this->post_srch = $this->session->userdata('srch');
  394.         }
  395.  
  396.  
  397.         $this->state = $this->crud->getState();
  398.         $this->state_info = $this->crud->getStateInfo();
  399.  
  400.  
  401.         //общий поиск по полям контрагента
  402.         if(!empty($this->post_srch)){
  403.  
  404.             $sms_status = (isset($this->post_srch['sms-status']))?$this->post_srch['sms-status']:'';
  405.             $sms_date_after = (isset($this->post_srch['sms-date-after']))?$this->post_srch['sms-date-after']:'';
  406.             $sms_date_before = (isset($this->post_srch['sms-date-before']))?$this->post_srch['sms-date-before']:'';
  407.             $call_status = (isset($this->post_srch['call-status']))?$this->post_srch['call-status']:'';
  408.             $call_date_after = (isset($this->post_srch['call-date-after']))?$this->post_srch['call-date-after']:'';
  409.             $call_date_before = (isset($this->post_srch['call-date-before']))?$this->post_srch['call-date-before']:'';
  410.  
  411.             $realtor_status = (isset($this->post_srch['realtor_status']))?$this->post_srch['realtor_status']:'';
  412.  
  413.             if(!empty($sms_status)){
  414.                  $this->crud->where('j03c540b8.sms_status',$sms_status); //j03c540b8 name of table "contractors"
  415.               }
  416.  
  417.               if(!empty($sms_date_after)){
  418.                  $this->crud->where('j03c540b8.sms_date >=',date('Y-m-d H:i:s',strtotime($sms_date_after))); //j03c540b8 name of table "contractors"
  419.               }
  420.  
  421.               if(!empty($sms_date_before)){
  422.                  $this->crud->where('j03c540b8.sms_date >=',date('Y-m-d H:i:s',strtotime($sms_date_before))); //j03c540b8 name of table "contractors"
  423.               }
  424.  
  425.               if(!empty($call_status)){
  426.                     $query = '(';
  427.                     $numItems = count($call_status);
  428.                     $i = 0;
  429.                     foreach($call_status as $c_s) {
  430.                       $query .= 'j03c540b8.call_status  = '.$this->db->escape($c_s);
  431.                       if(++$i !== $numItems) {
  432.                           $query .= ' OR ';
  433.                       } else {
  434.                          $query .= ')';
  435.                       }
  436.                     }
  437.                     $this->crud->where($query,null,false);
  438.  
  439.               }
  440.  
  441.               if(!empty($call_date_after)){
  442.                     $this->crud->where('j03c540b8.call_date >=',date('Y-m-d H:i:s',strtotime($call_date_after)));
  443.               }
  444.  
  445.               if(!empty($call_date_before)){
  446.                     $this->crud->where('j03c540b8.call_date <=',date('Y-m-d H:i:s',strtotime($call_date_before)));
  447.               }
  448.  
  449.               if(!empty($realtor_status)){
  450.                                 $query = '(';
  451.                                 $numItems = count($realtor_status);
  452.                                 $i = 0;
  453.                                 foreach($realtor_status as $r_s) {
  454.                                   $query .= 'j03c540b8.realtor_status  = '.$this->db->escape($r_s);
  455.                                   if(++$i !== $numItems) {
  456.                                       $query .= ' OR ';
  457.                                   } else {
  458.                                      $query .= ')';
  459.                                   }
  460.                                 }
  461.                                 $this->crud->where($query,null,false);
  462.  
  463.                     }
  464.  
  465.  
  466.         }
  467.  
  468.          //common CONTRACTOR DISPLAY AS
  469.         $this->display_as = array_merge($this->display_as,array(
  470.             'id'                        =>   lang('id'),
  471.             'contractor_name'           =>   lang('name'),
  472.             'contractor_phone'          =>   lang('phone'),
  473.             'contractor_phone2'         =>   lang('phone2'),
  474.             'contractor_call_status'    =>   lang('status_call'),
  475.             'contractor_call_date'      =>   lang('date_call'),
  476.             'contractor_sms_status'     =>   lang('status_sms'),
  477.             'contractor_sms_date'       =>   lang('date_sms'),
  478.             'contractor_comments'       =>   lang('comments'),
  479.             'delimeter'                 =>   lang('delimeter'),
  480.             'whom'                      =>   lang('whom'),
  481.             'who'                       =>   lang('who'),
  482.  
  483.             'agency'                    =>   lang('agency'),
  484.             'date'                      =>   lang('adding_date'),
  485.             //'status'                  =>   lang('realtor_status'),
  486.             'comments'                  =>   lang('comments')
  487.  
  488.         ));
  489.  
  490.         $this->crud->unset_print();
  491.  
  492.  
  493.         $this->crud
  494.              ->callback_field('contractor_name',array($this,'_field_callback_contractor_name'))
  495.              ->callback_field('contractor_phone',array($this,'_field_callback_contractor_phone'))
  496.              ->callback_field('contractor_phone2',array($this,'_field_callback_contractor_phone2'))
  497.              ->callback_field('contractor_call_status',array($this,'_field_callback_contractor_call_status'))
  498.              ->callback_field('contractor_call_date',array($this,'_field_callback_contractor_call_date'))
  499.              ->callback_field('contractor_sms_status',array($this,'_field_callback_contractor_sms_status'))
  500.              ->callback_field('contractor_sms_date',array($this,'_field_callback_contractor_sms_date'))
  501.              ->callback_field('contractor_comments',array($this,'_field_callback_contractor_comments'))
  502.  
  503.              /*->callback_field('contractor_realtor_status',array($this,'_field_callback_contractor_realtor_status'))*/;
  504.  
  505.         /************** callback field - start ************************************/
  506.         $this->crud
  507.              ->callback_before_insert(array($this,'_callback_before_insert_obj'))
  508.              ->callback_before_update(array($this,'_callback_before_update_obj'));
  509.  
  510.  
  511.          /***** fields type ***********/
  512.         $this->crud->field_type('del','hidden')
  513.                    ->field_type('contractor_sms_status', 'readonly')
  514.                    ->field_type('contractor_sms_date', 'readonly')
  515.                    ->field_type('contractor_call_date', 'readonly')
  516.                    ->field_type('delimeter','readonly');
  517.  
  518.         /************** common callback function ************************************/
  519.  
  520.         $this->crud
  521.             ->callback_column('contractor_name',array($this,'_callback_contractor_name'))
  522.             ->callback_column('contractor_call_status',array($this,'_callback_contractor_call_status'))
  523.             ->callback_column('contractor_sms_status',array($this,'_callback_contractor_sms_status'));
  524.  
  525.  
  526.         if(in_array($this->state,array('edit','update','add','read'))){
  527.             $this->crud->field_type('contractor_id','hidden');
  528.         }
  529.  
  530.  
  531.         if($this->state == 'list' || $this->state == 'success' || $this->state == 'ajax_list' || $this->state == 'export' || $this->state == 'ajax_list_info'){
  532.             $this->display_as['price_start'] = lang('price_start_list');
  533.             $this->display_as['contractor_name'] = lang('name_list');
  534.             //$this->load->model('mod_object_contractor');     // TODO: этот кусок
  535.             //$this->crud->set_model('mod_object_contractor'); // нужен?
  536.             //$this->firephp->log('here!');
  537.         }
  538.  
  539.  
  540.         /********* This for datetimepicker of GC to work ********/
  541.         if($this->state == 'add' || $this->state == 'edit' || $this->state == 'update'){
  542.             $this->crud->set_js('assets/grocery_crud/js/jquery-1.10.2.min.js');
  543.             $this->crud->set_js('assets/grocery_crud/js/jquery_plugins/config/jquery.datepicker.config.js');
  544.             $this->crud->set_js('assets/grocery_crud/js/jquery_plugins/ui/i18n/datepicker/jquery.ui.datepicker-ru.js');
  545.             $this->crud->set_css('assets/grocery_crud/css/ui/simple/jquery-ui-1.10.1.custom.min.css');
  546.             $this->crud->set_css('assets/grocery_crud/css/jquery_plugins/jquery.ui.datetime.css');
  547.         }
  548.  
  549.     }
  550.  
  551.  
  552.     function index(){
  553.  
  554.  
  555.         $this->breadcrumbs[$this->table_bd] = lang($this->table_bd);
  556.         $this->current_section = lang($this->table_bd);
  557.  
  558.  
  559.         if($this->state == 'add'){
  560.  
  561.             if($this->input->post('add_object')){
  562.  
  563.                $phone = $this->input->post('add_object');
  564.  
  565.                $len = strlen($phone);
  566.  
  567.                    if(in_array($len,array(6,11))){
  568.  
  569.                             switch($len){
  570.  
  571.                                 case 6:
  572.                                 $obj = $this->db->where('phone',$phone)->or_where('phone2',$phone)->get('contractors')->row();
  573.                                 break;
  574.                                 case 11:
  575.                                 $phone = substr($phone, -10);
  576.                                 $obj = $this->db->where('phone','8'.$phone)
  577.                                                    ->or_where('phone2','8'.$phone)
  578.                                                    ->or_where('phone','7'.$phone)
  579.                                                    ->or_where('phone2','7'.$phone)
  580.                                                    ->get('contractors')->row();
  581.                                 break;
  582.                                 default:
  583.                                 $obj = '';
  584.  
  585.                             }
  586.                                 if($obj){
  587.  
  588.                                        $this->session->set_userdata('contractor',$obj);
  589.                                 }   else {
  590.                                         $this->session->set_userdata('phone',$phone);
  591.                                 }
  592.  
  593.                   }
  594.  
  595.             } else {
  596.                 $this->session->unset_userdata('contractor');
  597.             }
  598.  
  599.             $this->crud
  600.                  ->field_type('call_status','enum',field_enums('contractors', 'call_status'));
  601.  
  602.         }else if(in_array($this->state,array('edit','update','read'))){
  603.  
  604.             $primary_key = $this->state_info->primary_key;
  605.             $cur_contractor = $this->db->get_where($this->current_object,array('id'=>$primary_key))->row()->contractor_id;
  606.             ($obj = $this->db->get_where('contractors',array('id'=>$cur_contractor))->row()) == false ? show_404(): '';
  607.  
  608.             $this->session->set_userdata('contractor',$obj);
  609.         }
  610.  
  611.  
  612.         //проверяем какие показывать удаленные или не удаленные
  613.         if(in_array('deleted',$this->uri->segment_array())){
  614.             //$this->crud->unset_add();
  615.             $this->extra_data->deleted = true;
  616.             $del = '0';
  617.             $this->breadcrumbs[$this->table_bd] = lang('remote').' '.$this->breadcrumbs[$this->table_bd];
  618.             $this->current_section = lang('remote').' '.$this->current_section;
  619.         } else {
  620.             $del = '1';
  621.             $this->extra_data->deleted = false;
  622.  
  623.         }
  624.  
  625.         $this->crud->where('del',$del);
  626.  
  627.  
  628.         if(!in_array('exportdata',$this->uri->segment_array())){
  629.             $this->extra_data->show_checkbox = true;
  630.             $this->crud->unset_export();
  631.         }
  632.  
  633.  
  634.         $this->crud
  635.                    ->set_rules('contractor_call_status',lang('status_call'),'required')
  636.                    ->set_rules('contractor_call_date',lang('date_call'),'required');
  637.  
  638.  
  639.         $this->fields = array_merge($this->fields,array(
  640.             'contractor_id',
  641.             'delimeter',
  642.             'contractor_name',
  643.             'contractor_phone',
  644.             'contractor_phone2',
  645.             'contractor_call_status',
  646.             'contractor_call_date',
  647.             'contractor_sms_status',
  648.             'contractor_sms_date',
  649.             //'contractor_realtor_status',
  650.             'contractor_comments')
  651.         );
  652.  
  653.  
  654.         if($this->state == 'list'){
  655.             $this->display_as['contractor_name'] = lang('name_list');
  656.         }
  657.  
  658.  
  659.  
  660.         if(!$this->session->userdata('contractor') && $this->state != 'update' && $this->state != 'update_validation'){
  661.  
  662.                 $this->crud->set_rules('contractor_phone', lang('phone'),'callback__validation_phone|required');
  663.         } else {
  664.                 $this->crud->set_rules('contractor_phone', lang('phone'),'required');
  665.         }
  666.  
  667.  
  668.  
  669.         $this->_example_output();
  670.  
  671.     }
  672.  
  673.  
  674.     // хелпер метод для запросов поиска
  675.     function  _return_query_where($table, $field, $array){
  676.         $query = '(';
  677.                     $numItems = count($array);
  678.                     $i = 0;
  679.                     foreach($array as $value) {
  680.                       $query .= ' '.$table.'.'.$field.' = '.(int)$value;
  681.                       if(++$i !== $numItems) {
  682.                           $query .= ' OR ';
  683.                       } else {
  684.                          $query .= ')';
  685.                       }
  686.                     }
  687.         return $query;
  688.  
  689.     }
  690.  
  691.  
  692.     /*********** callback function BEFORE INSERT ***********/
  693.     //callback to insert other objects data and contractor data
  694.      function _callback_before_insert_obj($post_array, $primary_key){
  695.  
  696.             $contractor = array();
  697.  
  698.             $contractor['name'] = strip_tags($post_array['contractor_name']);
  699.             $contractor['phone'] = preg_replace('/[^0-9]/','',$post_array['contractor_phone']);
  700.             $contractor['phone2'] = preg_replace('/[^0-9]/','',$post_array['contractor_phone2']);
  701.             $contractor['call_status'] = $post_array['contractor_call_status'];
  702.             $contractor['comments'] = strip_tags($post_array['contractor_comments']);
  703.             //$contractor['realtor_status'] = $post_array['contractor_realtor_status'];
  704.             if(!empty($post_array['contractor_call_date'])){
  705.                 $contractor['call_date'] = date('Y-m-d H:i:s',strtotime($post_array['contractor_call_date']));
  706.             }
  707.  
  708.  
  709.  
  710.             if($obj = $this->session->userdata('contractor')){
  711.  
  712.                $this->db->where('id',$obj->id)->set($contractor)->update('contractors');
  713.  
  714.  
  715.                 $post_array['contractor_id'] =  $obj->id;
  716.  
  717.  
  718.             } else {
  719.  
  720.                $this->db->set($contractor)->insert('contractors');
  721.  
  722.               $post_array['contractor_id'] = $this->db->insert_id();
  723.  
  724.             }
  725.  
  726.  
  727.             unset($post_array['contractor_call_date']);
  728.             unset($post_array['contractor_name']);
  729.             unset($post_array['contractor_phone']);
  730.             unset($post_array['contractor_phone2']);
  731.             unset($post_array['contractor_call_status']);
  732.             unset($post_array['contractor_comments']);
  733.             //unset($post_array['contractor_realtor_status']);
  734.  
  735.             return $post_array;
  736.  
  737.      }
  738.  
  739.      /*********** callback function BEFORE UPDATE ***********/
  740.      //callback to update other objects data and contractor data
  741.      function _callback_before_update_obj($post_array, $primary_key){
  742.  
  743.  
  744.             $contractor = array();
  745.  
  746.             $contractor['name'] = strip_tags($post_array['contractor_name']);
  747.             $contractor['phone'] = preg_replace('/[^0-9]/','',$post_array['contractor_phone']);
  748.             $contractor['phone2'] = preg_replace('/[^0-9]/','',$post_array['contractor_phone2']);
  749.             $contractor['call_status'] = $post_array['contractor_call_status'];
  750.             $contractor['comments'] = strip_tags($post_array['contractor_comments']);
  751.             //$contractor['realtor_status'] = $post_array['contractor_realtor_status'];
  752.  
  753.  
  754.  
  755.             if(!empty($post_array['contractor_call_date'])){
  756.  
  757.                 $contractor['call_date'] = date('Y-m-d H:i:s',strtotime($post_array['contractor_call_date']));
  758.                 $this->firephp->log($contractor['call_date']);
  759.             }
  760.             //die;
  761.              if($obj = $this->session->userdata('contractor')){
  762.  
  763.                $this->db->where('id',$obj->id)->set($contractor)->update('contractors');
  764.             }
  765.  
  766.  
  767.             unset($post_array['contractor_call_date']);
  768.             unset($post_array['contractor_name']);
  769.             unset($post_array['contractor_phone']);
  770.             unset($post_array['contractor_phone2']);
  771.             unset($post_array['contractor_call_status']);
  772.             unset($post_array['contractor_comments']);
  773.             //unset($post_array['contractor_realtor_status']);
  774.  
  775.             return $post_array;
  776.  
  777.      }
  778.  
  779.  
  780.      /***** common callback field function CONTRACTORS START **************/
  781.      /*public function _field_callback_contractor_realtor_status($value = null,$primary_key = null){
  782.         if($obj = $this->session->userdata('contractor')){
  783.             $value = $obj->realtor_status;
  784.         }
  785.         if(in_array('read',$this->uri->segment_array())){
  786.             return htmlspecialchars($value, ENT_QUOTES);
  787.         } else {
  788.             $options = field_enums('contractors', 'realtor_status');
  789.             $option_tag = '';
  790.             if(!$value){
  791.                 $value = 'нет';
  792.             }
  793.               foreach($options as $option)
  794.               {
  795.                $attribute  = 'value="'.$option.'"';
  796.                if ($option == $value)
  797.                {
  798.                 $attribute .= ' selected="selected"';
  799.                }
  800.                $option_tag .= "<option $attribute>$option</option>";
  801.               }
  802.               return '<select name="contractor_realtor_status">'.$option_tag.'</select>';
  803.         }
  804.     }*/
  805.  
  806.  
  807.  
  808.      public function _field_callback_contractor_name($value = null,$primary_key = null){
  809.         if($obj = $this->session->userdata('contractor')){
  810.             $value = $obj->name;
  811.         }
  812.  
  813.         if(in_array('read',$this->uri->segment_array())){
  814.             return htmlspecialchars($value, ENT_QUOTES);
  815.         } else {
  816.             return '<input type="text" maxlength="50" value="'.$value.'" name="contractor_name" style="width:462px">';
  817.         }
  818.  
  819.     }
  820.  
  821.     //contractor phone
  822.     public function _field_callback_contractor_phone($value = null){
  823.  
  824.         if($obj = $this->session->userdata('contractor')){
  825.             $value = $obj->phone;
  826.         }else if($this->session->userdata('phone')){
  827.             $value = $this->session->userdata('phone');
  828.             $this->session->unset_userdata('phone');
  829.         }
  830.         if(in_array('read',$this->uri->segment_array())){
  831.             return htmlspecialchars($value, ENT_QUOTES);
  832.         } else {
  833.             return '<input type="text" maxlength="50" value="'.$value.'" name="contractor_phone" style="width:462px">';
  834.         }
  835.     }
  836.  
  837.  
  838.     //contractor phone2
  839.     public function _field_callback_contractor_phone2($value = null){
  840.         if($obj = $this->session->userdata('contractor')){
  841.             $value = $obj->phone2;
  842.         }
  843.         if(in_array('read',$this->uri->segment_array())){
  844.             return htmlspecialchars($value, ENT_QUOTES);
  845.         } else {
  846.             return '<input type="text" maxlength="50" value="'.$value.'" name="contractor_phone2" style="width:462px">';
  847.         }
  848.     }
  849.  
  850.  
  851.     //contractor comments
  852.     public function _field_callback_contractor_comments($value = null){
  853.         if($obj = $this->session->userdata('contractor')){
  854.             $value = $obj->comments;
  855.         }
  856.         if(in_array('read',$this->uri->segment_array())){
  857.             return htmlspecialchars($value, ENT_QUOTES);
  858.         } else {
  859.             return '<textarea name="contractor_comments" style="width:462px">'.$value.'</textarea>';
  860.         }
  861.     }
  862.  
  863.     //contractor call status
  864.     public function _field_callback_contractor_call_status($value = null){
  865.  
  866.         if($obj = $this->session->userdata('contractor')){
  867.             $value = $obj->call_status;
  868.         }
  869.         if(in_array('read',$this->uri->segment_array())){
  870.             return htmlspecialchars($value, ENT_QUOTES);
  871.         } else {
  872.             $options = field_enums('contractors', 'call_status');
  873.             $option_tag = '';
  874.             if(!$value){
  875.                 $value = 'на связи';
  876.             }
  877.               foreach($options as $option)
  878.               {
  879.                $attribute  = 'value="'.$option.'"';
  880.                if ($option == $value)
  881.                {
  882.                 $attribute .= ' selected="selected"';
  883.                }
  884.                $option_tag .= "<option $attribute>$option</option>";
  885.               }
  886.               return '<select name="contractor_call_status">'.$option_tag.'</select>';
  887.         }
  888.     }
  889.  
  890.  
  891.     public function _field_callback_contractor_sms_status($value = null){
  892.         if($obj = $this->session->userdata('contractor')){
  893.             $value = $obj->sms_status;
  894.         }
  895.         return $value;
  896.  
  897.     }
  898.  
  899.     public function _field_callback_contractor_sms_date($value = null){
  900.         if($obj = $this->session->userdata('contractor')){
  901.             $value = $obj->sms_date;
  902.         }
  903.         if(!empty($value) && ($value  != '0000-00-00 00:00:00' )){
  904.            return date("d.m.Y H:i", strtotime($value));
  905.         } else {
  906.             return '';
  907.         }
  908.  
  909.  
  910.     }
  911.  
  912.  
  913.     public function _field_callback_contractor_call_date($value = null){
  914.          if($obj = $this->session->userdata('contractor')){
  915.             $value = $obj->call_date;
  916.         }
  917.         if(in_array('read',$this->uri->segment_array())){
  918.             return $value;
  919.         } else {
  920.             $value = (($value != null)&& ($value != '0000-00-00 00:00:00')) ? $value : date("d/m/Y");
  921.             $return = '<input type="text" name="contractor_call_date" value="'.$value.'" class="datepicker-input" /> ';
  922.             $return .= '<a class="datepicker-input-clear" tabindex="-1">Очистить</a> (dd/mm/yyyy)';
  923.             return $return;
  924.         }
  925.  
  926.     }
  927.  
  928.     /***** common callback field function CONTRACTORS END **************/
  929.  
  930.  
  931.  
  932.      public function _callback_contractor_name($value , $row){
  933.  
  934.        $return = '';
  935.  
  936.             if(isset($row->contractor_name) && $row->contractor_name){
  937.                 $return .= $row->contractor_name;
  938.             }
  939.  
  940.             if(isset($row->contractor_phone) && $row->contractor_phone){
  941.               if($row->contractor_name){
  942.                 $return .= "<br>";
  943.               }
  944.                 $return .= $row->contractor_phone;
  945.             }
  946.  
  947.  
  948.        return $return;
  949.  
  950.      }
  951.  
  952.  
  953.  
  954.       public function _callback_contractor_call_status($value , $row){
  955.  
  956.        $return = '';
  957.  
  958.             if(isset($row->contractor_call_status) && $row->contractor_call_status){
  959.                 $return .= $row->contractor_call_status;
  960.             }
  961.             if(isset($row->contractor_call_date) && $row->contractor_call_date && ($row->contractor_call_date !='0000-00-00 00:00:00')){
  962.  
  963.                   $date =  date("d.m.Y", strtotime($row->contractor_call_date));
  964.  
  965.                   if($row->contractor_call_status){
  966.                     $return .= "<br>$date";
  967.                   } else {
  968.                     $return = $date;
  969.                   }
  970.             }
  971.  
  972.        return $return;
  973.  
  974.       }
  975.  
  976.  
  977.  
  978.  
  979.        public function _callback_contractor_sms_status($value , $row){
  980.  
  981.        $return = '';
  982.  
  983.             if(isset($row->contractor_sms_status) && $row->contractor_sms_status){
  984.                 $return .= $row->contractor_sms_status;
  985.             }
  986.             if(isset($row->contractor_sms_date) && $row->contractor_sms_date && ($row->contractor_sms_date !='0000-00-00 00:00:00')){
  987.  
  988.                   $date =  date("d.m.Y", strtotime($row->contractor_sms_date));
  989.  
  990.                   if($row->contractor_sms_status){
  991.                     $return .= "<br>$date";
  992.                   } else {
  993.                     $return = $date;
  994.                   }
  995.             }
  996.  
  997.        return $return;
  998.  
  999.       }
  1000.  
  1001.  
  1002.  
  1003.       /************ validation function for contractor fields **********/
  1004.       public function _validation_phone($value){
  1005.  
  1006.  
  1007.         $value = trim($value);
  1008.         if(empty($value) || !is_numeric($value) || (!in_array(strlen($value),array(6,11)))){
  1009.  
  1010.            $this->form_validation->set_message(__FUNCTION__,lang('phone_error'));
  1011.            return FALSE;
  1012.           }
  1013.  
  1014.         switch(strlen($value)){
  1015.  
  1016.             case 6:
  1017.             $obj = $this->db->where('phone',$value)->or_where('phone2',$value)->get('contractors')->row();
  1018.             break;
  1019.             case 11:
  1020.             $value = substr($value, -10);
  1021.             $obj = $this->db->where('phone','8'.$value)
  1022.                                ->or_where('phone2','8'.$value)
  1023.                                ->or_where('phone','7'.$value)
  1024.                                ->or_where('phone2','7'.$value)
  1025.                                ->get('contractors')->row();
  1026.  
  1027.  
  1028.             break;
  1029.             default:
  1030.  
  1031.             $obj = '';
  1032.  
  1033.           }
  1034.  
  1035.           if(!empty($obj)){
  1036.               $this->form_validation->set_message(__FUNCTION__,lang('phone_error_exists'));
  1037.               return FALSE;
  1038.           }
  1039.  
  1040.  
  1041.            return TRUE;
  1042.  
  1043.  
  1044.     }
  1045.  
  1046.  
  1047. }
  1048.  
  1049. // Общий класс для Риэлторов
  1050. class Common_realtor_controller extends Common_contractor_controller {
  1051.  
  1052.         function __construct(){
  1053.  
  1054.             parent::__construct();
  1055.  
  1056.             $this->table_bd = 'realtors';
  1057.  
  1058.                 //общий поиск для риэлторов(Обычные,Удаленные,Экспорт)
  1059.                 if(!empty($this->post_srch)){
  1060.                     $name = (isset($this->post_srch['name']))?$this->post_srch['name']:'';
  1061.                     $agency = (isset($this->post_srch['agency']))?$this->post_srch['agency']:'';
  1062.                     $phone = (isset($this->post_srch['phone']))?$this->post_srch['phone']:'';
  1063.                     $phone2 = (isset($this->post_srch['phone2']))?$this->post_srch['phone2']:'';
  1064.  
  1065.  
  1066.  
  1067.                     if(!empty($name)){
  1068.                         $this->crud->like('j03c540b8.name',$this->db->escape_like_str($name));
  1069.                     }
  1070.  
  1071.                     if(!empty($agency)){
  1072.                        $this->crud->like($this->current_object.'.agency',$this->db->escape_like_str($agency));
  1073.                     }
  1074.  
  1075.                     if(!empty($phone)){
  1076.                        $this->crud->like('j03c540b8.phone',$this->db->escape_like_str($phone));
  1077.                     }
  1078.  
  1079.                     if(!empty($phone2)){
  1080.                        $this->crud->like('j03c540b8.phone2',$this->db->escape_like_str($phone2));
  1081.                     }
  1082.  
  1083.  
  1084.  
  1085.             }
  1086.         }
  1087.  
  1088.  }
  1089.  
  1090.  
  1091. // Общий класс для Объектов и Клиентов
  1092. class Common_object_and_client_controller extends Common_contractor_controller {
  1093.  
  1094.     //одно поле что отличается у Объекта и Клиента
  1095.     protected $whom_or_who;
  1096.  
  1097.     protected $action;
  1098.     protected $object_type;
  1099.  
  1100.  
  1101.     function __construct(){
  1102.         parent::__construct();
  1103.  
  1104.  
  1105.          //общий поиск по полям объекта или клиента
  1106.         if(!empty($this->post_srch)){
  1107.  
  1108.  
  1109.         $region_id = (isset($this->post_srch['region_id']))?$this->post_srch['region_id']:'';
  1110.         if(isset($this->post_srch['sub_region_id'])){
  1111.             $sub_region_post = $this->post_srch['sub_region_id'];
  1112.         }else{
  1113.             $sub_region_post = array();
  1114.         }
  1115.  
  1116.  
  1117.         $object_type_id = (isset($this->post_srch['object_type_id']))?$this->post_srch['object_type_id']:'';
  1118.         $object_view_id = (isset($this->post_srch['object_view_id']))?$this->post_srch['object_view_id']:'';
  1119.         $repair_id = (isset($this->post_srch['repair_id']))?$this->post_srch['repair_id']:'';
  1120.         $furniture_id = (isset($this->post_srch['furniture_id']))?$this->post_srch['furniture_id']:'';
  1121.         $price_start = (isset($this->post_srch['price_start']))?$this->post_srch['price_start']:'';
  1122.         $price_end = (isset($this->post_srch['price_end']))?$this->post_srch['price_end']:'';
  1123.         $adding_date_after = (isset($this->post_srch['adding-date-after']))?$this->post_srch['adding-date-after']:'';
  1124.         $adding_date_before = (isset($this->post_srch['adding-date-before']))?$this->post_srch['adding-date-before']:'';
  1125.         $text_field = (isset($this->post_srch['text_field']))?$this->post_srch['text_field']:'';
  1126.         $status_date_after_1 = (isset($this->post_srch['status-date-after-1']))?$this->post_srch['status-date-after-1']:'';
  1127.         $status_date_after_2 = (isset($this->post_srch['status-date-after-2']))?$this->post_srch['status-date-after-2']:'';
  1128.         $status_date_before_1 = (isset($this->post_srch['status-date-before-1']))?$this->post_srch['status-date-before-1']:'';
  1129.         $status_date_before_2 = (isset($this->post_srch['status-date-before-2']))?$this->post_srch['status-date-before-2']:'';
  1130.         $source_id = (isset($this->post_srch['source_id']))?$this->post_srch['source_id']:'';
  1131.  
  1132.  
  1133.  
  1134.         if(!empty($region_id)){
  1135.                 $query = '(';
  1136.  
  1137.                     $numItems = count($region_id);
  1138.                     $i = 0;
  1139.                     foreach($region_id as $region){
  1140.                         $sub_regions_obj_from_db = $this->db->select('id')->where('region_id',$region)->get('sub_regions')->result();
  1141.  
  1142.                         $sub_regions_from_db = array();
  1143.                         foreach($sub_regions_obj_from_db as $k => $v){
  1144.                             $sub_regions_from_db[] = $v->id;
  1145.                         }
  1146.  
  1147.                         $tmp_arr = array_intersect($sub_regions_from_db,$sub_region_post);
  1148.  
  1149.                         $total = count($tmp_arr);
  1150.  
  1151.  
  1152.                         if($total > 0){
  1153.                             $counter = 0;
  1154.                             foreach($tmp_arr as $val){
  1155.                                     $query .= '('.$this->current_object.'.region_id = '.(int)$region.' AND '.$this->current_object.'.sub_region_id = '.$val.') ';
  1156.                                     if(++$counter !== $total){
  1157.                                         $query .= ' OR ';
  1158.                                     }
  1159.                             }
  1160.  
  1161.                         } else {
  1162.                             $query .= $this->current_object.'.region_id = '.(int)$region;
  1163.                         }
  1164.  
  1165.                         //last?
  1166.                         if(++$i === $numItems) {
  1167.                            $query .= ')';
  1168.                         } else {
  1169.                            $query .= ') OR (';
  1170.                         }
  1171.  
  1172.                     }
  1173.  
  1174.  
  1175.                  $this->crud->where($query,null,false);
  1176.               }
  1177.  
  1178.  
  1179.  
  1180.               if(!empty($object_type_id)){
  1181.                     $query = $this->_return_query_where($this->current_object,'object_type_id',$object_type_id);
  1182.                     $this->crud->where($query,null,false);
  1183.               }
  1184.  
  1185.  
  1186.               if(!empty($object_view_id)){
  1187.                     $query = $this->_return_query_where($this->current_object,'object_view_id',$object_view_id);
  1188.                     $this->crud->where($query,null,false);
  1189.               }
  1190.  
  1191.                if(!empty($furniture_id)){
  1192.                     $query = $this->_return_query_where($this->current_object,'furniture_id',$furniture_id);
  1193.                     $this->crud->where($query,null,false);
  1194.               }
  1195.  
  1196.                if(!empty($repair_id)){
  1197.                     $query = $this->_return_query_where($this->current_object,'repair_id',$repair_id);
  1198.                     $this->crud->where($query,null,false);
  1199.               }
  1200.  
  1201.  
  1202.               if(!empty($price_end)){
  1203.  
  1204.                     $this->crud->where($this->current_object.'.price_end <=',(int)$price_end);
  1205.               }
  1206.  
  1207.               if(!empty($price_start)){
  1208.  
  1209.                     $this->crud->where($this->current_object.'.price_start >=',(int)$price_start);
  1210.  
  1211.               }
  1212.  
  1213.  
  1214.               if(!empty($text_field)){
  1215.                  $field = $this->post_srch['text_search'];
  1216.                  $this->crud->like($this->current_object.'.'.$field,$this->db->escape_like_str($text_field));
  1217.               }
  1218.  
  1219.  
  1220.  
  1221.               if(!empty($status_date_after_1)){
  1222.                     $this->crud->where('('.$this->current_object.'.status = '.$this->db->escape(lang('status_1')).
  1223.                     ' AND '.$this->current_object.'.status_date >= '.$this->db->escape(date('Y-m-d H:i:s',strtotime($status_date_after_1))).')',null,false);
  1224.               }
  1225.               if(!empty($status_date_after_2)){
  1226.                     $this->crud->where('('.$this->current_object.'.status = '.$this->db->escape(lang('status_2')).
  1227.                     ' AND '.$this->current_object.'.status_date >= '.$this->db->escape(date('Y-m-d H:i:s',strtotime($status_date_after_2))).')',null,false);
  1228.               }
  1229.               if(!empty($status_date_before_1)){
  1230.                     $this->crud->where('('.$this->current_object.'.status = '.$this->db->escape(lang('status_1')).
  1231.                     ' AND '.$this->current_object.'.status_date <= '.$this->db->escape(date('Y-m-d H:i:s',strtotime($status_date_before_1))).')',null,false);
  1232.               }
  1233.               if(!empty($status_date_before_2)){
  1234.                     $this->crud->where('('.$this->current_object.'.status = '.$this->db->escape(lang('status_2')).
  1235.                     ' AND '.$this->current_object.'.status_date <= '.$this->db->escape(date('Y-m-d H:i:s',strtotime($status_date_before_2))).')',null,false);
  1236.               }
  1237.  
  1238.               if(!empty($source_id)){
  1239.                     $this->crud->where($this->current_object.'.source_id',(int)$source_id);
  1240.               }
  1241.  
  1242.  
  1243.               if(!empty($adding_date_after)){
  1244.                     $this->crud->where($this->current_object.'.adding_date >=',date('Y-m-d H:i:s',strtotime($adding_date_after)));
  1245.               }
  1246.  
  1247.               if(!empty($adding_date_before)){
  1248.                     $this->crud->where($this->current_object.'.adding_date <=',date('Y-m-d H:i:s',strtotime($adding_date_before)));
  1249.               }
  1250.  
  1251.  
  1252.         }
  1253.  
  1254.         //сортировка по Дате редактирования
  1255.         $this->crud->order_by('edit_date','desc');
  1256.  
  1257.  
  1258.          //common OBJECT AND CLIENTS DISPLAY AS
  1259.         $this->display_as = array_merge($this->display_as,array(
  1260.  
  1261.  
  1262.             'regions'            =>   lang('region'),
  1263.             'status_date'        =>   lang('status_date'),
  1264.             'object_type_id'     =>   lang('object_types'),
  1265.             'object_view_id'     =>   lang('object_views'),
  1266.             'furniture_id'       =>   lang('furniture'),
  1267.             'descrip'            =>   lang('description_obj'),
  1268.             'edit_date'          =>   lang('edit_date'),
  1269.             'price_end'          =>   lang('price_end'),
  1270.             'price_start'        =>   lang('price_start'),
  1271.             'source_id'          =>   lang('source'),
  1272.             'adding_date'        =>   lang('adding_date'),
  1273.             'status'             =>   lang('status'),
  1274.             'repair_id'          =>   lang('repair'),
  1275.             'action'             =>   lang('action'),
  1276.             'rooms'              =>   lang('rooms'),
  1277.             'material_id'        =>   lang('material'),
  1278.             'area'               =>   lang('area')
  1279.         ));
  1280.  
  1281.  
  1282.         $this->crud
  1283.         ->set_relation('object_type_id','object_types','name')
  1284.         ->set_relation('object_view_id','object_views','name')
  1285.         ->set_relation('furniture_id','furnitures','name')
  1286.         ->set_relation('repair_id','repairs','name')
  1287.         ->set_relation_n_n('regions', 'objects_regions', 'regions', 'object_id', 'region_id', 'name')
  1288.         ->set_relation_n_n('rooms', 'objects_rooms', 'rooms', 'object_id', 'room_id', 'name')
  1289.         ->set_relation('source_id','sources','name')
  1290.         ->set_relation('material_id','materials','name');
  1291.  
  1292.  
  1293.  
  1294.     }
  1295.  
  1296.     //set columns to order like - region_id,street,object_type_id,repair_id,price_start,descrip,whom,status
  1297.     // in parent add - contractor_name, contractor_call_status,contractor_sms_status
  1298.     // and source_id in the end
  1299.     function index(){
  1300.  
  1301.         $actions = array(
  1302.             'sdayut' => lang('sdayut'),
  1303.             'snimut' => lang('snimut'),
  1304.             'sell' => lang('sell'),
  1305.             'buy' => lang('buy')
  1306.         );
  1307.  
  1308.         $object_types = array(
  1309.             'flats' => lang('flat'),
  1310.             'rooms' => lang('room'),
  1311.             'houses' => lang('house')
  1312.         );
  1313.  
  1314.         if (isset($actions[$this->action])) {
  1315.             $this->action = $actions[$this->action];
  1316.         } else {
  1317.             $this->action = '';
  1318.         }
  1319.  
  1320.         if (isset($object_types[$this->object_type])) {
  1321.             $this->object_type = $object_types[$this->object_type];
  1322.         } else {
  1323.             $this->object_type = '';
  1324.         }
  1325.  
  1326.         $this->table_bd = 'objects';
  1327.  
  1328.         $this->crud->columns('id','regions','street','object_type_id','repair_id','rooms','price_start',
  1329.         'descrip','whom','status','contractor_name','contractor_call_status','contractor_sms_status','source_id');
  1330.  
  1331.         //Установить по желанию заказчика
  1332.         //$this->crud->where('j03c540b8.realtor_status','нет');
  1333.  
  1334.  
  1335.         $this->filter_fields['whom'] = array('strip_tags');
  1336.         $this->filter_fields['descrip'] = array('strip_tags');
  1337.         $this->filter_fields['street'] = array('strip_tags');
  1338.  
  1339.  
  1340.         // Поля формы добвления
  1341.         if ($this->action == 'Снимут' && $this->object_type == 'Квартира' || $this->action == 'Сдают') {
  1342.             $this->fields[] = 'rooms';
  1343.         }
  1344.  
  1345.         $this->fields = array_merge($this->fields, array(
  1346.             'regions',
  1347.             'street',
  1348.             'object_view'
  1349.         ));
  1350.  
  1351.         if ($this->action == 'Сдают') {
  1352.             $this->fields[] = 'area';
  1353.             $this->fields[] = 'floor';
  1354.         }
  1355.  
  1356.         $this->fields = array_merge($this->fields, array(
  1357.             'repair_id',
  1358.             'furniture_id',
  1359.             'price_start',
  1360.             'price_end',
  1361.             'whom',
  1362.             'descrip',
  1363.             'status',
  1364.             'status_date',
  1365.             'source_id'
  1366.         ));
  1367.  
  1368.         /*if ($this->state != 'add') {
  1369.             $this->fields[] = 'adding_date';
  1370.             $this->fields[] = 'edit_date';
  1371.         }*/
  1372.  
  1373.  
  1374.         // Локализация
  1375.         $this->crud
  1376.             ->display_as('whom', lang($this->whom_or_who));
  1377.  
  1378.         if($this->state == 'list' || $this->state == 'success'){
  1379.             $this->display_as['price_start'] = lang('price_start_list');
  1380.             $this->display_as['repair_id'] = lang('repair_list');
  1381.             $this->display_as['contractor_name'] = lang('name_list');
  1382.         }
  1383.  
  1384.         if ($this->action == 'Сдают' && $this->object_type == 'Комната') {
  1385.             $this->crud->display_as('rooms', lang('rooms_in_flat'));
  1386.         } else {
  1387.             $this->crud->display_as('rooms', lang('rooms2'));
  1388.         }
  1389.  
  1390.         if ($this->action == 'Снимут') {
  1391.             $this->crud->display_as('street', lang('location'));
  1392.         } else {
  1393.             $this->crud->display_as('street', lang('street'));
  1394.         }
  1395.  
  1396.         if ($this->action == 'Сдают') {
  1397.             if ($this->object_type == 'Дом') {
  1398.                 $this->crud->display_as('floor', lang('floor'));
  1399.             } else {
  1400.                 $this->crud->display_as('floor', lang('floors'));
  1401.             }
  1402.         }
  1403.  
  1404.  
  1405.  
  1406.  
  1407.         $this->crud
  1408.             ->field_type('action', 'hidden', $this->action)
  1409.             ->field_type('object_type_id', 'hidden', $this->object_type);
  1410.  
  1411.         if ($this->action == 'Снимут' && $this->object_type == 'Квартира') {
  1412.             $this->crud->field_type('rooms', 'multiselect');
  1413.         } else {
  1414.             $this->crud->field_type('rooms', 'dropdown');
  1415.         }
  1416.  
  1417.         if ($this->action == 'Снимут') {
  1418.             $this->crud->field_type('regions', 'multiselect');
  1419.         } else {
  1420.             $this->crud->field_type('regions', 'dropdown');
  1421.         }
  1422.  
  1423.         /***************** rules ************************************/
  1424.         $this->crud
  1425.                 ->set_rules('floor', lang('floor'), 'integer')
  1426.                 ->set_rules('floors', lang('floors'), 'integer')
  1427.                 ->set_rules('area', lang('area'), 'integer')
  1428.                 ->set_rules('price_start', lang('price_start'), 'integer')
  1429.                 //->set_rules('price_start', lang('price_start'), 'integer')
  1430.                 //обязательные поля
  1431.                 ->required_fields('action', 'object_type_id', 'object_view_id', 'rooms', 'region', 'street', 'price_start', 'status', 'source_id');
  1432.  
  1433.  
  1434.  
  1435.         /************** callback column ************************************/
  1436.  
  1437.         $this->crud
  1438.              ->callback_column($this->_unique_field_name('object_type_id'),array($this,'_callback_object_type'))
  1439.              ->callback_column($this->_unique_field_name('repair_id'),array($this,'_callback_repair'))
  1440.              ->callback_column($this->_unique_field_name('region_id'),array($this,'_callback_region'))
  1441.              ->callback_column('descrip',array($this,'_callback_descrip'))
  1442.              ->callback_column('price_start',array($this,'_callback_price'))
  1443.              ->callback_column($this->_unique_field_name('source_id'),array($this,'_callback_source'))
  1444.              ->callback_column('status',array($this,'_callback_status'));
  1445.  
  1446.  
  1447.  
  1448.        /************** callback functions ************************************/
  1449.        $this->crud
  1450.             ->callback_after_insert(array($this,'_callback_after_insert_obj'))
  1451.             ->callback_after_update(array($this,'_callback_after_update_obj'));
  1452.  
  1453.  
  1454.         /************** callback field ************************************/
  1455.  
  1456.  
  1457.         $this->crud
  1458.              ->callback_field('edit_date',array($this,'_field_callback_edit_date'))
  1459.              ->callback_field('status',array($this,'_field_callback_status'))
  1460.              ->callback_field('status_date',array($this,'_field_callback_status_date'))
  1461.              ->callback_field('adding_date',array($this,'_field_callback_adding_date'));
  1462.  
  1463.  
  1464.  
  1465.  
  1466.  
  1467.         /***** unset ******/
  1468.         $this->crud->unset_texteditor('descrip');
  1469.  
  1470.  
  1471.  
  1472.         if (!empty($this->action)) {
  1473.             $this->crud->where('action', $this->action);
  1474.         }
  1475.  
  1476.         if (!empty($this->object_type)) {
  1477.             $this->crud->where('object_type_id', $this->object_type);
  1478.         }
  1479.  
  1480.  
  1481.         parent::index();
  1482.  
  1483.     }
  1484.  
  1485.  
  1486.     /************* callback function AFTER INSERT ***************************/
  1487.      //callback to update other objects data and contractor data after insert
  1488.      function _callback_after_insert_obj($post_array, $primary_key){
  1489.         //$this->firephp->log('callback after insert row - start');
  1490.      /**** insert adding date and edit date ****/
  1491.  
  1492.             $contractor = $this->db->where('id',$post_array['contractor_id'])->get('contractors')->row();
  1493.             if($contractor && $contractor->realtor_status){
  1494.                 $status = $contractor->realtor_status;
  1495.                 $realtor = $this->db->where('contractor_id',$contractor->id)->get('realtors')->row();
  1496.                 if($status != 'нет'){
  1497.                     if($realtor){
  1498.                         $this->db->where('id',$realtor->id)->set('date',date('Y-m-d H:i:s'))->update('realtors');
  1499.                     } else {
  1500.                         $this->db->set(array('date'=>date('Y-m-d H:i:s'),'contractor_id'=>$contractor->id))->insert('realtors');
  1501.                     }
  1502.  
  1503.                 } else {
  1504.                     if($realtor){
  1505.                         $this->db->where('id',$realtor->id)->delete('realtors');
  1506.                     }
  1507.                 }
  1508.             }
  1509.  
  1510.             //$this->db->set('edit_date',date('Y-m-d H:i:s'))->where('id',$primary_key)->update($this->table_bd);
  1511.  
  1512.  
  1513.         $this->session->unset_userdata('contractor');
  1514.         //$this->firephp->log('callback after insert row - end, here we delete session data => '.$this->session->userdata('contractor'));
  1515.         return $post_array;
  1516.      }
  1517.  
  1518.  
  1519.      /************* callback function AFTER UPDATE ***************************/
  1520.      //callback to update other objects data and contractor data after update
  1521.      function _callback_after_update_obj($post_array, $primary_key){
  1522.         //$this->firephp->log('callback after update row - start');
  1523.  
  1524.  
  1525.         $contractor = $this->db->where('id',$post_array['contractor_id'])->get('contractors')->row();
  1526.             if($contractor && $contractor->realtor_status){
  1527.                 $status = $contractor->realtor_status;
  1528.                 $realtor = $this->db->where('contractor_id',$contractor->id)->get('realtors')->row();
  1529.                 if($status != 'нет'){
  1530.                     $this->firephp->log('here update');
  1531.                     if($realtor){
  1532.                         $this->db->where('id',$realtor->id)->set('date',date('Y-m-d H:i:s'))->update('realtors');
  1533.                     } else {
  1534.                         $this->db->set(array('date'=>date('Y-m-d H:i:s'),'contractor_id'=>$contractor->id))->insert('realtors');
  1535.                     }
  1536.  
  1537.                 } else {
  1538.                     if($realtor){
  1539.                         $this->db->where('id',$realtor->id)->delete('realtors');
  1540.                     }
  1541.                 }
  1542.             }
  1543.  
  1544.  
  1545.  
  1546.         /****** update updating time ********/
  1547.        //$this->db->where('id',$primary_key)->set('edit_date',date('Y-m-d H:i:s'))->update($this->table_bd);
  1548.  
  1549.  
  1550.         /******* update changing status ***********/
  1551.  
  1552.         $this->session->unset_userdata('contractor');
  1553.         //$this->firephp->log('callback after update row - end, here we delete session data => '.$this->session->userdata('contractor'));
  1554.         return $post_array;
  1555.  
  1556.      }
  1557.  
  1558.  
  1559.  
  1560.     /***** common callback field function OBJECTS and CLIENTS START **************/
  1561.     function _field_callback_status_date($value = null){
  1562.         $value = ($value) ? $value : date("d/m/Y");
  1563.         //$return = '<script type="text/javascript">var js_date_format = "dd/mm/yyyy"; </script>';
  1564.         $return = '<input type="text" name="status_date" value="'.$value.'" class="datepicker-input" /> ';
  1565.         $return .= '<a class="datepicker-input-clear" tabindex="-1">Очистить</a> (dd/mm/yyyy)';
  1566.         return $return;
  1567.     }
  1568.  
  1569.  
  1570.  
  1571.     public function _field_callback_edit_date($value = null){
  1572.         $date = date("d.m.Y H:i");
  1573.         return "<input name='edit_date' type='text' value='$date' readonly />";
  1574.    }
  1575.  
  1576.  
  1577.  
  1578.     public function _field_callback_adding_date($value = null){
  1579.         if($value && ($value != '0000-00-00 00:00:00')){
  1580.            $date = date("d.m.Y H:i", strtotime($value));
  1581.         } else {
  1582.            $date = date("d.m.Y H:i");
  1583.         }
  1584.         return "<input name='adding_date' type='text' value='$date' readonly />";
  1585.     }
  1586.  
  1587.  
  1588.     function _field_callback_status($value = null){
  1589.           $options = field_enums($this->current_object, 'status');
  1590.  
  1591.           if(!$value){
  1592.             // if it was "objects"
  1593.             if($this->current_object == 'objects'){
  1594.                 $value = "сдается с";
  1595.             // it was "clients"
  1596.             } else {
  1597.                 $value = "снять";
  1598.             }
  1599.           }
  1600.  
  1601.           $option_tag = '';
  1602.           foreach($options as $option)
  1603.           {
  1604.            $attribute  = 'value="'.$option.'"';
  1605.            if ($option == $value)
  1606.            {
  1607.             $attribute .= ' selected="selected"';
  1608.            }
  1609.            $option_tag .= "<option $attribute>$option</option>";
  1610.           }
  1611.           return '<select name="status">'.$option_tag.'</select>';
  1612.     }
  1613.  
  1614.  
  1615.     /***** common callback field function OBJECTS and CLIENTS END **************/
  1616.  
  1617.  
  1618.  
  1619.  
  1620.      /****** common callback column functions OBJECTS and CLIENTS START *********/
  1621.  
  1622.      //for object ans client list view OBJECT_TYPE - ovveride for export!!!!
  1623.      public function _callback_object_type($value , $row){
  1624.  
  1625.         $field = $this->_unique_field_name('object_view_id');
  1626.  
  1627.         if($row->{$field}){
  1628.                 if($value){
  1629.                   $value .= "<br>".$row->{$field};
  1630.                 } else {
  1631.                   $value = $row->{$field};
  1632.                 }
  1633.  
  1634.         }
  1635.  
  1636.         return $value;
  1637.  
  1638.     }
  1639.  
  1640.  
  1641.     //for object ans client list view FURNITURE - ovveride for export!!!!
  1642.     /*public function _callback_repair($value , $row){
  1643.  
  1644.         if($row->furniture_id){
  1645.             $furniture_obj = $this->db->get_where('furnitures',array('id'=>$row->furniture_id))->row();
  1646.             if($furniture_obj->name){
  1647.                 if($value){
  1648.                   $value .= "<br>$furniture_obj->name";
  1649.                 } else {
  1650.                   $value = $furniture_obj->name;
  1651.                 }
  1652.             }
  1653.         }
  1654.         return $value;
  1655.  
  1656.     }*/
  1657.  
  1658.  
  1659.     public function _callback_repair($value , $row){
  1660.  
  1661.         $field = $this->_unique_field_name('furniture_id');
  1662.  
  1663.         if($row->{$field}){
  1664.  
  1665.                 if($value){
  1666.                   $value .= "<br>".$row->{$field};
  1667.                 } else {
  1668.                   $value = $row->{$field};
  1669.                 }
  1670.         }
  1671.         return $value;
  1672.  
  1673.     }
  1674.  
  1675.  
  1676.     //public function _callback_region($value , $row){
  1677.     //
  1678.     //        $field = $this->_unique_field_name('sub_region_id');
  1679.     //        if($row->{$field}){
  1680.     //
  1681.     //                if($value){
  1682.     //                  $value .= "<br>".$row->{$field};
  1683.     //                } else {
  1684.     //                  $value = $row->{$field};
  1685.     //                }
  1686.     //
  1687.     //        }
  1688.     //        return $value;
  1689.     //}
  1690.  
  1691.  
  1692.    //for object ans client list view PRICE - ovveride for export!!!!
  1693.     public function _callback_price($value , $row){
  1694.  
  1695.         if($row->price_end){
  1696.             if($value){
  1697.               $value .= "<br>$row->price_end";
  1698.             } else {
  1699.               $value = $row->price_end;
  1700.             }
  1701.         }
  1702.         return $value;
  1703.  
  1704.     }
  1705.  
  1706.    //for object ans client list view DESCRIP - ovveride for export!!!!
  1707.     public function _callback_descrip($value , $row){
  1708.             if($value){
  1709.  
  1710.               if(strlen($value)>130){
  1711.                 $value = (substr($value,0,130));
  1712.                 $value .= '...';
  1713.               }
  1714.  
  1715.               $value = htmlspecialchars($value, ENT_QUOTES);
  1716.             }
  1717.  
  1718.             return $value;
  1719.  
  1720.     }
  1721.  
  1722.  
  1723.  
  1724.     public function _callback_status($value , $row){
  1725.          if($row->status_date && ($row->status_date != '0000-00-00 00:00:00') ){
  1726.             $date =  date("d.m.Y", strtotime($row->status_date));
  1727.             return "$value <br> $date";
  1728.          }
  1729.          return $value;
  1730.     }
  1731.  
  1732.  
  1733.  
  1734.      public function _callback_source($value , $row){
  1735.         $return = '';
  1736.         if($value){
  1737.             $return .= $value;
  1738.         }
  1739.         if($row->adding_date && ($row->adding_date != '0000-00-00 00:00:00')){
  1740.  
  1741.           if($value){
  1742.             $return .= "<br>";
  1743.           }
  1744.             $return .= date("d.m.Y", strtotime($row->adding_date));
  1745.         }
  1746.          return $return;
  1747.  
  1748.     }
  1749.  
  1750.  
  1751.     /****** common callback column functions OBJECTS and CLIENTS - END *********/
  1752. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement