<?php



require_once('include/MVC/View/views/view.list.php');
require_once('include/Sugar_Smarty.php');
function display() {
    
}

class COMM1_communicationsViewList extends ViewList {

    /**
     * @see ViewList::preDisplay()
     */
    public function preDisplay() {
        parent::preDisplay();

    }

    function listViewProcess() {
        $GLOBALS['sugar_config'][$this->lv->lvd->limitName]= 50000;
        // asign variable de list templates
        $this->lv->ss->assign('dataListTemplates',$this->view_object_map['dataListTemplates']);//assign to smarty.
        $this->lv->ss->assign('dataListStuTiendas',$this->view_object_map['dataListStuTiendas']);//assign to smarty.
        $this->lv->ss->assign('strMsjResponse',$this->view_object_map['strMsjResponse']);//assign to smarty.
        $this->lv->ss->assign('request_custom',$_REQUEST);//assign to smarty.
        
        global $current_user;
        global $app_strings;
        $this->processSearchForm();
        //if(!$current_user->is_admin) // remove this condition if you dont want admin user to view the "Closed Lost" Opportunities.
        
        // get las condiciones
        $this->params['custom_where'] .= $this->getCondiSearchF();
        
        if(empty($_REQUEST['search_form_only']) || $_REQUEST['search_form_only'] == false){
            $this->lv->setup($this->seed, 'modules/COMM1_communications/tpl/ListViewListViewGeneric.tpl', $this->where, $this->params);
           
            $delimited="";
            $contatsIdList="";
           
            foreach ($this->lv->data[data] as $key => $currentDataContats){
               $contatsIdList.=$delimited.$currentDataContats[ID];
               if($delimited==""){
                   $delimited=",";
               }  
            }
            
            
            /*$ids=$this->lv->data["pageData"]["idIndex"];
            
            print_r($ids);*/
            
            //die();
            
            

            $this->lv->ss->assign('contatsIdList',$contatsIdList);//assign la lista de todos los id
            $this->lv->ss->assign('labelTotal',$app_strings['LBL_LISTVIEW_OPTION_ENTIRE']."&nbsp;&#x28;".$this->lv->data[pageData][offsets][total]."&#x29;&#x200E");//assign la lista de todos los id
            $savedSearchName = empty($_REQUEST['saved_search_select_name']) ? '' : (' - ' . $_REQUEST['saved_search_select_name']);
            echo get_form_header($GLOBALS['mod_strings']['LBL_LIST_FORM_TITLE'] . $savedSearchName, '', false);
            echo $this->lv->display();
        }
    }
    
    /**
     * 
     * @return string
     * @throws Exception
     */
    private function getCondiSearchF(){
        try{
            $where = "";
            
            if(isset($_REQUEST['first_name_basic']) && $_REQUEST['first_name_basic']!=""){
                $where .= " AND (con.first_name) LIKE '%".$_REQUEST['first_name_basic']."%' ";
            }
            if(isset($_REQUEST['last_name_basic']) && $_REQUEST['last_name_basic']!=""){
                $where .= " AND (con.last_name) LIKE '%".$_REQUEST['last_name_basic']."%' ";
            }
            if(isset($_REQUEST['cedula_nit_basic']) && $_REQUEST['cedula_nit_basic']!=""){
                $where .= " AND cstm.cedula_nit_c LIKE '%".$_REQUEST['cedula_nit_basic']."%' ";
            }
            if(isset($_REQUEST['telefono_casa_basic']) && $_REQUEST['telefono_casa_basic']!=""){
                $where .= " AND con.phone_home LIKE '%".$_REQUEST['telefono_casa_basic']."%' ";
            }
            if(isset($_REQUEST['telefono_celular_basic']) && $_REQUEST['telefono_celular_basic']!=""){
                $where .= " AND con.phone_mobile LIKE '%".$_REQUEST['telefono_celular_basic']."%' ";
            }
            if(isset($_REQUEST['mes_cumples_basic']) && $_REQUEST['mes_cumples_basic']!=""){
                $where .= " AND MONTH(con.birthdate) = '".$_REQUEST['mes_cumples_basic']."' ";
            }
            if(isset($_REQUEST['sexo_basic']) && $_REQUEST['sexo_basic']!=""){
                $where .= " AND cstm.sexo_c = '".$_REQUEST['sexo_basic']."' ";
            }
            if(isset($_REQUEST['ciudad_basic']) && $_REQUEST['ciudad_basic']!=""){
                $where .= " AND LOWER(con.primary_address_city) LIKE '%".strtolower($_REQUEST['ciudad_basic'])."%' ";
            }
            if(isset($_REQUEST['stu_tiendas_id_name_basic']) && $_REQUEST['stu_tiendas_id_name_basic']!=""){
                $where .= " AND tucon.stu_tiendas_contactsstu_tiendas_ida = '".strtolower($_REQUEST['stu_tiendas_id_name_basic'])."' ";
            }
            if(isset($_REQUEST['cliente_vip_c_basic']) && $_REQUEST['cliente_vip_c_basic']!=""){
                $where .= " AND cstm.cliente_vip_c = '".strtolower($_REQUEST['cliente_vip_c_basic'])."' ";
            }
            if(isset($_REQUEST['email_basic']) && $_REQUEST['email_basic']!=""){
                $where .= " AND 
                            (
                                SELECT
                                    COUNT(em.email_address)
                                FROM
                                    email_addresses em
                                        INNER JOIN
                                            email_addr_bean_rel bean ON
                                                em.id = bean.email_address_id
                                WHERE
                                    em.deleted = 0 AND
                                    bean.deleted  = 0 AND
                                    bean.bean_module = 'Leads' AND
                                    bean.primary_address = 1 AND
                                    UPPER(em.email_address) LIKE '%".$_REQUEST['email_basic']."%' AND
                                    bean.bean_id = con.id
                            ) > 0 ";

            }
            if(isset($_REQUEST['start_range_date_entered_basic']) && ($_REQUEST['start_range_date_entered_basic']!="")){
               if(isset($_REQUEST['end_range_date_entered_basic']) && ($_REQUEST['end_range_date_entered_basic']!="")){
                 $fechaInicio=$_REQUEST['start_range_date_entered_basic'];
                 $fechaFin=$_REQUEST['end_range_date_entered_basic'];            
                 $where .="AND con.date_entered >= '{$fechaInicio} 00:00:00' AND con.date_entered <= '{$fechaFin} 23:59:59'";
               }

            }
            if(isset($_REQUEST['range_date_entered_basic'])&&($_REQUEST['range_date_entered_basic']!=""))
            {
                 $fecha=$_REQUEST['range_date_entered_basic'];
                 $where .="AND con.date_entered = '{$fecha}'";


            }
            
            return $where;
        } catch (Exception $ex) {
            throw $ex;
        }
    }
}

?>