Displaying 51-60 of 112 results.
Wiki: Yii 1.1: How to work with flash messageswiki
posted by admin on May 22, 2016
Summary
Set your messages in a controller:

Yii::app()->user->setFlash('success', "Data1 saved!");
Yii::app()->user->setFlash('error', "Data2 failed!");
Yii::app()->user->setFlash('notice', "Data3 ignored.");

Display them in your view:

<?php
foreach(Yii::app()->user->getFlashes() as $key => $message) {
echo '<div class="flash-' . $key . '">' . $message . "</div>\n";
}
?>

Setting flash messages
A flash message is used in order to keep a message in session through one or several requests of the same user. By default, it is removed from session after it has been displayed to the user. Flash messages are usually used in combination with HTTP redirections, because in this case there is no view, so messages can only be displayed in the request that follows redirection.

A flash message has a name and a content (AKA key and value). It is an entry of an associative array. The name is a string: often "notice", "success", or "error", but it can be anything. The content is usually a string. You can put HTML tags in your message if you display it raw. You can also set the message value to a number or an array: it will be serialized and kept in session like a string.

Flash messages can be set using the setFlash() Method of CWebUser. For example, if you would like to inform the user that his changes were successfully saved, you could add the following line to your Controller:

<?php
Yii::app()->user->setFlash('success', "Data saved!");
$this->redirect(array('thing/view', 'id' => 1));

In this example we used the key 'success'. If you want to define more than one flash messages, you will have to use different keys.
Displaying flash messages
To check for flash messages we use the hasFlash() Method and to obtain the flash message we use the getFlash() Method. Since Yii v1.1.3, there is also a method getFlashes() to fetch all the messages.

By default, fetching a message deletes it from the session. This means that a message is meant to be displayed only on the first page served to the user. The fetching methods have a boolean parameter that can change this behavior. See the API links in the previous paragraph.

Displaying statically
So showing of the flash message defined above in a view is done by

<?php if(Yii::app()->user->hasFlash('success')):?>
<div class="info">
<?php echo Yii::app()->user->getFlash('success'); ?>
</div>
<?php endif; ?>

These few lines of code will make a flash message with the key "success" visible to the user within a div of class "info". The message will be displayed until this or another page is (re)loaded in the browser.

If you want to always display all the flash messages, then you should add a block to your layout (by default protected/views/layout/main.php). Here is a more elaborate example:

<?php
$flashMessages = Yii::app()->user->getFlashes();
if ($flashMessages) {
echo '<ul class="flashes">';
foreach($flashMessages as $key => $message) {
echo '<li><div class="flash-' . $key . '">' . $message . "</div></li>\n";
}
echo '</ul>';
}
?>

The default CSS created by the Yii script yiic webapp has directives for three classes of flash messages on a div tag: flash-error, flash-notice, flash-success.

The best way to know if some flash messages are set is to check if Yii::app()->user->getFlashes() is empty. Since v1.1.7, Yii keeps an associative array of the flash keys in the form array("key1" => 0, ...), or null if not flash message is set. You can fetch this with Yii::app()->user->getState(CWebUser::FLASH_COUNTERS) but this is not recommended, as Yii could change this internal process.

Displaying dynamically (with Javascript)
If you want the flash message to appear somewhere above the content and then automatically fade out after a few seconds, you will have to add the following lines to your view:

<?php
Yii::app()->clientScript->registerScript(
'myHideEffect',
'$(".info").animate({opacity: 1.0}, 3000).fadeOut("slow");',
CClientScript::POS_READY
);
?>

With these lines of code we register a piece of jQuery (already included with YII) javascript code, using 'myHideEffect' as ID. It will be inserted in the jQuery's ready function (CClientScript::POS_READY). Due to the chainablity of jQuery the little script will run two effects on the .info DIV sequentially:

.animate({opacity: 1.0}, 3000)

Normally this would animate the .info DIV to a full opacity within 3 seconds. But the DIV is already rendered with full opacity upon page load, so calling this effect will just cause a delay for 3 seconds.

.fadeOut("slow")

This is the fadeOut effect which will hide the .info DIV at slow speed.Read more
Resolved: Yii CGridView buttons url target _blank not working
posted by admin on April 25, 2016
I try to target _blank my CGridView buttons

<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'media-grid',
'dataProvider'=>$media->searchadmin(),
'filter'=>$media,
'columns'=>array(
'id',
. . .
array(
'class'=>'CButtonColumn',
'template'=>'{view}{update}',
'buttons'=>array
(
'update' => array(
'url'=>'CController::createUrl("/media/update/id/{$data->primaryKey}")',
'target'=>'_blank',
),
'view' => array(
'url'=>'CController::createUrl("/media/view/id/{$data->primaryKey}")',
'target'=>'_blank',
),
),
),

),
)); ?>
Read more
Resolved: Yii get current controller and action
posted by admin on April 25, 2016
Is their a way to get the name of the current controller and action that the app is executing? I was wondering if Yii:app stores this or not, but I can't find any reference to it. I need this information to use in the main layout view for a widget.Read more
Resolved: Yii CGridView custom column not and not show html data attribute
posted by admin on April 24, 2016

echo CHtml::link(
CHtml::image(Yii::app()->request->baseUrl.'/images/op-create.png', "image", array("width"=>30)),
Yii::app()->request->baseUrl.'/images/op-create.png',
array("class"=>"highslide", 'rel'=>'myrel'));

output :

<a class="highslide" rel="myrel" href="/web/images/op-create.png"><img width="30" src="/web/images/op-create.png" alt="image" /></a>



