Poll form (view) creates security errors in CakePHP - cakephp

I am writing a simple poll for Cakephp. I have created tables polls and poll_votes. Poll contains 7 columns: id,question,numberofanswers,answer1,answer2,answer3,answer4. PollVote contains 4: id,poll_id,ip,vote. If this works, I might be adding a new table called PollAnswers as a way to get rid of the answer-limit.
Unfortunately the view I made causes security errors. I would prefer to use the form-helper as a whole, but I have not figured out how since I need radio-buttons with multiple options that have id's 1 to 4, but show answer1 to answer4.
[daweb#directadmin01 public_html]$ cat models/poll.php
<?php
class Poll extends AppModel {
var $name = 'Poll';
var $displayField = 'question';
var $hasMany = array(
'PollVote' => array(
'className' => 'PollVote',
'foreignKey' => 'poll_id',
'dependent' => false
)
);
}
?>
[daweb#directadmin01 public_html]$ cat models/poll_vote.php
<?php
class PollVote extends AppModel {
var $name = 'PollVote';
var $belongsTo = array(
'Poll' => array(
'className' => 'Poll',
'foreignKey' => 'poll_id'
)
);
}
?>
[daweb#directadmin01 public_html]$ cat views/polls/view.ctp
<h2><?=$polls[0]['Poll']['question']?></h2>
<?php
echo $this->Form->create('Poll');
?>
<br /><p>
<select name="PollName" id="PollFieldId">
<option value="0"><?=$polls[0]['Poll']['answer1']?></option>
<option value="1"><?=$polls[0]['Poll']['answer2']?></option>
<option value="2"><?=$polls[0]['Poll']['answer3']?></option>
<option value="3"><?=$polls[0]['Poll']['answer4']?></option>
</select>
<?php
echo $this->Form->input('id', array('type' => 'hidden'));
echo $this->Form->end('Submit');
?>
</p>
[daweb#directadmin01 public_html]$
I am honestly not sure I am creating the Poll application with the right design, so general advice is welcome. Unfortunately I was unable to find examples of polls written in Cakephp to help me.

your design is fine, as long as you only need 4 questions for each poll. Change numberofanswers to pollVote_count and use counterCache
Take a look at Database design for a survey, although that might be overkill for your purpose.
for the radio input, that should be in the pollVotes controller/add action, not in the polls/view:
echo $this->Form->create('PollVote');
echo $this->Form->input('poll_id');
echo $this->Form->input('vote',array('type' => 'radio','options' => $polls[0]['Poll']));
echo $this->Form->end('Submit');

Related

comment plugin for cake2.0

I am using cakephp2.0 and want to integrate comment plugin but i got nothing .I was using commentDc plugin but its not working as my requirements.Because i am integreating my users login system with xenforo and commentDc plugin use Auth component so its not working properly.
Please let me know is there any simple comment plugin which i can integrate and modify as my needs.
Thanks,
Here's how I set up comments:
Comments table fields:
id
parent_type, matches the model name of the parent
parent_id
content
user_id, the sender
In any model that you want to be commentable, at this your associations:
public $hasMany = array(
'Comment' => array(
'className' => 'Comment',
'foreignKey' => 'parent_id',
'conditions' => array('Comment.parent_type' => 'question')
)
);
This is a view element:
<?php
/*
set variables:
$data : data of the parent
$type : the type of the parent
*/
if(!isset($name)) {
$name = 0;
}
foreach($data['Comment'] as $comment){
echo '<div class="comment">'.$comment['content'].
' - '.$this->Html->link($comment['User']['username'],array('controller'=>'users','action'=>'view',$comment['User']['id']))
.'</div>';
}
echo $this->Form->create(null, array('url' => '/comments/add','id'=>'qCommentForm'));
echo $this->Form->input('Comment.parent_id', array('type'=>'hidden','value'=>$data[$type]['id']));
echo $this->Form->input('Comment.parent_type', array('type'=>'hidden','value'=>$type));
echo $this->Form->textarea('Comment.content',array('div'=>'false','class'=>'small','label'=>false));
echo $this->Form->submit(__('Leave comment'),array('div'=>'false','class'=>'small'));
echo $this->Form->end();
?>
Then, in the view view for your model, add this (assuming you named the element comment.ctp:
<?php echo $this->element('comment',array('data'=>$modelData,'type'=>'MyModel')) ?>

Cakephp habtm relation updating instead of saving

Hello my problem is i try to save a new relation between a shop and a payment method
the relation is habtm... shop and payment already exist. i want to ad more payment methods.
But always when i save ,the old payment realtion in the shop_payment table is only updated, not a second one saved....
i read a lot i set unique to false but nothing changes that.
Anyone got an idea?
Model
class Payment extends AppModel {
var $hasAndBelongsToMany = array(
'Mainshop'=>array('className'=>'Mainshop', 'unique'=>'false')
);
}
View
echo $this->Form->create('Mainshop');
echo $this->Form->input('name',array('default'=>$mainshop['Mainshop']['name']));
echo $this->Form->input('Payment.id', array(
'type' => 'select',
'options' => array($payments),
));
echo $this->Form->input('id', array('type'=>'hidden','value'=>$mainshop['Mainshop'] ['id']));
echo $this->Form->end('Edit Shop');?>
Controller
if (!empty($this->data)){
$this->Mainshop->save($this->data);
$this->redirect(array('action' => 'edit',$this->data['Mainshop']['id']));
}
My recommendation defines the relationship with all fields in model:
var $hasAndBelongsToMany = array(
'Mainshop'=>array(
'className'=>'Mainshop',
'unique'=>'false',
'joinTable' => 'shop_payments',
'foreignKey' => 'payments_id',
'associationForeignKey' => 'shop_id'
)
);
In the controller add create():
if (!empty($this->data)){
$this->Mainshop->create();
$this->Mainshop->save($this->data);
$this->redirect(array('action' => 'edit',$this->data['Mainshop']['id']));
}

Not sure how to retrieve my the values from array

I have two issues with my CakePHP application (its my first one in CakePHP). I am trying to convert an old php website to cake.
1.Issue
I have my controller that accepts a parameter $id, but the data is comming from joined tables so in the cookbook it had something like this
MY Controller
dish_categories_controller.php
class DishCategoriesController extends AppController {
var $uses = array("DishCategory");
var $hasOne ='';
function get_categories($id)
{
$this->set('dishes',$this->DishCategory->find());
$this->layout = 'master_layout';
}
}
model
dish_category.php
class DishCategory extends AppModel{
var $name = 'DishCategory';
var $hasOne = array(
'Dish' => array(
'className' => 'Dish',
'conditions' => array('Dish.id' => '1'),
'dependent' => true
)
);
}
As you can see the Dish.id=> '1' is hard coded, how can make it dynamic there so that I pass a value and make it something like Dish.if =>$id ?.
So that was my first issue.
The second issue is related to the view
That model returns only one record, how can I make it so that it returns all and also how would I be able to loop through that, below the code in the view currently and the array format.
This is in my view
<?php
echo $dishes['DishCategory']['category_info'];
echo $dishes['DishCategory']['category_title'];
echo $dishes['Dish']['dish_name'];
echo $dishes['Dish']['dish_image'];
echo $this->Html->image($dishes['Dish']['dish_image'], array('alt' => 'CakePHP'))
?>
Array Format
Array ( [DishCategory] => Array
( [id] => 1 [category_name] => Appetizers
[category_keywords] => appetizer, appetizers
[category_title] => Our Side Dishes
[category_info] => Test Test
[dish_id] => 1 )
[Dish] => Array ( [id] => 1
[dish_name] => Rice
[dish_disc] => The Best flavor ever
[dish_price] => 2.90 [dish_image] => /img/rice_chicken.jpeg [dish_category_id] => 1
[dish_price_label] => Delicious Arepa ) )
I would appreciate your help to help me understand how to better do this. Thank you.
Firstly, you DishCategoriesController has model properties, you can remove them. In your controller, you will set up the conditions for the find like so:
class DishCategoriesController extends AppController {
function get_categories($id)
{
// find category with a dish of $id
$this->set('dishes', $this->DishCategory->find('all', array(
'conditions' => array(
'Dish.id' => $id
)
)));
// set master layout
$this->layout = 'master_layout';
}
}
Your DishCategory model will look very basic, you don't need to hard code the relationship condition:
class DishCategory extends AppModel {
/**
* hasOne associations
*
* #var array
*/
public $hasOne = array(
'Dish' => array(
'className' => 'Dish',
'foreignKey' => 'dish_category_id'
)
)
}
At this point it is worth noting that since the DishCategory hasOne Dish, using the above find query, it will only ever return a single result. But, it you were returning multiple results, you could loop through them in your view like so:
<?php foreach ($dishes as $key => $dish): ?>
<?php var_dump($dish) ?>
<?php endforeach ?>

CakePHP treated comments with pagination

Is there a way to paginate comments in cakePHP with a tree behavior? Should I use tree behavior anyway or to make my own code to view the comments?
Sorry, this has been asked before. I found an article: Managing Hierarchical Data in MySQL
...and I will post my solution.
Nope, I didn't find a good solution. I don't want to reinvent the wheel, I do want to make it in the cake way.
I wrote a helper for recursively print the comments:
# view/helpers/comments
class CommentsHelper extends AppHelper{
public function printComments($comments = array(), $params = array()){
if (empty($comments) || !is_array($comments)) return false;
echo '<ul id="comments-'.$comments[0]['Forum']['id'].'">';
if (is_array($comments)){
foreach($comments as $comment):
?>
<li id="<?php echo $comment['Forum']['id']; ?>">
<div class="inner">
<h4><?php echo $comment['Forum']['title']; ?></h4>
<div class="meta">
<?php echo $comment['User']['first_name']; ?>
</div>
<div class="content">
<?php echo $comment['Forum']['content']; ?>
</div>
</div>
<?php
if (isset ($comment['children']))
if (is_array($comment['children'])){
if (!empty($comment['children'])) $this->printComments($comment['children']);
}
echo '</li>';
endforeach;
}
else{
echo '<li>'.$comment['Forum']['title'].'</li>';
}
echo '</ul>';
}
Here is how I retrieve the data from the database:
# controllers/forums.php
$this->set('tree', $this->Forum->find('threaded'););
The problem is how to paginate the comments?
Currently I'm using 2 tables, one for the root and 2nd for the threaded comments. I didn't find a solution.
You have to do it manually in controller by paginate.
The key is properly order mysql query to find comment not by id by firstly by parent_id and then id (columns are just example) - that would work for 2 level comments:
$this->paginate = array(
'recursive' => -1,
'conditions' => array(
'Comment.blog_id' => 0,
),
'order' => array('parent_id', 'id'),
'limit' => 10
);
$this->set('comments', $this->paginate('Comment'));
You can't. What I did is a link which switched between full threaded comments view AND flat paginated list.
IMHO: do you think that paginate comments is a good idea? just minimize space comments take on the page, and put them into one float. don't make your user to click too many links...
You can achieve this as
Your modal code:
var $hasMany = array(
'Pages' => array(
'className' => 'Page',
'foreignKey' => 'parent_id',
)
);
Your Controller code:
$this->paginate = array(
'Page' => array(
'contain' => array(
'Pages'
),
'conditions' => array(
'Page.parent_id' => null
)
)
);
$pages = $this->paginate();
You should use tree behavior and order by lft.

cakephp: how to render related models in the view

My objective is to display records for a related child model (once removed) in the view. My problem is that I receive a 'Notice (8): Undefined index:' error message when I try to output the following query.
Tournaments have many tournamentDetails, and tournamentDetails have many updates. I'm trying to display all the updates for each tournamentDetail that is displayed on the tournaments view.
So my question is how to properly resolve the error message and display the updates for each tournamentDetail on the tournaments view page.
My find data in the 'tournaments' controller view action is:
$updates = $this->Tournament->TournamentDetail->Update->find('all', array( 'tournament_details.tournament_id'=> $this->data['Tournament']['id']));
In the view, I have this code.
<?php foreach ($tournament['Update'] as $update): ?>
<h3>Updates</h3>
<h1><?php echo $update ['title']; ?></h1>
<p><?php echo $update ['body']; ?></p>
<hr/>
<?php endforeach; ?>
This is the same 'foreach' code the I use for the other related child records without problem. I did use the debug() function to investigate but didn't see what I was missing.
The output of the $updates array from the debug function looks like this:
Array
(
[0] => Array
(
[Update] => Array
(
[id] => 1
[title] => first round matches start today
[date] => 2010-03-19
[body] => this tournament's first round matches start today.
[tournament_detail_id] => 4
[homeStatus] => no
)
Is there a special way to display records this deep?
As always, thanks in advance.
Paul
Update: Feb 23/11
After some testing the problem I seem to have is finding and passing the correct value to the $updates variable;
To summarize, I want the $updates variable to hold the current 'tournament_details_id'. This will then display the related update records in the 'tournaments' view.
I'm still a beginner and most likely overlooked the obvious.
Here is the model info:
class Tournament extends AppModel {
var $name = 'Tournament';
var $hasMany = array(
'TournamentDetail' => array(
'className' => 'TournamentDetail',
'foreignKey' => 'tournament_id',)
class TournamentDetail extends AppModel {
var $name = 'TournamentDetail';
var $belongsTo = array(
'Tournament' => array(
'className' => 'Tournament',
'foreignKey' => 'tournament_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
class Update extends AppModel {
var $name = 'Update';
var $belongsTo = array(
'TournamentDetails' => array(
'className' => 'TournamentDetails',
'foreignKey' => 'tournament_detail_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
Controller data:
class TournamentsController extends AppController
function view($slug = null) {
if (!$slug) {
$this->Session->setFlash(__('Invalid Tournament.', true));
$this->redirect(array('action'=>'index'));
}
$this->Tournament->recursive = 1;
$tournament = $this->Tournament->findBySlug($slug);
$updates = $this->Tournament->TournamentDetail->Update->find('all', array('conditions' => array( 'tournament_details_id' => $this->data['TournamentDetails']['id'] )));
$this->set(compact('tournament','updates', $updates ));
Tournament view. This is display the 'tournament details' and (ideally) related tournament detail 'updates'
<h2><?php echo $tournament['Tournament']['name']; ?></h2>
<?php foreach ($tournament['TournamentDetail'] as $tournamentDetail): ?>
<h1><?php echo $tournamentDetail ['title']; ?></h1>
<p><?php echo $tournamentDetail ['body']; ?></p>
<?php endforeach; ?>
<?php foreach ($updates['Update'] as $update): ?>
<h4>Update: <?php echo $update ['date']; ?></h4>
<p> <?php echo $update ['Update'] ['title']; ?></p>
<p><?php echo $update ['Update'] ['body']; ?></p>
<?php endforeach; ?>
I've tested this by adding in the tournament details 'id' as an integer and it pulls up the correct 'update' record. However I seem to have problems configuring the find operation to do the same.
As always I appreciate the help.
Thanks
Paul
Based on your debug output, it seems that your $tournaments variable consists of an array (instead of the associative array) so you might want to use the foreach to access each of those elements:
foreach ($tournaments as $update):
?>
<h3>Updates</h3>
<h1><?php echo $update ['Update']['title']; ?></h1>
<p><?php echo $update ['Update']['body']; ?></p>
<hr/>
<?php endforeach; ?>
It appears that you be using the wrong variable, you talk about the $updates variable but you're using the $tournament variable in your view.
<?php foreach ($updates['Update'] as $update): ?>
<h3>Updates</h3>
<h1><?php echo $update ['title']; ?></h1>
<p><?php echo $update ['body']; ?></p>
<hr/>
<?php endforeach; ?>
Additionally you haven't posted the whole controller method, which includes that find but you might not have sent the data to the view yet either
$this->set(compact('updates'));

Resources