Creating a PDF document from a filtered CGridView - Yii - 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);

Related

Nested Whgridview. Grids disappear on sort or pagination click

I'm a newbie in Yii programming.
I'm using boostrap library on Yii via Yiistrap/Yiiwheels
I've created a relation table view
The related view is a Whgridview itself
The first (master grid) has a TbRelationColum clicking it i display the second grid (detail grid).
When I click on the row to display the sub grid, everything appears ok. When I change the sort order or the page of the sub grid disappear both grid.
I understand we should differentiate the css class of the pager and the sort of sub grid from the main grid. How to do this specifically in Yii-Way?
Is This the problem?
This is the view of the main grid:
$this->widget('yiiwheels.widgets.grid.WhGridView',array(
'id'=>'masterGrid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'template' => "{summary}{items}<div class=\"row-fluid\"><div class=\"pull-right\">{pager}</div></div>",
'type' => array(TbHtml::GRID_TYPE_BORDERED, TbHtml::GRID_TYPE_STRIPED),
'columns'=>array(
array(
'class' => 'yiiwheels.widgets.grid.WhRelationalColumn',
//'name' => 'multiMembers.id',
'type' => 'raw',
'header' => 'Sub Items',
'url' => $this->createUrl('multiGroup/ajaxSubItems'),
'cacheData' => false,
'value' => "CHtml::tag('button',array('class'=>'btn btn-primary'),'Sub Items')",
'htmlOptions'=>array('style'=>'width:90px;'),
'cssClass' => 'showSubItems',
),
'id',
'title',
array(
'class'=>'bootstrap.widgets.TbButtonColumn',
),
),
));
This is the sub-grid:
echo CHtml::tag('h3',array(),'Sub Items Group #"'.$id.'"');
$this->widget('yiiwheels.widgets.grid.WhGridView', array(
'id'=>'subGrid_'.$id,
'type'=>array(TbHtml::GRID_TYPE_BORDERED, TbHtml::GRID_TYPE_STRIPED),
'dataProvider' => $gridDataProvider,
'template' => "{summary}{items}<div class=\"row-fluid\"><div class=\"pull-right\">{pager}</div></div>",
'columns' => $gridColumns,
));
This is the controller:
/**
* Manages all models.
*/
public function actionAdmin()
{
$model=new MultiGroup('search');
$model->unsetAttributes(); // clear any default values
if (isset($_GET['MultiGroup'])) {
$model->attributes=$_GET['MultiGroup'];
}
$this->render('admin',array(
'model'=>$model,
));
}
public function actionAjaxSubItems()
{
$id = Yii::app()->getRequest()->getParam('id');
$model = $this->loadModel($id);
if($model->numSubItems > 0) {
$this->renderPartial('_child', array('id' => $id,
'gridDataProvider' => $this->getGridDataProvider($id),
'gridColumns' => $this->getGridColumns()
), false, true);
} else {
echo 'Non ci sono Sub Items.';
}
}
public function getGridDataProvider($id) {
$sql = 'SELECT * FROM multi_member WHERE groupid = :groupid ORDER BY lastname,firstname';
$cmd = Yii::app()->db->createCommand($sql);
$cmd->bindParam(':groupid', $id, PDO::PARAM_INT);
$result = $cmd->queryAll();
$dataProvider = new CArrayDataProvider(
$result, array(
'sort' => array(
'attributes' => array('id','groupid','firstname','lastname','membersince'),
'defaultOrder' => array('lastname' => CSort::SORT_ASC, 'firstname' => CSort::SORT_ASC),
),
'pagination' => array(
'pageSize' => 2,
),
));
return $dataProvider;
}
public function getGridColumns() {
return array('id', 'lastname', 'firstname', 'membersince');
}
How can I do?
thank you ..
If the extensions you're using all extend CGgridView, then you should be able to use option 'ajaxUpdate' (link to documentation).
Try setting 'ajaxUpdate'=>false in one of the grids (or in both of them) to see whether it helps you.
Sometimes setting ajaxUpdate to false is the only way I get get some grids to behave the way I want them to...

Can CArrayDataProvider be used as a data source with exportablegridbehavior?

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]));

Displaying CSV file content in Yii Framework's CGridView

