$subject = "Тема сообщения"; $subject = "=?utf-8?B?".base64_encode($subject)."?=";
2014-11-18
How to set subject with "ru" locale in mail function (for utf-8)
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 ?>
Multi checkbox or multiselect in zend framework 1
For creating a form with multi checkboxes you can add to your form:
And when you save data to db you have to:
$possibleExchange = $this->createElement('MultiCheckbox', 'possible_exchange');
$possibleExchange->setRequired(true)->setLabel('MyTitle')->addMultiOptions($items);
then in populating the form you have to do something like that:
$populateData = $epsCurrencyItem->toArray();
$populateData['possible_exchange'] = explode(',', $populateData['possible_exchange']);
$form->populate($populateData);
this is needed for selecting checkboxes.And when you save data to db you have to:
$data['possible_exchange'] = implode(',', $data['possible_exchange']);
$epsCurrencyItem->setFromArray($data);
$epsCurrencyItem->save();
Similarly you can use multiselect. Difference will be only when you create the form:
$possibleExchange = $this->createElement('multiselect', 'possible_exchange');
$possibleExchange->setRequired(true)
->setLabel('MyTitle')
->setAttrib('size', 10)
->addMultiOptions($items);
Subscribe to:
Posts (Atom)