get date in a controller in cakephp - date

I have a date form with datapicker and I want to send these dates to a controller to place these dates in a query, my problem is that I do not know how to capture them to insert them in the query, in the view I have:
<?php $this->end(); ?>
<?=$this->Form->create(false, array('type' => 'get', 'controller' => 'informes', 'action' => 'crm')); ?>
<div class="row">
<div class="col-sm-2">Fecha del Informe</div>
<div class="col-sm-4">
<div class="input-daterange input-group" id="datepicker">
<span class="input-group-addon">Desde</span>
<input type="text" class="input-sm form-control" name="start" id="start"/>
<span class="input-group-addon">hasta</span>
<input type="text" class="input-sm form-control" name="end" id="end"/>
</div>
</div>
<div class="col-sm-2">
</div>
<div class="col-sm-2"><button class="btn btn-xs btn-primary" type="submit">Generar Informe</button></div>
</div>
<?=$this->Form->end(); ?>
and in the controller :
// Loading different models
$this->loadModel('Centro');
$this->loadModel('Items');
$this->loadModel('Sesion');
$this->loadModel('Zona');
$this->loadModel('TratamientosZona');
$this->loadModel('tratamiento');
$this->loadModel('Paciente');
// My query with CakePHP ORM
$sesion_pagada = $this->Centro->find('all', array(
array('fields' => 'Centro.nombre', SUM(IF('Paciente.sexo' = 'F', 'Zona.valor_mujer', 'Zona.valor_hombre')) AS suma
'joins' => array(
array('alias' => 'Sesion', 'table' => 'sesions', 'type' => 'INNER',
'conditions' => 'Centro.id = Sesion.centro_id'),
array('alias' => 'Items', 'table' => 'items', 'type' => 'LEFT',
'conditions' => 'Sesion.id = Item.sesion_id'),
array('alias'), => 'Zona', 'table' => 'zonas', 'type' => 'INNER',
'conditions' => 'Item.zona_id = Zona.id',
array('alias' => 'TratamientosZona', 'table' => 'tratamientos_zonas', 'type' => 'INNER',
'conditions' => 'Zona.id = TratamientosZona.zona_id'),
array('alias' => 'Tratamiento', 'table' => 'tratamientos', 'type' => 'INNER',
'conditions' => 'Tratamiento_zona.tratamiento_id = Tratamiento.id'),
array('alias' => 'Paciente', 'table' => 'pacientes', 'type' => 'INNER',
'conditions' => 'Tratamiento.paciente_id = Paciente.id')
),
'conditions' => array(
'Sesion.estado_id =' => 5,
'Sesion.fecha BETWEEN ? and ?'=> array( $query['start'], date('Y-m-d'))
)
)));

You can use this code to get data from GET request:
// For cakePHP 2.x
$start = $this->request->query['start'];
$end = $this->request->query['end'];
// For cakePHP 3.x >= 3.4.0
$start = $this->request->getQuery('start');
$end = $this->request->getQuery('end');
// cakePHP 3.x <= 3.4.0
$start = $this->request->query('start');
$end = $this->request->query('end');

Related

In Cakephp 3.6 How do I get the size and type of an image to send it by form?

