Using Placeholder Viewhelper - zend-framework

I have worked with partials and now I'd like to have something like an info box which shall be filled with additional information.
I'm a bit confused how to give the data to the placeholder.
What I did:
I have an additional layoutfile. layout2.phtml
<?php $this->placeholder('content')->captureStart(); ?>
<div class="row">
<div class="col-md-8">
<?= $this->content; ?>
</div>
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">additional part info</h3>
</div>
<div class="panel-body">
<strong>Approval Status</strong><p>
<strong>Alerts</strong> <p>
there are
<?= $this->AnzahlAlerts?> Alerts at the moment<p>
<strong>MoM</strong><p>
<p>see MoM here</p>
</div>
</div>
</div>
</div>
<?php
$this->placeholder('content')->captureEnd();
echo $this->partial('layout/layout',
['content'=>$this->placeholder('content')]);
?>
The placeholderbox will be shown like I wanted.
But I won't get the value of $this->AnzahlAlerts. I thought it must be given to the viewmodell, so I tried as follows:
controller/showAction
return new ViewModel([
'unitid' => $unit,
'part' => $part,
'dcls' => $this->table->fetchPartDcl($part,$dclid),
'pads' => $this->padtable->fetchPadPart($part, $unit),
'heritage' => $this->table->getHeritage($part, $projectid), //$this->unitpartTable->fetchHeritage($part)
'AnzahlAlerts' => $this->padtable->countAlert($part)
]);
My **onDispatchAction** here for completion:
public function onDispatch(MvcEvent $e)
{
$response = parent::onDispatch($e);
$this->layout()->setTemplate('layout/layout2');
return $response;
}
My Questions are, what is the error, and where is the postion to give the value AnzahlAlerts?
EDIT: count records
This is my modelfunction:
public function countAlert($partnumber)
{
$statment = "SELECT count(*) as AnzahlAlerts
from t_part_alert
WHERE t_part_alert.Part_Number = '" . $partnumber. "';";
// AND t_unit_part.UnitID =" . $unitid;
return $this->tableGateway->adapter->query($statment, "execute");
}
Just because it my be the problem.

You call and print a partial like so:
<?= $this->partial('partial/file/alias') ?>
This causes a partial to be rendered within the current partial where you called it. The name in the view_manager config is partial/file/alias.
If you wanted to pass variables to the partial file, you would have to pass an array as the second parameter, like so:
<?= $this->partial(
'partial/file/alias',
[
'param1' => 'value1',
'param2' => 'value2',
]
) ?>
The keys of the array become parameters in the called partial. So in the partial you can print a variable like so:
<?= $param1 ?>
When wanting to fill the 2nd partial with data, starting at the controller, you should pass the data you want to the first partial and from there fill the second.
Another option, the more difficult way, is to pre-render the partials in the Controller and then return the rendered whole from the Controller function.
Personally, I'm opposed to this as you would then not be separating concerns (viewing vs handling).
But of course, it's possible ;)
Full example from Controller into 2nd partial
class AwesomeController
{
public function demoAction()
{
// do stuff to create the values
return [
'key1' => 'value1',
'key2' => 'value2',
'fooKey1' => 'barValue1',
'fooKey2' => 'barValue2',
];
}
}
So now there's 4 values. These get passed to demo.phtml. However, the last 2 values are for the sub-partial, so we must call that. We even loop that, because we want to show them more than once!
<div class="row">
<div class="col-8 offset-2">
<table class="table table-striped table-bordered">
<tr>
<th><?= $this->translate('Key 1') ?></th>
<td><?= $this->escapeHtml($key1) // <-- Look, the key from Controller is now a variable! This is ZF magic :) ?></td>
</tr>
<tr>
<th><?= $this->translate('Key 2') ?></th>
<td><?= $this->escapeHtml($key2) ?></td>
</tr>
<?php for ($i = 0; $i < 3; $i++) : ?>
<?= $this->partial(
'custom/partial',
[
'value1' => $fooKey1, // <-- Look, the key from Controller is now a variable! This is ZF magic :)
'value2' => $fooKey2,
]
) ?>
<?php endfor ?>
</table>
</div>
</div>
Now, the above bit calls for the partial with name custom/partial. This must be registered. Example config below (place in module.config.php from your module):
'view_manager' => [
'template_map' => [
'custom/partial' => __DIR__ . '/../view/partials/demo-child.phtml',
// -- ^^ name -- ^^ location
],
'template_path_stack' => [
__DIR__ . '/../view',
],
],
File demo-child.phtml
<tr>
<th><?= $this->translate('Value 1') ?></th>
<td><?= $this->escapeHtml($value1) // <-- Look, from above partial - ZF magic :) ?></td>
</tr>
<tr>
<th><?= $this->translate('Value 2') ?></th>
<td><?= $this->escapeHtml($value2) ?></td>
</tr>