$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'loc-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array('name'=>'loc_picture',
'type'=>'html',
'header'=>'Picture',
'value'=> 'CHtml::link(CHtml::image(Yii::app()->request->baseUrl.$data->loc_picture, "image", array("width"=>30)), Yii::app()->request->baseUrl.$data->loc_picture, array("class"=>"highslide", \'rel\'=>\'myrel\'))',
'htmlOptions'=>array('width'=>'40','style'=>'text-align:center;'),
),
),
));

output :

<a class="highslide" href="/web/pictures/noimage.jpg"><img width="30" src="/web/pictures/noimage.jpg" alt="image" />

Why output in CGridView, REL="myrel" not shown???Read more
Resolved: Yii CGridView get selected row event on shoose
posted by admin on April 23, 2016
as someone in another topic said, "I did my best to search for answers but didn't find the a solution".

First, I am newbie in Yii.

I have 2 frames, the 1st has a CGridView component that print data from MySql tables. My goal is when I click on a row in CGridView, let I know in the 2nd frame which rows where selected.

I have the CGridView with multiple selection, the frames, and read that selectionChanged is a "a javascript function that will be invoked after the row selection is changed. The function signature is function(id) where 'id' refers to the ID of the grid view. In this function, you may use $.fn.yiiGridView.getSelection(id) to get the key values of the currently selected rows."

My problem is, I don't know how to use selectionChanged function.Read more
Resolved: Yii CGridView change pagination page parameter
posted by admin on April 23, 2016
I have a gridview widget:

$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $model->search(),
'summaryText' => 'صفحه {page} از مجموع {pages} صفحه',
'enableSorting' => false,
'enablePagination' => true,
'ajaxUpdate' => false,
'columns'=>array(
//....
),
));


In the pagination links it includes the model name as prefix to pageVar parameter. is there anyway to set the pageVar property of widget so it changes the links from "[del]index.php?r=item/list&Items_page=2[/del]" to "index.php?r=item/list&page=2" ?Read more
Resolved: Yii Cmenu add class to item
posted by admin on April 23, 2016
I need an output like this html code

<ul class="yw0">
<li><span class="highlighter"></span><a href="#">Sign in</a>
</li>
<li><a href="#">Register</a>
</li>
</ul>


The code would be similar something but what would be the exact code to create an extra <span class="highlighter"></span> after the li tag and before the a tag**

<?php $this->widget('zii.widgets.CMenu',array(
'id'=>'menu',
'items'=>array(
array('label'=>'Signin', 'url'=>array('/site/signin')),
array('label'=>'Register', 'url'=>array('/site/register'))
),
)); ?>
Read more
Resolved: Yii CGridView pagination not working and alert 500 or 404 error
posted by admin on April 23, 2016
I have trouble about cgridview pagination.
I am using admin page(/{module}/admin that) generated by gii.
it has search form and grid with pagination. its work well without urlmanager

but my team have create urlmanager rule long ago.

'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

'<module:\w+>/<controller:\w+>/<action:\w+>/<id:\d+>'=>'<module>/<controller>/<action>',

),
),


when I use admin page with this urlmanager rule. It has some problem.
- if not using grid sort or pagination. I can filter in grid and form.
- but if using sort or pagination. I can not filtering in grid and form anymore.

page link is wierd and i think because of some attribute(params) are null like this

/{module}/{controller}/admin/BookManageMain%5Bbmm_code%5D//BookManageMain%5Bbmm_tid%5D/2/.../BookManageMain_page/2/ajax/book-manage-main-grid

if I am copy this url to another tab and Its can not filtering in grid and form too.
but if I remove attribute that has null value(BookManageMain%5Bbmm_code%5D//) I can filter grid and form.

/{module}/{controller}/admin/BookManageMain%5Bbmm_tid%5D/2/.../BookManageMain_page/2/ajax/book-manage-main-grid

console log (not sure filter use POST and sort/page use GET)

before click pagerlink or sorting
.../{controller}/action?BookManageMain%5Bbmm_code%5D=&BookManageMain%5Bbmm_tid%5D=2&...&BookManageMain_page=1&ajax=book-manage-main-grid

after click

.../{controller}/action/BookManageMain%5Bbmm_code%5D//BookManageMain%5Bbmm_tid%5D/2/.../BookManageMain_page/2/ajax/book-manage-main-grid
Read more
Wiki: Yii on update, detect if a specific AR property has been changed on beforeSave()wiki
posted by admin on April 23, 2016
You need to store the old attributes in a local property in the AR class so that you can compare the current attributes to those old ones at any time.

Step 1. Add a new property to the AR class:

// Stores old attributes on afterFind() so we can compare
// against them before/after save
protected $oldAttributes;


Step 2. Override Yii's afterFind() and store the original attributes immediately after they are retrieved.

public function afterFind(){
$this->oldAttributes = $this->attributes;
return parent::afterFind();
}


Step 3. Compare the old and new attributes in beforeSave/afterSave or anywhere else you like inside the AR class. In the example below we are checking if the property called 'level' is changed.

public function beforeSave()
{
if(isset($this->oldAttributes['level']) && $this->level != $this->oldAttributes['level']){

// The attribute is changed. Do something here...

}

return parent::beforeSave();
}
Read more
Resolved: Yii isNewRecord is false in beforeSave?
posted by admin on April 23, 2016
I made a beforeSave method in my model (extends GXActiveRecord), the if isNewRecord never gets triggered. My beforeSave() gets called, though.

When I print the $this->isNewRecord variable, it's false. When is this variable set to false anyway? I'm very sure it's new

public function beforeSave(){


if(parent::beforeSave())
{
if($this->isNewRecord){
$this->setAttribute('doc_status','new');
print "something";
}else{
$this->setAttribute('doc_status','updated');
}
return true;
} else { return false;

}
Read more