porting template to zend framework - zend-framework

I got few issues proting a pear based form to zend form.
I have few elements I need :
Basic Elements
Groups
Group Elements
Sections
I previously used templates to render the forms on Pear. I obviously cannot use pre-existing zend decorators, since I need to specify css classes for each of the components of my base elements.
To see the issue I need to render this, which is the template for a basic element :
<li class = "{position_in_the_form} {error}">
<label class="{label_class}"> {label}
[<span class="required_class"> * </span>]
</label>
<div> {element_content} </div>
[<p class = "{error_class}"> {error_message} </p>]
</li>
So as you can see I have many dynamic things I would like to be able to specify : position in the form, class for the label, class for the required section, the class for the error.
I would also like to be able to specify this from an ini file. I manage to set up the basic meta from the ini but not custom fields.
One of the reason I cannot use basic decorators is that I need to have "error" in the "li" class when there is an error in the element or the sub_form.I'm not sure this is possible with the error decorator... (correct me if I'm wrong)
Also, for the group I need something handling the errors, and since the core groups don't handle errors I need to subclass the sub_form. But how can I create a subform in an ini file and I don't know how to provide parameters to the sub form fromn the ini.
The main idea here is to be able to have visual and logic groups of elements in a form. For example I need a 'name' group with fullname, middle name, etc. This also implies a global validator for this "name" group.
An other thing is that I want to be able to position these groups : left half, right half, full
I got the css ready for this and working with pear.
So what I need is a simple solution, with few code and ini configurations. Unfortunately I think I got stuck in something too complicated, so if someone has any idea about a simple architecture it would be amazing!
Thanks in advance for your help,
Best, Boris

In your complex decoration need, you might want to use the ViewScript Zend_Form_Element_Decorator
$element->setDecorators(array(
array('ViewScript', array('viewScript' => 'path/to/your/views/element.phtml')),
));
and then in path/to/your/views/element.phtml, more or less something like
<li class="<?php echo $this->element->getAttrib('position_in_the_form') ?> <?php echo $this->element->hasErrors() ? 'error' : '' ?>">
<label class="<?php echo $this->element->getAttrib('label_class') ?>">
<?php echo $this->formLabel($this->element->getName(),
$this->element->getLabel()) ?>
<? if ( $this->element->isRequired() ) { ?>
[<span class="required_class"> * </span>]
<? } ?>
</label>
<div> <?php echo $this->{$this->element->helper}(
$this->element->getName(),
$this->element->getValue(),
$this->element->getAttribs()
) ?> </div>
<? if ( $this->element->hasErrors() ) { ?>
[<p class="<?php echo $this->element->getAttrib('error_class') ?>"> <?php echo $this->formErrors($this->element->getMessages()) ?> </p>]
<? } ?>
</li>
This is only a drafty snippet of code, but should lead you in the direction you aim.
Regards

Related

Getting HTML of a Cake PHP form

I am creating a form using Cake PHP. Is there any way to retrieve the basic HTML of the created form. For example,if we use Form Helper, we can create form using PHP itself. But now, I need only the html part of the created form for other use. Is it possible to retrieve it??
For example, say if I give input form like,
<?php
echo $this->Form->create('User');
echo $this->Form->input('email');
echo $this->Form->end('Save');
?>
I need output like this
<form action="index.html">
<input type="email" />
<input type="submit" value="Save" />
</form>
I can even create seperate function for attaining this objective. But I would like to know, if there is any other method for achieving this output
you can store FormHelper output in a string variable
<?php
$html_string = '';
$html_string .= $this->Form->create('User');
$html_string .= $this->Form->input('email');
$html_string .= $this->Form->end('Save');
?>
and then use your string elsewhere. But I'm not sure this is what you're searching for.
If I understand the question correctly, you want to use the form you created on another part of your site?
If that is the case, I would put the form itself in an Element and then call the Element wherever I wanted the form.
View/Elements/form.ctp
<?php
echo $this->Form->create('User');
echo $this->Form->input('email');
echo $this->Form->end('Save');
?>
Then in any view on your site you can call the Element using:
<?php echo $this->element('form');?>

moving columns from left to right on homescreen

