Disable specific divs in CakePHP based on page selected - cakephp

I'm new with CakePHP and I need your assistance. I need to display a specific widget which is in the form of a div on a specific page i.e. my homepage and disable on the rest of the pages. Essentially I have been able to specifically display specific divs based on log in status as indicated below:
<?php if (!$this->Session->read('Auth.User.id')): ?>
<div class="register link right <?php if ($active == 'register') echo 'active'; ?>"><?php echo $html->link('Register', array('controller' => 'users', 'action' => 'register')); ?></div>
<div class="login link right <?php if ($active == 'login') echo 'active'; ?>"><?php echo $html->link('Login', array('controller' => 'users', 'action' => 'login')); ?></div>
<?php else: ?>
<div class="logout link right"><?php echo $html->link('Logout', array('controller' => 'users', 'action' => 'logout')); ?></div>
<div class="myaccount link right <?php if ($active == 'myaccount') echo 'active'; ?>"><?php echo $html->link('My account', array('controller' => 'account', 'action' => 'summary')); ?></div>
<?php endif; ?>
I was asking for any help with regards to displaying a specific div based on the selection of my homepage.
The pseudocode below indicates my the line of thinking I'm taking to solve this issue:
<?php if (the selected page is homepage or default.ctp)?>
// set the display property for the desired div to none
<?php else: ?>
// do not set the display property for the desired div to none
<?php endif; ?>

In cakephp you cannot use directly $this->Session->read('Auth.User.id') in your view is bettere to do this into your controller:
$this->set('authUser', $this->Auth->user());
and into your view
if (!$authUser)
{
//not logged
}
else{
//logged
}
And if you wanna check which is the page you can try something like that
echo Router::url($this->last, true);
Is what you want?

In your controller you can define something like:
$this->set('pageName', $pageName);
Then you can do in your view:
$class='';
if($pageName=='homepage') {
$class=' hide';
}
echo $this->Html->div($class, 'your content here');
Also think about why you need a structure of this in your view. Maybe you can just not supply the content if it is not needed to the view? So you make the decision in your controller. That makes most of the time more clean views and the smallest amount of data needed in your view.

Related

How to login with an element in CakePHP?

Currently I have a functional login procedure. My users go to a specific login view, type their username and password, access and are redirected to a specific dashboard.
In my users controller:
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect();
}
$this->Session->setFlash('Incorrect user or password.');
}
}
The login view:
<h1>Access with your username:</h1>
<?php
echo $this->Form->create('User', array('action' => 'login'));
echo $this->Form->input('username', array('label' => 'User:'));
echo $this->Form->input('password', array('label' => 'Password:'));
echo $this->Form->end('Login');
?>
The issue came when I was asked to add some login fields directly in my main menu. So I tried adding them as an element in my header. It didn't work.
The element I'm adding in my header:
<div>
<?php
echo $this->Form->create('user', array('action' => 'login'));
echo $this->Form->input('username', array('placeholder' => 'User', 'label' => false));
echo $this->Form->input('password', array('placeholder' => 'Password', 'label' => false));
echo $this->Form->submit('Ingresar', array('div' => true));
echo $this->Form->end();
?>
</div>
I enter username and password and click the "login" button. It throws me the "Incorrect user or password." error message and redirects me to the view for the login action (without login in the user).
In there, one can successfully login without further issue, but the point was to reduce by 1 the needed clicks for the action.
What am I missing? What should I change/add/move?
In your second html form, the model User is not typed as it should be
change
echo $this->Form->create('user', array('action' => 'login'));
To
echo $this->Form->create('User', array('action' => 'login'));
Note the u changed to U from the model name

pagination not working when clicked next button

Controler
public function search() {
$this->Paginator->settings = $this->paginate;
$this->loadmodel('Usermgmt.User');
if ($this->request -> isPost()) {
$this->User->set($this->data);
$keyword=$this->data['Doctors']['search'];
//$this->loadmodel('Usermgmt.User');
$cond=array('OR'=>array("User.username LIKE '%$keyword%'","User.email LIKE '%$keyword%'", "User.first_name LIKE '%$keyword%'", "User.last_name LIKE '%$keyword%'"));
//$result = $this->paginate('User',array('conditions'=>$cond));
$result = $this->paginate('User',array($cond));
$this->set('result', $result);
}
}
View
<?php
if (!empty($result)) { $sl=0;
foreach ($result as $row1) {
//print_r($row1);
$sl++; ?><div style="width:100%;display:inline-block;">
<div style="float:left">
<?php
//echo $row1['id'];
echo $this->Html->link($this->Html->image('../files/user/photo/'.$row1 ['User']['photo_dir'].'/'.$row1 ['User']['photo'], array('width' => '180', 'height' => '180')),
array('controller'=>'Profiles','action'=>'index',$row1['User']['id']),
array('escape' => false));
?>
</div>
<div>
<?php echo h($row1['User']['first_name'])." ".h($row1['User']['last_name'])."</br>";
echo h($row1['User']['username'])."</br>";
echo h($row1['User']['email'])."</br>";
echo h($row1['User']['mobile'])."</br>";
echo h($row1['UserGroup']['name'])."</br>";
?></div>
<div style="clear:both;"></div>
</div>
<?php }?>
<?php echo $this->Paginator->prev('previous'); ?>
<?php echo $this->Paginator->numbers(); ?>
<?php echo $this->Paginator->next('Next'); ?>
<?php }?>
here am search the user name or user details like fname, email like and display in view page
here i get output with pagination like 1 2 3 4 only first page displays when i click next page that shows empty pages may be $result getting unset how to solve this ??
The variable result is only sometimes set
if ($this->request->isPost()) {
...
$result = $this->paginate('User',array($cond));
$this->set('result', $result);
}
The variable result is only set for POST requests - clicking a link is not a post request, therefore the result variable is undefined.
Ensure you are paginating a GET request
There are several solutions, but the simplest solution to "How to paginate post data" is to not do so. Change your search form to use GET, and ensure the get parameters persist when paginating a request.
At the very least the controller code needs to call paginate and set for the variables in the view to exist irrespective of how the controller action was reached.

