Code snippets for symfony 1.x

Navigation

Refine Tags

Snippets tagged "module"

Generate modules from all available doctrine models

As I didn't want to run doctrine:generate-module for all models over and over again I created a little task overwriting the default on. With this task you can create modules for all available models from project and plugins at once.

Add under PROJECT_DIR/lib/task the file sfDoctrineGenerateAllModulesTask.class.php with the following content:

<?php
require_once(sfConfig::get('sf_symfony_lib_dir').'/plugins/sfDoctrinePlugin/lib/task/sfDoctrineGenerateModuleTask.class.php');
 
class sfDoctrineGenerateAllModulesTask extends sfDoctrineGenerateModuleTask
{
    /**
   * @see sfTask
   */
  protected function configure()
  {
    $this->addArguments(array(
      new sfCommandArgument('application', sfCommandArgument::REQUIRED, 'The application name'),
    ));
 
    $this->addOptions(array(
      new sfCommandOption('theme', null, sfCommandOption::PARAMETER_REQUIRED, 'The theme name', 'default'),
      new sfCommandOption('generate-in-cache', null, sfCommandOption::PARAMETER_NONE, 'Generate the module in cache'),
      new sfCommandOption('non-verbose-templates', null, sfCommandOption::PARAMETER_NONE, 'Generate non verbose templates'),
      new sfCommandOption('with-show', null, sfCommandOption::PARAMETER_NONE, 'Generate a show method'),
      new sfCommandOption('singular', null, sfCommandOption::PARAMETER_REQUIRED, 'The singular name', null),
      new sfCommandOption('plural', null, sfCommandOption::PARAMETER_REQUIRED, 'The plural name', null),
      new sfCommandOption('route-prefix', null, sfCommandOption::PARAMETER_REQUIRED, 'The route prefix', null),
      new sfCommandOption('with-doctrine-route', null, sfCommandOption::PARAMETER_NONE, 'Whether you will use a Doctrine route'),
      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
      new sfCommandOption('actions-base-class', null, sfCommandOption::PARAMETER_REQUIRED, 'The base class for the actions', 'sfActions'),
    ));
 
    $this->namespace = 'doctrine';
    $this->name = 'generate-all-modules';
    $this->briefDescription = 'Generates all Doctrine modules';
 
    $this->detailedDescription = <<<EOF
The [doctrine:generate-all-modules|INFO] task generates all Doctrine modules:
 
  [./symfony doctrine:generate-all-modules frontend|INFO]
 
The task creates all modules in the [%application%|COMMENT] application
for all existing model classes [%model%|COMMENT].
 
You can also create empty modules that inherit their actions and templates from
a runtime generated module in [%sf_app_cache_dir%/modules/auto%module%|COMMENT] by
using the [--generate-in-cache|COMMENT] option:
 
  [./symfony doctrine:generate-all-modules --generate-in-cache frontend|INFO]
 
The generator can use a customized theme by using the [--theme|COMMENT] option:
 
  [./symfony doctrine:generate-all-modules --theme="custom" frontend|INFO]
 
This way, you can create your very own module generator with your own conventions.
 
You can also change the default actions base class (default to sfActions) of
the generated modules:
 
  [./symfony doctrine:generate-all-modules --actions-base-class="ProjectActions" frontend|INFO]
EOF;
  }
 
  /**
   * @see sfTask
   */
  protected function execute($arguments = array(), $options = array())
  {
    $databaseManager = new sfDatabaseManager($this->configuration);
 
    $properties = parse_ini_file(sfConfig::get('sf_config_dir').'/properties.ini', true);
 
    $models = $this->getModels();
 
    foreach ($models as $model) {
        foreach ($model as $name => $data) {
            $arguments['model'] = $name;
            $arguments['module'] = strtolower($name);
 
            $this->constants = array(
                'PROJECT_NAME'   => isset($properties['symfony']['name']) ? $properties['symfony']['name'] : 'symfony',
                'APP_NAME'       => $arguments['application'],
                'MODULE_NAME'    => $arguments['module'],
                'UC_MODULE_NAME' => ucfirst($arguments['module']),
                'MODEL_CLASS'    => $arguments['model'],
                'AUTHOR_NAME'    => isset($properties['symfony']['author']) ? $properties['symfony']['author'] : 'Your name here',
                );
 
            $method = $options['generate-in-cache'] ? 'executeInit' : 'executeGenerate';
 
            $this->$method($arguments, $options);
        }
    }   
  }
 