I'm working on magento and I want the categories that appear on left to be appeared on right side. I tried many tutorial, but didnt get it. Please help me on this. I'm new to magento. Thanks.
To add categories into the sidebar firstly go to:
app/design/frontend/default/YOURTHEME/template/catalog/navigation
and create a .phtml file, for example: left-nav.phtml
In left-nav.phtml put this:
<div class="category-nav">
<div class="block-content">
<p class="block-subtitle"><?php echo $this->__('Categories') ?></p>
<ul id="nav_category" class="nav_category">
<?php foreach ($this->getStoreCategories(true) as $_category): ?>
<?php echo $this->drawItem($_category) ?>
<?php endforeach ?>
</ul>
</div>
</div>
This just loops through all the enabled categories and outputs them into a list.
Now you need to link up the list so firstly go into /public_html/app/design/frontend/default/sbs/layout/page.xml
Find the bit for 2columns-left, or right if you want it to output on the right. In the layout you are using find:
<block type="core/text_list" name="left" as="left" translate="label">
Inside the block add a line of code like this:
<block type="catalog/navigation" name="category.listing" as="left_nav" before="-" template="catalog/navigation/left_nav.phtml" />
The before=”-“ just adds it before everything else.
Finally go into:
/public_html/app/design/frontend/default/YOURTHEME/template/page/2columns-left.phtml and add:
<?php echo $this->getChildHtml('left_nav') ?>
Note:
You will need to clean the cache for this to work.
I have written a more in depth answer on my website http://brideo.co.uk/moving-categories-to-left-collumn-magento/

apply class active to anchor tag in Zend_Navigation

i am using partials to decorate Zend_Navigation to fetch desired output. everything is working good except i am having difficulty adding class="active" in <a href..> tag. here is my layout partial sidebar.phtml
<ul id="menu" class="nav">
<?php foreach($this->container as $page): ?>
<li class="<?php echo $page->class; ?>"><span><?php echo $page->label; ?></span></li>
<?php endforeach; ?>
</ul>
in the <a href="<?php echo $page->getHref(); ?>"> i want to add class="active" for the current page.
i tried some solutions which i found after searching. but nothing worked for me.
most of the solutions talk about doing it in controller for example
$page = $this->view->navigation()->findOneByLabel('Your Label');
if ( $page ) {
$page->setActive();
}
i haven't tested this yet. as i am using multiple navigations in the layout from one single navigation.xml file. i was wondering if is there a way i could do it in partials itself instead of in controllers or other helpers?
thank you
It's easy. In the view partial you can check which page is active:
if ($page->isActive()) { ... }
From ZF documentation:
Note: Note that when using the route property in a page, you should also specify the default params that the route defines (module, controller, action, etc.), otherwise the isActive() method will not be able to determine if the page is active. The reason for this is that there is currently no way to get the default params from a Zend_Controller_Router_Route_Interface object, nor to retrieve the current route from a Zend_Controller_Router_Interface object.

How can I create multiple forms on a single page in CakePHP?

