Dynamic Css for CMS and More!
2 : Dyna CSS Controller
Recently I was developing a cake app for a client who wanted to use the same system several times for a project and the same layout, but with different visual changes, like color etc. I had used simple version of css management based on the config component. This is a full blown version of this solution.
The controller setup for Dyna Css implements enum checking and uses table info via the $this->DynaCss->getEnumList() function implemented in AppModel
Controller Class:
<?php
class DynaCssController extends AppController {
var $name = 'DynaCss';
var $uses = array('DynaCss');
var $helpers = array ('Javascript');
/**
* Var for which column names contain enums to
* be used in the select tag
*/
var $fldArray = array(
'background_attachment','background_repeat',
'border_bottom_style','border_left_style',
'border_right_style','border_top_style',
'caption_side','clear','direction',
'empty_cells','float','font','font_style',
'font_variant','font_weight','list_style_position',
'list_style_type','outline_style','overflow',
'page_break_after','page_break_before','page_break_inside',
'position','quotes','table_layout','text_align',
'text_decoration','text_transform','unicode_bidi',
'visibility','white_space',
);
function index() {
$this->DynaCss->recursive = 0;
$this->set('dynaCsses', $this->DynaCss->findAll());
}
function view($id = null) {
if(!$id) {
$this->Session->setFlash('Invalid id for Dyna Css.');
$this->redirect('/dyna_css/index');
}
$this->set('dynaCss', $this->_assemble_css($id));
$this->set('dynaCssId', $id);
}
function add() {
$this->set('fldArray', $this->fldArray);
foreach ($this->fldArray as $fldValue)
{
$this->set($fldValue, $this->DynaCss->getEnumList($fldValue));
}
if(empty($this->data)) {
$fldNameArray = array();
$tmp = (array) $this->DynaCss->loadInfo();
foreach($tmp['value'] as $key => $name)
{
$fldNameArray[] = $name['name'];
}
$this->set('fldNameArray', $fldNameArray);
} else {
$this->cleanUpFields();
if($this->DynaCss->save($this->data)) {
$this->_build_css();
$this->Session->setFlash('The Dyna Css has been saved');
$this->redirect('/dyna_css/index');
} else {
$this->Session->setFlash('Please correct errors below.');
}
}
}
function edit($id = null) {
$this->set('fldArray', $this->fldArray);
foreach ($this->fldArray as $fldValue)
{
$this->set($fldValue, $this->DynaCss->getEnumList($fldValue));
}
if(empty($this->data)) {
if(!$id) {
$this->Session->setFlash('Invalid id for Dyna Css');
$this->redirect('/dyna_css/index');
}
$this->data = $this->DynaCss->read(null, $id);
} else {
$this->cleanUpFields();
if($this->DynaCss->save($this->data)) {
$this->_build_css();
$this->Session->setFlash('The DynaCss has been saved');
$this->redirect('/dyna_css/index');
} else {
$this->Session->setFlash('Please correct errors below.');
}
}
}
function delete($id = null) {
if(!$id) {
$this->Session->setFlash('Invalid id for Dyna Css');
$this->redirect('/dyna_css/index');
}
if($this->DynaCss->del($id)) {
$this->Session->setFlash('The Dyna Css deleted: id '.$id.'');
$this->_build_css();
$this->redirect('/dyna_css/index');
}
}
function _build_css()
{
$cssFile = APP.WEBROOT_DIR.DS."css".DS."dyna.css";
$result = $this->_assemble_css();
if ($cssFile = fopen($cssFile, 'w')) {
fwrite ($cssFile, $result, strlen($result));
fclose($cssFile);
}
$this->set('cssData', $result);
}
function _assemble_css($id=null)
{
$elementArray = array(
'background_attachment','background_color','background_image',
'background_position','background_repeat','border_bottom_color',
'border_bottom_style','border_bottom_width','border_collapse',
'border_left_color','border_left_style','border_left_width',
'border_right_color','border_right_style','border_right_width',
'border_spacing','border_top_color','border_top_style',
'border_top_width','bottom','caption_side','clear','clip',
'color','cursor','direction','empty_cells','float','font',
'font_family','font_size','font_style','font_variant',
'font_weight','height','left','letter_spacing','line_height',
'list_style_image','list_style_position','list_style_type',
'margin_bottom','margin_left','margin_right','margin_top',
'max_height','max_width','min_height','min_width','outline_color',
'outline_style','outline_width','overflow','padding_bottom',
'padding_left','padding_right','padding_top','page',
'page_break_after','page_break_before','page_break_inside',
'position','quotes','right','table_layout','text_align',
'text_decoration','text_indent','text_transform','top',
'unicode_bidi','vertical_align','visibility','white_space',
'width','word_spacing','z_index');
$output = '';
if (is_null($id))
{
$result = $this->DynaCss->findAll();
foreach($result as $tmp=>$tmp2)
{
$output .= $tmp2['DynaCss']['tag'] ."{\n\t";
foreach($elementArray as $element)
{
if (!empty($tmp2['DynaCss'][$element]))
{
$fixedElement = str_replace("_", "-",$element);
$output .= $fixedElement . " : " . $tmp2['DynaCss'][$element] . ";\n\t";
}
}
$output .= "\n}\n\n";
}
} else {
$result = $this->DynaCss->read(null, $id);
foreach($result as $tmp=>$tmp2)
{
$output .= $tmp2['tag'] ."{\n\t";
foreach($elementArray as $element)
{
if (!empty($tmp2[$element]))
{
$fixedElement = str_replace("_", "-",$element);
$output .= $fixedElement . " : " . $tmp2[$element] . ";\n\t";
}
}
$output .= "\n}\n\n";
}
}
return $output;
}
}
?>
Next up is the views and setting layouts to use this system.
Comments
Bug
1 error when add new element
Code | Context
$fldName = "background_attachment"
*/
function getEnumList($fldName){
$fldInfoArray = $this->_tableInfo->findIn( 'name' , $fldName );
DynaCss::getEnumList() - APP/models/dyna_css.php, line 12
DynaCssController::add() - APP/controllers/dyna_css_controller.php, line 44
Object::dispatchMethod() - CORE/cake/libs/object.php, line 114
Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 256
Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 230
[main] - APP/webroot/index.php, line 90
Fatal error: Call to a member function findIn() on a non-object in /app/models/dyna_css.php on line 12