Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php defined('BASEPATH') or exit('No direct script access allowed');
- class MY_Controller extends CI_Controller {
- public function __construct()
- {
- parent::__construct();
- if ($this->input->is_ajax_request()) {
- $this->output->enable_profiler(false);
- } else {
- $this->output->enable_profiler(true);
- }
- $this->lang->load('account', 'russian');
- $this->lang->load('admin', 'russian');
- }
- }
- class Admin_Controller extends MY_Controller
- {
- public $data; //Данные вида
- public $current_section; //Текущая страница
- public $breadcrumbs = array(); //Хлебный крошки
- public $catalog_menu; //Меню
- public $filter_fields = array();
- public $js_files = array();
- public $css_files = array();
- static $sdata;
- public $table_bd;
- //дополнительные данные в GC вид
- public $extra_data;
- public $fields = array();
- public $display_as = array();
- public $current_object = null;
- public $current_location = null;
- //USER DATA
- //group id
- public $group_id;
- //email
- public $user_email;
- //access rights
- public $access = null;
- //для поиска
- public $mydata;
- public $post_srch = null;
- //dependent select
- public $dependent_select = null;
- function __construct()
- {
- parent::__construct();
- if ($this->ion_auth->logged_in()==false) {
- redirect('auth');
- }
- $this->group_id = $this->session->userdata('group_id');
- $this->user_email = $this->session->userdata('email');
- $this->access = $this->initAccess();
- //если не ajax то проверяем права
- if(!$this->input->is_ajax_request()){
- switch ($this->ckeck_access()){
- case 'denied': redirect('main/access_denied');
- break;
- case 'restriction': redirect('main/access_restricted');
- break;
- default : '';
- }
- }
- $this->mydata = new stdClass();
- $this->extra_data = new stdClass();
- $this->load->model('mod_admin');
- $this->data = new stdClass();
- $this->wccache->delete_all();
- $this->load->library('grocery_CRUD');
- //$this->load->library('image_CRUD');
- $this->crud = new grocery_CRUD(); //Подключаю CRUD
- $this->load->model('mod_admin', 'mod_admin', true);
- $this->crud->set_theme('twitter-bootstrap');
- $url = $this->uri->segment_array();
- if(in_array('objects',$url)){
- $this->current_object = 'objects';
- }else if(in_array('realtors',$url)){
- $this->current_object = 'realtors';
- }else if(in_array('clients',$url)){
- $this->current_object = 'clients';
- }
- $url_string = $this->uri->uri_string();
- if(startsWith($url_string,'objects')){
- $this->current_location = 'objects';
- }else if(startsWith($url_string,'clients')){
- $this->current_location = 'clients';
- } else if(startsWith($url_string,'realtors')){
- $this->current_location = 'realtors';
- } else if(startsWith($url_string,'exportdata')){
- if(startsWith($url_string,'exportdata/objects')){
- $this->current_location = 'exportdata/objects';
- }else if(startsWith($url_string,'exportdata/realtors')){
- $this->current_location = 'exportdata/realtors';
- }
- } else if(startsWith($url_string,'deleted/objects')){
- $this->current_location = 'deleted/objects';
- } else if(startsWith($url_string,'deleted/realtors')){
- $this->current_location = 'deleted/realtors';
- } else if(startsWith($url_string,'deleted/clients')){
- $this->current_location = 'deleted/clients';
- }
- $this->load->helper('database');
- $this->mydata->custom_data = array();
- $this->mydata->current_location = $this->current_location;
- $this->mydata->object_types = $this->db->get('object_types')->result();
- $this->mydata->regions = $this->db->get('regions')->result();
- $this->mydata->object_views = $this->db->get('object_views')->result();
- $this->mydata->furnitures = $this->db->get('furnitures')->result();
- $this->mydata->repairs = $this->db->get('repairs')->result();
- $this->mydata->sources = $this->db->get('sources')->result();
- $this->mydata->materials = $this->db->get('materials')->result();
- $this->mydata->status = field_enums('objects', 'status');
- $this->mydata->sms_status = field_enums('contractors', 'sms_status');
- $this->mydata->call_status = field_enums('contractors', 'call_status');
- $this->mydata->realtor_status = field_enums('contractors', 'realtor_status');
- }
- function _example_output()
- {
- //если есть дополнительные данные в GC вид
- if(!empty($this->extra_data)){
- $this->crud->set_extra($this->extra_data);
- }
- //если не пустой массив с fields
- if(!empty($this->fields)){
- $this->crud->fields($this->fields);
- }
- //название колонок и полей
- if(!empty($this->display_as)){
- foreach($this->display_as as $field => $title){
- $this->crud->display_as($field,$title);
- }
- }
- $this->crud->set_table($this->table_bd);
- $this->data = $this->crud->render();
- //dependent select
- if($this->dependent_select){
- $this->data->output .= $this->dependent_select;
- }
- //тут выбираем какой поиск показывать соответственно страницам Объекты,Клиенты,Риэлторы
- switch ($this->current_object){
- case 'objects':
- $this->data->search = $this->load->view('object_search',$this->mydata,true);
- break;
- case 'realtors':
- $this->data->search = $this->load->view('realtor_search',$this->mydata,true);
- break;
- case 'clients':
- $this->data->search = $this->load->view('client_search',$this->mydata,true);
- break;
- default : $this->data->search = '';
- }
- $this->data->custom_data = $this->mydata->custom_data;
- $this->data->user_email = $this->user_email;
- $this->data->access = $this->access;
- $this->data->group_id = $this->group_id;
- $this->data->sdata = Admin_Controller::$sdata;
- $this->data->old_action = $this->crud->getState();
- $this->data->breadcrumbs = $this->breadcrumbs;
- $this->data->current_section = $this->current_section;
- $this->data->catalog_menu = $this->catalog_menu;
- $this->data->js_files = $this->data->js_files + $this->js_files;
- $this->data->css_files = $this->data->css_files + $this->css_files;
- $this->load->view('main', $this->data);
- }
- //инициализируем права доступа
- private function initAccess(){
- $access_temp = array();
- //текущий пользователь
- $online_user = $this->db
- ->select('expires_date')
- ->where('id',$this->session->userdata('user_id'))
- ->get('users')
- ->row();
- //если срок действия прав истек
- if(date('Y-m-d H:i:s') > $online_user->expires_date){
- /*$this->db
- ->where('user_id',$this->session->userdata('user_id'))
- ->delete('access_users');*/
- return $access_temp;
- } else {
- // извлекаем список доступов
- $access_temp = $this->db->select('a.name')
- ->where('user_id',$this->session->userdata('user_id'))
- ->join('access a','a.id = a_u.access_id')
- ->get('access_users a_u')
- ->result();
- // если не пустой то создаем список доступов
- if(!empty($access_temp)){
- foreach($access_temp as $key => $val){
- $access_temp[$key]=$val->name;
- }
- }
- }
- return $access_temp;
- }
- /*********************** ПРОВЕРКА ДОСТУПОВ ********************************/
- private function ckeck_access(){
- $uri_segments = explode('/',$this->uri->uri_string());
- if($uri_segments[0] != 'main'){
- if(empty($this->access)){
- return 'denied';
- } else {
- //если нет никаких вообще доступов или не main страница и нет доступа к текущей странице - ошибка
- if((!in_array($uri_segments[0],$this->access))){
- //$this->firephp->log('Пустые доступы или нет доступа к текущей странице');
- return 'restriction';
- }
- }
- }
- return 'ok';
- /*********************** ПРОВЕРКА ДОСТУПОВ *конец* ********************************/
- }
- public function _callback_filter($data, $id = 'null')
- {
- if (isset($data['uri'])) {
- $data['uri'] = $this->mod_admin->get_is_uri($data['name'], $data['uri'], $this->
- table_bd, $id);
- }
- foreach ($data as $k => &$v) {
- if (array_key_exists($k, $this->filter_fields)) {
- $filters = $this->filter_fields[$k];
- foreach ($filters as $key => $val) {
- if (function_exists($val) and $val != 'htmlspecialchars') {
- eval("\$data[\$k] = \$val(\$v);");
- } elseif (function_exists($val) and $val == 'htmlspecialchars') {
- $data[$k] = htmlspecialchars($v, ENT_QUOTES);
- }
- }
- }
- if(!is_array($v))
- {
- $data[$k] = $this->encode_php_tags(trim($v));
- }
- else
- {
- foreach ($v as $ks=>$vs) {
- $data[$k][$ks] = $this->encode_php_tags(trim($vs));
- }
- }
- }
- return $data;
- }
- public function encode_php_tags($str)
- {
- return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('<?php', '<?PHP', '<?', '?>'), $str);
- }
- public function _unique_field_name($field_name) {
- return 's'.substr(md5($field_name),0,8);
- }
- public function _unique_join_name($field_name){
- return 'j'.substr(md5($field_name),0,8);
- }
- }
- class Common_contractor_controller extends Admin_Controller {
- public $state = null;
- public $state_info = null;
- function __construct(){
- parent::__construct();
- if($this->input->get('refresh')){
- $this->session->unset_userdata('srch');
- }
- //реализация поиска - записываем пост данные в сессию.
- //удаление из сесси - реализовать кнопку для отчистки поиска с удалением из сессии.
- if($this->input->post('srch')){
- $this->post_srch = $this->input->post();
- $this->session->set_userdata('srch',$this->input->post());
- } else if($this->session->userdata('srch')){
- $this->post_srch = $this->session->userdata('srch');
- }
- $this->state = $this->crud->getState();
- $this->state_info = $this->crud->getStateInfo();
- //общий поиск по полям контрагента
- if(!empty($this->post_srch)){
- $sms_status = (isset($this->post_srch['sms-status']))?$this->post_srch['sms-status']:'';
- $sms_date_after = (isset($this->post_srch['sms-date-after']))?$this->post_srch['sms-date-after']:'';
- $sms_date_before = (isset($this->post_srch['sms-date-before']))?$this->post_srch['sms-date-before']:'';
- $call_status = (isset($this->post_srch['call-status']))?$this->post_srch['call-status']:'';
- $call_date_after = (isset($this->post_srch['call-date-after']))?$this->post_srch['call-date-after']:'';
- $call_date_before = (isset($this->post_srch['call-date-before']))?$this->post_srch['call-date-before']:'';
- $realtor_status = (isset($this->post_srch['realtor_status']))?$this->post_srch['realtor_status']:'';
- if(!empty($sms_status)){
- $this->crud->where('j03c540b8.sms_status',$sms_status); //j03c540b8 name of table "contractors"
- }
- if(!empty($sms_date_after)){
- $this->crud->where('j03c540b8.sms_date >=',date('Y-m-d H:i:s',strtotime($sms_date_after))); //j03c540b8 name of table "contractors"
- }
- if(!empty($sms_date_before)){
- $this->crud->where('j03c540b8.sms_date >=',date('Y-m-d H:i:s',strtotime($sms_date_before))); //j03c540b8 name of table "contractors"
- }
- if(!empty($call_status)){
- $query = '(';
- $numItems = count($call_status);
- $i = 0;
- foreach($call_status as $c_s) {
- $query .= 'j03c540b8.call_status = '.$this->db->escape($c_s);
- if(++$i !== $numItems) {
- $query .= ' OR ';
- } else {
- $query .= ')';
- }
- }
- $this->crud->where($query,null,false);
- }
- if(!empty($call_date_after)){
- $this->crud->where('j03c540b8.call_date >=',date('Y-m-d H:i:s',strtotime($call_date_after)));
- }
- if(!empty($call_date_before)){
- $this->crud->where('j03c540b8.call_date <=',date('Y-m-d H:i:s',strtotime($call_date_before)));
- }
- if(!empty($realtor_status)){
- $query = '(';
- $numItems = count($realtor_status);
- $i = 0;
- foreach($realtor_status as $r_s) {
- $query .= 'j03c540b8.realtor_status = '.$this->db->escape($r_s);
- if(++$i !== $numItems) {
- $query .= ' OR ';
- } else {
- $query .= ')';
- }
- }
- $this->crud->where($query,null,false);
- }
- }
- //common CONTRACTOR DISPLAY AS
- $this->display_as = array_merge($this->display_as,array(
- 'id' => lang('id'),
- 'contractor_name' => lang('name'),
- 'contractor_phone' => lang('phone'),
- 'contractor_phone2' => lang('phone2'),
- 'contractor_call_status' => lang('status_call'),
- 'contractor_call_date' => lang('date_call'),
- 'contractor_sms_status' => lang('status_sms'),
- 'contractor_sms_date' => lang('date_sms'),
- 'contractor_comments' => lang('comments'),
- 'delimeter' => lang('delimeter'),
- 'whom' => lang('whom'),
- 'who' => lang('who'),
- 'agency' => lang('agency'),
- 'date' => lang('adding_date'),
- //'status' => lang('realtor_status'),
- 'comments' => lang('comments')
- ));
- $this->crud->unset_print();
- $this->crud
- ->callback_field('contractor_name',array($this,'_field_callback_contractor_name'))
- ->callback_field('contractor_phone',array($this,'_field_callback_contractor_phone'))
- ->callback_field('contractor_phone2',array($this,'_field_callback_contractor_phone2'))
- ->callback_field('contractor_call_status',array($this,'_field_callback_contractor_call_status'))
- ->callback_field('contractor_call_date',array($this,'_field_callback_contractor_call_date'))
- ->callback_field('contractor_sms_status',array($this,'_field_callback_contractor_sms_status'))
- ->callback_field('contractor_sms_date',array($this,'_field_callback_contractor_sms_date'))
- ->callback_field('contractor_comments',array($this,'_field_callback_contractor_comments'))
- /*->callback_field('contractor_realtor_status',array($this,'_field_callback_contractor_realtor_status'))*/;
- /************** callback field - start ************************************/
- $this->crud
- ->callback_before_insert(array($this,'_callback_before_insert_obj'))
- ->callback_before_update(array($this,'_callback_before_update_obj'));
- /***** fields type ***********/
- $this->crud->field_type('del','hidden')
- ->field_type('contractor_sms_status', 'readonly')
- ->field_type('contractor_sms_date', 'readonly')
- ->field_type('contractor_call_date', 'readonly')
- ->field_type('delimeter','readonly');
- /************** common callback function ************************************/
- $this->crud
- ->callback_column('contractor_name',array($this,'_callback_contractor_name'))
- ->callback_column('contractor_call_status',array($this,'_callback_contractor_call_status'))
- ->callback_column('contractor_sms_status',array($this,'_callback_contractor_sms_status'));
- if(in_array($this->state,array('edit','update','add','read'))){
- $this->crud->field_type('contractor_id','hidden');
- }
- if($this->state == 'list' || $this->state == 'success' || $this->state == 'ajax_list' || $this->state == 'export' || $this->state == 'ajax_list_info'){
- $this->display_as['price_start'] = lang('price_start_list');
- $this->display_as['contractor_name'] = lang('name_list');
- //$this->load->model('mod_object_contractor'); // TODO: этот кусок
- //$this->crud->set_model('mod_object_contractor'); // нужен?
- //$this->firephp->log('here!');
- }
- /********* This for datetimepicker of GC to work ********/
- if($this->state == 'add' || $this->state == 'edit' || $this->state == 'update'){
- $this->crud->set_js('assets/grocery_crud/js/jquery-1.10.2.min.js');
- $this->crud->set_js('assets/grocery_crud/js/jquery_plugins/config/jquery.datepicker.config.js');
- $this->crud->set_js('assets/grocery_crud/js/jquery_plugins/ui/i18n/datepicker/jquery.ui.datepicker-ru.js');
- $this->crud->set_css('assets/grocery_crud/css/ui/simple/jquery-ui-1.10.1.custom.min.css');
- $this->crud->set_css('assets/grocery_crud/css/jquery_plugins/jquery.ui.datetime.css');
- }
- }
- function index(){
- $this->breadcrumbs[$this->table_bd] = lang($this->table_bd);
- $this->current_section = lang($this->table_bd);
- if($this->state == 'add'){
- if($this->input->post('add_object')){
- $phone = $this->input->post('add_object');
- $len = strlen($phone);
- if(in_array($len,array(6,11))){
- switch($len){
- case 6:
- $obj = $this->db->where('phone',$phone)->or_where('phone2',$phone)->get('contractors')->row();
- break;
- case 11:
- $phone = substr($phone, -10);
- $obj = $this->db->where('phone','8'.$phone)
- ->or_where('phone2','8'.$phone)
- ->or_where('phone','7'.$phone)
- ->or_where('phone2','7'.$phone)
- ->get('contractors')->row();
- break;
- default:
- $obj = '';
- }
- if($obj){
- $this->session->set_userdata('contractor',$obj);
- } else {
- $this->session->set_userdata('phone',$phone);
- }
- }
- } else {
- $this->session->unset_userdata('contractor');
- }
- $this->crud
- ->field_type('call_status','enum',field_enums('contractors', 'call_status'));
- }else if(in_array($this->state,array('edit','update','read'))){
- $primary_key = $this->state_info->primary_key;
- $cur_contractor = $this->db->get_where($this->current_object,array('id'=>$primary_key))->row()->contractor_id;
- ($obj = $this->db->get_where('contractors',array('id'=>$cur_contractor))->row()) == false ? show_404(): '';
- $this->session->set_userdata('contractor',$obj);
- }
- //проверяем какие показывать удаленные или не удаленные
- if(in_array('deleted',$this->uri->segment_array())){
- //$this->crud->unset_add();
- $this->extra_data->deleted = true;
- $del = '0';
- $this->breadcrumbs[$this->table_bd] = lang('remote').' '.$this->breadcrumbs[$this->table_bd];
- $this->current_section = lang('remote').' '.$this->current_section;
- } else {
- $del = '1';
- $this->extra_data->deleted = false;
- }
- $this->crud->where('del',$del);
- if(!in_array('exportdata',$this->uri->segment_array())){
- $this->extra_data->show_checkbox = true;
- $this->crud->unset_export();
- }
- $this->crud
- ->set_rules('contractor_call_status',lang('status_call'),'required')
- ->set_rules('contractor_call_date',lang('date_call'),'required');
- $this->fields = array_merge($this->fields,array(
- 'contractor_id',
- 'delimeter',
- 'contractor_name',
- 'contractor_phone',
- 'contractor_phone2',
- 'contractor_call_status',
- 'contractor_call_date',
- 'contractor_sms_status',
- 'contractor_sms_date',
- //'contractor_realtor_status',
- 'contractor_comments')
- );
- if($this->state == 'list'){
- $this->display_as['contractor_name'] = lang('name_list');
- }
- if(!$this->session->userdata('contractor') && $this->state != 'update' && $this->state != 'update_validation'){
- $this->crud->set_rules('contractor_phone', lang('phone'),'callback__validation_phone|required');
- } else {
- $this->crud->set_rules('contractor_phone', lang('phone'),'required');
- }
- $this->_example_output();
- }
- // хелпер метод для запросов поиска
- function _return_query_where($table, $field, $array){
- $query = '(';
- $numItems = count($array);
- $i = 0;
- foreach($array as $value) {
- $query .= ' '.$table.'.'.$field.' = '.(int)$value;
- if(++$i !== $numItems) {
- $query .= ' OR ';
- } else {
- $query .= ')';
- }
- }
- return $query;
- }
- /*********** callback function BEFORE INSERT ***********/
- //callback to insert other objects data and contractor data
- function _callback_before_insert_obj($post_array, $primary_key){
- $contractor = array();
- $contractor['name'] = strip_tags($post_array['contractor_name']);
- $contractor['phone'] = preg_replace('/[^0-9]/','',$post_array['contractor_phone']);
- $contractor['phone2'] = preg_replace('/[^0-9]/','',$post_array['contractor_phone2']);
- $contractor['call_status'] = $post_array['contractor_call_status'];
- $contractor['comments'] = strip_tags($post_array['contractor_comments']);
- //$contractor['realtor_status'] = $post_array['contractor_realtor_status'];
- if(!empty($post_array['contractor_call_date'])){
- $contractor['call_date'] = date('Y-m-d H:i:s',strtotime($post_array['contractor_call_date']));
- }
- if($obj = $this->session->userdata('contractor')){
- $this->db->where('id',$obj->id)->set($contractor)->update('contractors');
- $post_array['contractor_id'] = $obj->id;
- } else {
- $this->db->set($contractor)->insert('contractors');
- $post_array['contractor_id'] = $this->db->insert_id();
- }
- unset($post_array['contractor_call_date']);
- unset($post_array['contractor_name']);
- unset($post_array['contractor_phone']);
- unset($post_array['contractor_phone2']);
- unset($post_array['contractor_call_status']);
- unset($post_array['contractor_comments']);
- //unset($post_array['contractor_realtor_status']);
- return $post_array;
- }
- /*********** callback function BEFORE UPDATE ***********/
- //callback to update other objects data and contractor data
- function _callback_before_update_obj($post_array, $primary_key){
- $contractor = array();
- $contractor['name'] = strip_tags($post_array['contractor_name']);
- $contractor['phone'] = preg_replace('/[^0-9]/','',$post_array['contractor_phone']);
- $contractor['phone2'] = preg_replace('/[^0-9]/','',$post_array['contractor_phone2']);
- $contractor['call_status'] = $post_array['contractor_call_status'];
- $contractor['comments'] = strip_tags($post_array['contractor_comments']);
- //$contractor['realtor_status'] = $post_array['contractor_realtor_status'];
- if(!empty($post_array['contractor_call_date'])){
- $contractor['call_date'] = date('Y-m-d H:i:s',strtotime($post_array['contractor_call_date']));
- $this->firephp->log($contractor['call_date']);
- }
- //die;
- if($obj = $this->session->userdata('contractor')){
- $this->db->where('id',$obj->id)->set($contractor)->update('contractors');
- }
- unset($post_array['contractor_call_date']);
- unset($post_array['contractor_name']);
- unset($post_array['contractor_phone']);
- unset($post_array['contractor_phone2']);
- unset($post_array['contractor_call_status']);
- unset($post_array['contractor_comments']);
- //unset($post_array['contractor_realtor_status']);
- return $post_array;
- }
- /***** common callback field function CONTRACTORS START **************/
- /*public function _field_callback_contractor_realtor_status($value = null,$primary_key = null){
- if($obj = $this->session->userdata('contractor')){
- $value = $obj->realtor_status;
- }
- if(in_array('read',$this->uri->segment_array())){
- return htmlspecialchars($value, ENT_QUOTES);
- } else {
- $options = field_enums('contractors', 'realtor_status');
- $option_tag = '';
- if(!$value){
- $value = 'нет';
- }
- foreach($options as $option)
- {
- $attribute = 'value="'.$option.'"';
- if ($option == $value)
- {
- $attribute .= ' selected="selected"';
- }
- $option_tag .= "<option $attribute>$option</option>";
- }
- return '<select name="contractor_realtor_status">'.$option_tag.'</select>';
- }
- }*/
- public function _field_callback_contractor_name($value = null,$primary_key = null){
- if($obj = $this->session->userdata('contractor')){
- $value = $obj->name;
- }
- if(in_array('read',$this->uri->segment_array())){
- return htmlspecialchars($value, ENT_QUOTES);
- } else {
- return '<input type="text" maxlength="50" value="'.$value.'" name="contractor_name" style="width:462px">';
- }
- }
- //contractor phone
- public function _field_callback_contractor_phone($value = null){
- if($obj = $this->session->userdata('contractor')){
- $value = $obj->phone;
- }else if($this->session->userdata('phone')){
- $value = $this->session->userdata('phone');
- $this->session->unset_userdata('phone');
- }
- if(in_array('read',$this->uri->segment_array())){
- return htmlspecialchars($value, ENT_QUOTES);
- } else {
- return '<input type="text" maxlength="50" value="'.$value.'" name="contractor_phone" style="width:462px">';
- }
- }
- //contractor phone2
- public function _field_callback_contractor_phone2($value = null){
- if($obj = $this->session->userdata('contractor')){
- $value = $obj->phone2;
- }
- if(in_array('read',$this->uri->segment_array())){
- return htmlspecialchars($value, ENT_QUOTES);
- } else {
- return '<input type="text" maxlength="50" value="'.$value.'" name="contractor_phone2" style="width:462px">';
- }
- }
- //contractor comments
- public function _field_callback_contractor_comments($value = null){
- if($obj = $this->session->userdata('contractor')){
- $value = $obj->comments;
- }
- if(in_array('read',$this->uri->segment_array())){
- return htmlspecialchars($value, ENT_QUOTES);
- } else {
- return '<textarea name="contractor_comments" style="width:462px">'.$value.'</textarea>';
- }
- }
- //contractor call status
- public function _field_callback_contractor_call_status($value = null){
- if($obj = $this->session->userdata('contractor')){
- $value = $obj->call_status;
- }
- if(in_array('read',$this->uri->segment_array())){
- return htmlspecialchars($value, ENT_QUOTES);
- } else {
- $options = field_enums('contractors', 'call_status');
- $option_tag = '';
- if(!$value){
- $value = 'на связи';
- }
- foreach($options as $option)
- {
- $attribute = 'value="'.$option.'"';
- if ($option == $value)
- {
- $attribute .= ' selected="selected"';
- }
- $option_tag .= "<option $attribute>$option</option>";
- }
- return '<select name="contractor_call_status">'.$option_tag.'</select>';
- }
- }
- public function _field_callback_contractor_sms_status($value = null){
- if($obj = $this->session->userdata('contractor')){
- $value = $obj->sms_status;
- }
- return $value;
- }
- public function _field_callback_contractor_sms_date($value = null){
- if($obj = $this->session->userdata('contractor')){
- $value = $obj->sms_date;
- }
- if(!empty($value) && ($value != '0000-00-00 00:00:00' )){
- return date("d.m.Y H:i", strtotime($value));
- } else {
- return '';
- }
- }
- public function _field_callback_contractor_call_date($value = null){
- if($obj = $this->session->userdata('contractor')){
- $value = $obj->call_date;
- }
- if(in_array('read',$this->uri->segment_array())){
- return $value;
- } else {
- $value = (($value != null)&& ($value != '0000-00-00 00:00:00')) ? $value : date("d/m/Y");
- $return = '<input type="text" name="contractor_call_date" value="'.$value.'" class="datepicker-input" /> ';
- $return .= '<a class="datepicker-input-clear" tabindex="-1">Очистить</a> (dd/mm/yyyy)';
- return $return;
- }
- }
- /***** common callback field function CONTRACTORS END **************/
- public function _callback_contractor_name($value , $row){
- $return = '';
- if(isset($row->contractor_name) && $row->contractor_name){
- $return .= $row->contractor_name;
- }
- if(isset($row->contractor_phone) && $row->contractor_phone){
- if($row->contractor_name){
- $return .= "<br>";
- }
- $return .= $row->contractor_phone;
- }
- return $return;
- }
- public function _callback_contractor_call_status($value , $row){
- $return = '';
- if(isset($row->contractor_call_status) && $row->contractor_call_status){
- $return .= $row->contractor_call_status;
- }
- if(isset($row->contractor_call_date) && $row->contractor_call_date && ($row->contractor_call_date !='0000-00-00 00:00:00')){
- $date = date("d.m.Y", strtotime($row->contractor_call_date));
- if($row->contractor_call_status){
- $return .= "<br>$date";
- } else {
- $return = $date;
- }
- }
- return $return;
- }
- public function _callback_contractor_sms_status($value , $row){
- $return = '';
- if(isset($row->contractor_sms_status) && $row->contractor_sms_status){
- $return .= $row->contractor_sms_status;
- }
- if(isset($row->contractor_sms_date) && $row->contractor_sms_date && ($row->contractor_sms_date !='0000-00-00 00:00:00')){
- $date = date("d.m.Y", strtotime($row->contractor_sms_date));
- if($row->contractor_sms_status){
- $return .= "<br>$date";
- } else {
- $return = $date;
- }
- }
- return $return;
- }
- /************ validation function for contractor fields **********/
- public function _validation_phone($value){
- $value = trim($value);
- if(empty($value) || !is_numeric($value) || (!in_array(strlen($value),array(6,11)))){
- $this->form_validation->set_message(__FUNCTION__,lang('phone_error'));
- return FALSE;
- }
- switch(strlen($value)){
- case 6:
- $obj = $this->db->where('phone',$value)->or_where('phone2',$value)->get('contractors')->row();
- break;
- case 11:
- $value = substr($value, -10);
- $obj = $this->db->where('phone','8'.$value)
- ->or_where('phone2','8'.$value)
- ->or_where('phone','7'.$value)
- ->or_where('phone2','7'.$value)
- ->get('contractors')->row();
- break;
- default:
- $obj = '';
- }
- if(!empty($obj)){
- $this->form_validation->set_message(__FUNCTION__,lang('phone_error_exists'));
- return FALSE;
- }
- return TRUE;
- }
- }
- // Общий класс для Риэлторов
- class Common_realtor_controller extends Common_contractor_controller {
- function __construct(){
- parent::__construct();
- $this->table_bd = 'realtors';
- //общий поиск для риэлторов(Обычные,Удаленные,Экспорт)
- if(!empty($this->post_srch)){
- $name = (isset($this->post_srch['name']))?$this->post_srch['name']:'';
- $agency = (isset($this->post_srch['agency']))?$this->post_srch['agency']:'';
- $phone = (isset($this->post_srch['phone']))?$this->post_srch['phone']:'';
- $phone2 = (isset($this->post_srch['phone2']))?$this->post_srch['phone2']:'';
- if(!empty($name)){
- $this->crud->like('j03c540b8.name',$this->db->escape_like_str($name));
- }
- if(!empty($agency)){
- $this->crud->like($this->current_object.'.agency',$this->db->escape_like_str($agency));
- }
- if(!empty($phone)){
- $this->crud->like('j03c540b8.phone',$this->db->escape_like_str($phone));
- }
- if(!empty($phone2)){
- $this->crud->like('j03c540b8.phone2',$this->db->escape_like_str($phone2));
- }
- }
- }
- }
- // Общий класс для Объектов и Клиентов
- class Common_object_and_client_controller extends Common_contractor_controller {
- //одно поле что отличается у Объекта и Клиента
- protected $whom_or_who;
- protected $action;
- protected $object_type;
- function __construct(){
- parent::__construct();
- //общий поиск по полям объекта или клиента
- if(!empty($this->post_srch)){
- $region_id = (isset($this->post_srch['region_id']))?$this->post_srch['region_id']:'';
- if(isset($this->post_srch['sub_region_id'])){
- $sub_region_post = $this->post_srch['sub_region_id'];
- }else{
- $sub_region_post = array();
- }
- $object_type_id = (isset($this->post_srch['object_type_id']))?$this->post_srch['object_type_id']:'';
- $object_view_id = (isset($this->post_srch['object_view_id']))?$this->post_srch['object_view_id']:'';
- $repair_id = (isset($this->post_srch['repair_id']))?$this->post_srch['repair_id']:'';
- $furniture_id = (isset($this->post_srch['furniture_id']))?$this->post_srch['furniture_id']:'';
- $price_start = (isset($this->post_srch['price_start']))?$this->post_srch['price_start']:'';
- $price_end = (isset($this->post_srch['price_end']))?$this->post_srch['price_end']:'';
- $adding_date_after = (isset($this->post_srch['adding-date-after']))?$this->post_srch['adding-date-after']:'';
- $adding_date_before = (isset($this->post_srch['adding-date-before']))?$this->post_srch['adding-date-before']:'';
- $text_field = (isset($this->post_srch['text_field']))?$this->post_srch['text_field']:'';
- $status_date_after_1 = (isset($this->post_srch['status-date-after-1']))?$this->post_srch['status-date-after-1']:'';
- $status_date_after_2 = (isset($this->post_srch['status-date-after-2']))?$this->post_srch['status-date-after-2']:'';
- $status_date_before_1 = (isset($this->post_srch['status-date-before-1']))?$this->post_srch['status-date-before-1']:'';
- $status_date_before_2 = (isset($this->post_srch['status-date-before-2']))?$this->post_srch['status-date-before-2']:'';
- $source_id = (isset($this->post_srch['source_id']))?$this->post_srch['source_id']:'';
- if(!empty($region_id)){
- $query = '(';
- $numItems = count($region_id);
- $i = 0;
- foreach($region_id as $region){
- $sub_regions_obj_from_db = $this->db->select('id')->where('region_id',$region)->get('sub_regions')->result();
- $sub_regions_from_db = array();
- foreach($sub_regions_obj_from_db as $k => $v){
- $sub_regions_from_db[] = $v->id;
- }
- $tmp_arr = array_intersect($sub_regions_from_db,$sub_region_post);
- $total = count($tmp_arr);
- if($total > 0){
- $counter = 0;
- foreach($tmp_arr as $val){
- $query .= '('.$this->current_object.'.region_id = '.(int)$region.' AND '.$this->current_object.'.sub_region_id = '.$val.') ';
- if(++$counter !== $total){
- $query .= ' OR ';
- }
- }
- } else {
- $query .= $this->current_object.'.region_id = '.(int)$region;
- }
- //last?
- if(++$i === $numItems) {
- $query .= ')';
- } else {
- $query .= ') OR (';
- }
- }
- $this->crud->where($query,null,false);
- }
- if(!empty($object_type_id)){
- $query = $this->_return_query_where($this->current_object,'object_type_id',$object_type_id);
- $this->crud->where($query,null,false);
- }
- if(!empty($object_view_id)){
- $query = $this->_return_query_where($this->current_object,'object_view_id',$object_view_id);
- $this->crud->where($query,null,false);
- }
- if(!empty($furniture_id)){
- $query = $this->_return_query_where($this->current_object,'furniture_id',$furniture_id);
- $this->crud->where($query,null,false);
- }
- if(!empty($repair_id)){
- $query = $this->_return_query_where($this->current_object,'repair_id',$repair_id);
- $this->crud->where($query,null,false);
- }
- if(!empty($price_end)){
- $this->crud->where($this->current_object.'.price_end <=',(int)$price_end);
- }
- if(!empty($price_start)){
- $this->crud->where($this->current_object.'.price_start >=',(int)$price_start);
- }
- if(!empty($text_field)){
- $field = $this->post_srch['text_search'];
- $this->crud->like($this->current_object.'.'.$field,$this->db->escape_like_str($text_field));
- }
- if(!empty($status_date_after_1)){
- $this->crud->where('('.$this->current_object.'.status = '.$this->db->escape(lang('status_1')).
- ' AND '.$this->current_object.'.status_date >= '.$this->db->escape(date('Y-m-d H:i:s',strtotime($status_date_after_1))).')',null,false);
- }
- if(!empty($status_date_after_2)){
- $this->crud->where('('.$this->current_object.'.status = '.$this->db->escape(lang('status_2')).
- ' AND '.$this->current_object.'.status_date >= '.$this->db->escape(date('Y-m-d H:i:s',strtotime($status_date_after_2))).')',null,false);
- }
- if(!empty($status_date_before_1)){
- $this->crud->where('('.$this->current_object.'.status = '.$this->db->escape(lang('status_1')).
- ' AND '.$this->current_object.'.status_date <= '.$this->db->escape(date('Y-m-d H:i:s',strtotime($status_date_before_1))).')',null,false);
- }
- if(!empty($status_date_before_2)){
- $this->crud->where('('.$this->current_object.'.status = '.$this->db->escape(lang('status_2')).
- ' AND '.$this->current_object.'.status_date <= '.$this->db->escape(date('Y-m-d H:i:s',strtotime($status_date_before_2))).')',null,false);
- }
- if(!empty($source_id)){
- $this->crud->where($this->current_object.'.source_id',(int)$source_id);
- }
- if(!empty($adding_date_after)){
- $this->crud->where($this->current_object.'.adding_date >=',date('Y-m-d H:i:s',strtotime($adding_date_after)));
- }
- if(!empty($adding_date_before)){
- $this->crud->where($this->current_object.'.adding_date <=',date('Y-m-d H:i:s',strtotime($adding_date_before)));
- }
- }
- //сортировка по Дате редактирования
- $this->crud->order_by('edit_date','desc');
- //common OBJECT AND CLIENTS DISPLAY AS
- $this->display_as = array_merge($this->display_as,array(
- 'regions' => lang('region'),
- 'status_date' => lang('status_date'),
- 'object_type_id' => lang('object_types'),
- 'object_view_id' => lang('object_views'),
- 'furniture_id' => lang('furniture'),
- 'descrip' => lang('description_obj'),
- 'edit_date' => lang('edit_date'),
- 'price_end' => lang('price_end'),
- 'price_start' => lang('price_start'),
- 'source_id' => lang('source'),
- 'adding_date' => lang('adding_date'),
- 'status' => lang('status'),
- 'repair_id' => lang('repair'),
- 'action' => lang('action'),
- 'rooms' => lang('rooms'),
- 'material_id' => lang('material'),
- 'area' => lang('area')
- ));
- $this->crud
- ->set_relation('object_type_id','object_types','name')
- ->set_relation('object_view_id','object_views','name')
- ->set_relation('furniture_id','furnitures','name')
- ->set_relation('repair_id','repairs','name')
- ->set_relation_n_n('regions', 'objects_regions', 'regions', 'object_id', 'region_id', 'name')
- ->set_relation_n_n('rooms', 'objects_rooms', 'rooms', 'object_id', 'room_id', 'name')
- ->set_relation('source_id','sources','name')
- ->set_relation('material_id','materials','name');
- }
- //set columns to order like - region_id,street,object_type_id,repair_id,price_start,descrip,whom,status
- // in parent add - contractor_name, contractor_call_status,contractor_sms_status
- // and source_id in the end
- function index(){
- $actions = array(
- 'sdayut' => lang('sdayut'),
- 'snimut' => lang('snimut'),
- 'sell' => lang('sell'),
- 'buy' => lang('buy')
- );
- $object_types = array(
- 'flats' => lang('flat'),
- 'rooms' => lang('room'),
- 'houses' => lang('house')
- );
- if (isset($actions[$this->action])) {
- $this->action = $actions[$this->action];
- } else {
- $this->action = '';
- }
- if (isset($object_types[$this->object_type])) {
- $this->object_type = $object_types[$this->object_type];
- } else {
- $this->object_type = '';
- }
- $this->table_bd = 'objects';
- $this->crud->columns('id','regions','street','object_type_id','repair_id','rooms','price_start',
- 'descrip','whom','status','contractor_name','contractor_call_status','contractor_sms_status','source_id');
- //Установить по желанию заказчика
- //$this->crud->where('j03c540b8.realtor_status','нет');
- $this->filter_fields['whom'] = array('strip_tags');
- $this->filter_fields['descrip'] = array('strip_tags');
- $this->filter_fields['street'] = array('strip_tags');
- // Поля формы добвления
- if ($this->action == 'Снимут' && $this->object_type == 'Квартира' || $this->action == 'Сдают') {
- $this->fields[] = 'rooms';
- }
- $this->fields = array_merge($this->fields, array(
- 'regions',
- 'street',
- 'object_view'
- ));
- if ($this->action == 'Сдают') {
- $this->fields[] = 'area';
- $this->fields[] = 'floor';
- }
- $this->fields = array_merge($this->fields, array(
- 'repair_id',
- 'furniture_id',
- 'price_start',
- 'price_end',
- 'whom',
- 'descrip',
- 'status',
- 'status_date',
- 'source_id'
- ));
- /*if ($this->state != 'add') {
- $this->fields[] = 'adding_date';
- $this->fields[] = 'edit_date';
- }*/
- // Локализация
- $this->crud
- ->display_as('whom', lang($this->whom_or_who));
- if($this->state == 'list' || $this->state == 'success'){
- $this->display_as['price_start'] = lang('price_start_list');
- $this->display_as['repair_id'] = lang('repair_list');
- $this->display_as['contractor_name'] = lang('name_list');
- }
- if ($this->action == 'Сдают' && $this->object_type == 'Комната') {
- $this->crud->display_as('rooms', lang('rooms_in_flat'));
- } else {
- $this->crud->display_as('rooms', lang('rooms2'));
- }
- if ($this->action == 'Снимут') {
- $this->crud->display_as('street', lang('location'));
- } else {
- $this->crud->display_as('street', lang('street'));
- }
- if ($this->action == 'Сдают') {
- if ($this->object_type == 'Дом') {
- $this->crud->display_as('floor', lang('floor'));
- } else {
- $this->crud->display_as('floor', lang('floors'));
- }
- }
- $this->crud
- ->field_type('action', 'hidden', $this->action)
- ->field_type('object_type_id', 'hidden', $this->object_type);
- if ($this->action == 'Снимут' && $this->object_type == 'Квартира') {
- $this->crud->field_type('rooms', 'multiselect');
- } else {
- $this->crud->field_type('rooms', 'dropdown');
- }
- if ($this->action == 'Снимут') {
- $this->crud->field_type('regions', 'multiselect');
- } else {
- $this->crud->field_type('regions', 'dropdown');
- }
- /***************** rules ************************************/
- $this->crud
- ->set_rules('floor', lang('floor'), 'integer')
- ->set_rules('floors', lang('floors'), 'integer')
- ->set_rules('area', lang('area'), 'integer')
- ->set_rules('price_start', lang('price_start'), 'integer')
- //->set_rules('price_start', lang('price_start'), 'integer')
- //обязательные поля
- ->required_fields('action', 'object_type_id', 'object_view_id', 'rooms', 'region', 'street', 'price_start', 'status', 'source_id');
- /************** callback column ************************************/
- $this->crud
- ->callback_column($this->_unique_field_name('object_type_id'),array($this,'_callback_object_type'))
- ->callback_column($this->_unique_field_name('repair_id'),array($this,'_callback_repair'))
- ->callback_column($this->_unique_field_name('region_id'),array($this,'_callback_region'))
- ->callback_column('descrip',array($this,'_callback_descrip'))
- ->callback_column('price_start',array($this,'_callback_price'))
- ->callback_column($this->_unique_field_name('source_id'),array($this,'_callback_source'))
- ->callback_column('status',array($this,'_callback_status'));
- /************** callback functions ************************************/
- $this->crud
- ->callback_after_insert(array($this,'_callback_after_insert_obj'))
- ->callback_after_update(array($this,'_callback_after_update_obj'));
- /************** callback field ************************************/
- $this->crud
- ->callback_field('edit_date',array($this,'_field_callback_edit_date'))
- ->callback_field('status',array($this,'_field_callback_status'))
- ->callback_field('status_date',array($this,'_field_callback_status_date'))
- ->callback_field('adding_date',array($this,'_field_callback_adding_date'));
- /***** unset ******/
- $this->crud->unset_texteditor('descrip');
- if (!empty($this->action)) {
- $this->crud->where('action', $this->action);
- }
- if (!empty($this->object_type)) {
- $this->crud->where('object_type_id', $this->object_type);
- }
- parent::index();
- }
- /************* callback function AFTER INSERT ***************************/
- //callback to update other objects data and contractor data after insert
- function _callback_after_insert_obj($post_array, $primary_key){
- //$this->firephp->log('callback after insert row - start');
- /**** insert adding date and edit date ****/
- $contractor = $this->db->where('id',$post_array['contractor_id'])->get('contractors')->row();
- if($contractor && $contractor->realtor_status){
- $status = $contractor->realtor_status;
- $realtor = $this->db->where('contractor_id',$contractor->id)->get('realtors')->row();
- if($status != 'нет'){
- if($realtor){
- $this->db->where('id',$realtor->id)->set('date',date('Y-m-d H:i:s'))->update('realtors');
- } else {
- $this->db->set(array('date'=>date('Y-m-d H:i:s'),'contractor_id'=>$contractor->id))->insert('realtors');
- }
- } else {
- if($realtor){
- $this->db->where('id',$realtor->id)->delete('realtors');
- }
- }
- }
- //$this->db->set('edit_date',date('Y-m-d H:i:s'))->where('id',$primary_key)->update($this->table_bd);
- $this->session->unset_userdata('contractor');
- //$this->firephp->log('callback after insert row - end, here we delete session data => '.$this->session->userdata('contractor'));
- return $post_array;
- }
- /************* callback function AFTER UPDATE ***************************/
- //callback to update other objects data and contractor data after update
- function _callback_after_update_obj($post_array, $primary_key){
- //$this->firephp->log('callback after update row - start');
- $contractor = $this->db->where('id',$post_array['contractor_id'])->get('contractors')->row();
- if($contractor && $contractor->realtor_status){
- $status = $contractor->realtor_status;
- $realtor = $this->db->where('contractor_id',$contractor->id)->get('realtors')->row();
- if($status != 'нет'){
- $this->firephp->log('here update');
- if($realtor){
- $this->db->where('id',$realtor->id)->set('date',date('Y-m-d H:i:s'))->update('realtors');
- } else {
- $this->db->set(array('date'=>date('Y-m-d H:i:s'),'contractor_id'=>$contractor->id))->insert('realtors');
- }
- } else {
- if($realtor){
- $this->db->where('id',$realtor->id)->delete('realtors');
- }
- }
- }
- /****** update updating time ********/
- //$this->db->where('id',$primary_key)->set('edit_date',date('Y-m-d H:i:s'))->update($this->table_bd);
- /******* update changing status ***********/
- $this->session->unset_userdata('contractor');
- //$this->firephp->log('callback after update row - end, here we delete session data => '.$this->session->userdata('contractor'));
- return $post_array;
- }
- /***** common callback field function OBJECTS and CLIENTS START **************/
- function _field_callback_status_date($value = null){
- $value = ($value) ? $value : date("d/m/Y");
- //$return = '<script type="text/javascript">var js_date_format = "dd/mm/yyyy"; </script>';
- $return = '<input type="text" name="status_date" value="'.$value.'" class="datepicker-input" /> ';
- $return .= '<a class="datepicker-input-clear" tabindex="-1">Очистить</a> (dd/mm/yyyy)';
- return $return;
- }
- public function _field_callback_edit_date($value = null){
- $date = date("d.m.Y H:i");
- return "<input name='edit_date' type='text' value='$date' readonly />";
- }
- public function _field_callback_adding_date($value = null){
- if($value && ($value != '0000-00-00 00:00:00')){
- $date = date("d.m.Y H:i", strtotime($value));
- } else {
- $date = date("d.m.Y H:i");
- }
- return "<input name='adding_date' type='text' value='$date' readonly />";
- }
- function _field_callback_status($value = null){
- $options = field_enums($this->current_object, 'status');
- if(!$value){
- // if it was "objects"
- if($this->current_object == 'objects'){
- $value = "сдается с";
- // it was "clients"
- } else {
- $value = "снять";
- }
- }
- $option_tag = '';
- foreach($options as $option)
- {
- $attribute = 'value="'.$option.'"';
- if ($option == $value)
- {
- $attribute .= ' selected="selected"';
- }
- $option_tag .= "<option $attribute>$option</option>";
- }
- return '<select name="status">'.$option_tag.'</select>';
- }
- /***** common callback field function OBJECTS and CLIENTS END **************/
- /****** common callback column functions OBJECTS and CLIENTS START *********/
- //for object ans client list view OBJECT_TYPE - ovveride for export!!!!
- public function _callback_object_type($value , $row){
- $field = $this->_unique_field_name('object_view_id');
- if($row->{$field}){
- if($value){
- $value .= "<br>".$row->{$field};
- } else {
- $value = $row->{$field};
- }
- }
- return $value;
- }
- //for object ans client list view FURNITURE - ovveride for export!!!!
- /*public function _callback_repair($value , $row){
- if($row->furniture_id){
- $furniture_obj = $this->db->get_where('furnitures',array('id'=>$row->furniture_id))->row();
- if($furniture_obj->name){
- if($value){
- $value .= "<br>$furniture_obj->name";
- } else {
- $value = $furniture_obj->name;
- }
- }
- }
- return $value;
- }*/
- public function _callback_repair($value , $row){
- $field = $this->_unique_field_name('furniture_id');
- if($row->{$field}){
- if($value){
- $value .= "<br>".$row->{$field};
- } else {
- $value = $row->{$field};
- }
- }
- return $value;
- }
- //public function _callback_region($value , $row){
- //
- // $field = $this->_unique_field_name('sub_region_id');
- // if($row->{$field}){
- //
- // if($value){
- // $value .= "<br>".$row->{$field};
- // } else {
- // $value = $row->{$field};
- // }
- //
- // }
- // return $value;
- //}
- //for object ans client list view PRICE - ovveride for export!!!!
- public function _callback_price($value , $row){
- if($row->price_end){
- if($value){
- $value .= "<br>$row->price_end";
- } else {
- $value = $row->price_end;
- }
- }
- return $value;
- }
- //for object ans client list view DESCRIP - ovveride for export!!!!
- public function _callback_descrip($value , $row){
- if($value){
- if(strlen($value)>130){
- $value = (substr($value,0,130));
- $value .= '...';
- }
- $value = htmlspecialchars($value, ENT_QUOTES);
- }
- return $value;
- }
- public function _callback_status($value , $row){
- if($row->status_date && ($row->status_date != '0000-00-00 00:00:00') ){
- $date = date("d.m.Y", strtotime($row->status_date));
- return "$value <br> $date";
- }
- return $value;
- }
- public function _callback_source($value , $row){
- $return = '';
- if($value){
- $return .= $value;
- }
- if($row->adding_date && ($row->adding_date != '0000-00-00 00:00:00')){
- if($value){
- $return .= "<br>";
- }
- $return .= date("d.m.Y", strtotime($row->adding_date));
- }
- return $return;
- }
- /****** common callback column functions OBJECTS and CLIENTS - END *********/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement