Can CArrayDataProvider be used as a data source with exportablegridbehavior? - yii

ExportableGridBehavior - tinyurl.com/expgrid
This works fantastically with usual dataproviders. But when using with CArrayDataProvider it is causing issues, and giving blank data in the CSV. Is it possible to use this extension with a CArrayDataProvider. What should my syntax in the Controller look like?
Model Code: searchProfitCurrency method
$rawData=Yii::app()->db->createCommand($selectStatment)->queryAll();
return new CArrayDataProvider($rawData, array(
'id'=>'id',
'sort'=>array(
'attributes'=>array(
'Currency',
'profitMarginCurrency'
),
),
'pagination'=>array(
'pageSize'=>50,
),
));
Controller Code:
$search_dataProvider = $model->searchProfitCurrency();
$this->exportCSV($search_dataProvider,
array(
$search_dataProvider['Currency'],
$search_dataProvider['profitMarginCurrency'],
));

I got the same problem and actually I don't know which way could the example of code the author posted and you paste here, work.
I modified the class in a simple way and it works for me now.
So
in class ExportableGridBehavior
if ($data instanceof CActiveDataProvider) {
$this->csvRowHeaders($fileHandle, $attributes, $data->model);
$this->csvRowModels($fileHandle, new CDataProviderIterator($data, 150), $attributes);
} else if ($data instanceof CArrayDataProvider) { // #luca
fputcsv($fileHandle, $attributes, $this->csvDelimiter, $this->csvEnclosure);
foreach ($data->rawData as $i=>$r) {
$row = array();
foreach ($attributes as $attr) {
$row[] = $data->rawData[$i][$attr];
}
fputcsv($fileHandle, $row, $this->csvDelimiter, $this->csvEnclosure);
}
} else if (is_array($data) && current($data) instanceof CModel) {
and
in Controller
$dataProvider = new CArrayDataProvider($filteredData, array(
'keyField' => 'idvr',
'id'=>'idvr',
'sort'=>array( 'attributes'=>array('numfor','codfis','driscos','nominativo','codtrib','ravved','impdeb'),
'defaultOrder'=>array('driscos'=>'ASC')
),
'pagination' => array( 'pageSize'=>50, ),
'keyField' => 'codfis',
));
if ($this->isExportRequest())
$this->exportCSV($dataProvider, array_keys($dataProvider->rawData[0]));

Related

Filters in CGridView not filtering

Can You check why filtering is not working in CGridView? When i type for exaple 'Adam' in filter field, nothing happens. I can't find my mistake, everything looks ok but not working. I helped with that article: Yii: CGridView Filter examples
CONTROLLER
<?php
class UzytkownikController extends CController
{
public function actionIndex()
{
$Dane = new Uzytkownik('search');
$Dane -> unsetAttributes(); // clear any default values
if(isset($_GET['Uzytkownik']))
{
$Dane->attributes=$_GET['Uzytkownik'];
}
$this -> render ('index', array(
'Dane' => $Dane,
));
}
}
?>
MODEL
<?php
class Uzytkownik extends CActiveRecord
{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function search()
{
$criteria = new CDbCriteria;
$criteria -> compare('imie', $this -> imie, true);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
)
);
}
}
?>
WIEV
<?php
$this -> widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $Dane -> search(),
'filter' => $Dane,
'columns' => array(
array(
'name' => 'imie',
'type'=>'raw',
),
array(
'name' => 'nazwisko',
'type'=>'raw',
'filter' => false,
),
array(
'name' => 'data',
'filter' => false,
),
),
)
);
?>
For future reference:
In order to make sure the $model->attributes "saves" the attributes the model needed the following addition:
public function rules() {
return array(
array('imie', 'safe', 'on'=>'search')
);
}
And $_GET should have been used instead of $_POST Because the CGridView widget uses GET when posting to the server:
class UzytkownikController extends CController
{
public function actionIndex()
{
$Dane = new Uzytkownik('search');
$Dane -> unsetAttributes(); // clear any default values
if(isset($_GET['Uzytkownik']))
{
$Dane->attributes=$_GET['Uzytkownik'];
}
$this -> render ('index', array(
'Dane' => $Dane,
));
}
}

Creating a PDF document from a filtered CGridView - Yii

