cakephp value isnt sent to action - php

I have the following action:
public function admin_website_status($id = null){
$i = 0;
$this->Paginator->settings = array(
'Website' => array(
)
);
$this->set('websites',$this->Paginator->paginate('Website'));
}
Now i want to send a value to this action so in my view i have the following link:
<?php echo $this->Html->link(__('Active'), array('action' => 'website_status', $website['Website']['website_id'])); ?>
When i debug this no value is sent even though i know that the value should be 1
So what am i doing wrong?
My full view
<div class="portlet box red">
<div class="portlet-title">
<div class="caption"><i class="icon-globe"></i>Website Status</div>
</div>
<div class="portlet-body">
<table class="table table-striped table-bordered table-hover table-full-width" id="sample_1">
<thead>
<tr>
<th><?php echo $this->Paginator->sort('Website.domain', 'Website domain');?></th>
<th><?php echo $this->Paginator->sort('Website.client_id', 'Affiliate ID');?></th>
<th><?php echo $this->Paginator->sort('User.username', 'E-mail Address');?></th>
<th><?php echo $this->Paginator->sort('Status.status', 'Status'); ?> </th>
<th class="actions"><?php echo __('Actions'); ?></th>
</tr>
</thead>
<?php foreach ($websites as $site): ?>
<tr>
<td><?php echo $site['Website']['domain']; ?></td>
<td><?php echo $site['Client']['client_id'];?></td>
<td><?php echo $site['Client']['user_id'];?></td>
<td><?php echo $site['Status']['Status']; ?></td>
<td class="actions">
<?php echo $this->Html->link(__('Active'), array('action' => 'website_status', $website['Website']['website_id'])); ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<div class="paging">
<?php
echo $this->Paginator->prev('« Previous', null, null, array('class' => 'disabled'));
echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled'));
?>
</div>
</div>

$websites['Website']['website_id']
and not
$website['Website']['website_id']
(plural and not singular)
edit: after seeing your whole code
$site['Website']['website_id']

In your $this->Html->link you set 'action' => 'website_status'
Is it supposed to be admin_website_status ?
Also make sure $website['Website']['website_id'] has a value. Maybe it is empty.

Related

Cakephp 3.6.14: Create and submit form with table and checkbox for each row (2nd form - same page)

I want to implement 2 different forms in a page for 2 different actions in the same controller. I am trying to create the second form where the user can check a checkbox for each row of a table. On submit of the form I want to execute some code in the add action of the controller.
This is my AddElement\index.ctp :
<div class="row"> //First form is working fine
<?php echo $this->Form->create('AddElement', array('action' => 'index')); ?>
<?php echo $this->Form->control('element_categories', ['options' => $elementCategories, 'empty' => true]); ?>
<?php echo $this->Form->button(__('Filter'), ['class'=>'btn-success']); ?>
<?php echo $this->Form->end(); ?>
</div>
<h3><?= __('Task Elements') ?></h3>//2nd form not working as expected
<?php echo $this->Form->create('AddElement', array('action' => 'add')); ?>
<?php foreach ($taskElements as $taskElement): ?>
<table cellpadding="0" cellspacing="0">
<thead>
<tr>
<th scope="col"><?= $this->Paginator->sort('id') ?></th>
<th scope="col"><?= $this->Paginator->sort('name') ?></th>
<th scope="col"><?= $this->Paginator->sort('element_category_id') ?></th>
<th scope="col">Select</th>
</tr>
</thead>
<tbody>
<tr>
<td><?= $this->Number->format($taskElement->id) ?></td>
<td><?= h($taskElement->name) ?></td>
<td><?= $taskElement->element_category_id != 0 ? $this->Html->link($taskElement->element_category->name, ['controller' => 'ElementCategories', 'action' => 'view', $taskElement->element_category->id]) : '' ?></td>
<td><?= $this->Form->control('selected', ['type' => 'checkbox']);?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table><?php
echo $this->Form->button('Add');
echo $this->Form->end();?>
And this is the add action of the AddElementController.php:
public function add($tasktypeid)
{
$this->autoRender = false; //no need for view
debug($this->request->data());
if($this->request->is('post'))
{
debug($this->request->data());
}
}
But when I click submit it does nothing. Its just loading for a second but it doesn't execute the code in the controller.

