<action method="addItem">
<type>js</type>
<name>path/to/js.js</name>
<params><![CDATA[name="zz_new_name"]]></params>
</action>
2016-07-07
Change javascript order in magento 1.x
To change order of javascript files you need to add "params" tag to your xml layout:
2015-10-30
How to add external js to magento
Add to your layout:
<reference name="head">
<block type="core/text" name="my.external.js">
<action method="setText">
<text>
<![CDATA[<script type="text/javascript" src="https://domain.com/my_file.js"></script>]]>
</text>
</action>
</block>
</reference>
2015-10-29
How to show global messages in own controller/block
Add to your controller:
$this->_initLayoutMessages('customer/session');
after loadLayout():
$this->loadLayout();
$this->_initLayoutMessages('customer/session');
$this->renderLayout();
Then in phtml:
<?= $this->getMessagesBlock()->toHtml() ?>
2015-10-15
How to create table in magento installation script
$installer = $this;
$installer->startSetup();
$table = $installer->getConnection()
->newTable($installer->getTable('wmpayment/webmoney_response'))
->addColumn('id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
'identity' => true,
'unsigned' => true,
'nullable' => false,
'primary' => true,
), 'Id')
->addColumn('id_order', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
'unsigned' => true,
'nullable' => false
), 'Id order')
->addColumn('response', Varien_Db_Ddl_Table::TYPE_TEXT, null, array(
'nullable' => false,
), 'Response in json format')
->addIndex('id_order', 'id_order');
$installer->getConnection()->createTable($table);
2015-10-06
Change magento configuration programmatically
$configModel = Mage::getModel('core/config');
$configModel->saveConfig('path/to/config', 'new value');
2015-08-19
How to use regex route rewrite in zf1
Just add construction like this to your config file:
resources.router.routes.exhange.type = "Zend_Controller_Router_Route_Regex" resources.router.routes.exhange.route = "([a-zA-Z0-9]+)-to-([a-zA-Z0-9]+).html" resources.router.routes.exhange.defaults.module = default resources.router.routes.exhange.defaults.controller = index resources.router.routes.exhange.defaults.action = exchange resources.router.routes.exhange.map.1 = "from" resources.router.routes.exhange.map.2 = "to"Then you will be able to use links like: /one-to-two.html, where $_GET['from'] will be "one" and $_GET['to'] - "two".
2015-07-15
How to compare float numbers in php
/**
* Return true if $a equals to $b
*
* @param float $a
* @param float $b
*
* @return bool
*/
function isEqualFloat($a, $b)
{
$epsilon = 0.00001;
if (abs($a - $b) < $epsilon) {
return true;
}
return false;
}
2015-04-03
How to create table in bootstrap
Add this code:
And then you can create table:
$this->bootstrap('db');
$db = $this->getResource('db');
Zend_Registry::set('db', $db);
after
parent::__construct($application);in your bootstrap constructor.
And then you can create table:
$tableAuthSession = new Application_Model_DbTable_AuthSession();
2015-03-24
How to group WHERE clause in zf1
$db = $this->getAdapter();
$select = $db->select();
$select->from($this->_name);
$select->where('ip = ?', '120.0.0.1');
$select->orWhere('login = ?', 'test');
$subQuery = $select->getPart(Zend_Db_Select::WHERE);
$select->reset(Zend_Db_Select::WHERE);
$select->where(implode(' ', $subQuery));
$select->where('created_at = ?', date('Y-m-d'));
$res = $select->query()->fetchAll();
This will create sql with WHERE clause like as: "((ip = '127.0.0.1') OR (login = 'test')) AND (created_at = '2015-03-24')"
2015-02-23
Get helper from template (.phtml) in magento 2.0
Let's imagine that you have a helper app/code/SliRx/News/Helper/Date.php.
You can get the helper from template (.phtml file) like this:
You can get the helper from template (.phtml file) like this:
$helper = $this->helper('SliRx\News\Helper\Date');
2015-02-22
Changing page layout in magento 2.0
You can change page layout by adding:
$layout = '2columns-left'; $this->pageConfig->setPageLayout($layout);to your block's constructor.
2015-02-13
Magento google tag manager gtm
Google Tag Manager integration
With Google Tag Manager you can manage your Analytics events, Adwords conversion tracking, remarketing, etc. in one place. This module allows you to integrate Google Tag Manager to your site.All you need is insert your container id to configuration of this extension.
You can get Google Tag Manager Integration GTM for magento here
Where to configure the extension?
System > Configuration > Google Tag Manager > Configuration.Extension features:
- Support multi-store
- Support transactions (ecommerce tracking)
- Support remarketing
Transaction features:
- Support all product types
- You can specify a name of your shop for each store
2014-11-18
Sending email via mail function in base64
$messsage = base64_encode($message); $send = mail($address, "=?utf-8?B?".base64_encode($subject)."?=", $message, "Content-type:text/plain; charset=UTF-8\r\nContent-Transfer-Encoding: base64");
How to set subject with "ru" locale in mail function (for utf-8)
$subject = "Тема сообщения"; $subject = "=?utf-8?B?".base64_encode($subject)."?=";
2014-11-09
Access control list (ACL) in zend framework 1
For implementing ACL you have to create class, in which will be created roles, resources and settings permissions.
For this reason add to application/configs/application.ini:
Then create file library/App/Acl/Roles.php:
autoloadernamespaces.app = "App_"And create file library/App/Acl.php:
/**
* Class App_Acl
*/
class App_Acl extends Zend_Acl
{
function __construct()
{
$defaultResource = new Zend_Acl_Resource('default');
$adminResource = new Zend_Acl_Resource('admin');
$this->addResource($adminResource);
$this->addResource($defaultResource);
// guest/client resources
$this->addResource(new Zend_Acl_Resource('index'), $defaultResource);
$this->addResource(new Zend_Acl_Resource('payment'), $defaultResource);
$this->addResource(new Zend_Acl_Resource('panel'), $defaultResource);
$this->addResource(new Zend_Acl_Resource('user'), $defaultResource);
// admin resources
$this->addResource(new Zend_Acl_Resource('admin_user'), $adminResource);
$this->addResource(new Zend_Acl_Resource('admin_exchange'), $adminResource);
$this->addResource(new Zend_Acl_Resource('admin_rate'), $adminResource);
$this->addResource(new Zend_Acl_Resource('admin_eps'), $adminResource);
$this->addResource(new Zend_Acl_Resource('admin_page'), $adminResource);
$this->addRole(new Zend_Acl_Role(App_Acl_Roles::GUEST));
$this->addRole(new Zend_Acl_Role(App_Acl_Roles::CLIENT), App_Acl_Roles::GUEST);
$this->addRole(new Zend_Acl_Role(App_Acl_Roles::ADMIN), App_Acl_Roles::CLIENT);
$this->deny();
$this->allow(App_Acl_Roles::GUEST, 'index');
$this->allow(App_Acl_Roles::GUEST, 'payment');
$this->allow(App_Acl_Roles::CLIENT, 'user');
$this->allow(App_Acl_Roles::CLIENT, 'panel');
// Allow all to administrator
$this->allow(App_Acl_Roles::ADMIN);
}
/**
* Check if user has permission to the requested resource
*
* @param null $resource
* @param null $privilege
*
* @return bool Return true if user has permission
*/
public static function checkPermissions($resource = null, $privilege = null)
{
$acl = new App_Acl();
$auth = Zend_Auth::getInstance()->getIdentity();
$role = App_Acl_Roles::GUEST;
if (isset($auth->role) && $auth->role) {
$role = $auth->role;
}
return $acl->isAllowed($role, $resource, $privilege);
}
}
Note: your auth instance must contains 'role' property.Then create file library/App/Acl/Roles.php:
/**
* Class App_Acl_Roles
*/
class App_Acl_Roles
{
const ADMIN = 'admin';
const CLIENT = 'client';
const GUEST = 'guest';
}
From this moment you can add to your controller:
public function preDispatch()
{
parent::preDispatch();
if (!App_Acl::checkPermissions($this->getRequest()->getModuleName())) {
$this->redirect('/login');
}
}
And if user doesn't have permissions to the controller - it will be redirected to the login page or whatever you want.
2014-11-08
Writing errors/exceptions to a log file in zend framework 1
Add to application.ini
Don't forget to create application/data/logs/application.log and set writing permissions to it. Exceptions will be written to this file.
resources.log.stream.writerName = "Stream" resources.log.stream.writerParams.stream = APPLICATION_PATH "/data/logs/application.log" resources.log.stream.writerParams.mode = "a" resources.log.stream.filterName = "Priority" resources.log.stream.filterParams.priority = 5Add method to application/Bootstrap.php:
/**
* Error handler
*
* @param $errno
* @param $errstr
* @param $errfile
* @param $errline
*
* @throws ErrorException
*/
public function exceptionErrorHandler($errno, $errstr, $errfile, $errline)
{
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
and to constructor in the same file:
set_error_handler([$this, 'exceptionErrorHandler']);Then in application/controllers/ErrorController.php. Find method "errorAction" and replace:
$log->log($this->view->message, $priority, $errors->exception);
$log->log('Request Parameters', $priority, $errors->request->getParams());
with
$logMessage = $errors->exception->getMessage() . PHP_EOL .
$errors->exception->getTraceAsString() . PHP_EOL .
'Request Parameters:' . PHP_EOL .
var_export($errors->request->getParams(), true) . PHP_EOL .
str_repeat('-', 50) . PHP_EOL;
$log->log($logMessage, $priority, $errors->exception);
And in the same file change $priority = Zend_Log::CRIT; to $priority = Zend_Log::NOTICE;Don't forget to create application/data/logs/application.log and set writing permissions to it. Exceptions will be written to this file.
2014-11-06
How to enable ssl/https in zend framework 1
Add this to application/Bootstrap.php:
protected function _initForceSSL() {
if($_SERVER['SERVER_PORT'] != '443') {
header('Location: https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
exit();
}
}
2014-11-04
Localization in zend framework 1
In zf1 you can localize text, date, numbers and many other.
First of all you have to create folders for each locale. E.g. for russian locale: application/languages/ru_RU/, where ru - locale, RU - country.
Then you can create files with translations. As example create FormLabel.csv in your ru_RU folder:
But if you want to handy translate text in your templates add this function
And then in your phtml you can translate text:
First of all you have to create folders for each locale. E.g. for russian locale: application/languages/ru_RU/, where ru - locale, RU - country.
Then you can create files with translations. As example create FormLabel.csv in your ru_RU folder:
"Login:";"Логин:" "Password:";"Пароль:" "Password confirmation:";"Подтверждение пароля:" "First name:";"Имя:" "Last name:";"Фамилия:"Also you need to add locale initialization to your bootstrap file (application/Bootstrap.php):
protected function _initTranslate()
{
$locale = new Zend_Locale('ru_RU');
$registry = Zend_Registry::getInstance();
$registry->set('Zend_Locale', $locale);
$translate = new Zend_Translate(Zend_Translate::AN_CSV, APPLICATION_PATH . '/languages', null,
['scan' => Zend_Translate::LOCALE_DIRECTORY]);
$registry->set('Zend_Translate', $translate);
}
From this moment localization will be enabled and if you create form with label "Login:" it'll be displayed as "Логин:".But if you want to handy translate text in your templates add this function
function __()
{
return Zend_Registry::get('Zend_Translate')->translate(func_get_args());
}
after
require_once 'Zend/Application.php';to public/index.php.
And then in your phtml you can translate text:
<?= __('Password:') ?>
2014-10-30
Creating pagination in zf1
For creating pagination in zend framework 1 you have to perform several steps:
1) Set up pagination template.
Create file application/views/scripts/pagination.phtml:
For instance, you can create method getPaginator in your table model:
Add to application.ini:
Add in view template:
1) Set up pagination template.
Create file application/views/scripts/pagination.phtml:
<div>
<ul class="pagination">
<?php if (isset($this->previous)): ?>
<li><a href="<?= $this->url(array('page' => $this->first)); ?>"><span>«</span></a>
<?php else: ?>
<li class="disabled"><span>«</span></li>
<?php endif; ?>
<?php if (isset($this->previous)): ?>
<li><a href="<?= $this->url(array('page' => $this->previous)); ?>">←</a></li>
<?php else: ?>
<li class="disabled"><span>←</span></li>
<?php endif; ?>
<!-- Numbered page links -->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<li><a href="<?= $this->url(array('page' => $page)); ?>"><?= $page; ?></a></li>
<?php else: ?>
<li class="active"><span><?= $page; ?></span></li>
<?php endif; ?>
<?php endforeach; ?>
<!-- Next page link -->
<?php if (isset($this->next)): ?>
<li><a href="<?= $this->url(array('page' => $this->next)); ?>">→</a></li>
<?php else: ?>
<li class="disabled"><span>→</span></li>
<?php endif; ?>
<!-- Last page link -->
<?php if (isset($this->next)): ?>
<li><a href="<?= $this->url(array('page' => $this->last)); ?>">»</a></li>
<?php else: ?>
<li class="disabled"><span>»</span></li>
<?php endif; ?>
</ul>
</div>
2) Create an instance of Zend_Paginator with paginator adapter.For instance, you can create method getPaginator in your table model:
/**
* Class Application_Model_DbTable_Task
*/
class Application_Model_DbTable_Task extends Zend_Db_Table_Abstract
{
protected $_name = 'task';
protected $_rowClass = 'Application_Model_Task';
public function getPaginator(array $where = [])
{
$db = $this->getAdapter();
$select = $db->select();
$select->from($this->_name);
foreach ($where as $key => $value) {
$select->where($key, $value);
}
$select->order('created_at DESC');
$adapter = new Zend_Paginator_Adapter_DbSelect($select);
$paginator = new Zend_Paginator($adapter);
return $paginator;
}
}
and in your controller:
$user = Application_Model_User::getCurrent();
$tableTask = new Application_Model_DbTable_Task();
$paginator = $tableTask->getPaginator(['id_user = ?' => $user->id]);
$page = $this->getRequest()->getParam('page', 1);
$paginator->setCurrentPageNumber($page);
$config = $this->getInvokeArg('bootstrap')->getOptions();
$paginator->setItemCountPerPage($config['pagination']['per_page']);
$this->view->paginator = $paginator;
3) Configure pagination in settings file and in your bootstrap.Add to application.ini:
pagination.per_page = 10and in bootstrap:
protected function _initPagination()
{
Zend_Paginator::setDefaultScrollingStyle('Sliding');
Zend_View_Helper_PaginationControl::setDefaultViewPartial(
'pagination.phtml'
);
}
4) Print pagination html.Add in view template:
<?php if (count($this->paginator)): ?>where $item - a row from db
<?php foreach ($this->paginator as $item): ?>
<div class="item">
<?= $item['value'] ?> (<?= $item['state'] ?>)
</div>
<?php endforeach; ?>
<?php else: ?>
<div class="empty">
Nothing is found.
</div>
<?php endif; ?>
<?= $this->paginator ?>
Subscribe to:
Posts (Atom)