ZendFramework - Zend_Navigation. Find* methods removes pages from container object - zend-framework

When using findAll method on Zend_Navigation_Container pages, that are found, are being removed from container object.
Same thing happens, even when I clone container object and findAll is being called on this cloned one.
I can't figure how is it happens.
However, i've noticed that it only happens, when i try to "find" pages on the second level (or deeper)
Same problem seems to be described here, no answer was given...
Here is some code, to reproduce problem:
1. First method, no cloning, any page that is "found" is removed from container
<?php
$container = new Zend_Navigation(array(
array(
'label' => 'Page 1',
'route' => 'default',
'pages' => array(
array(
'label' => 'Page 1.1',
'route' => 'default',
),
)
),
array(
'label' => 'Page 2',
'route' => 'default',
),
array(
'label' => 'Page 3',
'route' => 'default',
)
));
echo $this->navigation($container)->menu()->renderMenu($container);
echo '<br /><hr /><br />';
echo $this->navigation($container)->menu()->renderMenu(new Zend_Navigation($container->findAllByLabel('Page 2')));
echo '<br /><hr /><br />';
echo $this->navigation($container)->menu()->renderMenu($container);
?>
2. Second try, cloning, page on second level (and deeper) that is "found" is removed from container
<?php
$container = new Zend_Navigation(array(
array(
'label' => 'Page 1',
'route' => 'default',
'pages' => array(
array(
'label' => 'Page 1.1',
'route' => 'default',
),
)
),
array(
'label' => 'Page 2',
'route' => 'default',
),
array(
'label' => 'Page 3',
'route' => 'default',
)
));
$container1 = clone $container;
$container2 = clone $container;
$container3 = clone $container;
echo $this->navigation($container1)->menu()->renderMenu($container1);
echo '<br /><hr /><br />';
echo $this->navigation($container2)->menu()->renderMenu(new Zend_Navigation($container2->findAllByLabel('Page 1.1')));
echo '<br /><hr /><br />';
echo $this->navigation($container3)->menu()->renderMenu($container3);
?>
3. Last one, cloning, page on first level (and deeper) that is "found" is NOT removed from container
Same code as above, only 'Page 2' in place of 'Page 1.1'
Can someone tell me what's going on here?
All I want to achieve, is to display same menu in two different places. In both places menu is build from part of container pages, filtered with findXXX method...
But with described problem, it seems to be impossible :(
Thanks in advance for any suggestions.

Try this, you're on the right track, I just think your having trouble with the difference between Zend_Navigation and navigation() view helper.
<?php
//instantiate Zend_navigation object... This also registers this container to the view helper
$container = new Zend_Navigation(array(
array(
'label' => 'Page 1',
'route' => 'default',
'pages' => array(
array(
'label' => 'Page 1.1',
'route' => 'default',
),
)
),
array(
'label' => 'Page 2',
'route' => 'default',
),
array(
'label' => 'Page 3',
'route' => 'default',
)
));
//now we use the view helper
echo $this->navigation()->menu()->renderMenu($container);
echo '<br /><hr /><br />';
$label = $this->navigation()->findAllByLabel('Page 2');
echo $this->navigation()->menu()->renderMenu($label);
echo '<br /><hr /><br />';
echo $this->navigation()->menu()->renderMenu($container);
?>
personally I like to set navigation up in the bootstrap and use config files to make containers.
protected function _initNavigation() {
$config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/site.xml');
$container = new Zend_Navigation($config);
$registry = Zend_Registry::getInstance();
$registry->set('Zend_Navigation', $container);
}
Hope this helps :)

Related

the first input block after a post request. cakephp