  protected function getModels() {
      $plugin_schemas = sfFinder::type('files')->name('schema.yml')->in(sfConfig::get('sf_plugins_dir'));
      $models = array();
 
      foreach($plugin_schemas as $schema) {
          $models[] = sfYaml::load($schema);
      }
 
      $models[] = sfYaml::load(sfConfig::get('sf_config_dir') . '/doctrine/schema.yml');
 
      return $models;
  }
}
 

Than just run: ./symfony doctrine:generate-all-modules %APPLICATION_NAME%

by Thomas Ohms on 2011-01-09, tagged doctrine  generate  module  task 

Nested Set Navigation Component Module (PART 2)

NestedSet Model for Navigation

model scheme

enhanced nested set model class

enhanced module action class

navigation list tabular cell template

Features:

enhanced module action class

/apps/backend/modules/navigation/actions/actions.class.php

<?php
 
/**
 * navigation actions.
 */
class navigationActions extends autonavigationActions
{
    public function executeList()
    {
 
        $this->typeOfList = 1;
 
        switch($this->typeOfList)
        {
            case 1:
                $pager = new Navigation();
                $this->pager = $pager->getTree(0,200);
                break;
            default:
 
                $this->processSort();
 
                $this->processFilters();
 
                // pager
 
                $navigation = new Navigation();
                $this->pager = new sfPropelPager('Navigation', 100);
                $c = new Criteria();
                $c->addAscendingOrderByColumn(NavigationPeer::TREE_LEFT);
 
                if($this->getUser()->hasCredential('SHOW_ALL_NAVIGATION_ROOTS')){
 
                } else {
                    $rootNodes = $navigation->getRootNodes();
                    $conditions = array();
                    foreach($rootNodes as $credentialName => $credential) {
                        if($this->getUser()->hasCredential($credentialName)){
                            $c1 = $c->getNewCriterion(NavigationPeer::TREE_LEFT, $credential["left"], Criteria::GREATER_THAN);
                            $c2 = $c->getNewCriterion(NavigationPeer::TREE_RIGHT, $credential["right"], Criteria::LESS_THAN);
                            $conditions[] = $c1->addAnd($c2);
                        }
                    }
                    if(count($conditions) > 1) {
                        foreach($conditions as $condition){
                            $c->addOr($condition);
                        }
                    } else {
                        $c->add($conditions[0]);
                    }
                }
 
 
                $this->addSortCriteria($c);
                $this->addFiltersCriteria($c);
                $this->pager->setCriteria($c);
 
                $this->pager->setPage($this->getRequestParameter('page', 1));
                $this->pager->init();
 
                break;
        }
    }
 
    public function executeShow() {
        $this->component = NavigationPeer::retrieveByPk($this->getRequestParameter('component_id'));
        $this->paths = $this->component->getPath();
    }
 
    public function executeBlockable() {
        $navigation = NavigationPeer::retrieveByPk($this->getRequestParameter('component_id'));
        $state = $this->getRequestParameter('state')==1 ? 1 : 0;
        $navigation->setIsBlocked($state);
        $navigation->save();
        $this->state = $state;
        $this->navigation = $navigation;
    }
 
    public function executeNavigatable() {
        $navigation = NavigationPeer::retrieveByPk($this->getRequestParameter('component_id'));
        $state = $this->getRequestParameter('state')==1 ? 1 : 0;
        $navigation->setIsNavigation($state);
        $navigation->save();
        $this->state = $state;
        $this->navigation = $navigation;
    }
 
 
    private function getPrimaryRoot(){
        return NavigationPeer::retrieveByPk(1);
    }
 
 
    // Default Root Node
    public function executeMakeRoot(){
 
        $root = new Navigation();
        $root->makeRoot();
        $root->save();
 
        return $this->redirect('navigation/edit?component_id='. $root->getNavigationId());
 
    }
 
    // Further Root Node
    public function executeMakeNextRoot(){
 
        $root = new Navigation();
        $root->makeNextRoot();
        $root->save();
 
 
        return $this->redirect('navigation/edit?component_id='. $root->getNavigationId());
 
    }
 
    // Delete Branch
    public function executeDeleteDescendants(){
        $navigation = NavigationPeer::retrieveByPk($this->getRequestParameter('component_id'));
        $navigation->deleteDescendants();
        $navigation = $navigation->reload();
        $navigation->delete();
        return $this->redirect('navigation/list');
    }
 
    // insert first child node
    public function executeInsertAsFirstChildOf(){
        $this->message = "Nun wird ein Kindelement erzeugt";
        $navigation = NavigationPeer::retrieveByPk($this->getRequestParameter('component_id'));
 
        $child = new Navigation();
        $child->insertAsFirstChildOf($navigation);
        $child->save();
        return $this->redirect('navigation/edit?component_id='. $child->getNavigationId());
 
    }
 