I'm trying to come up with a standard way to create multiple forms for the same model on an index page. Here's a little more detail.
I have a list of binders, each with a memo being displayed. I would like the memo fields to be editable from the index page. Obviously it doesn't work to just copy and paste the view for the edit_memo action, like so:
<?php echo $this->Form->create('Binder');?>
<fieldset>
<legend><?php __('Edit Memo'); ?></legend>
<?php
echo $this->Form->input('id');
echo $this->Form->input('memo');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
But that's essentially what I'm looking to do. In context, I'm just looping through the binders in the index action and the memos are part of the model.
I've tried changing $this->Form->create('Binder') to the following:
$this->Form->create(null,array(
'id'=>"BinderEditMemo.$i",
'controller' => 'binders',
'action' => 'edit_memo',
'id' => $binder['Binder']['id']
));
But no luck. The memo field still gets the regular id, so I think I might need to change that as well. When I submit the form it does perform the action, however it does not save. FYI, I have routed the id parameter to the action in my routes.
I'm sure there has to be a standard way to render multiple forms in the index loop. Any thoughts?
This isn't too difficult, but you have to rely somewhat less on Cake's FormHelper magic. The following works (or has worked for me on a number of occasions, at least):
<?php
$baseUrl = array('controller'=>'binders','action'=>'edit');
foreach ($binders as $_binder) {
$url = $baseUrl; $url['id'] = $_binder['id'];
echo $this->Form->create('Binder',array('url'=>$url));
echo $this->Form->input('Binder.id', array('type'=>'hidden','value'=>$_binder['id']));
echo $this->Form->input('Binder.memo', array('value'=>$_binder['memo']));
echo $this->Form->end(__('Submit',true));
}
?>
I'm not exactly sure of the structure of your data, so it the above will require some tweaking, but you should get the idea. I have no idea purpose what is served by creating false models and etc.
I wonder if this link would help solve your problem: http://bakery.cakephp.org/articles/RabidFire/2010/06/26/multiple-forms-per-page-for-the-same-model
Essential it suggests creating two new (empty) models that extend your base model, which your forms can call on independently. Seems like it could be a relatively hassle-free way of keeping your forms distinct from one another...
Daniel, you were exactly right! Very nice. This is the code I ended up with. It's important for one main reason. The HTML spec says that ID's need to be unique. So I added a lil sumthin to the form, and fields to prevent problems. Still works.
<?php
$baseUrl = array('controller'=>'binders','action'=>'edit_memo');
$url = $baseUrl;
$url['id'] = $binder['Binder']['id'];
echo $this->Form->create(null,array(
'id'=>"BinderEditMemo-{$binder['Binder']['id']}",
'url'=>$url
));
?>
<fieldset>
<legend><?php __('Memo'); ?></legend>
<?php
echo $this->Form->input('Binder.id', array('id' => "BinderId-{$binder['Binder']['id']}", 'type'=>'hidden','value'=>$binder['Binder']['id']));
echo $this->Form->input('Binder.memo', array('id' => "BinderMemo-{$binder['Binder']['id']}", 'value' => $binder['Binder']['memo'], 'label' => '', ));
?>
</fieldset>
<?php echo $this->Form->end(__('Update Memo',true)); ?>

Zend reusable widgets / plugins / miniapplications?

I'm new to Zend framework and trying to get some insights about code re-usability. I definitely know about modules but there seems to be a bit of uncertainty about what functionality should go into modules and what not.
What I'm trying to accomplish:
1) to have reusable mini programs/widgets/plugins (whatever you may call them) that one can simply plug into any site be doing this in layout or view:
<?php echo $this->contactform;?>
or this in the view:
<?php echo $this->layout()->blog;?>
I'd call them extension. so basically sort of what you'd see in Joomla/ WordPress/Concrete5 templates.
2) All code that is related to that specific extension should be in it's separate directory.
3) We should be able to output extensions only for certain modules/controllers where they are required. they shouldn't be rendered needlessly if it won't be displayed.
4) each extension may output multiple content areas on the page.
Do you have a nicely laid out structure / approach that you use?
It sounds like you need study up on view helpers. View helpers can be a simple as returning the App Version number or as complicated as adding html to multiple place holders. For example:
layout.phtml:
<h1><?php echo $this->placeholder('title'); ?>
<div class="sidebar">
<?php echo $this->placeholder('sidebar'); ?>
</div>
<div class="content">
<?php echo $this->layout()->content; ?>
</div>
in your view script foo.phtml for example:
<?php
$this->placeholder('title')->set('Hello World!');
$this->placeholder('sidebar')->set('Hello World!');
?>
<h1>Bar Bar!</h1>
Now if you want to be able to reuse that over and over again you can do this:
<?php
class Zend_View_Helper_MyHelper extends Zend_View_Helper_Abstract
{
public function myHelper()
{
$this->view->placeholder('title')->set('Hello World!');
$this->view->placeholder('sidebar')->set('Hello World!');
return '<h1>Bar Bar!</h1>';
}
}
Now, replace the code in your foo.pthml with:
<?php
echo $this->myHelper();
Both examples of foo.phtml output:
Hello World!
Hello World!
Bar Bar!
Of course this is very simplified example, but I hope this helps point you in the right direction. Happy Hacking!