I have created an advanced search form that will search the model using OR or IN condition. However it fails to update CGridView because it returns an array for the attribute of a model. if i remove these columns from the gridview then it will work, but i need these column to be visible on the gridview.
Model
public function weeklystatus($arr = array())
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
//$criteria->compare('id',$this->id);
$criteria->compare('PROJECT',$this->PROJECT,true);
if(!empty($arr) && isset($arr['PROJCODE'])){
$str = '';
foreach($arr['PROJCODE'] as $value) {
$str .= "PROJCODE = '$value' || ";
}
$criteria->addCondition(substr($str, 0, -3));
}else
$criteria->compare('PROJCODE',$this->PROJCODE,true);
$criteria->compare('PROJID',$this->PROJID);
$criteria->mergeWith($this->dateRangeSearchCriteria('StartDATE', $this->StartDATE));
$criteria->mergeWith($this->dateRangeSearchCriteria('ProjectEndDate', $this->ProjectEndDate));
$criteria->mergeWith($this->dateRangeSearchCriteria('ActualEndDate', $this->ActualEndDate));
$criteria->compare('PROCESSOR',$this->PROCESSOR,true);
$criteria->compare('OFFICE',$this->OFFICE,true);
$criteria->compare('DEPTCODE',$this->DEPTCODE,true);
//$criteria->compare('PERCENT',$this->PERCENT,true);
$criteria->compare('PERCENTPlanned',$this->PERCENTPlanned,true);
$criteria->compare('KM',$this->KM,true);
$criteria->compare('KMPlanned',$this->KMPlanned,true);
if(!empty($arr) && isset($arr['MC'])){
$str = '';
foreach($arr['MC'] as $value) {
$str .= "MC = '$value'";
}
$criteria->addCondition($str);
}else{
$criteria->compare('MC',$this->MC,true);
}
$criteria->compare('MCSALE',$this->MCSALE);
$criteria->compare('CATEGORY',$this->CATEGORY,true);
$criteria->compare('AREA',$this->AREA,true);
$criteria->compare('COUNTRY',$this->COUNTRY,true);
$criteria->compare('PROJINFO',$this->PROJINFO,true);
$criteria->compare('REGION',$this->REGION,true);
$criteria->compare('ASAAREA',$this->ASAAREA,true);
$criteria->compare('LORM',$this->LORM,true);
$date = date('Y-m-d',strtotime("-2 week"));
$criteria->addCondition("StartDATE > '2013-01-01'");
$criteria->addCondition("StartDATE < '$date' AND PERCENT <100 || StartDATE > '$date' AND PERCENT =100 ");
/*AND (StartDATE < '$date' AND PERCENT <100)
|| (StartDATE > '$date' AND PERCENT =100) ");*/
//$criteria->addCondition("PERCENT < 100 AND StartDATE < '$date'");
//$criteria->addCondition('StartDATE > '.$date . ' AND PERCENT > -1' );
$criteria->order = 'ASAAREA';
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination'=>array(
'pageSize'=>25,
),
));
}
Controller
public function actionWeeklystatus(){
$model=new ViewWebprojectreport('weeklystatus');
$model->unsetAttributes(); // clear any default values
$globalController = new GlobalController;
if(isset($_GET['ViewWebprojectreport'])){
$model->attributes=$_GET['ViewWebprojectreport'];
}
//print_r($model->attributes);
//exit();
$processorList = $globalController->getProcessorList();
$projCodeList = $globalController->getProjectcodeList();
$asaarea = Yii::app()->db->createCommand("SELECT DISTINCT
( view_webprojectreport.ASAAREA )
FROM
view_webprojectreport
ORDER BY
view_webprojectreport.ASAAREA ASC")->queryAll();
$asaareaList = CHtml::listData($asaarea, 'ASAAREA','ASAAREA');
$officeList = $globalController->getOfficeList();
$deptCode = $this->getDepartmentCode();
$deptCodeList = CHtml::listData($deptCode, 'Code','Code');
$categoryList = $globalController->getCategoryList(array("condition"=>"category_id < 8 || (category_id>8 && category_id < 14) || category_id > 31"));
$countryList = $globalController->getCountryList();
$region =Yii::app()->db->createCommand("SELECT DISTINCT
( view_webprojectreport.REGION )
FROM
view_webprojectreport
ORDER BY
view_webprojectreport.REGION ASC")->queryAll();
$regionList = CHtml::listData($region, 'REGION','REGION');
$mcList = array('MC' => "MC","P"=>"Propietary");
Yii::app()->user->setState('exportModel',$model); //this can be any other storage
$this->render("weekly_status",array(
"model"=>$model,
'processorList'=>$processorList,
'asaareaList'=>$asaareaList,
'projCodeList'=>$projCodeList,
'officeList'=>$officeList,
'deptCodeList'=>$deptCodeList,
'categoryList'=>$categoryList,
'countryList'=>$countryList,
'regionList'=>$regionList,
'mcList'=>$mcList
));
}
View
<?php
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
$('.search-form').toggle();
return false;
});
$('.search-form form').submit(function(){
$.fn.yiiGridView.update('view-weekly-status-grid', {
data: $(this).serialize(),
});
return false;
});
",CClientScript::POS_LOAD);
?>
</head>
<h1>Weekly Status Report</h1>
<div style="font-size: 12pt;">
<?php //echo CHtml::link('<img src="images/pdficon.gif"/>',array("ViewWebprojectreport/exportToPdf","report"=>"weeklystatus")); ?>
<?php echo $this->widget('zii.widgets.jui.CJuiButton', array(
'buttonType'=>'link',
'name'=>'btnDownloadWeeklyStatus',
'caption'=>'Download Weekly Status',
'options'=>array('icons'=>'js:{primary:"ui-icon-document"}'),
"url"=>"index.php?r=ViewWebprojectreport/exportToPdf&report=weeklystatus",
'htmlOptions'=>array('style'=>"font-size: 12px;height: 30px;"),
'themeUrl'=>Yii::app()->baseUrl.'/themes',//url of your themes for JUI
'theme'=>'redmond', //theme name the above url should have a folder named by the theme name specified here eg. /protected/extensions/jui/jquery/css/redmond/
'cssFile'=>'jquery-ui-1.10.3.custom.min.css', // the css file in the theme folder
),true); ?>
</div>
<div id="weekly_status_graph" style="height:800px;display:none;width: 1120px;">
</div>
<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>
<div class="search-form" style="display:none">
<?php $this->renderPartial('_search',array(
'model'=>$model,
'processorList'=>$processorList,
'asaareaList'=>$asaareaList,
'projCodeList'=>$projCodeList,
'officeList'=>$officeList,
'deptCodeList'=>$deptCodeList,
'categoryList'=>$categoryList,
'countryList'=>$countryList,
'regionList'=>$regionList,
'mcList'=>$mcList
)); ?>
</div><!-- search-form -->
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'view-weekly-status-grid',
'dataProvider'=>$model->weeklystatus(),
'filter'=>$model,
'rowCssClassExpression'=>'($data->PERCENT == 100)? "yellow" : ($row%2?"even":"odd")',
//'ajaxUpdate' => TRUE,
'afterAjaxUpdate'=>'function(id, data){
$.ajax({
url: "index.php?r=ViewWebprojectreport/weeklystatusgraph",
dataType: "json",
type: "get",
success: function(data) {
if(data.totalprojects>40){
$("#weekly_status_graph").css("width","1900px");
$("#weekly_status_graph").css("margin-left","-390px");
}else if(data.totalprojects>20){
$("#weekly_status_graph").css("width","1250px");
$("#weekly_status_graph").css("margin-left","-50px");
}else{
$("#weekly_status_graph").css("width","1120px");
$("#weekly_status_graph").css("margin-left","-0px");
}
$("#weekly_status_graph").show();
buildColumnGraph(data);
}
})
}',
'columns'=>array(
//'MC',
array(
"name"=>"MC",
"header"=>"MC/P",
"htmlOptions"=>array(
"width"=>"20px"
)
),
//'OFFICE',
array(
"name"=>"PROJCODE",
"header"=>"Center",
"htmlOptions"=>array(
"width"=>"15px"
)
),
//'ASAAREA',
array(
"name"=>"ASAAREA",
"header"=>"Region",
"htmlOptions"=>array(
"width"=>"80px"
)
),
//'PROJECT',
array(
"name"=>"PROJECT",
"htmlOptions"=>array(
"width"=>"150px"
)
),
//'LORM',
array(
"name"=>"LORM",
"htmlOptions"=>array(
"width"=>"20px"
)
),
'CATEGORY',
//'KM',
array(
"name"=>"KM",
"header"=>"KM/KM<sup>2</sup>"
/*"htmlOptions"=>array(
"width"=>"20px"
)*/
),
//PROC TYPE
//'StartDATE',
array(
"name"=>"StartDATE",
"htmlOptions"=>array(
"width"=>"65px"
)
),
//'ProjectEndDate',
array(
"name"=>"ProjectEndDate",
"htmlOptions"=>array(
"width"=>"65px"
)
),
//current date
/*array(
"header"=>"Current date",
"value"=>$date
),*/
// planned poc
// actiaul poc
//expected
//'ActualEndDate',
array(
"name"=>"ActualEndDate",
"htmlOptions"=>array(
"width"=>"65px"
)
),
array(
"name"=>"PROJINFO",
'type'=>'raw',
"value"=>'( strlen($data->PROJINFO) > 200 ) ? CHtml::tag("span", array("title"=>$data->PROJINFO), CHtml::encode(substr($data->PROJINFO, 0, 200)) . "..") : CHtml::encode($data->PROJINFO)',
// '$data->GetTruncatedPROJINFO()' ,
"htmlOptions"=>array(
//"width"=>"300px"
)
),
array(
'class'=>'CButtonColumn',
'template'=>'{view}',
'htmlOptions' => array('style'=>'width:5px'),
),
),
)); ?>
Say ASAAREA I created a check box so i can search the model using 2 condition on 1 column. it returns the results but fails rendering page because I am displaying ASAAREA on CGridView. How can I resolve this issue?
If I comment out //'filter'=>$model, on the view it works fine. But i do not want to do this.
It will produce stack trace if i leave the filter uncommented
Error 500: <h1>PHP Error [2]</h1>
<p>htmlspecialchars() expects parameter 1 to be string, array given (/opt/yii-1.1.14.f0fee9/framework/web/helpers/CHtml.php:103)</p>
<pre>#0 unknown(0): CWebApplication->handleError()
#1 /opt/yii-1.1.14.f0fee9/framework/web/helpers/CHtml.php(103): htmlspecialchars()
#2 /opt/yii-1.1.14.f0fee9/framework/web/helpers/CHtml.php(2610): encode()
#3 /opt/yii-1.1.14.f0fee9/framework/web/helpers/CHtml.php(158): renderAttributes()
#4 /opt/yii-1.1.14.f0fee9/framework/web/helpers/CHtml.php(2255): tag()
#5 /opt/yii-1.1.14.f0fee9/framework/web/helpers/CHtml.php(1434): activeInputField()
#6 /opt/yii-1.1.14.f0fee9/framework/zii/widgets/grid/CDataColumn.php(103): activeTextField()
#7 /opt/yii-1.1.14.f0fee9/framework/zii/widgets/grid/CGridColumn.php(116): CDataColumn->renderFilterCellContent()
#8 /opt/yii-1.1.14.f0fee9/framework/zii/widgets/grid/CGridView.php(532): CDataColumn->renderFilterCell()
#9 /opt/yii-1.1.14.f0fee9/framework/zii/widgets/grid/CGridView.php(510): CGridView->renderFilter()
#10 /opt/yii-1.1.14.f0fee9/framework/zii/widgets/grid/CGridView.php(480): CGridView->renderTableHeader()
#11 /opt/yii-1.1.14.f0fee9/framework/zii/widgets/CBaseListView.php(167): CGridView->renderItems()
#12 unknown(0): CGridView->renderSection()
#13 /opt/yii-1.1.14.f0fee9/framework/zii/widgets/CBaseListView.php(150): preg_replace_callback()
#14 /opt/yii-1.1.14.f0fee9/framework/zii/widgets/CBaseListView.php(135): CGridView->renderContent()
#15 /opt/yii-1.1.14.f0fee9/framework/web/CBaseController.php(173): CGridView->run()
#16 /opt/paradox/protected/views/viewWebprojectreport/weekly_status.php(314): ViewWebprojectreportController->widget()
#17 /opt/yii-1.1.14.f0fee9/framework/web/CBaseController.php(126): require()
#18 /opt/yii-1.1.14.f0fee9/framework/web/CBaseController.php(95): ViewWebprojectreportController->renderInternal()
#19 /opt/yii-1.1.14.f0fee9/framework/web/CController.php(869): ViewWebprojectreportController->renderFile()
#20 /opt/yii-1.1.14.f0fee9/framework/web/CController.php(782): ViewWebprojectreportController->renderPartial()
#21 /opt/paradox/protected/controllers/ViewWebprojectreportController.php(970): ViewWebprojectreportController->render()
#22 /opt/yii-1.1.14.f0fee9/framework/web/actions/CInlineAction.php(49): ViewWebprojectreportController->actionWeeklystatus()
#23 /opt/yii-1.1.14.f0fee9/framework/web/CController.php(308): CInlineAction->runWithParams()
#24 /opt/yii-1.1.14.f0fee9/framework/web/filters/CFilterChain.php(133): ViewWebprojectreportController->runAction()
#25 /opt/yii-1.1.14.f0fee9/framework/web/CController.php(291): CFilterChain->run()
#26 /opt/yii-1.1.14.f0fee9/framework/web/CController.php(265): ViewWebprojectreportController->runActionWithFilters()
#27 /opt/yii-1.1.14.f0fee9/framework/web/CWebApplication.php(282): ViewWebprojectreportController->run()
#28 /opt/yii-1.1.14.f0fee9/framework/web/CWebApplication.php(141): CWebApplication->runController()
#29 /opt/yii-1.1.14.f0fee9/framework/base/CApplication.php(180): CWebApplication->processRequest()
#30 /opt/paradox/index.php(31): CWebApplication->run()
</pre>
public function actionWeeklystatus(){is my action forview.weeklyStatusis a method in my model which i use to filter the model for certain conditions. This is being used by advanced search and CGridView - shorif2000arr=array()is for a a report i do which i use in another function. the if statements ignore it so it shouldn't affect. anything. - shorif2000