Code Drive-By: Edit Anywhere Component for CakePHP
I call this a code drive-by because I’m just going to dump the code without much explanation. When I have time I’ll come back and walk through it and turn it into a useful article on the CakePHP bakery.
This component allows you to manage content (primarily text blocks) on a site if you’re logged in. The editor pops up in a thickbox.
Some requirements off the top of my head:
- CakePHP, duh.
- jquery
- thickbox
- Auth Component
Again, sorry for the drive-by but hopefully someone will get use out of this.
-
-
<?php
-
-
class ContentComponent extends Object
-
{
-
var $controller;
-
var $Setting;
-
-
function initialize(&$controller)
-
{
-
$this->controller =& $controller;
-
App::import(‘Model’, ‘Setting’);
-
$this->Setting = new Setting();
-
}
-
-
function read($field = null) {
-
return $this->render($field);
-
}
-
-
function render($field = null)
-
{
-
$content = $this->Setting->findByField($field);
-
-
return $this->output($field, $content[‘Setting’][‘value’]);
-
} else {
-
return $this->output($field, ”);
-
}
-
}
-
-
function output($field, $str)
-
{
-
$output = $str;
-
if($this->controller->u[‘User’][‘role’] == ‘admin’) {
-
$output .= $this->editButton($field);
-
}
-
}
-
-
function editButton($field) {
-
return ‘<a href="/settings/edit/’.$field.‘/?r=’.$_SERVER[‘REQUEST_URI’].‘" class="thickbox edit-button"><img src="/img/edit.png" width="10" height="10" /></a>’;
-
}
-
}
-
?>
-
And my Settings Controller looks like:
-
-
<?php
-
-
class SettingsController extends AppController
-
{
-
var $name = ‘Settings’;
-
-
function edit($field)
-
{
-
$content = $this->Setting->findByField($field);
-
-
$this->Setting->id = $content[‘Setting’][‘id’];
-
}
-
-
{
-
‘field’ => $field,
-
‘value’ => $this->data[‘Setting’][‘value’],
-
);
-
-
$this->Setting->save($content);
-
}
-
-
}
-
}
-
?>
-




Add New Comment
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Add New Comment