How do I get the size and type of an image to send it by form?
Well that, I want to save in the table of images the size and type of an image that is uploaded through a form. With Ajax, I can recover those data, but to pass them to PHP I can only do when sending the form, also directly with PHP:
if ($this->request->is('post')) {
$isData = $this->request->getdata();
$imagene->imagen = $isData['image_path'];
$imagene->tipo = $isData['type']
$imagene->tamano = $isData['size'];
...
But I want to do it before sending the form, which is when the insertion is done in the database.
Form:
<?= $this->Form->create($imagene, ['novalidate', 'id' => 'addimageform', 'class' => 'form addimageform']); ?>
<?= $this->Form->control('imagen', ['type' => 'file', 'class' => 'imagen-addimage']); ?>
<?= $this->Form->hidden('tipo', ['value' => $tipo, 'class' => 'tipo-addimage']); ?>
<?= $this->Form->hidden('$tipo', ['value' => $size, 'class' => 'tamano-addimage']); ?>
<?= $this->Form->button('Subir imagen', ['id' => 'submit', 'class' => 'submit-addimage']); ?>
<?= $this->Form->button('Omitir', ['id' => 'omitir', 'class' => 'omitir-addimage', 'redirect' => ['controller' => 'administracion', 'action' => 'index']]); ?>
<?= $this->Form->end(); ?>
Now I see that if I do a debug of $isData, the field: "imagen" does not appear:
'tabla' => 'users',
'id_tabla' => '22',
'tipo' => '',
'tamano' => ''
UPDATING
I've changed things in the form and the controller:
form:
<?= $this->Form->create($imagene, ['enctype' => 'multipart/form-data', 'novalidate', 'id' => 'addimageform', 'class' => 'form addimageform']); ?>
<?= $this->Form->control('imagen', ['type' => 'file', 'class' => 'imagen-addimage']); ?>
<div class="centrar-submit">
<?= $this->Form->button('Subir imagen', ['id' => 'submit', 'class' => 'submit-addimage']); ?>
<?= $this->Form->button('Omitir', ['id' => 'omitir', 'class' => 'omitir-addimage', 'redirect' => ['controller' => 'administracion', 'action' => 'index']]); ?>
</div>
<?= $this->Form->end(); ?>
Controller:
public function add($table, $idTable) {
$imagene = $this->Imagenes->newEntity();
if ($this->request->is('post')) {
$isData = $this->request->getdata();
debug($this->request->getData('imagen')); // <---- Is null
debug($isData); // <---- Is empty
...
Why? I don't know.
You should mentioned what type of form you used. That is file type you should use
echo $this->Form->create($article, ['type' => 'file']);
Hope this help you.

CakePHP saveMany doesnt save

I have the following Form Code.
<?php echo $this
->Form
->create(
'PagePhoto',
array(
'type' => 'file',
'url' => array(
'controller' => 'page_photos',
'action' => 'add'
)
)
); ?>
<div class="modal-body has-padding">
<div class="form-group">
<?php echo $this->Form->label('PagePhoto.0.filename', 'Photos:'); ?>
<br/><br/>
<?php echo $this->Form->file('PagePhoto.0.filename', array('required' => false)); ?>
<?php echo $this->Form->error('PagePhoto.0.filename', null, array('class' => 'label label-block label-danger text-left', 'wrap' => 'label')); ?>
<br/>
<?php echo $this->Form->file('PagePhoto.1.filename', array('required' => false)); ?>
<?php echo $this->Form->error('PagePhoto.1.filename', null, array('class' => 'label label-block label-danger text-left', 'wrap' => 'label')); ?>
<br/>
<?php echo $this->Form->file('PagePhoto.2.filename', array('required' => false)); ?>
<?php echo $this->Form->error('PagePhoto.2.filename', null, array('class' => 'label label-block label-danger text-left', 'wrap' => 'label')); ?>
<br/>
<?php echo $this->Form->file('PagePhoto.3.filename', array('required' => false)); ?>
<?php echo $this->Form->error('PagePhoto.3.filename', null, array('class' => 'label label-block label-danger text-left', 'wrap' => 'label')); ?>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-warning" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Upload Photos</button>
</div>
Which returns the following array to the add action
array(
'PagePhoto' => array(
(int) 0 => array(
'filename' => array(
'name' => 'IMG_1683.jpg',
'type' => 'image/jpeg',
'tmp_name' => '/Applications/MAMP/tmp/php/phpoIrbZ6',
'error' => (int) 0,
'size' => (int) 94131
)
),
(int) 1 => array(
'filename' => array(
'name' => 'IMG_1683.jpg',
'type' => 'image/jpeg',
'tmp_name' => '/Applications/MAMP/tmp/php/phpAcbXbC',
'error' => (int) 0,
'size' => (int) 94131
)
),
(int) 2 => array(
'filename' => array(
'name' => 'IMG_1683.jpg',
'type' => 'image/jpeg',
'tmp_name' => '/Applications/MAMP/tmp/php/phppCeN8G',
'error' => (int) 0,
'size' => (int) 94131
)
),
(int) 3 => array(
'filename' => array(
'name' => 'IMG_1683.jpg',
'type' => 'image/jpeg',
'tmp_name' => '/Applications/MAMP/tmp/php/php8Ib3bO',
'error' => (int) 0,
'size' => (int) 94131
)
)
)
)
Add Action
public function admin_add() {
if ($this->request->is('post')) {
debug($this->request->data);
$this->PagePhoto->create();
if ($this->PagePhoto->saveMany($this->request->data)) {
$this->Session->setFlash(__('The gallery has been saved.'), 'admin/flash_success');
// return $this->redirect(array('action' => 'view', $this->request->data['Gallery']['album_id']));
} else {
$this->Session->setFlash(__('The gallery could not be saved. Please, try again.'), 'admin/flash_error');
}
} else {
// $this->request->data['Gallery']['album_id'] = $album_id;
}
}
Error:
FAILURE: The gallery could not be saved. Please, try again.
$this->PagePhoto->saveMany($this->request->data['PagePhoto']);
Note: When saving multiple records of same model the records arrays should be just numerically indexed without the model key.

Zend Framework 2 formInput or formElement ID tag not rendering

In Zend framework 2, when I use the view's formRow method like so
$this->formRow($form->get('product_name'));
it will generate HTML like this
<label for="product_name">
<span>Name</span>
<input type="text" id="product_name" name="product_name">
</label>
but if I use formInput
<div class="control-group">
<?php echo $this->formLabel($form->get('product_name')->setLabelAttributes(array('class'=>'control-label'))); ?>
<div class="controls">
<?php echo $this->formInput($form->get('product_name')); ?>
</div>
</div>
$this->formInput($form->get('product_name'));
i don't get the id tag
<input type="" name="product_name">
I've tried with formElement with same results.
How can I get it to render just the input with all attributes and values?
This is how my Zend Framework 2 View looks like (simplified)
<?php echo $this->form()->openTag($form); ?>
<div class="control-group">
<?php echo $this->formLabel($form->get('product_name')->setLabelAttributes(array('class'=>'control-label'))); ?>
<div class="controls"><?php echo $this->formInput($form->get('product_name')); ?></div>
</div>
<div class="control-group">
<div class="controls"><?php echo $this->formSubmit($form->get('submit')); ?></div>
</div>
<?php echo $this->form()->closeTag(); ?>
and the Zend Framework 2 Form
<?php
namespace Product\Form;
use Zend\Form\Form;
class ProductForm extends Form
{
public function __construct($name = null)
{
// we want to ignore the name passed
parent::__construct('product');
$this->setAttribute('method', 'post');
$this->setAttribute('class','form-horizontal');
$this->add(array(
'name' => 'product_name',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Name',
),
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Save',
'id' => 'submitbutton',
'class'=>'btn btn-success btn-large'
),
));
}
}
?>
Change:
$this->add(array(
'name' => 'product_name',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Name',
),
));
to:
$this->add(array(
'name' => 'product_name',
'attributes' => array(
'type' => 'text',
'id' => 'product_name',
),
'options' => array(
'label' => 'Name',
),
));
Actually this code:
$this->add(array(
'type' => 'Zend\Form\Element\Text',
'name' => 'product_name',
'attributes' => array(
'id' => 'product_name',
'class' => 'span3',
),
'options' => array(
'label' => 'Your label',
),
));
could be used correctly by a css renderer like Bootstrap due to fact that the $this->formRow(...) form helper will produce:
<label for="product_name">Your label</label><input type="text" name="product_name" class="span3" id="product_name" value="">
which is more readable than the original formRow output (without id neither class attributes)