Related

How do i loop to display post category title?

Please, pardon me for unending questions. I am just a newbie as far as wordpress theme development is concerned. I am just cracking my brain to get something done. I have successfully worked on my code and it gives one category title. The category title of the post is not appropriate. I beg someone out there to help look into my code below and help me correct where necessary. I am just tired over this issue.
I want it to display appropriate category title above the post as you can see about. The category title is outside the loop and not within. It is equally not in category page rather at the front page.
`<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 6,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'building',
)
)
);
$q = new WP_Query( $args );
$leadingcount = 3;
$cats = get_terms( array( 'taxonomy' => 'product_cat' ) );
// loop through the categries
foreach ($cats as $cat) {
// setup the cateogory ID
$cat_id= $cat->term_id;
// Make a header for the cateogry
echo "<h6 class='mt-5 text-center'>".$cat->name."</h6>"; ?>
<?php if (have_posts()) : while ( $q->have_posts() ) : $q->the_post(); ?>
<article class="item col-lg-<?php echo $leadingcount; ?>">
<div class="card rounded-0">
<div class="card-body">
<a href="<?php the_permalink(); ?>">
<h5><?php the_title(); ?></h5>
</a>
<?php the_excerpt(); ?>
<?php the_category(); ?>
</div>
</div>
</article>
<?php endwhile; endif; // done our wordpress loop. Will start again for each category ?>
<?php } // done the foreach statement ?>
`

how to show a form in a grid view