I am trying to create a PDF from a filtered CGridView. The value will be passed via dropdown in Advanced search but the problem is that i am unable to filter the search by my pdf function.
Controller
public function actionPrint() {
$mPDF1 = Yii::app()->ePdf->mpdf('ar','A4','14','dejavusanscondensed');
$model=new Country('search');
$model->center_id = 1;// This value will be passed from dropdown
//and i want the report to be made on this
$model->unsetAttributes();
if(isset($_GET['Country']))
$model->attributes=$_GET['Country'];
$html = '';
$html .= $this->renderPartial('candidates', array('model'=>$model, 'enablePagination' => false),true);
$mPDF1->WriteHTML($html, false);
$mPDF1->Output('list.pdf','D');
}
View
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'country-grid',
'dataProvider'=>$model->search($enablePagination),
'summaryText' => '',
// 'enablePagination' => false,
'filter'=>$model,
'columns'=>array(
'name',
array(
'header'=>' Total Registered Candidates',
'value'=>'$data->itemsTotal',
),
),
));
echo CHtml::link(
'Save as PDF',
Yii::app()->createUrl('country/print'),
array('class'=>'btnPrint btn btn-danger','target'=>'_blank'));
Model
public function search($enablePagination = true)
{
$criteria->together= true;
$criteria->with=array('center');
$criteria->compare('center.name', $this->center_id, true);
..........
if ($enablePagination)
{
$pagination = array(
'pageSize' => 30,
);
}
else
{
$pagination = false;
}
return new CActiveDataProvider($model, array(
'criteria' => $criteria,
'pagination' => $pagination,
));
}
Since center_id is a foreign key the line
$criteria->compare('center.name', $this->center_id, true);
should read
$criteria->compare('center_id', $this->center_id);
You could also do the following but this adds a condition on the joined table and could lead to slower queries.
$criteria->compare('center.id', $this->center_id);

Yii CJuiAutoComplete widget: event of empty response message

If the response does not contain data about the city, I want to see the output message. Also i want to change css of text in textfield when I get an empty response.
I have:
View:
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name'=>'city_id',
'value'=>'',
'source'=>CController::createUrl('/PromouterCity/autoComplete'),
'options'=>array(
'showAnim'=>'fold',
'minLength'=>'0',
'select'=>'js:function( event, ui ) {
$("#city_id").val( ui.item.name );
$("#selectedvalue").val( ui.item.id);
return false;
}',
),
'placeholder' => "Search...",
),
));
Controller:
public function actionAutoComplete(){
$match=$_GET['term'];
if(!empty($match)){
$match = addcslashes($match, '%_');
$q = new CDbCriteria( array(
'condition' => "name LIKE :match",
'params' => array(':match' => "$match%")
));
$query = City::model()->findAll($q);
}else{
$query=array(
'0'=>array(
'id'=>'1',
'name'=>'London',
)
);
}
$list = array();
foreach($query as $q){
$data['label']=$q['name'];
$data['id']= $q['id'];
$data['name']= $q['name'];
$list[]= $data;
unset($data);
}
echo CJSON::encode($list);
Yii::app()->end();
}
Decision:
'response'=> 'js:function( event, ui ) {
if (ui.content.length === 0) {
$("#empty-message").text("your message");
} else {
$("#empty-message").empty();
}
}',

Yii - CGridView activerecord relation

I need to print out the result in CActiveDataProvider with CGridView and with pagination
The following is my function in model
public function getCompaniesJobsByCompanyId ( $companyId )
{
$criteria = new CDbCriteria(array(
'with'=>array(
'jobs'=>array(
'scopes' => array( 'offline' => array(0), ),
'vacancies'=>array(
'scopes'=>array(
'removed' => array(0),
'archived' => array(0),
),
),
),
),
'condition' => $this->getTableAlias(false) . '.company_id = ' . (int) $companyId,
)
);
$criteria->together = true;
$dataProvider = new CActiveDataProvider($this, array(
'criteria' => $criteria,
'pagination' => array(
'pageSize' => 20, //Yii::app()->params['pageSize'],
),
));
return $dataProvider;
}
How could be the CGridView to render my data?
By this way I iterate the result
$dataProvider = Employers::model() -> getCompaniesJobsByCompanyId(2);
foreach ( $dataProvider->getData() as $data ) {
echo $data['name'];
foreach ( $data->jobs as $jobs ) {
echo ' ---- ' .($jobs->employer_id) . ' ---- ';
foreach ( $jobs->vacancies as $vacancies ) {
echo '<br />' . ($vacancies->id) . '<br />';
}
}
}
And My view
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider' => $dataProvider,
'columns'=>array(
'title', // display the 'title' attribute
'id', // d
array(
'name'=>'job id',
//'value'=> '$data->jobs[0]["id"]',
//'value'=> 'jobs.id',
//'type' => 'raw'
),
array(
'name'=>'vacancy id',
//'value'=> '$data->jobs[0]->vacancies[0]->id',
//'value'=> 'print_r($data->jobs[0])',
'value'=> '$data->jobs["id"]',
//'type' => 'raw'
),
array(
'name'=>'employer name',
'type'=>'raw', // to encode html string
'value'=>'$data->name',
),
),
));
Any one can help me to print the values in jobs and vacancies relations?
UPDATE
I tried adding 'value' => '$data->jobs->id' but get an error Trying to get property of non-object
Update :
I tried 'value' => '$data->jobs[0]["id"]' and it display the the result correctly, but if there are only 1 record on the table. When there is more than 1 record on the table,
I need to print all result, how to loop on it ?
This line 'value' => '$data->jobs->id' raised an error Trying to get property of non-object because you have been permitted to accessed the property of object instead of array of objects (jobs)
The workaround is you declare a function to do the task on the controller which rendered your gridview
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
...
array(
'name'=>'newColumn',
//call the method 'gridDataColumn' from the controller
'value'=>array($this,'gridDataColumn'),
'type'=>'raw'
)
),
));
class MyController extends Controller
{
//called on rendering the column for each row
protected function gridDataColumn($data,$row)
{
$cellResult = "";
//do your loop here
//example
foreach ( $data->children as $child ) {
$cellResult .=$child->id . "<br/>";
$cellResult .=$child->name . "<br/>";
}
return $cellResult ;
}
...
}
Yii Tutorial
CGridView: Render customized/complex datacolumns

