In yii framework demos, there is an blog demo. In this blog demo a Post controller has two different actions: index and view.
/**
* Lists all models.
*/
public function actionIndex()
{
$criteria=new CDbCriteria(array(
'condition'=>'status='.Post::STATUS_PUBLISHED,
'order'=>'update_time DESC',
'with'=>'commentCount',
));
if(isset($_GET['tag']))
$criteria->addSearchCondition('tags',$_GET['tag']);
$dataProvider=new CActiveDataProvider('Post', array(
'pagination'=>array(
'pageSize'=>Yii::app()->params['postsPerPage'],
),
'criteria'=>$criteria,
));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
/**
* Displays a particular model.
*/
public function actionView()
{
$post=$this->loadModel();
$comment=$this->newComment($post);
$this->render('view',array(
'model'=>$post,
'comment'=>$comment,
));
}
and index view is:
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
'template'=>"{items}\n{pager}",
)); ?>
and view view is:
<?php $this->renderPartial('_view', array(
'data'=>$model,
)); ?>
but both index and view use _view:
<div class="author">
posted by <?php echo $data->author->username . ' on ' . date('F j, Y',$data->create_time); ?>
</div>
<div class="content">
<?php
$this->beginWidget('CMarkdown', array('purifyOutput'=>true));
echo $data->content;
$this->endWidget();
?>
</div>
here is my question: I can understand the view assign the 'data' => $model, so in _view, $data is valid. In index action, the widget clistview is applied, but i cannot understand where is $data variable being set? I know the $data presents the current post(from dataprovider). I just cannot figure out how and where did yii did this?
Thanks for any help.
The above code first creates a data provider for the Post ActiveRecord class. It then uses CListView to display every data item as returned by the data provider. The display is done via the partial view named '_post'. This partial view will be rendered once for every data item. In the view, one can access the current data item via variable $data.
By using the itemView property of CListView which is used for rendering each data item. This property value will be passed as the first parameter to CController property renderpartial to render each data item.
public string renderPartial(string $view, array $data=NULL, boolean $return=false, boolean $processOutput=false)
public function renderPartial($view,$data=null,$return=false,$processOutput=false)
{
if(($viewFile=$this->getViewFile($view))!==false)
{
$output=$this->renderFile($viewFile,$data,true);
if($processOutput)
$output=$this->processOutput($output);
if($return)
return $output;
else
echo $output;
}
else
throw new CException(Yii::t('yii','{controller} cannot find the requested view "{view}".',
array('{controller}'=>get_class($this), '{view}'=>$view)));
}
Renders a view.
If $data is an associative array, it will be extracted as PHP variables and made available to the script The named view refers to a PHP script .the Script Which is resolved via getViewFile Used in the renderPartial method the script for getViewFile as shown below
public function getViewFile($viewName)
{
if(($theme=Yii::app()->getTheme())!==null && ($viewFile=$theme->getViewFile($this,$viewName))!==false)
return $viewFile;
$moduleViewPath=$basePath=Yii::app()->getViewPath();
if(($module=$this->getModule())!==null)
$moduleViewPath=$module->getViewPath();
return $this->resolveViewFile($viewName,$this->getViewPath(),$basePath,$moduleViewPath);
}
Looks for the view file according to the given view name.
renderItems is the abstract method defined in CBaseListView ClassFile
/**
* Renders the data items for the view.
* Each item is corresponding to a single data model instance.
* Child classes should override this method to provide the actual item rendering logic.
*/
abstract public function renderItems();
And This Method is Overriden by ClistView Class
CListView widget loops thru $dataProvider, and for each item it does something like that:
$this->renderPartial($itemView, array(
'data'=>$model,
));
Where $itemView is view file set in CListView config.
And that's it.
Edit: To clarify of how CListView iterates over dataprovider items: it is defined in CListView::renderItems, in short, the most important parts are:
// Get dataprovider data as array
$data=$this->dataProvider->getData();
...
// Get viewfile
$viewFile=$owner->getViewFile($this->itemView);
...
// Loop thru $data items
foreach($data as $i=>$item)
{
...
// Here data is assigned from dataprovider item
$data['data']=$item;
...
// Render view file
$owner->renderFile($viewFile,$data);
}
Related
In CListView there is a property called "itemsCssClass" which basically adds HTML class attribute to the Itemwrapper.
What if I like to add an ID or any other htmlOptions how will I do it on that wrapper..?
My code is :
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$model->search(),
'viewData'=>array('x'=>''),
'itemView'=>'_classifieds',
'id'=>'boa_ads',
'itemsCssClass'=>'test'
));
This code will produce this HTML:
<div id="boa_ads" class="list-view">
<div class="summary">Displaying 1-4 of 4 results.</div>
<div class="items"><!-- HERE ID LIKE TO ADD AN ID-->
----ITEMS GOES HERE ----
</div>
</div>
</div>
Thanks for your help in advance
From the Yii's source:
public function renderItems()
{
echo CHtml::openTag($this->itemsTagName,array('class'=>$this->itemsCssClass))."\n";
.....
echo CHtml::closeTag($this->itemsTagName);
}
I just see class attribute is passed to the itemsTagName so you probably have to extend CListView to do it.
You can create a CCustomListView class (inside application/widgets folder) which extends from CListView and overwrite renderItems() function. For example:
<?php
Yii::import("zii.widgets.CListView");
class CCustomListView extends CListView
{
public $itemsHtmlOptions;
/**
* Renders the data item list.
*/
public function renderItems()
{
echo CHtml::openTag($this->itemsTagName, array_merge(array('class'=>$this->itemsCssClass), $this->itemsHtmlOptions))."\n";
$data=$this->dataProvider->getData();
if(($n=count($data))>0)
{
$owner=$this->getOwner();
$viewFile=$owner->getViewFile($this->itemView);
$j=0;
foreach($data as $i=>$item)
{
$data=$this->viewData;
$data['index']=$i;
$data['data']=$item;
$data['widget']=$this;
$owner->renderFile($viewFile,$data);
if($j++ < $n-1)
echo $this->separator;
}
}
else
$this->renderEmptyText();
echo CHtml::closeTag($this->itemsTagName);
}
}
In your view, you can use it like:
<?php $this->widget('application.widgets.CCustomListView', array(
'dataProvider'=> 'your_data_provider',
'itemsHtmlOptions' => array('style' => 'color:blue', 'id' => 'your_id'),
'itemView'=>'your_item_view',
'template'=>'your_template',
)); ?>
So the style which in itemsHtmlOptions will be applied for the listview.
This link is also useful for you: How to extend CListView in order to remove extra yii added markup?
I have two modules in my installation. Both modules' controllers pass a variable called
$data['content']
to their views. Also, the first module's view runs the second module via
<?php echo Modules::run('module2'); ?>
and after that is supposed to display values from its $data['content'] variable. Unfortunately, that's when the first module's $data content is substituted with the second module's $data. This is pretty inconvenient for me, so I would like to know if there is a way to "protect" the $content variables and keep them only within their associated modules?
I would like to avoid renaming $data['content'] if possible. I've found a not-so-perfect solution in using
$data(__CLASS__)
but I am curious if it is possible not to change $data['content'].
class Some extends MX_Controller{
public function __construct(){parent::__construct();}
public function index(){
$this->load->view('template', array( //Primary template
'content' => 'some_index' // index view
));
}
public function _module_1(){
$this->load->view('module_1_view', array( //Module View : NO primary template
'' => '' <= no need to load view here, only data
));
}
public function _module_2(){
$this->load->view('module_2_view', array( //Module View : NO primary template
'' => '' <= no need to load view here, only data
));
}
}
-
template.php
<html>
<?php $this->load->view($content); ?>
</html>
-
some_index.php
<html>
//call modules
<?php echo Modules::run('some/_module_1'); ?>
<?php echo Modules::run('some/_module_2'); ?>
//call module from another class
<?php echo Modules::run('another_class/_module_1'); ?>
</html>
I'm trying to work with a YII CGridview to display some data.
This is home my model search function looks like:
/**
* Retrieves a list of models based on the current search/filter conditions.
* #return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('ip',$this->ip,true);
$criteria->compare('first_use',$this->first_use,true);
$criteria->compare('last_use',$this->last_use);
$criteria->compare('memberid',$this->memberid);
$criteria->compare('countryid',$this->countryid);
return new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
));
}
And this is how my view looks like
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'iplog-grid',
'dataProvider'=>$oIPLog->search(),
'filter'=>$oIPLog,
'summaryText' => 'showing you {start} - {end} of {count} logged Ips',
'columns'=>array(
array(
'name'=>'ip',
'type'=>'raw',
),
array(
'name'=>'first_use',
'type'=>'datetime',
),
array(
'name'=>'last_use',
'type'=>'datetime',
),
),
));
Displaying the CGridview works, but I can't seem to get the filter on top of it to work. It sends the call and I don't get any error as reponse, it just returns the whole unfiltered data again..
Any clues?
And how exactly does your controller look like?
For the CGridview filter to work you need to check in your controller if there are any filters set and then return the filtered object.
To clarify, something like this should be placed into your controller action
$oObject = new Object('search');
if (isset($_GET['Object'])) {
$oObject->attributes = $_GET['Object'];
}
Hope this helps
You Have to apply these points:
1.Specify the global variable($_REQUEST) in function of your controller
for example
$model = new User('search');
$model->unsetAttributes(); // clear any default values
if (isset($_REQUEST['User'])){
$model->attributes = $_REQUEST['User'];
}
$this->render('admin', array(
'model' => $model,
));
Set method type in search form
<?php $form=$this->beginWidget('CActiveForm', array(
'action'=>Yii::app()->createUrl('user/admin'),
'method'=>'POST',
)); ?>
3.In Cgrid view you have to define the url like
'ajaxUrl'=>Yii::app()->createUrl( 'controller/function' ),
I am getting the following error when I try and access:
domain.co.nz/admin/editpage/home/
I get the following error:
PHP Fatal error: Call to a member function getCMSPage() on a non-object in controllers/home.php on line 22
The issue with this is that I cannot understand why it is being passed back into the main “home” controller - which is the main controller.
All of my models are loaded by default - http://cl.ly/2U1F3a2B0s2K0i3k3g13
Ideal Situation
What I am trying to do with this is load the content into a text area on for editing and when submit is clicked I would like it to go back to the same page with a message saying content updated.
Admin Template
<li><?php echo anchor('#','Edit Pages');?>
<?php if(is_array($cms_pages)): ?>
<ul>
<?php foreach($cms_pages as $page): ?>
<li><a >permalink?>"><?=$page->name?></a></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
Page Model
function updatePage($data){
$data = array('content' => $content);
$this ->db->where('id',$id);
$this->db->update('pages',$data);
}
View:
<?php
//Setting form attributes
$formpageEdit = array('id' => 'pageEdit', 'name' => 'pageEdit');
$formInputTitle = array('id' => 'title', 'name' => 'title');
$formTextareaContent = array('id' => 'content', 'name' => 'content');
?>
<section id = "validation"><?php echo validation_errors();?></section>
<h4><?= $title ?> </h4>
<?php
echo form_open('admin/editpage/'.$page->permalink, $formpageEdit);
echo form_fieldset();
echo form_label ('Content', 'content');
echo form_textarea("content", $page['content']);
echo form_submit('submit','Submit');
echo form_fieldset_close();
echo form_close();
?>
Controller:
function index(){
if($this->session->userdata('logged_in')){
}else{
redirect('admin/home');
}
$page = $this->navigation_model->getCMSPage($this->uri->segment(3));
$data['cms_pages'] = $this->navigation_model->getCMSPages();
$data['title'] = $page->name;
$data['content'] = $this->load->view('admin/editpage', array('page' => $page, TRUE));
$this->load->view('admintemplate', $data);
}
I haven't really tested this yet but it should give you a good start. What you're asking is quite a bit of work to fully code out quickly.
This is the page_model model it's essentially what you posted with a few minor tweaks. Its best to use id's rather than strings. You may also want to do some htmlspecialchars or mysql hijack validation prior to passing these to your DB.
<?php
/**
* This is the Page_model model, this model handles the retrieval and modification of all pages.
*
**/
class Page_model extends CI_Model {
/**
* the getCMSPage function retrieves the data from the db given the ID that was passed via $id.
**/
function getCMSPage($id = NULL) {
$this->db->where('permalink', $permalink);
$query = $this->db->get('pages', 1);
#check to make sure row's were returned, if so continue, otherwise return false.
if ($query->num_rows() > 0){
#set the results into an array and return $row, should return $row['content'], and $row['id'];
#if you were editing more than one page this is where you would use a foreach($query)
$row = $query->result_array();
return $row;
}else{
return false;
}// END if ($query->num_rows() > 0)
}// END function getCMSPage()
}// END Page_model class
?>
This is the site controller and for sake of time i just echo'd your form_textarea straight from the edit function.. you shouldn't do this as it goes against MVC standards. You should create a view for that portion of the section.
<?php
/**
* This is the Site controller, primary controller for your site.
*
**/
class Site extends CI_Controller {
/**
* construct function, in our case we are going to load the Posts_model , you may not want to do this.
*
**/
function __construct()
{
parent::__construct();
#load Page_model, should be located in app/models/page_model.php
$this->load->model('Page_model');
}//END function __construct();
/**
* edit function, this function handles retrieval of the page from the URI, and the page's content for editing.
*
* This function uses $id which auto retrieves the page's ID from the uri. Your URL should look similiar to:
* http://yourdomain.com/site/edit/3/yourunecessaryinformationhere
* everything after the id is not really required but could help with SEO.
*
**/
function edit($id){
#retrieve the page's content in array form.
$page = $this->Page_model->getCMSPage($id);
echo form_textarea("content", $page['content']);
}
}//END Site Class
Keep in mind i wrote this up in 5-10 minutes so its rushed and not very well commented or even tested but itll give you a great head start. This only gives you an example of how to retrieve information from the DB and echo it into a textarea. You would still need to create another function within your page_model to insert/update the information and another snippet of code in your controller to pass the edited content to the model.
You can hit me up on AIM or twitter if you have more questions ThirdGenSup, twitter is #gorelative
// this line
$data['content'] = $this->load->view('admin/editpage', $data);
// needs to be
$data['content'] = $this->load->view('admin/editpage', array('page' => $page, TRUE);
Given an html/javascript 'widget' which needs to have certain fields customized before use. For example, the css class ids need to be unique as the widget may appear more than once on the same page.
Let's say I want to keep the markup (js/html) of the widget stored as a template so that I can fill in the values that need to be customized during resuse.
I know that Zend Framework's views give you at least part of this functionality, but each view is generally associated with a particular controller. Given that this widget could be created from any controller, yes still needs to be able to access some properties stored in a controller (or model). Where should I put the widget markup and how then do I fill in the custom values?
Can I create a custom view that can be reused within the same page (appear more than once) as well as on other pages? If so, how do I set that up?
Sounds like you need a ViewHelper http://framework.zend.com/manual/en/zend.view.helpers.html. Create a custom helper that will fetch the data from a model and just simply output it. This way it won't depend on any controller, can be called in either the layout or in any view script. Example:
// views/helpers/Widget.php
class Zend_View_Helper_Widget extends Zend_View_Helper_Abstract
{
protected $_model = null;
protected $_view = null;
public function widget()
{
$data = $this->_getDataFromModel();
return $this->_view->partial('widget.phtml', array('data' => $data));
}
public function setView(Zend_View_Interface $view)
{
if($this->_view === null) {
$this->_view = $view;
}
return $this->_view;
}
protected function _getDataFromModel()
{
$this->_model = $this->_getModel();
return $this->_model->getDataForWidget();
}
protected function _getModel()
{
if($this->_model === null) {
$this->_model = new Model_Widget(); // or whatever it's called
}
return $this->_model;
}
The partial script:
// views/scripts/widget.phtml
<div class="widget-class"><?php echo $this->data; ?></div>
And when you need it in your views just call it like <?php echo $this->widget(); ?>
Note that I'm rendering the widget in a separate partial view script, just to avoid having html/css in the helper itself.
Hope this helps to get you started :)
Zend_View_Helper_Partial
Example:
<?php echo $this->partial('partial.phtml', array(
'css_id' => 'foobar')); ?>
To run this from any other module:
<?php echo $this->partial('partial.phtml', 'partials_module', array(
'css_id' => 'foobar')); ?>
In your partial view script (partial.html) you would then have access to $this->css_id.