errorSummary message for two models - yii

I want to show errorSummary custom text but for two models.
<?php echo CHtml::errorSummary(array($a,$b)); ?>

This is already supported in Yii. You can pass an array of models to the method, as said here.

Related

Using a third party plugin with Yii

i am using a third party plugin with yii, it provides chat functionality, it has its own DB and php files that provide the functionality,
now i am want to use it in the view, but the simple include statements are not working, do i need to convert it to yii or can i use it as is?
<?php
session_start();
// Load MySQL DB settings
include_once('config.inc.php');
$_SESSION['username'] = 'Currently logged in users's username from database';
$_SESSION['user_id'] = 'Currently logged in user's id';
?>
//That's it! To print online users, you need to do it like this:
<?php
$users = mysql_query("SELECT id,username FROM ".$sql_table_users." WHERE chat_status='online' AND id!='".$_SESSION['user_id']."'");
if(mysql_num_rows($users) > 0){
while($user = mysql_fetch_assoc($users)){
print ''.$user['username'].'<br />';
}
}
?>
this is the interface plugin has provided for me.
plugin location is /assets/plugin.
i cant use direct php query commands to another Database, which i want to keep seperate from mine, plus the js file that comes with the plugin calls the script with wrong URL parameters, so what is the best method to incorperate this into my yii app. thanks
You should create a Yii extension that'll wrap your plugin.
Then in your view you'll have to call a widget that'll display your chat.
I think this is the best way to do it because using this all your call to the plugin will be performed with the yii strucutre and philosophy. Only your extension will be structured using the chat philosophy.
Source about creating widgets

Invoke action in Yii

This is my scenario. A home page with a upload form, user upload the images and submit, then the controller will catch the data from user, add it in the Model. Finally the image info will display in the another page.
I put the actionIndex() in the siteController to render the home page, the actionUpload() in the same file to handle the data user. So in the view, what should I put in the form action to invoke the actionUpload(). I think the flow is quite weird in Yii when I read the blog demo code, I just follow same way with the ASP.NET MVC. Suggest me the right way, plz. Thanks
Depends on how you build your form. If using CHtml, do the following:
<?= CHtml::beginForm($this->createUrl('site/upload'))?>
If you have a model that sits behind it:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form',
'action' => $this->createUrl('site/upload'),
)); ?>
Please check the Yii documentation and examples on how to properly set up forms. You have several choices here.

separate header, content and footer in yii

I have a login form in my header section of the website. If user is logged in than insted of the login form user profile details will be shown. The question is how to separate header footer and content into different views and call them from one controller? Or maybe there is another solution...Thanks for help.
In your header view you could write something like this.
<?php if(Yii::app()->user->getId()): ?>
<?php $this->renderPartial('//world/_header_user')); ?>
<?php else: ?>
<?php $this->renderPartial('//world/_header_guest')); ?>
<?php endif; ?>
Using the Model-View-Controller (MVC) design pattern, the look of a Yii-based site is naturally controlled by the View files. These files are a combination of HTML and PHP that help to create the desired output. Specific pages in a site will use specific View files. In fact, the View files are designed to be broken down quite atomically, such that, for example, the form used to both create and edit an employee record is its own file, and that file can be included by both create.php and update.php. As with most things in OOP, implementing atomic, decoupled functionality goes a long way towards improving reusability. But the individual View files are only part of the equation for rendering a Web page. Individual view files get rendered within a layout file. And although I’ve mentioned layouts a time or two in my writings on Yii, it’s a subject that deserves its own post.
To be clear, layouts are a type of View file. Specifically, whereas other View files get placed within a directory for the corresponding Controller (i.e., the SiteController pulls from views/site), layout files go within views/layouts. But while the other View files are associated with individual Controllers (and therefore, individual pages), layouts are communal, shared by all the pages. Simply put, a layout file is the parent wrapper for the entire site’s templating system. I’ll explain
ypu can see more details
http://www.larryullman.com/2012/05/16/working-with-layouts-in-yii
The easiest way is probably to use a different layout, which you just switch on login. If not, showing partials / components based on Yii::app()->user->isGuest also works well.
Your default generated Yii application has a parent Controller in protected/components/Controller.php.
If you need to access additional parameters in layout, add public properties to Controller, set them in your child controller, and use them in your view/layout files.

