rendered paste body<?php
class PostsController extends AppController
{
//public $scaffold;
public $name = 'Posts';
public $helpers = array('Ajax','Time','Text','Pagination');
public $uses = array ('Category', 'Post');
public $access = array (
'add' => array('role'=>array('Admin','Author')),
'edit' => array('role'=>array('Admin','Author')),
'delete' => array('role'=>array('Admin'))
);
function edit($id=null)
{
$cat = $this->_flattenArray($this->Category->query ('select id, title from categories'));
$selected = $this->_flattenArray( $this->Post->query('SELECT category_id FROM categories_posts WHERE post_id = '. $id));
foreach($cat as $key => $value):
if (! ($key % 2)):
$categories[$value] = $cat[$key + 1];
endif;
endforeach;
$this->set('categories', $categories);
$this->set('selected', $selected);
// $this->set('css_array',array($this->rdTheme.'/form',$this->rdTheme.'/view'));
$this->Post->id = $id;
if (empty($this->data))
{
$this->data = $this->Post->read();
//converting nl2br back so it looks better
$this->Output->br2nl($this->data);
$this->render();
}
else
{
//set the user id
$this->data['Post']['user_id'] = $this->rdAuth->id;
//make sure data is clean
$this->Output->filter($this->data);
$this->Sanitize->cleanArray($this->data);
if ($this->Post->save($this->data))
{
$this->action = 'index';
$this->set('message','post edited. click publish to allow immediate viewing.');
$this->index();
$this->render('index');
}
else
{
//converting nl2br back so it looks better
$this->Output->br2nl($this->data);
$this->render();
}
}
}
?>
<pre>
<?php
print_r ($categories);
print_r ($selected);
?>
</pre>
<div class="rd11">
<?php echo $html->formTag('/posts/edit')?>
<?php echo $html->hidden('Post/id');?>
<fieldset class="posts">
<legend>Edit Post</legend>
<div class="set-column">
<div class="label">
<label>Title:</label> required
</div>
<div class="input">
<?php echo $html->input('Post/title',array('class'=>'expanding'));?>
<?php echo $html->tagErrorMsg('Post/title', '*required*');?>
</div>
<div class="clear"> </div>
</div>
<div class="set-column">
<div class="label">
<label>Category:</label> required
</div>
<div class="input">
<?php
echo $html->selectTag('Category/title', $categories, $selected, array('multiple' => 'multiple'))
?>
<?php echo $html->tagErrorMsg('Category/title', '*required*');?>
</div>
<div class="clear"> </div>
</div>
<div class="set-row">
<div class="label">
<label>Article:</label> required, <a href="#bb_help" onclick="javascript:Element.toggle('bb_help')"class="small">html help</a>
<?php echo $html->tagErrorMsg('Post/article', '*required*')?>
<div id="bb_help" style="display:none;"><?php echo $this->renderElement('bb_help')?></div>
</div>
<div class="input">
<?php echo $html->textarea('Post/body',array('class'=>'expanding'))?>
</div>
<div class="clear"> </div>
</div>
<div class="submit">
<?php echo $html->submit('Save') ?>
</div>
</fieldset>
</form>
</div>