Snippets: resolving recurring issues

Simple and fast solutions. DVelum 0.9.x

Define a page path based on link building rules

Request::url(array('controller','action','subaction'),true);

The standard configuration uses «/» as a separator and «.html» as an extension; The following path will be built: /controller/action/subaction.html

Define a path to the public page using a certain functionality (module)

Assuming ‘news’ functionality is attached to the page with newspage code and we need to define the path to the second news page:

$frontendRouter = new Frontend_Router();
$url = Request::url(array($frontendRouter->findUrl('news'),2),true);

The following path will be built: /news/2.html

Define whether a user is authorized or not

$user = User::getInstance();
$authorized = $user->isAuthorized();

Authorize user by login and password

$user = User::login($login, $password);

If both login and password are correct, $user will contain a link to the User object, otherwise, false

Extract a column from a (applicable for database query results)

$data = array(
	array('id'=>10,'name'=>'Name1','group'=>'admin'),
	array('id'=>22,'name'=>'Name2','group'=>'admin'),
	array('id'=>34,'name'=>'Name3','group'=>'dev'),
	array('id'=>45,'name'=>'Name4','group'=>'admin'),
	array('id'=>52,'name'=>'Name5','group'=>'dev'),
	array('id'=>61,'name'=>'Name6','group'=>'user'),
);
$ids = Utils::fetchCol('id' , $data);
print_r($ids);

Array
(
    [0] => 10
    [1] => 22
    [2] => 34
    [3] => 45
    [4] => 52
    [5] => 61
)

Reindex a multidimensional array (applicable for database query results)

$data = array(
	array('id'=>10,'name'=>'Name1','group'=>'admin'),
	array('id'=>22,'name'=>'Name2','group'=>'admin'),
	array('id'=>34,'name'=>'Name3','group'=>'dev'),
	array('id'=>45,'name'=>'Name4','group'=>'admin'),
	array('id'=>52,'name'=>'Name5','group'=>'dev'),
	array('id'=>61,'name'=>'Name6','group'=>'user'),
);
$ids = Utils::rekey('id' , $data);
print_r($ids);

Array
(
    [10] => Array([id] => 10 [name] => Name1 [group] => admin)
    [22] => Array( [id] => 22 [name] => Name2 [group] => admin)
    [34] => Array( [id] => 34 [name] => Name3 [group] => dev)
    [45] => Array([id] => 45[name] => Name4[group] => admin)
    [52] => Array([id] => 52[name] => Name5[group] => dev)
    [61] => Array([id] => 61[name] => Name6[group] => user)
)

Regroup multidimensional array data (applicable for database query results)

$data = array(
	array('id'=>10,'name'=>'Name1','group'=>'admin'),
	array('id'=>22,'name'=>'Name2','group'=>'admin'),
	array('id'=>34,'name'=>'Name3','group'=>'dev'),
	array('id'=>45,'name'=>'Name4','group'=>'admin'),
	array('id'=>52,'name'=>'Name5','group'=>'dev'),
	array('id'=>61,'name'=>'Name6','group'=>'user'),
);
$ids = Utils::groupByKey('group' , $data);
print_r($ids);
Array
(
     [admin] => Array (
         [0] => Array( [id] => 10 [name] => Name1 [group] => admin)
         [1] => Array([id] => 22 [name] => Name2 [group] => admin)
         [2] => Array([id] => 45 [name] => Name4 [group] => admin)
     )
     [dev] => Array (
         [0] => Array([id] => 34 [name] => Name3 [group] => dev)
         [1] => Array([id] => 52 [name] => Name5 [group] => dev)
     )
     [user] => Array (
         [0] => Array([id] => 61 [name] => Name6 [group] => user)
     )
)

Get hash using system hash function (uses ‘salt’ available in the configuration file)

Is applied for password hashing:

$hash = Utils::hash('string');

The method should be run upon initializing the Application class. Otherwise, manually set ‘salt’ calling the following method:

Utils::setSalt('somestring');

Get configuration object. Connect php configuration file (containing an array)

Get news configuration object located here: ./system/app/config/news.php

/**
 * @var Config_Abstract
 */
$someCfg = Config::factory(Config::File_Array, './system/app/config/news.php');

Get an object containing the main configuration (main Config)

/**
 * @var Config_Abstract
 */
$cfg = Registry::get('main','config');

The configuration is stored in the register and is put there by index.php file.

Count the number of objects of a certain type. Count the number of database rows

Get the number of the Active user profiles (User object)

$count = Model::factory('user')->getCount(array('enabled'=>1));

Object data query (get DB rows)

/*
 * Query parameters: get 10 rows sorted by news_date in descending order
 */
$params = array(
	'sort'=>'news_date',
	'dir'=>'DESC',
	'start'=>0,
	'limit'=>10
);	
/*
 * Filters: only published records
 */
$filters = array('published'=>true);
/*
 * List of fields, which need to be selected
 */
$fields = array('id','title','news_date');
/*
 * Initialize the model
 */
$model = Model::factory('News');
$data = $model->getList($params , $filters , $fields);

Get link to the instantiated adapter connecting to the database

// Default adapter connecting to the database
/**
 * @var Zend_Db_Adapter_Abstract
 */
$db = Model::getGlobalDbConnection();
// Adapter connecting to the database and used by User model 	
/**
 * @var Zend_Db_Adapter_Abstract
 */
$db = Model::factory('user')->getDbConnection();

Get link to the instantiated cache adapter

// Get cache adapter
$cm = new Cache_Manager();
/**
 * @var Cache_Abstract | false
 */
$cache = $cm->get('data');

GInsert HTML into the central part of a page not referred to the template (public interface)

$page = Page::getInstance();
$page->text.=’Some text’;

Attention: some objects already contain a link to Page object. For instance, you can add the following code to the frontend controller:

class Frontend_Controller_News extends Frontend_Controller
{
    public function someAction()
    {
        $this->_page->text.=’Some text’;
    }
}

Changing HTML of a page title and meta content

class Frontend_Controller_News extends Frontend_Controller
{
    public function someAction()
    {
         // adding text 
         $this->_page->text.=’Some text’;
         // changing html title of a page (the one displayed in the browser window or tab address bar)
         $this->_page->html_title = 'Page HTML Title';
         // changing page title (usually is entered within the tag)
         $this->_page->page_title = 'Page Title'; 
         // changing tag content in
         $this->_page->meta_keywords = 'page meta key words';
         // changing tag content in 
         $this->_page->meta_description = 'page meta description';
     }
 }

Template. Display blocks assigned to a certain location (placeholder)

Blocks are displayed in templates. We will need a link to the block manager. it will be automatically passed to layout.php template. if you need block manager at the embedded level of templates, pass the link to block manager before rendering. The layout configuration contains a number of placeholders to assign blocks to from the admin panel. Each placeholder has its own code, which can used to show blocks in a template.

$blockManager = $this->get('blockManager');
echo $blockManager->getBlocksHtml('placecode');