How to get total number of records retrieved with pagination in CakePHP 3?

I have a problem in CakePHP 3 to have pagination correctly.
How to get the total number of records retrieved with pagination in CakePHP 3?
This might help you.
In your controller i.e UsersController.php make pagination like this:
public function index()
{
//set your pagination limit 10
$this->paginate = [
'limit' => 10
];
//query all users
$users = $this->paginate($this->Users->find('all'));
//pass to template
$this->set(compact('users'));
}
In you Template View i.e users/index.ctp put this code to display your pagination
<div class="users index large-9 medium-8 columns content">
<h3><?= __('Users') ?></h3>
<table cellpadding="0" cellspacing="0" border="1">
<thead>
<tr>
<th scope="col"><?= $this->Paginator->sort('id') ?></th>
<th scope="col"><?= $this->Paginator->sort('fullname') ?></th>
<th scope="col"><?= $this->Paginator->sort('email') ?></th>
<th scope="col"><?= $this->Paginator->sort('handphone') ?></th>
<th scope="col" class="actions"><?= __('Actions') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($users as $user): ?>
<tr>
<td><?= $this->Number->format($user->uid) ?></td>
<td><?= h($user->fullname) ?></td>
<td><?= h($user->email) ?></td>
<td><?= h($user->handphone) ?></td>
<td class="actions">
<?= $this->Html->link(__('View'), ['action' => 'view', $user->id]) ?>
<?= $this->Html->link(__('Edit'), ['action' => 'edit', $user->id]) ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<div class="paginator">
<ul class="pagination">
<?= $this->Paginator->first('<< ' . __('first')) ?>
<?= $this->Paginator->prev('< ' . __('previous')) ?>
<?= $this->Paginator->numbers() ?>
<?= $this->Paginator->next(__('next') . ' >') ?>
<?= $this->Paginator->last(__('last') . ' >>') ?>
</ul>
<p><?= $this->Paginator->counter(['format' => __('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')]) ?></p>
</div>
Change above variable accordingly to match your table entities (columns' name).
TIPS: If you use CakePHP bake command, you will get all this
pagination by default

Array variable link to another page

Currently I am stuck. I have to get an overview of domains. These have to link to an record page. The problem is, I don't know how. I have tried to set an $_SESSION, but then it picks the first variable of my array. At the moment things are hardcoded with 1 example.
<?php
unset($command);
$command = array(
"command" => "DomainsListActive"
);
$api = new Versio_api();
$versio = $api->api_send($command);
if($versio['success']==0) {
echo("Fout opgetreden. Fout code: ".$versio['command_response_code'].". Fout text: ".$versio['command_response_message']."");
}
else {
if($versio['total_count']>0)
{
require_once("includes/submenu.php");
?>
<div class="col-sm-9 col-md-9" style="width:80%;">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Klantenpaneel</h3>
</div>
<div class="panel-body">
<form method="post" action="">
<table class="table table-striped">
<thead>
<tr>
<th>Domein</th>
<th>TLD</th>
<th>Verloop datum</th>
<th>Automatisch verlengen</th>
</tr>
</thead>
<table class="table table-striped table-bordered">
<tbody>
<?php
$teller = 1;
while($versio['total_count']>=$teller) {
?>
<tr>
<td><?php echo $versio['id_'.$teller]; ?></td>
<td><?php echo $versio['domain_'.$teller]; ?> </td>
<td><?php echo $versio['tld_'.$teller]; ?></td>
<td><?php echo $versio['expiration_date_'.$teller]; ?></td>
<td><?php echo $versio['auto_renew_'.$teller]; ?></td>
</tr>
<?php $teller++; } ?>
</tbody>
</table>
</form>
</div>
</div>
<?php
} else {
echo("Er zijn geen DNS records gevonden voor dit domein.");
}
}
?>
I have to get for example, the third variable of $versio['domain_'.$teller] to put in my array in antoher document, record.php. If there is somebody who could help me that would be awesome!
If I understand you correctly, could you pass the variables in a query string?
eg:
<a href="records.php?domain=<?php echo $versio['domain_'.$teller]; ?>&tld=<?php echo $versio['tld_'.$teller]; ?>">
<?php echo $versio['domain_'.$teller]; ?>
</a>
Then in records.php you could get those values with:
<?php
$domain = $_GET['domain'];
$tld = $_GET['tld'];

cakePHP: filter rows before passing to view based on user role

I developed a cakePHP application for managing some books and it uses 'Auth' componenet for its authentication mechanism. I have 2 user roles: admin & user. In this application i have below rules for Book and its index view:
every admin can view all books, edit them and ...
every normal user can only view his/her books, edit his/hers only (not all books)
in other words, I want to know is it possible to filter data based on user role before passing model information to view or not?
I guess that passing all data to views and then filter them based on user role inside view, may cause DB overhead but I'm not sure if i am right.
Currently my index.ctp is like this and it only displays books related to logged-in user (without caring about his role):
<div class="books index">
<h2><?php echo __('Books'); ?></h2>
<?php if ($loggedIn) { ?>
<table cellpadding="0" cellspacing="0">
<thead>
<tr>
<th><?php echo $this->Paginator->sort('id'); ?></th>
<th><?php echo $this->Paginator->sort('book_user_id'); ?></th>
<th><?php echo $this->Paginator->sort('name'); ?></th>
<th><?php echo $this->Paginator->sort('revision'); ?></th>
<th><?php echo $this->Paginator->sort('price'); ?></th>
<th><?php echo $this->Paginator->sort('publication_year'); ?></th>
<th><?php echo $this->Paginator->sort('url_cover_page'); ?></th>
<th><?php echo $this->Paginator->sort('url_last_page'); ?></th>
<th><?php echo $this->Paginator->sort('author'); ?></th>
<th><?php echo $this->Paginator->sort('publisher'); ?></th>
<th><?php echo $this->Paginator->sort('translator'); ?></th>
<th><?php echo $this->Paginator->sort('tags'); ?></th>
<th><?php echo $this->Paginator->sort('created'); ?></th>
<th><?php echo $this->Paginator->sort('modified'); ?></th>
<th class="actions"><?php echo __('Actions'); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($books as $book): ?>
<?php if ($book['BookUser']['id'] == $loggedIn) { ?>
<tr>
<td><?php echo h($book['Book']['id']); ?> </td>
<td>
<?php echo $this->Html->link($book['BookUser']['username'], array('controller' => 'users', 'action' => 'view', $book['BookUser']['id'])); ?>
</td>
<td><?php echo h($book['Book']['name']); ?> </td>
<td><?php echo h($book['Book']['revision']); ?> </td>
<td><?php echo h($book['Book']['price']); ?> </td>
<td><?php echo h($book['Book']['publication_year']); ?> </td>
<td><?php echo h($book['Book']['url_cover_page']); ?> </td>
<td><?php echo h($book['Book']['url_last_page']); ?> </td>
<td><?php echo h($book['Book']['author']); ?> </td>
<td><?php echo h($book['Book']['publisher']); ?> </td>
<td><?php echo h($book['Book']['translator']); ?> </td>
<td><?php echo h($book['Book']['tags']); ?> </td>
<td><?php echo h($book['Book']['created']); ?> </td>
<td><?php echo h($book['Book']['modified']); ?> </td>
<td class="actions">
<?php echo $this->Html->link(__('View'), array('action' => 'view', $book['Book']['id'])); ?>
<?php echo $this->Html->link(__('Edit'), array('action' => 'edit', $book['Book']['id'])); ?>
<?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $book['Book']['id']), array(), __('Are you sure you want to delete # %s?', $book['Book']['id'])); ?>
</td>
</tr>
<?php } ?>
<?php endforeach; ?>
</tbody>
</table>
<?php } else { ?>
<p>To view your books, please login or Sign Up.</p>
<?php } ?>
<p>
<?php
echo $this->Paginator->counter(array(
'format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {:end}')
));
?> </p>
<div class="paging">
<?php
echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('separator' => ''));
echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
?>
</div>
Html->link(__('New Book'), array('action' => 'add')); ?>
Html->link(__('List Users'), array('controller' => 'users', 'action' => 'index')); ?>
Html->link(__('New Book User'), array('controller' => 'users', 'action' => 'add')); ?>
Html->link(__('List Pgs'), array('controller' => 'pgs', 'action' => 'index')); ?>
Html->link(__('New Book Pgs'), array('controller' => 'pgs', 'action' => 'add')); ?>
You should not be retrieving the data, let alone sending all data to the view. You should be filtering in your model queries based on the role of the currently logged in user. Then passing it along.
If for some reason it's difficult to filter in the actual queries (not sure why it would be), I'd recommend still filtering the data in the Model method using Cake's Hash or php array functions....etc depending on your needs before passing back to the Controller.
In your view, it's acceptable to write if statements for different display blocks, but that's based on the role of the user.

