First of all, excuse my poor english.

Well, I need to develop a  homepage for my job. They want a dynamic
homepage similar to Wordpress. A homepage formed by differents parts
like a dashboard, showing specific  information about the whole
website, and ordering/showing these parts in different ways. In
addition, they could add new parts in the homepage at future easily.

I thought a lot about the best way to do it, and I've based it on this
post
http://teknoid.wordpress.com/2008/12/16/how-to-build-a-dashboard-for-your-application-in-cakephp/
of teknoid blog.

I named this part 'widget'. Every widget could have its own
propierties that would be stored on a DB table. For example, a
recurrent propierty of a widget is the number of elements (news,
videos, ...) that could be changed by web administrators.

My solution is more or less this:

I created a model 'widget' and «submodels» that extends widget:
'articles_widget', 'recipe_widget', 'video_widget', and so on, with a
getData() function on it, to return the data needed to the view (don't
worry about this part yet, maybe every widget will have a element to
show its data).

class Widget extends AppModel {
        var $name = 'Widget';

        function getWidgets($all = null) {
                $conditions = array();
                if (!$all) {
                        $all = false;
                        $conditions['show'] = 1;
                }

                return $this->find('all', array('conditions' => $conditions, 
'order'
=> 'order ASC'));
        }

        function getData(&$widget) {
                return ClassRegistry::init($widget['Widget']['type'])->getData
($widget);
        }
}


class ArticleWidget extends Widget {
        var $name = 'ArticleWidget';
        var $primaryKey = 'widget_id';

        function getData(&$widget) {
                $aw = $this->read('num_articles', $widget['Widget']['id']);
                $news = 
ClassRegistry::init('Article')->getLast($aw['ArticleWidget']
['num_articles']);
                return $news;
        }

}

A concret widget might not use a table in DB, or even have a related
model (not often).

class RecipeWidget extends Widget {
        var $name = 'RecipeWidget';
        var $primaryKey = 'widget_id';
        var $useTable = false;

        function getData(&$widget) {
                $recipe = ClassRegistry::init('Recipe')->randomRecipe();
                return $recipe;
        }

}

The dashboard controller is easy. I get all widgets and, for each
widget I will call getData() function.

function index() {
               $widgets = $this->Widget->getWidgets();
                foreach ($widgets as $widget) {
                        $this->set($widget['Widget']['varname'], 
$this->Widget->getData
($widget));
                }
}

I have some doubts about it:

- Is it really the best way to do it?
- Can I use cache in models to limit the requests to the DB? I know
about cache in elements but I'll only use elements to show the data
passed from the dashboard controller.
- Is there any other solution better than I'm using at getData()
function to call a subclasse function?

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to