    // insert last child node
    public function executeInsertAsLastChildOf(){
 
        $this->message = "Nun wird ein Kindelement erzeugt";
 
        $navigation = NavigationPeer::retrieveByPk($this->getRequestParameter('component_id'));
 
        $child = new Navigation();
        $child->insertAsLastChildOf($navigation);
        $child->save();
 
 
        return $this->redirect('navigation/edit?component_id='. $child->getNavigationId());
 
    }
 
    // insert sibling before
    public function executeInsertAsNextSiblingOf(){
 
        $navigation = NavigationPeer::retrieveByPk($this->getRequestParameter('component_id'));
 
        $child = new Navigation();
        $child->insertAsNextSiblingOf($navigation);
        $child->save();
 
 
        return $this->redirect('navigation/edit?component_id='. $child->getNavigationId());
    }
 
    // insert sibling after
    public function executeInsertAsPrevSiblingOf(){
 
        $navigation = NavigationPeer::retrieveByPk($this->getRequestParameter('component_id'));
 
        $child = new Navigation();
        $child->insertAsPrevSiblingOf($navigation);
        $child->save();
 
        return $this->redirect('navigation/edit?component_id='. $child->getNavigationId());
    }
 
    /*Tree Modification*/
    // UP
    public function executeMoveToPrevSiblingOf(){
 
        $navigation = NavigationPeer::retrieveByPk($this->getRequestParameter('component_id'));
        $siblingNavigation = $navigation->retrievePrevSibling($navigation);
 
        $navigation->moveToPrevSiblingOf($siblingNavigation);
        $navigation->save();
 
        return $this->redirect('navigation/list');
 
    }
    //DOWN
    public function executeMoveToNextSiblingOf(){
 
        $navigation = NavigationPeer::retrieveByPk($this->getRequestParameter('component_id'));
        $siblingNavigation = $navigation->retrieveNextSibling($navigation);
 
        $navigation->moveToNextSiblingOf($siblingNavigation);
        $navigation->save();
 
        return $this->redirect('navigation/list');
 
    }
 
 
    // LEFT UP
    public function executeMoveLeftUp(){
 
        $navigation = NavigationPeer::retrieveByPk($this->getRequestParameter('component_id'));
        $parentNavigation = NavigationPeer::retrieveByPk($navigation->getParent());
        $navigation->moveToPrevSiblingOf($parentNavigation);
        $navigation->save();
        return $this->redirect('navigation/list');
    }
 
    // LEFT DOWN
    public function executeMoveLeftDown(){
 
        $navigation = NavigationPeer::retrieveByPk($this->getRequestParameter('component_id'));
        $parentNavigation = NavigationPeer::retrieveByPk($navigation->getParent());
        $navigation->moveToNextSiblingOf($parentNavigation);
        $navigation->save();
        return $this->redirect('navigation/list');
 
    }
 
    // RIGHT FIRST
    public function executeMoveRightPrev(){
 
        $navigation = NavigationPeer::retrieveByPk($this->getRequestParameter('component_id'));
        $prevSibling = $navigation->retrievePrevSibling($navigation);
        $navigation->moveToLastChildOf($prevSibling);
        $navigation->save();
        return $this->redirect('navigation/list');
    }
 
    // RIGHT LAST
    public function executeMoveRightNext(){
 
        $navigation = NavigationPeer::retrieveByPk($this->getRequestParameter('component_id'));
        $nextSibling = $navigation->retrieveNextSibling($navigation);
        $navigation->moveToLastChildOf($nextSibling);
        $navigation->save();
        return $this->redirect('navigation/list');
 
    }
 
    // ROOT PREV
    public function executeMoveRootPrev(){
        $navigation = NavigationPeer::retrieveByPk($this->getRequestParameter('component_id'));
        $rootNavigation = NavigationPeer::retrieveByPk($this->getRequestParameter('root_id'));
        $prevRootNavigation = $rootNavigation->retrievePrevSibling($rootNavigation);
        $navigation->moveToLastChildOf($prevRootNavigation);
        $navigation->save();
 
        return $this->redirect('navigation/list');
    }
 
    // ROOT NEXT
    public function executeMoveRootNext(){
        $navigation = NavigationPeer::retrieveByPk($this->getRequestParameter('component_id'));
        $rootNavigation = NavigationPeer::retrieveByPk($this->getRequestParameter('root_id'));
        $nextRootNavigation = $rootNavigation->retrieveNextSibling($rootNavigation);
        $navigation->moveToLastChildOf($nextRootNavigation);
        $navigation->save();
        return $this->redirect('navigation/list');
    }
 