I am trying to display CSV file content in CGridView, I want to display CSV file header as CGridView "Column" and its content as DataProvider. Please provide any idea to achieve this?
You can use CArrayDataProvider.
http://www.yiiframework.com/doc/api/1.1/CArrayDataProvider
$dataProvider = new CArrayDataProvider(str_getcsv(file_get_contents('file.csv')));
You can do something like this.
$file = fopen('test.csv', 'r');
$data = array();
while (($line = fgetcsv($file)) !== FALSE) {
//$line is an array of the csv elements
$data[] = $line;
}
fclose($file);
$columns = array();
foreach ($data[0] as $key => $value) {
$columns[] = array(
'name' => $key,
'header' => $value,
);
}
$data = array_slice($data, 1);
$dataProvider = new CArrayDataProvider($data, array(
'keyField' => 0,
));
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $dataProvider,
'columns' => $columns
));
If you want to create a generic data provider for CSV, you can create a new class.
class CsvDataProvider extends CArrayDataProvider {
private $_columns = array();
public function __construct($file, $config = array()) {
$handler = fopen($file, 'r');
$data = array();
while (($line = fgetcsv($handler)) !== FALSE) {
$data[] = $line;
}
fclose($handler);
$this->_columns = array();
foreach ($data[0] as $key => $value) {
$this->_columns[] = array(
'name' => $key,
'header' => $value,
);
}
$data = array_slice($data, 1);
parent::__construct($data, array_merge($config, array(
'keyField' => 0,
)));
}
public function getColumns() {
return $this->_columns;
}
}
Then you can do something like this.
$dataProvider = new CsvDataProvider('file.csv');
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $dataProvider,
'columns' => $dataProvider->getColumns(),
));

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

Zend - inputfilter get randomize name

Hello I'm trying ZF2 form with an input file.
I have a form with a file input and I want to insert the randomize name into my db.
How I can return the randomized name?
thanks.
This is the simple form class:
class OrdineForm extends Formhttp://stackoverflow.com/questions/ask
public function __construct($name = null)
{
parent::__construct('ordine');
$this->setAttribute('method', 'post');
$this->addElements();
$this->addInputFilter();
}
public function addElements(){
$this->add(array(
'name' => 'pdf',
'attributes' => array(
'type' => 'text',
'disabled' =>'true',
),
'options' => array(
'label' => 'PDF',
),
));
// FILE INPUT
$file = new File('file');
$file
->setLabel('PDF attach')
->setAttributes(array(
'id' => 'file',
));
$this->add($file);
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Add',
'id' => 'submitbutton',
'class' => 'btn btn-success'
),
));
}
public function addInputFilter()
{
$inputFilter = new InputFilter\InputFilter();
$fileInput= new FileInput('file');
$fileInput->setRequired(false);
$fileInput->getFilterChain()->attachByName(
'filerenameupload',
array(
'target' => './public/tmpuploads/',
'randomize' => true,
"UseUploadname" => true,
)
);
$inputFilter->add($fileInput);
$this->setInputFilter($inputFilter);
}
}
After you have validated the form in your controller you can use $form->getData();
there should be a key 'file' as that is what you named your file element. Within that a key 'tmp_name'.
This will be the randomized name.
Eg:
public function uploadfileAction()
{
//get form and filter
$form = $this->getServiceLocator()->get('SomeModule\Form\UploadFileForm');
$filter = $this->getServiceLocator()->get('SomeModule\Form\UploadFileFilter');
$form->setInputFilter($filter->getInputFilter());
if ($this->getRequest()->isPost()) {
//merge files with post
$post = array_merge_recursive(
$this->getRequest()->getPost()->toArray(),
$this->getRequest()->getFiles()->toArray()
);
//set data in form
$form->setData($post);
//check is valid- form data will now contain new name
if ($form->isValid()) {
var_dump($form->getData());
}
}
}
The resulting array dump may look something like this:
array(13) {
["file"]=>
array(5) {
["name"]=>
string(14) "some-pdf.pdf"
["type"]=>
string(24) "application/pdf"
["tmp_name"]=>
string(46) "/public/tmpuploads/new-randomized-name.pdf"
["error"]=>
int(0)
["size"]=>
int(651264)
}
["submit"]=>
string(6) "Submit"
["csrf"]=>
string(32) "4df771bb2fb14e28992a408583745946"
}
You can then just do:
$formData = $form->getData();
$fileNewLocation = $formData['file']['tmp_name'];
//do what you want with $fileNewLocation