Calling a element after login

I have a post index view calling a element which content input field for comment.
I call it in this way
<?php echo $this->element('addcomment', array('post_id' => $post['Post']['id'])); ?>
This work fine, I pass the post id parameter, in the addcomment element, because the post_id input field is hidden in the addcomment. and of course I dont want that the user type the post id.
I have set up the authorization mechanism in order to allow adding comment to the user identified (connected).
When a non-connected user try to add a comment, he receives the login screen.
After login, he is redirected to the add commment form. The problem is that in the mean time it loose the value of the post_id variable.
Rem: If the user is connected before adding comments to the post, it works.
Dont hesitate to contact me in case my explanation is not clear or if you need more information.
This is my addcomment element
<div class="Addcomment form">
<?php
echo $this->Form->create('Comment', array(
'url' => array('controller' => 'comments', 'action' => 'add')
)); ?>
<fieldset>
<legend><?php echo __('Add Comment'); ?></legend>
<?php if (isset($current_user['id']) && isset($post_id)): ?>
<?php $this->request->data['Comment']['user_id'] = $current_user['id']; ?>
<?php $this->request->data['Comment']['post_id'] = $post_id; ?>
<?php echo $this->Form->input('post_id', array('type' => 'hidden')); ?>
<?php echo $this->Form->input('user_id', array('type' => 'hidden')); ?>
<?php else: echo $this->Form->input('post_id'); ?>
<?php endif; ?>
<?php echo $this->Form->input('content', array('class' => 'comment')); ?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
As per i understand your problem. You can use a session variable to store a post id which should not affected if user logs in. And using this session you can redirect a user to particular post after login.
Set a session value before redirecting user to login.
$this->Session->write('last_post_id',YOUR_POST_ID);
Redirect user to the post if user successfully logged in and if you find a session value not empty.
if ($this->Session->check('last_post_id') && $this->Session->read('last_post_id') != '') {
$this->redirect(YOUR_URL_TO_POST . '?postid=' . $this->Session->read('last_post_id'));
exit;
}else{
//NORMAL REDIRECTION
}
Hope this will help you. Unset session last_post_id after redirection and if no longer required.

CakePHP: creating a dropdown on homepge through an element problem

I try to create a search function on my homepage where the user can limit the search results by country.
It all works in my posts/index controller whereby the country list is automatically retrieved by a find('list).
However, on the homepage, the country dropdown remains empty. Below some code:
I try to retrieve the dropdown by using requestAction (please omit 'requestAction is slow from the comments, thanks)
homesearch.ctp ELEMENT:
<?php $this->requestAction('countries/getCountries');?>
<?php
echo $this->Form->create('Post', array(
'url' => array_merge(array('controller' => 'posts','action' => 'index'), $this->params['pass'])
));
echo $this->Form->input('title', array('div' => false, 'empty' => true, 'label' => false));
echo $this->Form->input('country_id');
echo $this->Form->submit(__('Search', true), array('div' => false));
echo $this->Form->end();
?>
getCountries function in countries controller:
function getCountries(){
$countries = $this->Country->find('list');
$this->set(compact('countries'));
}
Before diving into alternatives (loadmodule('Country') in PagesController etc), I think I am doing something wrong, there is no data flowing back from the requestAction function as debug taught me.
How do you guys wash this cow? Thanks!
... (please omit 'requestAction is slow from the comments, thanks)
For improved performance, replace:
<?php $this->requestAction('countries/getCountries');?>
with:
<?php $this->viewVars['countries'] = ClassRegistry::init('Country')->find('list'); ?>
This approach doesn't generate a second request.
function getCountries(){
$countries = $this->Country->find('list');
if (!empty($this->params['requested'])) {
return $countries;
} else {
$this->set(compact('countries'));
}
}
and in the element: <?php $countries = $this->requestAction('countries/getCountries');?>
man, it's right in the book: http://book.cakephp.org/view/991/requestAction

Embedding view in another view with CakePHP

I have a news controller with a 'view' action for each news item. In the 'view' action of each news item I would like to include another view, the 'add' action of the comments controller.
Basically, I need a form on each news item's page to add comments.
I have the two views but I can't manage to link them. I tried with elements, but it seems it doesn't render the view for the add comment view.
What should I do?
Thank you very much!
EDIT
Code in element:
<?php echo $this->Form->create('Comment', array('class' => 'big', 'url' => array('controller' => 'comments', 'action' => 'add', $news_id)));?>
<?php
echo $this->Form->input('comment', array('label' => false));
?>
Form->end(__('Submit', true));?>
Code in view:
<?php echo $this->element('add_comment', array('news_id' => $news['News']['id'])); ?>
move the code from your view into an element and then call the element all over the place.

Resources