    // Duplicate Tree
    public function executeCopyDescendants(){
 
        $navigation = NavigationPeer::retrieveByPk($this->getRequestParameter('component_id'));
        $parentNavigation = NavigationPeer::retrieveByPk($navigation->getParent());
 
        $newParentNavigation = $this->copyNode($navigation, $parentNavigation);
 
        $this->iterateOnDescendants($navigation->getDescendants() , $newParentNavigation);
 
        return $this->redirect('navigation/list');
    }
 
    private function iterateOnDescendants($navigations, $prevNavigation){
 
        foreach($navigations as $navigation){
            $actualNavigation = $this->copyNodeDescendants($navigation, $prevNavigation);
            $prevNavigation = $actualNavigation;
        }
    }
 
    private function transferData(Navigation $navigation){
 
        $newNavigation = new Navigation();
        $newNavigation->setName($navigation->getName());
        $newNavigation->setAction($navigation->getAction());
        $newNavigation->setParameter($navigation->getParameter());
        $newNavigation->setCaption($navigation->getCaption());
        $newNavigation->setDescription($navigation->getDescription());
        $newNavigation->setIsNavigation(0);
        $newNavigation->setIsBlocked(0);
        $newNavigation->setIsTemplate(0);
        return $newNavigation;
    }
 
    private function copyNodeDescendants($navigation, $prevNavigation){
 
        $newNavigation = $this->transferData($navigation);
 
        if($navigation->getLevel() == $prevNavigation->getLevel() ){
            $newNavigation->insertAsNextSiblingOf($prevNavigation);
        } elseif($navigation->getLevel() > $prevNavigation->getLevel()){
            $newNavigation->insertAsLastChildOf($prevNavigation);
        } else {
            $parentNavigation = NavigationPeer::retrieveByPk($prevNavigation->getParent());
            $newNavigation->insertAsNextSiblingOf($parentNavigation);
        }
        $newNavigation->save();
        return $newNavigation->reload();
    }
 
    private function copyNode($navigation, $parentNavigation){
 
        $newNavigation = $this->transferData($navigation);
 
        $newNavigation->insertAsLastChildOf($parentNavigation);
        $newNavigation->save();
 
        return $newNavigation->reload();
    }
 
}
 
by Thomas Schäfer on 2008-03-19, tagged module  navigation  nested  set 

Nested Set Navigation Component Module (PART 1)

NestedSet Model for Navigation

(This module is for administering big nested navigation trees, not for being used in a high performance web site. If you want to build a resultset for navigations then setup a custom query. There are enough examples out there for querying a nested set )

model scheme

enhanced nested set model class

enhanced module action class

navigation list tabular cell template

Features:

NavigationModel Scheme

  <table name="md_components" phpName="Navigation">
    <column name="component_id" type="INTEGER" size="11" primaryKey="true" required="true" autoIncrement="true"/>
    <column name="name" type="VARCHAR" size="50" required="true" phpName="Name"/>
    <column name="action" type="VARCHAR" size="50" phpName="Action"/>
    <column name="caption" type="VARCHAR" size="50" phpName="Caption"/>
    <column name="description" type="LONGVARCHAR" phpName="Description"/>
    <column name="parameter" type="VARCHAR" size="50" phpName="Parameter"/>
    <column name="image" type="VARCHAR" size="50" phpName="Image"/>
    <column name="tree_left" type="INTEGER" size="11" phpName="Left"/>
    <column name="tree_right" type="INTEGER" size="11" phpName="Right"/>
    <column name="tree_parent" type="INTEGER" size="11" default="0" phpName="Parent"/>
    <column name="tree_scope" type="INTEGER" size="11" default="0" phpName="Scope"/>
    <column name="created_at" type="TIMESTAMP" required="true" phpName="CreationDate"/>
    <column name="created_by" type="INTEGER" size="11" default="1" phpName="Creator"/>
    <column name="templatable" type="BOOLEAN" phpName="IsTemplate"/>
    <column name="blockable" type="BOOLEAN" phpName="IsBlocked"/>
    <column name="navigatable" type="BOOLEAN" phpName="IsNavigation"/>
    <column name="deleted_at" type="TIMESTAMP"/>
    <column name="deleted_by" type="INTEGER" size="11"/>
    <foreign-key foreignTable="sf_guard_user" name="creator" onDelete="setnull">
      <reference local="created_by" foreign="id"/>
    </foreign-key>
    <foreign-key foreignTable="sf_guard_user" name="FK_USER" onDelete="setnull">
      <reference local="deleted_by" foreign="id"/>
    </foreign-key>
  </table>
 

enhanced nested set model class

<?php
 