I can't find user by id all my valors is empty in cake php

hello i need find user by id for my reports this is my code.
Now in this part of my code.
<?php echo $this->Html->link('soya existencia', array('controller' => 'Reportesoyaexistencias', 'action' => 'export_xls', $soyareporte['Soyareporte']['id'])); ?>
I set the id of user because just a need the report of this user.In my index just I do a link,this link set my id to reporteexistenciassoya where i need that just show me the id sent and all records . this is all code:
SoyareportesController.php
index.ctp
<table>
<thead>
<tr>
<th><?php echo $this->Paginator->sort('username', 'Nombre de Usuario');?> </th>
<th><?php echo $this->Paginator->sort('nombre', 'Nombre (s)');?> </th>
<th><?php echo $this->Paginator->sort('apellido', 'Apellido (s)');?> </th>
<th><?php echo $this->Paginator->sort('email', 'Email');?></th>
<th><?php echo $this->Paginator->sort('Grupo.categoria', 'Categoria');?></th>
<th><?php echo $this->Paginator->sort('Grupo.subcategoria', 'Sub-Categoria');?></th>
<th><?php echo $this->Paginator->sort('Perfil.codigogeneral', 'Codigo General');?></th>
<th>Soya report</th>
</tr>
</thead>
<tbody>
<?php $count=0; ?>
<?php foreach ($soyareportes as $soyareporte): ?>
<?php $count ++;?>
<?php if($count % 2): echo '<tr>'; else: echo '<tr class="zebra">' ?>
<?php endif; ?>
<td style="text-align: center;"><?php echo $soyareporte['Soyareporte']['username']; ?></td>
<td style="text-align: center;"><?php echo $soyareporte['Soyareporte']['nombre']; ?></td>
<td style="text-align: center;"><?php echo $soyareporte['Soyareporte']['apellido']; ?></td>
<td style="text-align: center;"><?php echo $soyareporte['Soyareporte']['email']; ?></td>
<td style="text-align: center;"><?php echo $soyareporte['Grupo']['categoria']; ?></td>
<td style="text-align: center;"><?php echo $soyareporte['Grupo']['subcategoria']; ?></td>
<td style="text-align: center;"><?php echo $soyareporte['Perfil']['codigogeneral']; ?></td>
<td><?php echo $this->Html->link('soya existencia', array('controller' => 'Reportesoyaexistencias', 'action' => 'export_xls', $soyareporte['Soyareporte']['id'])); ?>
</td>
</tr>
<?php endforeach; ?>
<?php unset($soyarreporte); ?>
</tbody>
</table>
Now in this part of my code. I receipt values the id but when I want to display the valors, but all values ​​are empty.
Reportesoyaexistencias.php
<?php
class ReportesoyaexistenciasController extends AppController {
var $name = 'Reportesoyaexistencias';
function export_xls($id = null) {
$this->loadModel("Reportesoyaexistencia");
//$this->Reportesoyaexistencia->recursive = 1;
$data = $this->Reportesoyaexistencia->findById($id);
if (!$this->request->data) {
$this->request->data = $data;
}
$this->set('soyaexistencias',$data);
//$this->render('export_xls','export_xls');
}
}
?>

Categories