I'm trying a bit around with grid views. I show different contents so the user can have something like a dashboard.
Please have a look at the screenshot:
The notizen area should be a search form.
Here part of my controller action:
$view = new ViewModel();
$wiedervorlageView = new ViewModel(['wvs' => $this->wvTable->fetchAll()]);
$wiedervorlageView->setTemplate('layout/template/wiedervorlage');
$notizenView = new ViewModel(['notizen' => $this->notizenTable->fetchAll()]);
$notizenView->setTemplate('layout/template/notizen');
$geburtstageview = new ViewModel();
$geburtstageview->setTemplate('layout/template/geburtstageview');
$sidebarBlockView = new ViewModel(['aps' => $this->ansprechpartnerTable->getGeburtstage()]);
$sidebarBlockView->setTemplate('layout/template/block');
$geburtstageview->addChild($sidebarBlockView, 'block');
$view->addChild($wiedervorlageView, 'wiedervorlage')
->addChild($notizenView, 'notizen')
->addChild($geburtstageview, 'geburtstage');
return $view;
And here the search viewscript:
<?php
$title = 'Suche';
$this->headTitle($title);
?>
<h1><?= $this->escapeHtml($title) ?></h1>
<?php
$suche= $form->get('suche');
$suche->setAttribute('class', 'form-control');
$suche->setAttribute('placeholder', 'suche');
$suchtyp= $form->get('suchtyp');
$suchtyp->setAttribute('class', 'form-control');
$suchtyp->setAttribute('placeholder', 'suchtyp');
$submit = $form->get('submit');
$submit->setAttribute('class', 'btn btn-primary');
$form->prepare();
echo $this->form()->openTag($form);
?>
<div class="form-group">
<?= $this->formLabel($suche) ?>
<?= $this->formElement($suche) ?>
<?= $this->formElementErrors()->render($suche, ['class' => 'help-block']) ?>
</div>
<div class="form-group">
<?= $this->formLabel($suchtyp) ?>
<?= $this->formElement($suchtyp) ?>
<?= $this->formElementErrors()->render($suchtyp, ['class' => 'help-block']) ?>
</div>
<?php
echo $this->formSubmit($submit);
echo $this->form()->closeTag();
My question is how to change the notizen area so that it works with the form?
I tried different possibilities but I came to the conclusion that I didn't understood the logic in this case.
Any help or explanation appreciated.
Solution:
ok I changed to partials, before I wanted to try with the described concept to see the difference to the partials. I worked before with partials,my challenge here was really only the form, now it is clear hopefully. But either way, the concept with the partials is much more convenient and readable.
So, I changed the viewscripts like suggested, here for completion the one with the form:
<div class="row">
<h2> Suche </h2>
<?php
$suche= $form->get('suche');
$suche->setAttribute('class', 'form-control');
$suche->setAttribute('placeholder', 'suche');
$suchtyp= $form->get('suchtyp');
$suchtyp->setAttribute('class', 'form-control');
$suchtyp->setAttribute('placeholder', 'suchtyp');
$submit = $form->get('submit');
$submit->setAttribute('class', 'btn btn-primary');
$form->prepare();
echo $this->form()->openTag($form);
?>
<div class="form-group">
<?= $this->formRow($suche) ?>
<?= $this->formElementErrors()->render($suche, ['class' => 'help-block']) ?>
<?= $this->formRow($suchtyp) ?>
<?= $this->formElementErrors()->render($suchtyp, ['class' => 'help-block']) ?>
<?php
echo $this->formSubmit($submit);
echo $this->form()->closeTag(); ?>
</div>
</div>
As I posted this I thought I would need to redirect to the controller Action with the search script. Now I copied it to the same controller action. So it works and it is ok for me.
Here part of the changed controller action for anybody who has the same understanding issue:
$form = new SearchForm(NULL);
$form->get('submit')->setValue('suche');
return new ViewModel([
'wvs' => $this->wvTable->fetchAll(),
'form' => $form,
'aps' => $this->ansprechpartnerTable->getGeburtstage()
]);
You should really learn to create partials. What you're doing now is mixing concerns.
What you're doing in your controller will give you a giant mess if/when you have to refactor.
With partials you would do something like this.
File: notizen-partial.phtml
<?php
/** #var array $notizen */
?>
<table>
<tr>
<th>datum</th>
<th>text</th>
</tr>
<tr>
<td><?= $notizen['datum'] ?></td>
<td><?= $notizen['text'] ?></td>
</tr>
</table>
So, the above expects a variable $notizen, of type array (hinted in the PHP at the top). You could add some validation that the array is actually present, contains the values, etc. but that's up to you.
Now, register this partial in the configuration of the module, with the following config:
'view_manager' => [
'template_map' => [
'partial/notizen-partial' => __DIR__ . '/../view/partials/notizen-partial.phtml',
],
],
Make sure you correct the path for your situation!!!
Repeat the above for each little "container" of your data handling (wvs, aps, etc.).
Next, have an Action return the data you need to the View:
// ... create/get data
return [
'wvs' => $this->wvTable->fetchAll(),
'notizen' => $this->notizenTable->fetchAll(),
'aps' => $this->ansprechpartnerTable->getGeburtstage(),
];
This goes to "action-view.phtml", or whatever. This file handles the "place data where it needs to be".
The layout of the view, based on your screenshot, would be something like this (I'm going under the assumption your using Bootstrap 4.*):
<div class="row">
<div class="col-12">
<!-- That top row - full width -->
<!-- ## notizen here ## -->
</div>
</div>
<div class="row">
<div class="col-8">
<!-- second row - two thirds of width -->
<!-- Wiedervorlagen here -->
</div>
<div class="col-4">
<!-- second row, second column - one thirds of width -->
<!-- Geburtstage here -->
</div>
</div>
Now, you need to use the additional partials to display the data in the right locations. For example, your 'notizen' data, is now $notizen variable in this view. However, we're not using it in this view, so lets pass it on to the partial we created earlier, like so:
<?= $this->partial('partial/notizen-partial, ['notizen' => $notizen]) ?>
A lot happening here:
Calling the partial ViewHelper (look in the folder of this file to see other ViewHelpers already available!)
Passing it the name of the partial, which we set in the configuration earlier
Passing key/value pairs. The keys will be variable names, the values will be their values (you can add as many as you like/need)
Place this call in your action view, like so:
<div class="row">
<div class="col-12">
<!-- That top row - full width -->
<?= $this->partial('partial/notizen-partial, ['notizen' => $notizen]) ?>
</div>
</div>
And you're done.
You now have:
Single partials to display collections of data
Using ViewHelpers, so you can recycle the same view in different locations throughout your application
Separated concerns of your code (kept displaying in phtml template files, kept data collection / handling in Controller, kept logic in Repositories/Models/Entities)
I've studiously ignored your pasted Form. The principle for that is the same, you could pass the $form in to a partial and have that partial contain the displaying of the form. Creating something re-usable.
If you'd like to know more about customizing Form ViewHelpers (e.g. FormRow, FormElement, FormNumber, etc.), please see answers I've given others, such as this one.

Dynamic form input fields in Cakephp 3

I have seen this: https://waltherlalk.com/blog/dynamic-form-input-fields and have been active in this: Dynamically add form field rows - cakePHP. I have reached the stage where the setup is as per the original tutorial with changes made as per the Stackoverflow post from monsur.hoq.
The form is working fine but, upon saving, it only saves the 'student' part of the data: nothing is sent to grades. The add part of my controller currently looks like this:
public function add()
{
$student = $this->Students->newEntity();
if ($this->request->is('post')) {
$student = $this->Students->patchEntity($student, $this->request->data);
if ($this->Students->save($student)) {
$this->Flash->success(__('The student has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The student could not be saved. Please, try again.'));
}
}
$this->set(compact('student'));
$this->set('_serialize', ['student']);
}
All code is as per bake or the tutorial shaped by the monsur.hoq post.
If anyone could help me to arrive at a working Cakephp3 example of the Walther Lalk tutorial I'd be very grateful.
The debugging toolbar shows the following SQL being produced on submitting the form:
INSERT INTO students (name, created, modified)
VALUES
(
'Test Two', '2016-09-13 16:04:07',
'2016-09-13 16:04:07'
)
All that serves to do is confirm the problem. Debugging in PHP Storm on form submission reveals the following:
$_POST = {array} [3]
_method = "POST"
name = "Test Four"
Grade = {array} [1]
0 = {array} [3]
id = ""
subject = "Maths"
grade = "3"
The add.ctp is as follows:
<nav class="large-3 medium-4 columns" id="actions-sidebar">
<ul class="side-nav">
<li class="heading"><?= __('Actions') ?></li>
<li><?= $this->Html->link(__('List Students'), ['action' => 'index']) ?></li>
<li><?= $this->Html->link(__('List Grades'), ['controller' => 'Grades', 'action' => 'index']) ?></li>
<li><?= $this->Html->link(__('New Grade'), ['controller' => 'Grades', 'action' => 'add']) ?></li>
</ul>
</nav>
<div class="students form large-9 medium-8 columns content">
<?= $this->Form->create($student) ?>
<fieldset>
<legend><?= __('Add Student') ?></legend>
<?php
echo $this->Form->input('name');
?>
</fieldset>
<fieldset>
<legend><?php echo __('Grades');?></legend>
<table id="grade-table">
<thead>
<tr>
<th>Subject</th>
<th>Grade achieved</th>
<th> </th>
</tr>
</thead>
<tbody></tbody>
<tfoot>
<tr>
<td colspan="2"></td>
<td class="actions">
Add grade
</td>
</tr>
</tfoot>
</table>
</fieldset>
<script id="grade-template" type="text/x-underscore-template">
<?php echo $this->element('grades');?>
</script>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
<script>
$(document).ready(function() {
//I changed undescore default template settings
_.templateSettings = {
interpolate: /\{\{(.+?)\}\}/g
}
var
gradeTable = $('#grade-table'),
gradeBody = gradeTable.find('tbody'),
gradeTemplate = _.template($('#grade-template').remove().text()),
numberRows = gradeTable.find('tbody > tr').length;
gradeTable
.on('click', 'a.add', function(e) {
e.preventDefault();
$(gradeTemplate({key: numberRows++}))
.hide()
.appendTo(gradeBody)
.fadeIn('fast');
})
.on('click', 'a.remove', function(e) {
e.preventDefault();
$(this)
.closest('tr')
.fadeOut('fast', function() {
$(this).remove();
});
});
if (numberRows === 0) {
gradeTable.find('a.add').click();
}
});
</script>
Change from CakePHP 2 to CakePHP 3 fields name conventions,
Grade.{$key}.grade to grades.{$key}.grade
Create View/Elements/grades.ctp file with the following contents.
https://waltherlalk.com/blog/dynamic-form-input-fields
<?php
$key = isset($key) ? $key : '<%= key %>';
?>
<tr>
<td>
<?= $this->Form->hidden('grades.{$key}.id') ?>
<?= $this->Form->text('grades.{$key}.subject'); ?>
</td>
<td>
<?= $this->Form->select("grades.{$key}.grade",
[
'A+',
'A',
'B+',
'B',
'C+',
'C',
'D',
'E',
'F'
],
[
'empty' => '-- Select grade --'
]); ?>
</td>
<td class="actions">
Remove grade
</td>
</tr>

Disable or change validation for embedded form fields in Symfony1 form

I am trying to cancel validation in embedded forms based on a value from main form.
By default, embedded forms fields have validator option set to 'required'=>true. So it gets validated like that. If user leave any field blank, the form does not pass validation and blank fields get marked in template (different style).
What I am trying to do is to change option:"required" to false for all fields in embedded form.
I tried to do that in post validator callback method, but it seems that it is not possible that way.
The main form code:
class TestForma extends sfForm
{
public function configure()
{
$this->setWidgets(array(
'validate_items' => new sfWidgetFormChoice(array(
'choices' => array('no' => 'No', 'yes' => 'Yes'),
'multiple' => false,'expanded'=>true,'default' => 'no')),
));
$this->setValidators(array('validate_items' => new sfValidatorPass()));
$this->widgetSchema->setNameFormat('testforma[%s]');
$subForm = new sfForm();
for ($i = 0; $i < 2; $i++)
{
$form = new ItemForma();
$subForm->embedForm($i, $form);
}
$this->embedForm('items', $subForm);
$this->validatorSchema->setPostValidator(
new sfValidatorCallback(array('callback' => array($this, 'postValidate')))
);
}
Post-validator code:
public function postValidate($validator,$values)
{
$validatorSchema = $this->getValidatorSchema();
if($values['validate_items']=='no')
{
$itemsValidatorSchema = $validatorSchema['items'];
$itemsFieldsValidatorSchemes = $itemsValidatorSchema->getFields();
foreach($itemsFieldsValidatorSchemes as $itemValidatorScheme)
{
$itemValidatorScheme['color']->setOption('required',false);
$itemValidatorScheme['shape']->setOption('required',false);
}
}
return $values;
}
Embedded form class:
class ItemForma extends sfForm
{
public function configure()
{
$this->setWidgets(array(
'color' => new sfWidgetFormInputText(),
'shape' => new sfWidgetFormInput(),
));
$this->setValidators(array(
'color' => new sfValidatorString(array('required'=>true)),
'shape' => new sfValidatorEmail(array('required'=>true)),
));
$this->widgetSchema->setNameFormat('items[%s]');
}
}
Template code:
<form action="<?php echo url_for('weather/formiranje')?>" method="post">
<?php
foreach($form->getErrorSchema()->getErrors() as $e)
{
echo $e->__toString();
}
?>
<table>
<tfoot>
<tr>
<td colspan="2">
<input type="submit" value="OK" />
</td>
</tr>
</tfoot>
<tbody>
<tr><th>Main form</th></tr>
<tr><td><?php echo $form['validate_items']->renderLabel() ?>
<span class="<?php echo $form['validate_items']->hasError() ? 'rowError' : ''?>">
<?php echo $form['validate_items'] ?></span>
</td></tr>
<tr><td> </td></tr>
<tr><th>Embedded forms</th></tr>
<?php
foreach($form['items'] as $item)
{
?>
<tr>
<td><span class="<?php echo $item['color']->hasError() ? 'rowError' : ''?>">
<?php echo $item['color']->renderLabel() ?>
<?php echo $item['color'] ?></span>
</td>
</tr>
<tr>
<td><span class="<?php echo $item['shape']->hasError() ? 'rowError' : ''?>">
<?php echo $item['shape']->renderLabel() ?>
<?php echo $item['shape'] ?></span>
</td></tr>
<?php
}
echo $form['_csrf_token'];
?>
</tbody>
</table>
</form>
The way you organised it won't work because the post validator is run after all the field validators, so they've already been checked and marked as failed. (because the fields were required).
You could try the same approach you have here but with setting a preValidator instead of a postValidator. I think it should work then.
If it still won't work as expected what I would do is to change the default settings on the embedded form's fields to 'required' = false and use the postValidator. In the validator you could check whether or not you need to validate the embedded fields. If you need to validate them you can check if their values are set and if not you can throw errors for those fields. (I hope this is explained clearly)
Another thing you could try is to re-run the validation for the chosen fields. So something like that in your postValidator:
$itemValidatorScheme['color']->setOption('required',false);
$itemValidatorScheme['color']->clean($values['name_of_the_field']);

ZendFramework echo db values if first string = a?

I have this code at the moment to echo out all my database entries, I am wondering what it looks like in ZF to echo out the entries if the first value of the db entry is a.
Code:
<table>
<?php foreach($this->clubs as $clubs) : ?>
<tr>
<td><a href="<?php echo $this->url(array('controller' => 'club-description', 'action' => 'index', 'club_id' => $clubs->id));?>">
<?php echo $this->escape($clubs->club_name);?></a></td>
<td><?php echo $this->escape($clubs->rating);?></td>
</tr>
<?php endforeach; ?>
</table>
Thanks
Rik
So, based on your comment it sounds like you want to group the clubs by letter on the page. Assuming they are being ordered alphabetically in your database query, the simplest way to do this is to keep a variable which stores the first letter of the last club in the loop. Then, on each iteration, you compare the first letter of the current club with the first letter of the previous club. If they are different, you output a new heading.
With your code this would look something like this:
<?php
$previousLetter = false;
?>
<table>
<?php foreach($this->clubs as $clubs) : ?>
<?php
$firstLetter = substr($clubs->_club_name, 0, 1);
if ($firstLetter != $previousLetter) {
?>
<tr>
<td><?php echo $firstLetter; ?></td>
</tr>
<?php } ?>
<tr>
<td><a href="<?php echo $this->url(array('controller' => 'club-description', 'action' => 'index', 'club_id' => $clubs->id));?>">
<?php echo $this->escape($clubs->club_name);?></a></td>
<td><?php echo $this->escape($clubs->rating);?></td>
</tr>
<?php $previousLetter = $firstLetter; ?>
<?php endforeach; ?>
</table>