/**
 * Subclass for representing a row from the 'md_components' table.
 *
 *
 *
 * @package lib.model
 */
class Navigation extends BaseNavigation
{
    public function makeNextRoot()
    {
        $max = $this->getMaxRight();
        $this->setLeftValue($max + 1);
        $this->setRightValue($max + 2);
    }
 
    public function getNavigationId(){
        return $this->getComponentId();
    }
 
    public function getMaxRight(){
 
        $connection = Propel::getConnection();
        $query = 'SELECT MAX(%s) AS max FROM %s';
 
        $query = sprintf($query, NavigationPeer::TREE_RIGHT, NavigationPeer::TABLE_NAME);
        $statement = $connection->prepareStatement($query);
        $resultset = $statement->executeQuery();
        $resultset->next();
        return $resultset->getInt('max');
    }
 
    public function getMaxLeft(){
        $connection = Propel::getConnection();
        $query = 'SELECT MAX(%s) AS max FROM %s';
        $query = sprintf($query, NavigationPeer::TREE_LEFT, NavigationPeer::TABLE_NAME);
        $statement = $connection->prepareStatement($query);
        $resultset = $statement->executeQuery();
        $resultset->next();
        return $resultset->getInt('max');
    }
 
    public function getRootNodes(){
 
        $connection = Propel::getConnection();
        $query = 'SELECT * FROM %s WHERE %s %s %s;';
 
        $query = sprintf($query, NavigationPeer::TABLE_NAME, NavigationPeer::TREE_PARENT, Criteria::EQUAL, 0);
        $statement = $connection->prepareStatement($query);
        $resultset = $statement->executeQuery();
        $credential = array();
        foreach($resultset as $result){
            $credential[$result["name"]]["component_id"] = $result["component_id"];
            $credential[$result["name"]]["name"] = $result["name"];
            $credential[$result["name"]]["left"] = $result["tree_left"];
            $credential[$result["name"]]["right"] = $result["tree_right"];
        }
        return $credential;
    }
 
 
    public function getTree($offset = 0, $limit = 10, $options = null){
 
 
        $query = '
SELECT
    *,
    (
        SELECT
            CONCAT(COUNT(*))
        FROM md_components MA
        WHERE
        (
            (
                MA.TREE_LEFT < MM.TREE_LEFT AND MA.TREE_RIGHT > MM.TREE_RIGHT
            )
            AND MA.TREE_SCOPE = MM.TREE_SCOPE
        )
    )
    AS isLevel,
    (
        SELECT MZ4.COMPONENT_ID
                FROM md_components MZ4 , md_components MZ5
                WHERE MZ5.TREE_LEFT BETWEEN MZ4.TREE_LEFT AND MZ4.TREE_RIGHT
                AND MZ5.COMPONENT_ID = MM.COMPONENT_ID
                ORDER BY MZ4.TREE_LEFT  LIMIT 1,1
    )
    AS selfFirstLevel,
    (
        SELECT
            IF( MB.TREE_RIGHT - MB.TREE_LEFT > 1, 1, 0)
        FROM md_components MB
        WHERE MB.COMPONENT_ID = MM.COMPONENT_ID
        AND MB.TREE_SCOPE = MM.TREE_SCOPE
    )
    AS hasChilds,
    (
        SELECT MC.COMPONENT_ID FROM md_components MC
        WHERE (MM.TREE_LEFT = MC.TREE_RIGHT + 1)
        AND MC.TREE_SCOPE = MM.TREE_SCOPE
    )
    AS prevSibling,
    (
        SELECT
            MD.COMPONENT_ID
        FROM md_components MD
        WHERE (MM.TREE_RIGHT = MD.TREE_LEFT - 1)
        AND MD.TREE_SCOPE = MM.TREE_SCOPE
    )
    AS nextSibling,
    (
        SELECT
            IF
            (
                (
                    SELECT
                        IF
                        (
                            MQ.TREE_RIGHT - MQ.TREE_LEFT > 1,
                            1,
                            0
                        )
                    FROM md_components MQ
                    WHERE MQ.COMPONENT_ID = MM.COMPONENT_ID
                    AND MQ.TREE_SCOPE = MM.TREE_SCOPE
                ) = 1 ,
                ME.COMPONENT_ID,
                NULL
            )
        FROM md_components ME
        WHERE ME.TREE_LEFT = MM.TREE_LEFT + 1
        AND ME.TREE_SCOPE = MM.TREE_SCOPE
    )
    AS firstChild,
    (
        SELECT
            IF
            (
                (
                    SELECT
                        IF(
                            MS.TREE_RIGHT - MS.TREE_LEFT > 1,
                            1,
                            0
                        )
                    FROM md_components MS
                    WHERE MS.COMPONENT_ID = MM.COMPONENT_ID
                    AND MS.TREE_SCOPE = MM.TREE_SCOPE
                ) = 1 ,
                MF.COMPONENT_ID,
                NULL
            )
        FROM md_components MF
        WHERE MF.TREE_RIGHT = MM.TREE_RIGHT - 1
        AND MF.TREE_SCOPE = MM.TREE_SCOPE
 
    )
    AS lastChild,
    (
              SELECT
            MX.COMPONENT_ID
            FROM md_components MX
        WHERE MX.TREE_LEFT - 1 =
        (
                  SELECT
            MZ.TREE_RIGHT
            FROM md_components MZ
            WHERE (MZ.TREE_RIGHT > MM.TREE_RIGHT) AND (MZ.TREE_LEFT < MM.TREE_LEFT) AND (MZ.TREE_PARENT = 0)
            AND MZ.TREE_SCOPE = MM.TREE_SCOPE
        )
 
    )
    AS nextRoot,
    (
        SELECT
            MX.COMPONENT_ID
            FROM md_components MX
        WHERE MX.TREE_RIGHT + 1 =
        (
                  SELECT
            MZ2.TREE_LEFT
            FROM md_components MZ2
            WHERE (MZ2.TREE_RIGHT > MM.TREE_RIGHT) AND (MZ2.TREE_LEFT < MM.TREE_LEFT) AND (MZ2.TREE_PARENT = 0)
            AND MZ2.TREE_SCOPE = MM.TREE_SCOPE
        )
 
    )
    AS prevRoot,
    (
            SELECT
            MZ3.COMPONENT_ID
            FROM md_components MZ3
            WHERE (MZ3.TREE_RIGHT > MM.TREE_RIGHT) AND (MZ3.TREE_LEFT < MM.TREE_LEFT) AND (MZ3.TREE_PARENT = 0)
            AND MZ3.TREE_SCOPE = MM.TREE_SCOPE
 
    )
    AS selfRoot
 
FROM md_components MM
';
 
        $query .= ' ORDER BY MM.TREE_LEFT ASC ';
        $query .= 'LIMIT '. $offset . ', ' . $limit;
 
        $connection = Propel::getConnection();
        $statement = $connection->createStatement(  );
        $result = $statement->executeQuery( $query , ResultSet::FETCHMODE_NUM);
 
        $objects = NavigationPeer::populateObjects( $result );
 
        $phpNames = NavigationPeer::getFieldNames(BasePeer::TYPE_PHPNAME);
 
        $additionalColumns =
            array_merge(
                $phpNames,
                array("isLevel", "selfFirstLevel", "hasChilds", "prevSibling", "nextSibling", "firstChild", "lastChild", "prevRoot", "nextRoot", "selfRoot")
            );
 
        foreach($result as $key => $object){
            foreach($object as $column => $value){
                if($column > count($phpNames)-1){
                    $method = $additionalColumns[$column];
 
                    $objects[$key-1]->$method = $value;
                }
            }
        }
        return $objects;
    }
 