I have a little problem with a form in cakephp. When the view render the first time I can introduce info in all the inputs but after a submit if a validation fail the first input blocks, I can't introduce more info in there but in the rest I can. here is the view:
<?php
echo $this->Form->create('User');
echo $this->Form->input('phone');
echo $this->Form->input('complete_name');
echo $this->Form->input('adress');
echo $this->Form->input('state');
echo $this->Form->input('city');
echo $this->Form->input('zip_code');
echo $this->Form->button('Save', array('name' =>'save'));
echo $this->Form->end();
?>
In the controller:
public function user_info(){
if($this->request->is('post'))
{
if($this->User->save($this->request->data))
{
$this->redirect(array('controller' => 'users', 'action' => 'user_store'));
}
else
{
$this->Session->setFlash(__('problem saving'), 'flash_bad');
}
}
Validations of the model:
public $validate = array(
'complete_name' => array(
'notempty' => array(
'rule' => array('custom','/^[a-z A-ZáéíóúÁÉÍÓÚñÑäëïöüÄËÏÖÜ]{5,}$/i'),
'message' => 'fail the name'
),
'maxlength' => array(
'rule' => array('maxlength', 45)
)
),
'adress' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'fail adress',
),
'maxlength' => array(
'rule' => array('maxlength', 60),
),
'minlength' => array(
'rule' => array('minlength', 10),
'message' => 'fail adress'
)
),
'state' => array(
'notempty' => array(
'rule' => array('custom', '/^[a-z A-ZáéíóúÁÉÍÓÚñÑäëïöüÄËÏÖÜ]{5,}$/i'),
'message' => 'fail state'
),
'maxlength' => array(
'rule' => array('maxlength', 30)
)
),
'city' => array(
'notempty' => array(
'rule' => array('custom', '/^[a-z A-ZáéíóúÁÉÍÓÚñÑäëïöüÄËÏÖÜ]{5,}$/i'),
'message' => 'fail city'
),
'maxlength' => array(
'rule' => array('maxlength', 30)
)
),
'zip_code' => array(
'notempty' => array(
'rule' => '/^[1-9]{4,4}$/i',
'message' => 'fail zip code'
)
)
);
when a validation fail I can see the message in the view and the session flash too and I can edit everything except the phone. if I exchange lines in the view code like this:
<?php
echo $this->Form->create('User');
echo $this->Form->input('complete_name'); // change position with phone
echo $this->Form->input('phone');
echo $this->Form->input('adress');
echo $this->Form->input('state');
echo $this->Form->input('city');
echo $this->Form->input('zip_code');
echo $this->Form->button('Save', array('name' =>'save'));
echo $this->Form->end();
?>
Then I can't edit complete_name. someone know what is happening?
Based on what you describe, I think css or jQuery is placing a div that extends to that input preventing you from editing it, perhaps a div with an error class or even padding on that first input can cause issues. Regardless, use a developer tool like Firebug in Firefox to investigate. You will be able to see whether that is what is happening.

ZendFramework2 - Literal- or Segment-Routing and Parameters