How can I use zend form decorator to render errors inside my paragraph tag wrapping label and input

I would like to render the following markup:
<div class="row">
<p>
<label>Your Name</label>
<input type="text" class="text_field" name="name">
<ul class="errors">
<li>Waarde is vereist en kan niet leeg worden gelaten</li>
</ul>
</p>
</div>
This is my Zend form element + decorator:
$this->addElement('text', 'name', array(
'label' => 'Naam:',
'class' => 'text_field',
'required' => true,
'decorators' => array(
'ViewHelper',
'Label',
'Errors',
array(array('row' => 'HtmlTag'), array('tag' => 'p')),
array(array('content' => 'HtmlTag'), array('tag' => 'div', 'class' => 'row'))
)));
But this always renders the ul list below the p tag and never inside. It also adds an additional p tag below the list.
<div class="row">
<p>
<label class="required" for="name">Naam:</label>
<input type="text" class="text_field" value="" id="name" name="name">
</p>
<ul class="errors">
<li>Waarde is vereist en kan niet leeg worden gelaten</li>
</ul>
<p></p>
</div>
What am I doing wrong?
Found it! My stupid mistake. I did only check the final rendered output in my browser. I am using a template which also loads javascript and this changes the DOM which creates the unwanted result.
So the first decorator setup was working correct.
Try to do the following:
$this->addElement('text', 'name', array(
'label' => 'Naam:',
'class' => 'text_field',
'required' => true,
'decorators' => array(
'ViewHelper',
'Label',
'Errors',
array(array('content' => 'HtmlTag'), array('tag' => 'p')),
array(array('content' => 'HtmlTag'), array('tag' => 'div', 'class' => 'row'))
)));

using zend form decorators

<div class="field50Pct">
<div class="fieldItemLabel">
<label for='First Name'>First Name:</label>
</div>
<div class="fieldItemValue">
<input type="text" id="firstname" name="firstname" value="" />
</div>
</div>
<div class="clear"></div>
I want the code to appear like this in source code . how do i write the same thing in zend using decorators ?
The element is like
$firstname = new Zend_Form_Element_Text('FirstName');
$firstname->setLabel('FirstName')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addErrorMessage('Error in First Name')
->addValidator('NotEmpty');
This seems to work for me:
(with <div class="clear"></div> after the input)
$firstname->setDecorators(array(
'ViewHelper',
'Description',
'Errors',
array('HtmlTag', array('tag' => 'div', 'class' => 'fieldItemValue')),
array(array('labelDivOpen' => 'HtmlTag'),
array('tag' => 'div',
'placement' => 'prepend',
'closeOnly' => true)),
'Label',
array(array('labelDivClose' => 'HtmlTag'),
array('tag' => 'div',
'class' => 'fieldItemLabel',
'placement' => 'prepend',
'openOnly' => true)),
array(array('fieldDiv' => 'HtmlTag'),
array('tag' => 'div', 'class' => 'field50Pct')),
array(array('divClear' => 'HtmlTag') ,
array('tag' => 'div' ,
'class' => 'clear',
'placement' => 'append'))
));