yii CActiveForm submitting by AJAX

I create form using the following code:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'contacts-form',
'enableAjaxValidation'=>false,
)); ?>
Is there any way to submit the form by AJAX? Remember not I am not talking about AJAX validation.
One way to do this is to use the built in ajaxSubmitButton helper, like so:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'contacts-form',
'enableAjaxValidation'=>false,
)); ?>
<!-- your form elements here -->
<?php echo CHtml::ajaxSubmitButton(Yii::t('app', 'Submit')); ?>
<?php $this->endWidget(); ?>
This automatically binds a jQuery AJAX call to the SUbmit button which will POST the form values to your from's action URL.
You could also write the AJAX code yourself, of course, but Yii has this helper function too.
This helper ajaxSubmitButton function isn't terribly useful, especially since it uses jquery.ajax() without using the "Promise interface" in jquery 1.5+, so you have to process the response through the success callback. This would have been cleaner if they just used jquery.submit(). You're better off just rolling your own, honestly.
I'm disappointed that none of the (relatively popular) Bootstrap integrations like Yii-Bootstrap or YiiBooster offer much of anything in terms of generating forms that refresh themselves with ajax response data either (I'm not talking validation). I go through all the trouble of learning & adopting a framework, only to end up html/js/css-coding my own frontend presentation and logic anyway... Oh well.
I'm still a Yii enthusiast, mostly due to their gii generator and support for quasi-mixin patterns with "behaviors". It leads to clean code on the backend, but the framework has some growing to do on the frontend & view rendering. Their "CHtml" library really isn't cutting it right now.

Kohana 3: How to provide API functions in template/view like WordPress?

I'm working on a project which allows the advanced user to define their own way of showing the information and access some basic API. For example, I provide a show_search_box() function so that the user can call this function in the view file when they want to show the standard search box, or they could call the function with parameters to customize the search form.
e.g. this code in the template will show a search form with watermark text "Enter keyword here".
<div><?php show_search_box('Enter keyword here'); ?></div>
What I'm thinking actually is exactly like what WordPress does in its template tags. (http://codex.wordpress.org/Stepping_Into_Template_Tags)
My idea is to create a class that provide all those API functions and pass an object instance of the class to the view file, so users can call the API functions in view like:
<div><?php $API->show_search_box('Enter keyword here'); ?></div>
I think it will work, (but have not tested it yet), but I prefer providing a set of direct called functions just like WordPress. What's the best way to do this with kohana 3?
======Update: I have tested the method of pass $API object to view, and it works as expected.
class API {
public function show_search_box($watermark){....}
}
In the controller, pass the $API to the view/template
public function action_index()
{
$this->template->API = new API();
}
Then call the function inside view/template as described above.
Unlike those controller methods, $API cannot access the controller's variables unless they're explicitly assigned: e.g. $API->setVar('VarName', $a_controller_variable), which is quite tedious i think.
Well, unlike Kohana 2.3, views don't execute in the controller namespace, so you can't simply do $this->something().
If you have all your functions in one Model, let's call it API, then you could do this in the view (or base controller if you want it available everywhere)...
$this->template->internalView = View::factory('your_view')
->set('API', Model::factory('API));
(assuming you have a <?php echo $internalView; ?> in a parent view).
Then you could do in your view...
<div><?php $API->show_search_box('Enter keyword here'); ?></div>
Which will run your method on your model. Views shouldn't really know about the existence of models, but your case ma be an exception. Perhaps you could use a helper sort of class instead of a model, if you are worried about breaking the MVC paradigm.
If you want to do what WordPress does (have a bunch of global functions, which I don't recommend), then you will need to define them somewhere. Kohana doesn't really have an easy spot to place them, as it doesn't really cater for a bunch of global functions.