    /*no setter functions*/
    public function lastChild(){
        return $this->lastChild;
    }
 
    public function selfFirstLevel(){
        return $this->selfFirstLevel;
    }
 
    public function firstChild(){
        return $this->firstChild;
    }
 
    public function prevRoot(){
        return $this->prevRoot;
    }
 
    public function nextRoot(){
        return $this->nextRoot;
    }
 
    public function selfRoot(){
        return $this->selfRoot;
    }
 
    public function nextSibling(){
        return $this->nextSibling;
    }
 
    public function prevSibling(){
        return $this->prevSibling;
    }
 
    public function isLevel(){
        return $this->isLevel;
    }
 
    public function hasChilds(){
        return $this->hasChilds;
    }
}
 
$columns_map = array
(
    'left'   => NavigationPeer::TREE_LEFT,
    'right'  => NavigationPeer::TREE_RIGHT,
    'parent' => NavigationPeer::TREE_PARENT,
    'scope'  => NavigationPeer::TREE_SCOPE
);
 
sfPropelBehavior::add('Navigation', array('actasnestedset' => array('columns' => $columns_map)));
 
by Thomas Schäfer on 2008-03-19, tagged module  navigation  nested  set 

Use helpers from specific modules..

If you have come across the need to use a helper that is from another module, with this function you can do so by specifying the helper in this format "my_module/HelperName". If you do not specify a module, it will look in the current module name.