Yii framework CMultifileupload not working

I'm trying to implement a multiple file upload using CMultiFileUpload with CUploadedFile, but it doesn't work. Specifically, _POST is not working even considering that I'm using 'enctype' => 'multipart/form-data' in the options in the view:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'examen-form',
'enableAjaxValidation'=>false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); ?>
and this is the widget and parameters used for CMultiFileUpload:
<div class="row">
<?php echo $form->labelEx($model,'archivo_foto')?>
<?php //echo CHtml::activeFileField($model,'archivo_foto')?>
<?php $this->widget('CMultiFileUpload', array(
'model' => $model,
'name' => 'archivo_foto',
'accept' => 'jpeg|jpg|gif|png|txt', // useful for verifying files
'duplicate' => 'Duplicate file!', // useful, i think
'denied' => 'Invalid file type', // useful, i think
'max' => 10,
'htmlOptions' => array( 'multiple' => 'multiple', 'size' => 25 ),
)); ?>
<?php echo $form->error($model,'archivo_foto')?>
</div>
On the other hand, the controller action is implemented this way:
public function actionUpdateam($id)
{
$model=$this->loadModel($id);
$dir=Yii::getPathOfAlias('application.uploads');
$model->archivo_documento='funciona 0';
if(isset($_POST['Examen'])) {
$model->attributes=$_POST['Examen'];
// THIS is how you capture those uploaded images: remember that in your CMultiFile widget, you set 'name' => 'archivo_foto'
$images = CUploadedFile::getInstancesByName('archivo_foto');
// proceed if the images have been set
$model->archivo_documento='funciona uno';
if (isset($images) && count($images) > 0) {
$model->archivo_documento='funciona dos';
// go through each uploaded image
foreach ($images as $image) {
echo $image->name.'<br />';
$image->saveAs($dir.'/'.$image->name);
$model->archivo_foto = $model->archivo_foto."+".$image->name;
}
// save the rest of your information from the form
if ($model->save()) {
$this->redirect(array('view','id'=>$model->id));
}
}
}
$this->render('update_am',array(
'model'=>$model,
));
}
And at last, I think that is important to mention the rule used for the model (it might be the cause of the problem as well):
array('archivo_foto','file','allowEmpty'=>true,'maxFiles'=>10),
I think that the problem is in post method, because the controller is not uploading the files and is not making any changes in the database. But I'm not sure.
Try changing to 'htmlOptions'=>array('multiple'=>true).
Add following code in the Form
'htmlOptions' => array(
'enctype' => 'multipart/form-data',
),
Like
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'form',
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.
// There is a call to performAjaxValidation() commented in generated controller code.
// See class documentation of CActiveForm for details on this.
'enableAjaxValidation'=>true,
'htmlOptions' => array(
'enctype' => 'multipart/form-data',
),
)); ?>
I am using the same extension and it is wokring for me with these codes
In the view file
<?php $form = $this->beginWidget('CActiveForm', array(
'id'=>'adpost-form',
'enableClientValidation'=>true,
'clientOptions' => array(
'validateOnSubmit'=>true,
'validateOnChange'=>false,
'afterValidate'=>'js:submiAjaxForm'
),
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); ?>
<?php
$this->widget('CMultiFileUpload', array(
'name' => 'photo_name',
'accept' => 'jpeg|jpg|gif|png',
'duplicate' => 'File is not existed!',
'denied' => 'Not images', // useful,
'htmlOptions' => array(
'style' =>'color: transparent;',
),
));
?>
And in the contoller
$images = CUploadedFile::getInstancesByName('photo_name');
foreach ($images as $image => $pic) {
//----------------- Renaming image before uploading
$extension = $pic->getExtensionName();
$newName = "image_".$adModel->id;
$newName .= "_".$imgCount.".".$extension;
$imgCount++;
if ($pic->saveAs($newPath.$newName)) {
// add it to the main model now
$img_add = new AdsPhotosTbl;
$img_add->photo_name = $newName;
$img_add->ad_id = $adModel->id;
$img_add->status = 1;
if(!$img_add->save()){
$error = true;
$upload_error = true;
break;
}
}else{
$upload_error = true;
$error = true;
break;
}
}