With YII, I'm trying to make a query from a page called from ajax
echo CHtml::ajaxLink(
'Test request',
array('ajax/z1_gerar_excel.php',
'type'=>'POST',
'data' => array('sql' => $sql)
),
array('update'=>'#req_res'
)
);
?>
<div id="req_res">...</div>
and here is the AJAX page:
$sql = $_REQUEST['data']['sql'];
//echo $sql;
$connection=Yii::app()->db;
$command = $connection->createCommand($sql);
$dataReader = $command->query();
$cont_l = 1;
while(($row = $dataReader->read()) !== false)
{
echo "<br>Linha $cont_l: ";
print_r($row);
$cont_l++;
}// WHILE EOF
$connection->active = false;
and I have the following problem, the YII class is not initialized in the page called from ajax, so I get the following result:
Fatal error: Class 'Yii' not found in D:\Programas\Xampp\Instalado2\htdocs\atayo-teste\ajax\z1_gerar_excel.php on line 4
I've only been using YII for one week. How can I resolve this error?
UPDATE:
So i tried change it to a more YII way, i followed the example from http://lostmahbles.com/simple-yii-ajaxlink/ and add this to my view(z1_sql.php):
echo CHtml::ajaxLink(
"Link Text",
Yii::app()->createUrl( 'application/controllers/RelatorioController/ajaxRequest' ),
array( // ajaxOptions
'type' => 'POST',
'beforeSend' => "function( request )
{
// Set up any pre-sending stuff like initializing progress indicators
}",
'success' => "function( data )
{
// handle return data
alert( data );
}",
'data' => array( 'val1' => '1', 'val2' => '2' )
),
array( //htmlOptions
'href' => Yii::app()->createUrl( 'application/controllers/RelatorioController/ajaxRequest' ),
'class' => 'actionAjaxRequest'
)
);
and this to my controller(RelatorioController.php):
public function accessRules()
{
return array(
array('allow',
'actions'=>array('ajaxrequest'),
'users'=>array('#'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
public function actionAjaxRequest()
{
$val1 = $_POST['val1'];
$val2 = $_POST['val2'];
echo "some sort of response";
Yii::app()->end();
}
but i`m getting a 404 ERROR. And again, really noobie into YII, so anything at all helps...
You're not requesting a Yii php function/page.
Change your code to something like this: (not tested)
<?php
echo ajaxLink('text', array('ajax/z1_gerar_excel'), array(
'type' =>'POST',
'data' => array('sql' => $sql),
), array(
'update'=>'#req_res' // this array is for htmlOptions, so I think you want this parameter in the array with the ajax options
));
?>
Make sure you have an ajax controller with a function z1_gerar_excel.
For more information about the parameters of ajaxLink take a look at:
http://www.yiiframework.com/doc/api/1.1/CHtml#ajaxLink-detail
Related
I have simple Yii cgridview code with pagination. Pagination is working fine but in the last page I have faced one issue.
For example, If I have 13 records in DB table, and set pagination for 10 pages per page then for first page It will show "1 - 10 of 13 results" but when I clicked on 2nd page link then It will show "4 - 13 of 13" instead of "11 - 13 of 13".
Here is my code.
1) Controller :
function actiontransactionHistory(){
$creditTransactionObj = new CreditTransaction();
$this->render('history',array(
'creditTransactionObj'=>$creditTransactionObj,
));
}
2) Model :
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id,true);
$criteria->compare('fkasmtGroupId',$this->fkasmtGroupId);
$criteria->compare('fkgroupSurveyUserId',$this->fkgroupSurveyUserId,true);
$criteria->compare('fkasmtId',$this->fkasmtId);
$criteria->compare('transaction_type',$this->transaction_type);
$criteria->compare('credit_type',$this->credit_type,true);
$criteria->compare('credit_qty',$this->credit_qty);
$criteria->compare('transaction_date',$this->transaction_date,true);
$criteria->compare('isDelete',$this->isDelete);
$criteria->compare('status',$this->status);
$criteria->compare('created_at',$this->created_at,true);
$criteria->compare('modified_at',$this->modified_at,true);
$sort = array(
"defaultOrder" => "transaction_date DESC",
);
return new CActiveDataProvider($this, array(
"criteria"=>$criteria,
"sort" => $sort,
));
}
3) View:
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'history-grid',
'dataProvider' => $creditTransactionObj->search(),
'loadingCssClass' => '',
'enableSorting' => true,
'itemsCssClass' => 'my-teams',
'summaryText' => "Displaying {start} - {end} of {count} results.",
"emptyText" => "There is no transaction history available.",
'columns' => array(
array('name' => 'transaction_date', 'header' => 'Date', 'type' => 'raw', 'value' => 'date("d-M-Y",strtotime($data->transaction_date))', 'htmlOptions' => array('class' => '')),
array('name' => 'credit_qty', 'header' => '# of Credits', 'sortable'=>false, 'type' => 'raw', 'value' => '($data->transaction_type == 1) ? - $data->credit_qty : $data->credit_qty', 'htmlOptions' => array('class' => '')),
array('name' => 'credit_type', 'header' => 'Type', 'type' => 'raw', 'value' => '$data->credit_type', 'htmlOptions' => array('class' => '')),
array('name' => 'transaction_type', 'header' => 'Activity', 'type' => 'raw', 'value' => '($data->transaction_type == 0) ? "Purchased" : (($data->transaction_type == 1) ? "Spent" : "Refunded")', 'htmlOptions' => array('class' => '')),
array('name' => 'fkasmtGroupId', 'header' => 'Group Name', 'type' => 'raw', 'value' => array($this,'getGroupName'), 'htmlOptions' => array('width' => '35%')),
)
));
?>
I have also attached both pages screenshot.
Any help will be appreciate. Thanks in advance !
After spending lots of hours finally I found the solution for this issue.
Actually the problem was in fetchData() function which is used in
yii/framework/web/CActiveDataProvider.php framework class file.
In fetchData() method, limit was not calculated properly for the last page pagination. so I have made changes to calculate correct limit.
Old Code:
protected function fetchData()
{
$criteria=clone $this->getCriteria();
if(($pagination=$this->getPagination())!==false)
{
$pagination->setItemCount($this->getTotalItemCount());
$pagination->applyLimit($criteria);
}
$baseCriteria=$this->model->getDbCriteria(false);
if(($sort=$this->getSort())!==false)
{
// set model criteria so that CSort can use its table alias setting
if($baseCriteria!==null)
{
$c=clone $baseCriteria;
$c->mergeWith($criteria);
$this->model->setDbCriteria($c);
}
else
$this->model->setDbCriteria($criteria);
$sort->applyOrder($criteria);
}
$this->model->setDbCriteria($baseCriteria!==null ? clone $baseCriteria : null);
$data=$this->model->findAll($criteria);
$this->model->setDbCriteria($baseCriteria); // restore original criteria
return $data;
}
New Code:
protected function fetchData()
{
$criteria=clone $this->getCriteria();
if(($pagination=$this->getPagination())!==false)
{
$pagination->setItemCount($this->getTotalItemCount());
$pagination->applyLimit($criteria);
// update limit to the correct value for the last page
$limit=$pagination->getLimit();
$offset=$pagination->getOffset();
if ( $offset+$limit > $pagination->getItemCount() )
$criteria->limit = $pagination->getItemCount() - $offset;
}
$baseCriteria=$this->model->getDbCriteria(false);
if(($sort=$this->getSort())!==false)
{
// set model criteria so that CSort can use its table alias setting
if($baseCriteria!==null)
{
$c=clone $baseCriteria;
$c->mergeWith($criteria);
$this->model->setDbCriteria($c);
}
else
$this->model->setDbCriteria($criteria);
$sort->applyOrder($criteria);
}
$this->model->setDbCriteria($baseCriteria!==null ? clone $baseCriteria : null);
$data=$this->model->findAll($criteria);
$this->model->setDbCriteria($baseCriteria); // restore original criteria
return $data;
}
But remember, never update a core class file of framework. so I just extend this method in my Model file and write below code.
Final Code In My Model file without change in framework file:
class CustomActiveDataProvider extends CActiveDataProvider
{
/**
* Fetches the data from the persistent data storage.
* #return array list of data items
*/
protected function fetchData()
{
$criteria=clone $this->getCriteria();
if(($pagination=$this->getPagination())!==false)
{
$pagination->setItemCount($this->getTotalItemCount());
$pagination->applyLimit($criteria);
// update limit to the correct value for the last page
$limit=$pagination->getLimit();
$offset=$pagination->getOffset();
if ( $offset+$limit > $pagination->getItemCount() )
$criteria->limit = $pagination->getItemCount() - $offset;
}
$baseCriteria=$this->model->getDbCriteria(false);
if(($sort=$this->getSort())!==false)
{
// set model criteria so that CSort can use its table alias setting
if($baseCriteria!==null)
{
$c=clone $baseCriteria;
$c->mergeWith($criteria);
$this->model->setDbCriteria($c);
}
else
$this->model->setDbCriteria($criteria);
$sort->applyOrder($criteria);
}
$this->model->setDbCriteria($baseCriteria!==null ? clone $baseCriteria : null);
$data=$this->model->findAll($criteria);
$this->model->setDbCriteria($baseCriteria); // restore original criteria
return $data;
}
}
// Used this custome active data provider as shown in below.
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id,true);
$criteria->compare('isDelete',$this->isDelete);
$criteria->compare('status',$this->status);
$criteria->compare('created_at',$this->created_at,true);
$criteria->compare('modified_at',$this->modified_at,true);
$sort = array(
"defaultOrder" => "transaction_date DESC",
);
return new CustomActiveDataProvider($this, array(
"criteria"=>$criteria,
"sort" => $sort,
"pagination" => array('pageSize' => (isset($_REQUEST['pageSize'])?$_REQUEST['pageSize']:10))
));
}
After this change, Last page pagination works completely fine.
Thanks!
I had a cgridview with an ajaxlink in one of the column. The ajaxlink works fine in the first page only. For other pages, no matter how i click the ajaxlink it will update the result in first page.
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'promotion-facility-grid',
'ajaxUpdate'=>true,
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array( 'name'=>'pf_branch', 'value'=>'$data->pfBranch->branch_name'),
array( 'name'=>'pf_category', 'value'=>'$data->pfCategory->pc_name'),
array( 'name'=>'pf_photo', 'type' => 'raw', 'value'=>'CHtml::image(Yii::app()->baseUrl . "/images/facility/" .$data->pf_photo, "", array("width"=>"150px"))'),
'pf_name',
array(
'name' => 'pf_main_view',
'htmlOptions'=>array('style'=>'text-align: center'),
'value' => 'CHtml::ajaxLink("<span id=\'MV$data->pf_id\'>$data->pf_main_view</span>", array("promotionFacility/Ajaxcontent", "id"=>$data["pf_id"]),array("update" => "#MV$data->pf_id"))',
'type' => 'raw',
),
/*
'pf_price',
'pf_currency',
'pf_link',
'pf_status',
*/
array(
'class'=>'CButtonColumn',
'template'=>'{update}{delete}',
'buttons'=>array
(
'update' => array
(
'label'=>'update',
'url'=>'Yii::app()->createUrl("promotionFacility/updatepanel", array("id"=>$data->pf_id,"view"=>$data->pf_view,"branch"=>$data->pf_branch))',
),
),
),
),
));
controller
public function actionAjaxcontent($id)
{
include("js/databaseconnection.php");
$query = "select * from holiday_promotion_facility where pf_id='$id'";
$result= mysql_query($query) or die (mysql_error());
while ($row=mysql_fetch_array($result))
{
$pf_main_view = $row['pf_main_view'];
}
if ($pf_main_view == "yes")
{
mysql_query("update holiday_promotion_facility set pf_main_view='no' where pf_id='$id'");
echo "no";
}
else
{
mysql_query("update holiday_promotion_facility set pf_main_view='yes' where pf_id='$id'");
echo "yes";
}
}
what do I miss out? is it anything to do with ajaxupdate or afterajaxupdate?
Yes you miss afterAjaxUpdate(). You loading grid, but does'nt update events.
I recommend to do:
array(
'name' => 'pf_main_view',
'htmlOptions'=>array('style'=>'text-align: center'),
'value' => function($data){
return CHtml::link("<span id='MV$data->pf_id'>$data->pf_main_view</span>","",array("onclick"=>"js:ajax_function(".$data->pf_id.")")),
},
'type' => 'raw',
),
In your js you'll have to do:
function ajax_function(id){
$.ajax({
//your request here
//id is your parameter from grid
});
}
In this case you don't need to do afterAjaxUpdate() and bind events by hands.
I'm using Cake 2.3.0. If I submit my form using POST, the selected form fields carry over however if I submit my form using GET, all of the form fields return to their default values.
Is there a way to make the GET submission to work like that of the POST?
Here's my contorller:
class ListingsController extends AppController {
public function results() {
$conditions = array(
'Listing.Beds >=' => $this->request->query['beds'],
'Listing.ListingStatus >=' => $this->request->query['status'],
);
$this->paginate = array(
'conditions' => $conditions,
);
$this->set('listings', $this->paginate());
}
}
Here's what my view looks like.
echo $this->Form->create(null, array(
'controller' => 'listings',
'action' => 'results',
'type' => 'get'
));
echo $this->Form->input('name');
$beds = array('1' => '1+', '2' => '2+', '3' => '3+', '4' => '4+', '5' => '5+');
echo $this->Form->input('beds', array('options' => $beds));
$status = array('Active' => 'Active', 'Pending' => 'Pending', 'ActivePending' => 'Active and Pending');
echo $this->Form->input('status', array('options' => $status));
echo $this->Form->end('Update');
So basically if I change 'type' => 'get' to 'type' => 'post' it works just fine. But I need to be able to do this via GET.
Thanks
I agree that this is annoying (IMO CakePHP should be smart enough to automatically determin 'where' to get its data from, based on the 'type').
You'll have to copy the 'query' of the request to the 'data' of the request;
$this->request->data = $this->request->query;
Not behind my computer to test it (as usual, lol), but should probably work.
Try adding this to your controller:
$this->request->data = $this->params['url'];
My solution:
$this->params->data = array('Tablefilter' => $this->params->query);
where "Tablefilter" depends on form definition (usually the model name)
$this->Form->create('Tablefilter', array('type' => 'get'))
Use PRG. Checkout this plugin.
What I ended up doing was looping through the $this->request->query array and pass those values to the matching $this->request->data.
So I added this:
foreach($this->request->query as $k => $v){
$this->request->data['Listing'][$k] = $this->request->query[$k];
}
Which ultimately gave me this:
class ListingsController extends AppController {
public function results() {
foreach($this->request->query as $k => $v){
$this->request->data['Listing'][$k] = $this->request->query[$k];
}
$conditions = array(
'Listing.Beds >=' => $this->request->query['beds'],
'Listing.ListingStatus >=' => $this->request->query['status'],
);
$this->paginate = array(
'conditions' => $conditions,
);
$this->set('listings', $this->paginate());
}
}
I'm not going to accept this as the answer though as I don't know if this is the optimal or suggested way to do it. But it works for me for now.
This works for me :
$this->request->data['Cluster'] = $this->params->query ; //controller side
Form definition:
$this->Form->create('Cluster',array('type'=>'get'));
I have a working webService using NuSOAP. Now, I have to make a validation before returning the data requested. If everything is ok, I return it normally, otherwise I would like to return a String message explaining why I'm not giving the information requested. Problem is that I can't get to add two different types of return to RegisterFunction of NuSOAP. If I add a ComplexType as return, I can't return a String.
The function can't have two return-values. You should add the error-message-string to your complex type. If you don't wanna touch your complex type, then you should create another
complex type wich contains your datatype and a string.
Example - the complex type you have right now:
$server->wsdl->addComplexType('myData','complexType','struct','all','',
array( 'important' => array('name' => 'important','type' => 'xsd:string'),
'stuff' => array('name' => 'stuff','type' => 'xsd:string')
)
);
the extra complex type:
$server->wsdl->addComplexType('package','complexType','struct','all','',
array( 'data' => array('name' => 'data','type' => 'tns:myData'),
'errormsg' => array('name' => 'errormsg','type' => 'xsd:string')
)
);
registration of the function:
$server->register(
'getData',
array('validation'=>'xsd:string'),
array('return'=>'tns:package'),
$namespace,
false,
'rpc',
'encoded',
'description'
);
the function:
function GetData($validation)
{
if($validation == "thegoodguy") {
$result['data'] = array(
"important" => "a top secret information",
"stuff" => "another one"
);
$result['errormsg'] = null;
} else {
$result['data'] = null;
$result['errormsg'] = "permission denied!";
}
return $result;
}
That way the client could try to analyse the received data and if it is null then he
shows up the errormessage.
You first need to define a new type that describes an array of strings like so:
$server->wsdl->addComplexType(
'ArrayOfString',
'complexType',
'array',
'sequence',
'',
array(
'itemName' => array(
'name' => 'itemName',
'type' => 'xsd:string',
'minOccurs' => '0',
'maxOccurs' => 'unbounded'
)
)
);
Then you can use tns:ArrayOfString as the return type.
What I want to do is: I want to render a form on a page with a view. This view has a list of 'notes' (=CT Note). When you fill in the form, the note is stored, the form is cleared, and the new note is added to the list. All without page refreshes.
I create a module new_note, and addes this function:
function new_note_form($nodeid = NULL) {
ctools_include('ajax');
// drupal_add_js(drupal_get_path('module', 'custom_forms') . '/js/note_form.js');
//dpm($nodeid);
module_load_include('inc', 'node', 'node.pages');
$form = node_add('note');
$form['field_note_reference']['und']['#value'] = '2';
$form['field_note_reference']['und']['#validated'] = 'TRUE';
$form['field_note_reference']['#attributes']['class'][] = "hidden";
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
'#executes_submit_callback' => FALSE,
'#ajax' => array(
'callback' => 'ajax_note',
'wrapper' => 'status',
),
);
$output= drupal_render($form);
dpm($form);
print $output;
}
function ajax_note(&$form, &$form_state) {
return 'test';
}
I use this function in a display suite block field, which is rendered above the note list. So far so good.
The only problem is, that when I submit the form, the ajax is not called, and the normal submit is done.
Can anyone help me out
# Edit.
After what clive suggested I changed the code, and got the ajax working.
function new_notes_form($nodeid = NULL) {
global $user;
$node = (object) array(
'uid' => $user->uid,
'name' => (isset($user->name) ? $user->name : ''),
'type' => 'note',
'language' => LANGUAGE_NONE,
);
$form_state = array();
$form_state['build_info']['args'] = array($node);
form_load_include($form_state, 'inc', 'node', 'node.pages');
$form = drupal_build_form('note_node_form', $form_state);
$form['field_note_reference']['und']['#value'] = '2';
$form['field_note_reference']['#attributes']['class'][] = "hidden";
$form['submit'] = array(
'#type' => 'button',
'#value' => 'Submit',
'#limit_validation_errors' => array(),
'#ajax' => array(
'callback' => 'ajax_note_replace',
'wrapper' => 'status',
),
);
return $form;
}
function ajax_note_replace(&$form, &$form_state) {
dpm("test");
dpm($form);
$output = '<h1>' . t('Hello World') . '</h1>';
// $node = node_load('6');
// $output .= drupal_render(node_view($node, $style = 'teaser', $options = array()));
ctools_include('ajax');
$commands = array();
$commands[] = ajax_command_prepend(".view-content", $output);
print ajax_render($commands); // this function exits.
exit;
}
#Clive, can you help me out with the rest ? I want to save the node on callback (if valid?). In the ajax callback my node-id is null because it is not stored yet? how can I validate and save the node, otherwise set the not valid form items to red as normal.
It's likely to be because you're sidestepping Drupal's proper methods for creating forms so the AJAX preprocessing won't be performed. If you have a look at drupal_get_form() (the function used pretty much exclusively to prepare a form in Drupal) you'll see it in turn calls drupal_build_form() which is what you need to do:
function new_note_form($form, &$form_state, $nodeid = NULL) {
$form_state['build_info']['args'] = array('note');
$form = drupal_build_form('node_add', $form_state);
$form['submit'] = array(
'#type' => 'button',
'#value' => 'Submit',
'#limit_validation_errors' => array(),
'#ajax' => array(
'callback' => 'advanced_form_callback',
'wrapper' => 'status',
),
);
return $form;
}
echo render(drupal_get_form('new_note_form'));
I've changed the element from type submit to button because I find it works much better when you want to do some ajax processing, removed #executes_submit_callback, and added #limit_validation_errors which will stop the form from otherwise validating (I think that's what you were trying to do by setting #validated to TRUE but I might be wrong).
Hope that helps, it's untested but should give you a good place to start.