function my_use_helper()
{
  $args = func_get_args();
 
  foreach($args AS $arg)
  {
    if( strstr($arg, '/') )
    {
      $moduleName = substr($arg, 0, strpos($arg, '/'));
      $helper = substr($arg, strpos($arg, '/') + 1, strlen($arg));
    } else {
      $moduleName = sfContext::getInstance()->getModuleName();
      $helper = $arg;
    }
 
    sfLoader::loadHelpers(array($helper), $moduleName);
  } 
}
 
// Usage
my_use_helper('my_module/HelperName');
by Jonathan Wage on 2007-04-24, tagged helpers  module 

Flash and Request parameters outside of an action

I don't know about you, but I often farm out action logic--like building a datastructure to pass to a template--to static functions in a class in my application or module lib/ directory.

However, these functions don't typically have access to the shortcut methods for setting/getting flash and request parameters (etc.). Like:

$foo = $this->getRequestParameter('foo');

Unless I've missed something, the code to do something similar outside of an action is just a little messy, and quite repetitive if used more than once. So I add a few things to my project to make the job simpler.

/**
 * This class adds a few more useful functions to the sfUser instance
 */
class myBasicUser extends sfBasicSecurityUser
{
    const FLASH_NAMESPACE = 'symfony/flash';
 
    /** User flash parameters (stored in the session until the next request) */
    public function setFlash($name, $value)
    {
        $this->setAttribute($name, $value, self::FLASH_NAMESPACE);
    }
 
    public function getFlash($name, $default = null)
    {
        return $this->getAttribute($name, $default, self::FLASH_NAMESPACE);
    }
 
    public function hasFlash($name)
    {
        return $this->hasAttribute($name, self::FLASH_NAMESPACE);
    }
 
    /** User attribute parameters (stored in the session until removed) */
    public function removeAttribute($name, $ns = null)
    {
        $this->getAttributeHolder()->remove($name, $ns);
    }
 
    public function getAttributes($ns = null)
    {
        return $this->getAttributeHolder()->getAll($ns);
    }
 
    public function removeAttributes($ns = null)
    {
        $this->getAttributeHolder()->removeNamespace($ns);
    }
 
    /** User parameter parameters (erased after every request) */
    public function removeParameter($name, $ns = null)
    {
        $this->getParameterHolder()->remove($name, $ns);
    }
 
    public function getParameters($ns = null)
    {
        return $this->getParameterHolder()->getAll($ns);
    }
 
    public function removeParameters($ns = null)
    {
        $this->getParameterHolder()->removeNamespace($ns);
    }
}
 
class myUser extends myBasicUser
{
//...
}

*Note: add those files to your lib/ director as myBasicUser.class.php and myUser.class.php respectively.

That gives me an easy way to do some things with the user object, but now I need an easy way to get the user outside of an action and an easier way to get/set request parameters/attributes, etc. I defined a class in my application lib/ directory like this:

/**
 * Shortcuts to all request and user attribute setting/getting/has functions
 * This class simply makes repetitive tasks a few characters (and in some cases, lines) shorter to accomplish
 */
class myTools
{
    /** Get context singleton */
    public static function getContext()
    {
        return sfContext::getInstance();
    }
 
    /** Get request singleton */
    public static function getRequest()
    {
        return sfContext::getInstance()->getRequest();
    }
 
    /** Request parameter holder */
    public static function getRequestParameterHolder()
    {
        return sfContext::getInstance()->getRequest()->getParameterHolder();
    }
 
    /** Request attribute holder */
    public static function getRequestAttributeHolder()
    {
        return sfContext::getInstance()->getRequest()->getAttributeHolder();
    }
 
    /** Request parameters (read-only) */
    public static function getRequestParameter($name, $default = null)
    {
        return sfContext::getInstance()->getRequest()->getParameter($name, $default);
    }
 
    public static function hasRequestParameter($name)
    {
        return sfContext::getInstance()->getRequest()->hasParameter($name);
    }
 
    /** Request attributes (read/write - erased after every request) */
    public static function getRequestAttribute($name, $default = null)
    {
        return sfContext::getInstance()->getRequest()->getAttribute($name, $default);
    }
 
    public static function setRequestAttribute($name, $value)
    {
        sfContext::getInstance()->getRequest()->setAttribute($name, $value);
    }
 
    public static function hasRequestAttribute($name)
    {
        return sfContext::getInstance()->getRequest()->hasAttribute($name);
    }
 
    /** Get user singleton */
    public static function getUser()
    {
        return sfContext::getInstance()->getUser();
    }
 
    /** User attribute holder */
    public static function getAttributeHolder()
    {
        return sfContext::getInstance()->getUser()->getAttributeHolder();
    }
 
    /** User parameter holder */
    public static function getParameterHolder()
    {
        return sfContext::getInstance()->getUser()->getParameterHolder();
    }
 
