Code snippets

From SocialStrap Wiki
Jump to: navigation, search

Useful code snippets that you can use in your add-ons

Get main application options

echo Zend_Registry::get('config')->network_name;
echo Zend_Registry::get('config')->heartbeatfreq;
echo Zend_Registry::get('config')->facebook_appid;
echo Zend_Registry::get('config')->mail_host;
echo Zend_Registry::get('config')->username_minchars;

Overriding main config, usually within hook_app_init

// get application config
$config = Zend_Registry::get('config');

// override email adapter
$config->__set('mail_adapter', 'null');

// write config back
Zend_Registry::set('config', $config);

How to get paths, urls and redirect

    // base url
    var_dump( Application_Plugin_Common::getFullBaseUrl() );
    // request uri without server part
    var_dump( Zend_Controller_Front::getInstance()->getRequest()->getRequestUri() );
    // full current url
    var_dump( Application_Plugin_Common::getSafeUri() );
    // current addon's base url
    var_dump( Application_Plugin_Common::getFullBaseUrl().'/addons/'.basename(__DIR__) );
    // base url without server part (outside layout)
    $view = new Zend_View();
    var_dump( $view->baseUrl() );
    // base url without server part (inside layout only)
    var_dump( $this->baseUrl() );
    // get storage url for avatars (inside layout only)
    var_dump( $this->GetStorageUrl('avatar') );
    // current controller name
    var_dump( Zend_Controller_Front::getInstance()->getRequest()->getControllerName() );
    // in views
    var_dump( $this->php_controller );

    // current action name
    var_dump( Zend_Controller_Front::getInstance()->getRequest()->getActionName() );
    // in views
    var_dump( $this->php_action );
    // universal redirector (controller/action)

Getting data from the database


    $model = new Application_Model_Addons();
    $sql = "
    SELECT count(*) as count_users
    FROM profiles
    WHERE type = 'user'
    // most common operations: fetchOne, fetchAll, fetchRow, fetchAssoc, query
    $count_users = $model->getAdapter()->fetchOne($sql);
    echo "Counting ".$count_users ." users so far.";

Getting user's main data and meta values

$current_user_id = Zend_Auth::getInstance()->getIdentity()->id;
$current_user_email = Zend_Auth::getInstance()->getIdentity()->email;

$Profiles = new Application_Model_Profiles();
$user1 = $Profiles->getProfileRow('admin');
$user2 = $Profiles->getProfileByField('id', 105);

$ProfilesMeta = new Application_Model_ProfilesMeta();
$current_user_meta = $ProfilesMeta->getMetaValue('last_login');
$user_meta = $ProfilesMeta->getMetaValue('date_created', 105);

Using php session

    // no need to start the session, Zend will handle this
    $session = new Zend_Session_Namespace('Default');
    $session->test = 'Saving some string to session';
    // this could be another script
    $session = new Zend_Session_Namespace('Default');
    echo $session->test;

Using translator

    echo Zend_Registry::get('Zend_Translate')->translate('File not allowed or too big');
    echo sprintf(Zend_Registry::get('Zend_Translate')->translate('%s has mentioned you in a post'), 'George')
    // shorter
    $t = Zend_Registry::get('Zend_Translate');
    echo $t->translate('Cover Picture');
    echo $t->translate('Request a Review');

Sending messages

$current_user_id = Zend_Auth::getInstance()->getIdentity()->id;
$current_user_email = Zend_Auth::getInstance()->getIdentity()->email;

$admin_id = 1; // from admin, admin's id = 1

$content = 'Hi ho!';
// sending in-application private message
$Messages = new Application_Model_Messages();
	'type' => 'pm',
	'from_user_id' => $admin_id,
	'to_user_id' => $current_user_id,
	'content' => $content,
	'is_new' => 1,
	'is_hidden' => 0,
	'sent_on' => Application_Plugin_Common::now()
// sending the real email
$subject = 'Test';
Application_Plugin_Common::sendEmail($current_user_email, $subject, $content);


$popup_message = Zend_Registry::get('Zend_Translate')->translate('Not allowed!');
Application_Plugin_Alerts::error($popup_message, 'on');
Application_Plugin_Alerts::info($popup_message, 'off');
Application_Plugin_Alerts::success($popup_message, 'on');