I am using ZendFramework 2.x and trying to add a route to some existing ones. Moreover I want to put some parameters in die URL as well. If I use the segment-type for my new route ('showroom') I am able to call my new URL and will get forwarded to the corresponding view. Unfortunately I am not able to set some parameters in the URL. The other option is to use segment type in my module.config.php-file, but I will get some ZF-2-Exception, that my route is not configured correctly, this happens even before rendering my intro-view. Thanks in advance for showing me, how to combine segment and literal-type usage in route-child-route-combination or for telling me how to add parameters to literal type URLs.
Route-configuration in module.config.php of Module:
'router' => array(
'routes' => array(
'zfcuser' => array(
'type' => 'Literal',
'priority' => 1000,
'options' => array(
'route' => '/user',
'defaults' => array(
'controller' => 'zfcuser',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'showroom' => array(
'type' => 'Segment',
'options' => array(
'route' => '/:id',
'defaults' => array(
'controller' => 'zfcuser',
'action' => 'showRoom',
'id' => '1',
),
),
),
'login' => array(
'type' => 'Literal',
UPDATE: snippet which triggers the error (see comments)
<?php
$roomIndex = 1;
foreach($roomsPaginator->getCurrentItems() as $room){
$roomURL = 'zfcuser/showroom' . '/' . $roomIndex;
echo "<p>Name: " . $room['name'] . "; Luftfeuchtigkeit: " . $room['humidity'] . "; <a class='btn' href='" . $this->url($roomURL) . "'>Betreten »</a></p>";
$roomIndex++;
}
?>
I'm not sure if this is the cause of your error, but I don't think you're using the URL helper correctly. You don't build a URL and pass it to the helper, the idea is the helper builds the URL for you using the route name and params. You probably want something like this:
$roomIndex = 1;
foreach($roomsPaginator->getCurrentItems() as $room){
echo "<p>Name: " . $room['name'] . "; Luftfeuchtigkeit: " . $room['humidity'] . "; <a class='btn' href='" . $this->url('showroom', array('id' => $roomIndex)) . "'>Betreten »</a></p>";
$roomIndex++;
}

CodeIgniter: Controller structure for forms with many inputs

I'm developing a site using CodeIgniter and am trying to adhere to the "Fat Model / Skinny Controller" paradigm, but am running into some problems when it comes to pages containing forms with a number of inputs. The code below is what I'm using to define the inputs for the address fields
Part of my Controller (where I'm defining the form inputs and their attributes):
$this->data['address1'] = array(
'name' => 'address1',
'id' => 'address1',
'type' => 'text',
'class' => 'field text addr',
'tabindex' => '10',
'value' => $this->form_validation->set_value('address1'),
'placeholder' => 'Street Address'
);
$this->data['address2'] = array(
'name' => 'address2',
'id' => 'address2',
'type' => 'text',
'class' => 'field text addr',
'tabindex' => '11',
'value' => $this->form_validation->set_value('address2'),
'placeholder' => 'Address Line 2',
);
$this->data['city'] = array(
'name' => 'city',
'id' => 'city',
'type' => 'text',
'class' => 'field text addr',
'tabindex' => '12',
'value' => $this->form_validation->set_value('city'),
'placeholder' => 'City'
);
$this->data['state'] = array(
'name' => 'state',
'id' => 'state',
'class' => 'field addr',
'tabindex' => '13',
'value' => $this->form_validation->set_value('state'),
'label' => array('class' => 'desc')
);
$this->data['zip'] = array(
'name' => 'zip',
'id' => 'zip',
'type' => 'text',
'class' => 'field text addr',
'tabindex' => '14',
'maxlength' => '20',
'value' => $this->form_validation->set_value('zip'),
'placeholder' => 'Zip / Postal Code'
);
$this->data['country'] = array(
'name' => 'country',
'id' => 'country',
'class' => 'field addr',
'tabindex' => '15',
'value' => $this->form_validation->set_value('country')
);
Part of my View (minus all the HTML to position the form inputs):
<?php
echo form_open("address/add");
echo form_input($address1);
echo form_input($address2);
echo form_input($city);
$options = array();
$options[''] = 'State / Province / Region';
foreach($province_options AS $prov)
{
$options[$prov->id] = $prov->province;
}
echo form_dropdown('state',$options,'',$state);
echo form_input($zip);
$options = array();
$options[''] = 'Country';
foreach($country_options AS $cnt)
{
$options[$cnt->id] = $cnt->country;
}
echo form_dropdown('country',$options,'',$country);
echo form_submit('submit', 'Submit & Continue');
echo form_close();
?>
I feel like my Controller is overly verbose, but I can't think of what the alternative would be for how to organize the information necessary to represent my form if I'm planning on using the Form Helper to generate the form inputs in my view. Is this the right way to be doing things, or is there a better approach?
Just because Codeigniter provides all of these helpers does not mean you have to use them!
All you need is form_open() because this adds the CRSF token (if used).
Raw HTML is much cleaner and I suspect much faster than waiting for PHP to render markup.
Edit: I would like to add, the reason its cleaner is because you have control over the output, where as CI might adere to certain specifications.
I don't see a problem in your question.
This is just silly
$options = array();
$options[''] = 'State / Province / Region';
There is a little bit of logic that can be moved to the controller or even model layer:
$options = array();
$options[''] = 'Country';
foreach($country_options AS $cnt)
{
$options[$cnt->id] = $cnt->country;
}
echo form_dropdown('country',$options,'',$country);
Could probably look like:
echo form_dropdown('country', $countries, '', $country);
...if you move the options to the controller or view.
This is a problem I run into all the time trying to keep things DRY as possible, where to define the form data? I think sometimes we forget the power of the "V" in "MVC". You could define all the view logic in the view instead.
Things like id, tabindex and placeholder are only necessary and useful in the view. Things like form validation rules and data checking/prepping belong in the Controller/Model layer.
The form helper functions are useful, but sometimes raw HTML is better. For example:
// Controller
$this->data['address1'] = array(
'name' => 'address1',
'id' => 'address1',
'type' => 'text',
'class' => 'field text addr',
'tabindex' => '10',
'value' => $this->form_validation->set_value('address1'),
'placeholder' => 'Street Address'
);
// View
echo form_input($address1);
Or simply:
<input name="address1" id="address1" tabindex="10" type="text" placeholder="Street Address" value="<?php echo set_value('address1'); ?>" class="field text addr">
I wrote a bunch of applications last year where I defined all this stuff in the Model, and now I'm regretting it as I've been going back to do maintenance on them and all the view logic is obscured away in the Model or Controller. Editing a Controller or Model to change a class attribute is just silly.

Zend framework: using BreadCrumbs with partial

I have some trouble making a customized breadcrumb:
I my layout.phtml I have:
$container = new Zend_Navigation();
$this->navigation($container);
$container->addPage(
array(
'label' => 'Dashboard',
'module' => 'default',
'controller' => 'dashboard',
'action' => 'index',
'pages' =>
array(
array(
'label' => 'Create Order',
'module' => 'default',
'controller' => 'createorder',
'action' => 'index'
),
array(
'label' => 'Query',
'module' => 'default',
'controller' => 'query',
'action' => 'index',
'pages' => array(
array(
'label' => 'View Order',
'module' => 'default',
'controller' => 'order',
'action' => 'vieworder'
)
)
),
array(
'label' => 'Administration',
'module' => 'default',
'controller' => 'admin',
'action' => 'index',
'pages' =>
array(
array(
'label' => 'News and Announcements',
'module' => 'default',
'controller' => 'admin',
'action' => 'addnews',
'pages' => array(
array(
'label' => 'Edit News and Announcements',
'module' => 'default',
'controller' => 'admin',
'action' => 'editnews'
)
)
)
)
)
)
)
);
The next line is:
echo $this->navigation()->breadcrumbs()->setPartial(array('BreadCrumb.phtml','default'));
The BreadCrumb.phtml is called well, but I don't know how to make an ul-li menu in my BreadCrumb.phtml. How do I get the navigation I'm actually in?
Thanks in advance for any help. Andrea
To do so, your BreadCrumb.phtml should look like this:
<?php
if (null === $this->container) {
$this->container = $this->breadcrumbs()->getContainer();
}
// find deepest active
if (!$active = $this->breadcrumbs()->findActive($this->container)) {
return '';
}
$active = $active['page'];
// put the deepest active page last in breadcrumbs
if ($this->breadcrumbs()->getLinkLast()) {
$html = ' <li>' . $this->breadcrumbs()->htmlify($active) . '</li>' . PHP_EOL;
} else {
$html = $active->getLabel();
if ($this->breadcrumbs()->getUseTranslator() && $t = $this->breadcrumbs()->getTranslator()) {
$html = $t->translate($html);
}
$html = ' <li>' . $this->escape($html) . '</li>' . PHP_EOL;
}
// walk back to root
while (($parent = $active->getParent()) != null) {
if ($parent instanceof Zend_Navigation_Page) {
// prepend crumb to html
$html = ' <li>' . $this->breadcrumbs()->htmlify($parent) . '</li>' . PHP_EOL . $html;
}
if ($parent === $this->container) {
// at the root of the given container
break;
}
$active = $parent;
}
echo strlen($html) ? $this->breadcrumbs()->getIndent() . '<ul id="breadcrumbs">' . PHP_EOL
. $html . '</ul>' . PHP_EOL : '';
?>
Then you will get something like this:
<ul id="breadcrumbs">
<li>Home</li>
<li>Articles</li>
<li>Shop</li>
<li>Last link</li>
</ul>
Inside the view script, the zend navigation container is retrievable via $this->container. The container is iterable, so you can walk through it with a simple foreach loop (e.g. foreach($this->container as $page)
I've found simple solution for render breadcrumbs wrapped in ul-li menu.
You can place this code in your breadcrumbs.phtml:
<?php $breadcrumbs = $this->navigation()->breadcrumbs()->setMinDepth(0); ?>
<?php $items = array_filter(explode($breadcrumbs->getSeparator(), $breadcrumbs->render())); ?>
<?php if ($items) : ?>
<ul class="breadcrumbs">
<?php foreach ($items as $item) : ?>
<li><?= $item ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

ZendX_JQuery dialogContainer usage

I'm aming to make use of the ZendX_JQuery dialogContainer view helper, in order to produce a modal window, were users can input specified information(for example send a message). I'm trying to use the dialogContainer view helper in this fashion.
First of, include the ZendX library in the applications library folder.
Secondly, include the following row in the initViewHelper method within the Bootstrap.php file
"$view->addHelperPath('ZendX/JQuery/View/Helper/', 'ZendX_JQuery_View_Helper');"
third, adding the following conditional enabling of js in the layout.phtml
"<?php if($this->jQuery()->isEnabled()){
$this->jQuery()->setLocalPath($this->baseUrl()
.'/js/jquery/js/jquery-1.4.2.min.js')
->setUiLocalPath($this->baseUrl()
.'/js/jquery/js/jquery-ui-1.8.custom.min.js')
->addStylesheet($this->baseUrl()
.'/js/jquery/css/ui-lightness/jquery-ui-1.8.custom.css');
echo $this->jQuery();
}
?>"
fourth, creating my Application_Form_JQueryForm extending ZendX_JQuery_Form
"<?php
class Application_Form_JQueryForm extends ZendX_JQuery_Form
{
private $form;
public function init()
{
$this->form = $this->setAction(Zend_Controller_Front::getInstance()->getBaseUrl() . '/index/index')
->setMethod('post');
$this->form->setDecorators(array(
'FormElements',
'Form',
array ('DialogContainer', array(
'id' => 'tabContainer',
'style' => 'width: 600px;',
'title' => 'Send a private message to Kalle',
'JQueryParams' => array(
'tabPosition' => 'top',
),
)),
));
$topic = new Zend_Form_Element_Text('topic');
$topic->setValue('topic')
->setRequired(true)
->setValidators(array('validators' => array(
'validator' => 'StringLength',
'options' => array(1,15)
)))
->setDecorators(array(
'ViewHelper',
'Description',
'Errors',
array('HtmlTag', array('tag' => 'dl'))));
$textarea = new Zend_Form_Element_Textarea('textarea');
$textarea->setValue('post a comment')
->setAttribs(array(
'rows' => 4,
'cols' => 20
))
->setRequired(true)
->setValidators(array('validators' => array(
'validator' => 'StringLength',
'options' => array(1,15)
)))
->setDecorators(array(
'ViewHelper',
'Description',
'Errors',
array('HtmlTag', array('tag' => 'dl'))));
$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel('Send your comment')
->setDecorators(array(
'ViewHelper',
'Description',
'Errors',
array('Description', array('escape' => false, 'tag' => 'span')),
array('HtmlTag', array('tag' => 'dl'))))
->setDescription('or Cancel');
$this->form->addElements(array($topic, $textarea, $submit));
}
}"
This form is then instanciated in the controllers action method, and called in the view.
And so to the problem of mine, no matter what i try, in order to for instance set, the width of the dialogContainer or any other parameter (color, css, height, so on and so forth), this being in the JQueryForm's setDecorator part for the form, i can't seem to get any change whatsoever in the resulting modal when called in the view, any help in the proper direction would be greatly appreciated
Thanks in advance, Kalle Johansson
A late answer for sure - but lots of views - so figured I would answer. The parameters you want to set for the modal should be set from the JQueryParams array. So - for example:
$this->form->setDecorators(array(
'FormElements',
'Form',
array ('DialogContainer', array(
'id' => 'tabContainer',
'style' => 'width: 600px;',
'title' => 'Send a private message to Kalle',
'JQueryParams' => array(
'tabPosition' => 'top',
'width' => '600',
'height' => '450'
),
)),
));
You can find these param options on the jQuery UI site.
LK