    /** User flash parameters (stored in the session until the next request) */
    public static function setFlash($name, $value)
    {
        sfContext::getInstance()->getUser()->setFlash($name, $value);
    }
 
    public static function getFlash($name)
    {
        return sfContext::getInstance()->getUser()->getFlash($name);
    }
 
    public static function hasFlash($name)
    {
        return sfContext::getInstance()->getUser()->hasFlash($name);
    }
 
    /** User attribute parameters (stored in the session until removed) */
    public static function getAttribute($name, $default = null, $ns = null)
    {
        return sfContext::getInstance()->getUser()->getAttribute($name, $default, $ns);
    }
 
    public static function setAttribute($name, $value, $ns = null)
    {
        sfContext::getInstance()->getUser()->setAttribute($name, $value, $ns);
    }
 
    public static function removeAttribute($name, $ns = null)
    {
        sfContext::getInstance()->getUser()->removeAttribute($name, $ns);
    }
 
    public static function getAttributes($ns = null)
    {
        return sfContext::getInstance()->getUser()->getAttributes($ns);
    }
 
    public static function removeAttributes($ns = null)
    {
        sfContext::getInstance()->getUser()->removeAttributes($ns);
    }
 
    /** User parameter parameters (erased after every request) */
    public static function getParameter($name, $default = null, $ns = null)
    {
        return sfContext::getInstance()->getUser()->getParameter($name, $default, $ns);
    }
 
    public static function setParameter($name, $value, $ns = null)
    {
        sfContext::getInstance()->getUser()->setParameter($name, $value, $ns);
    }
 
    public static function removeParameter($name, $ns = null)
    {
        sfContext::getInstance()->getUser()->removeParameter($name, $ns);
    }
 
    public static function getParameters($ns = null)
    {
        return sfContext::getInstance()->getUser()->getParameters($ns);
    }
 
    public static function removeParameters($ns = null)
    {
        sfContext::getInstance()->getUser()->removeParameters($ns);
    }
 
    /** User credentials */
    public static function clearCredentials()
    {
        sfContext::getInstance()->getUser()->clearCredentials();
    }
 
    public static function listCredentials()
    {
        return sfContext::getInstance()->getUser()->listCredentials();
    }
 
    public static function removeCredential($credential)
    {
        sfContext::getInstance()->getUser()->removeCredential($credential);
    }
 
    public static function addCredential($credential)
    {
        sfContext::getInstance()->getUser()->addCredential($credential);
    }
 
    public static function addCredentials()
    {
        sfContext::getInstance()->getUser()->addCredentials(func_get_args());
    }
 
    public static function hasCredential($credential)
    {
        return sfContext::getInstance()->getUser()->hasCredential($credential);
    }
 
    /** User authentication */
    public static function isAuthenticated()
    {
        return sfContext::getInstance()->getUser()->isAuthenticated();
    }
 
    public static function setAuthenticated($authenticated)
    {
        sfContext::getInstance()->getUser()->setAuthenticated($authenticated);
    }
 
    /** User culture */
    public static function setCulture($culture)
    {
        sfContext::getInstance()->getUser()->setCulture($culture);
    }
 
    public static function getCulture()
    {
        return sfContext::getInstance()->getUser()->getCulture();
    }
}

There you have it. Now it's as simple as...

class myModuleLib
{
  public static function myFunc()
  {
    ...
    $foo = myTools::getRequestParameter('foo', 'default_value');
    myTools::setFlash($foo);
    ...
  }
}

Overkill...? Maybe. You may use any of these functions that are useful, and simply omit those you think are unnecessary. The goal here is to standardize (even more) the methods used to access attributes of the request and user, etc. I'm guessing this will be beneficial for beginners (if nothing else, as an example), and also for people (like me) who like ridiculously consistent code.

Any comments, including comments on naming conventions of the functions are appreciated.

by Stephen Riesenberg on 2006-11-19, tagged action  attributes  module  parameters  request  session  user 
(3 comments)

Get the module, action and parameters from a url

The problem

You want to get the module, action and parameters associated to a given url, pretty much as the routing system does.

The solution

First you will have to remove the part of the url which is not symfony specific. That part typically looks like yoursite.com/path/to/symfony. Once you've done that execute the following code:

$r = new sfRouting();
$r->setRoutes(sfRouting::getInstance()->getRoutes());
$params = $r->parse($myUrl);
$module = $params['module'];
$action = $params['action'];

Now the module and action associated to this url are as above in $module and $action and the parameters are the remaining elements of the array $params.

by Olivier Verdier on 2006-07-21, tagged action  module  parameters  routing  url 
(12 comments)