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:
<div>
    <ul class="pagination">
        <?php if (isset($this->previous)): ?>
            <li><a href="<?= $this->url(array('page' => $this->first)); ?>"><span>&laquo;</span></a>
        <?php else: ?>
            <li class="disabled"><span>&laquo;</span></li>
        <?php endif; ?>

        <?php if (isset($this->previous)): ?>
            <li><a href="<?= $this->url(array('page' => $this->previous)); ?>">&larr;</a></li>
        <?php else: ?>
            <li class="disabled"><span>&larr;</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)); ?>">&rarr;</a></li>
        <?php else: ?>
            <li class="disabled"><span>&rarr;</span></li>
        <?php endif; ?>
        <!-- Last page link -->
        <?php if (isset($this->next)): ?>
            <li><a href="<?= $this->url(array('page' => $this->last)); ?>">&raquo;</a></li>
        <?php else: ?>
            <li class="disabled"><span>&raquo;</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 = 10
and 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)): ?>
    <?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 ?>
where $item - a row from db

Multi checkbox or multiselect in zend framework 1

For creating a form with multi checkboxes you can add to your form:
$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);

2014-08-30

Sending POST/GET request with Zend Framework 1

For POST request:
$data = [
 'login' => 'admin',
 'password' => 'pass',
];
$client = new Zend_Http_Client('https://your.URL');
$client->setMethod(Zend_Http_Client::POST);
$client->setParameterPost($data);
$json = $client->request()->getBody();
For GET request:
$client = new Zend_Http_Client('https://blahblahblah.blah');
$client->setMethod(Zend_Http_Client::GET);
$client->request();

Get config in zf 1

From controller:
$config = $this->getInvokeArg('bootstrap')->getOptions();
then $config is associative array
From other places:
$config = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getOptions();

2014-08-22

How to add own class to library in ZF1

You have to add to your application.ini:
autoloadernamespaces.broker = "Broker_"
where Broker is your directory into library folder. As a result you can create class Broker_Object in /library/Broker/Object.php and it will be loaded by autoloader.

2014-08-14

How to add compare link to top links

First of all you have to create helper in your own module with two methods:
/**
 * Return label for comparing products list
 *
 * @return int
 */
public function getCompareLabel()
{
    $count = Mage::helper('catalog/product_compare')->getItemCollection()->getSize();
    return $this->__('Compare (%s)', $count);
}

/**
 * Return whether visible compare link
 *
 * @return bool
 */
public function getCompareLinkIsVisibleClass()
{
    $count = Mage::helper('catalog/product_compare')->getItemCollection()->getSize();

    if ($count > 0) {
        return '';
    }

    return 'class="no-display"';
}
Then add to layout xml (in block where you want to add link):

 
Where helper="slirx_ajaxpanel - your helper, module="slirx_ajaxpanel" - your module.

And in app.js (for rwd theme, for another you have to add this code to any js file that will be loaded on pages with your compare link)
$j('body').delegate('#header-account .compare', 'click', function(event) {
 var _url = $j(this).attr('href');
 popWin(_url, 'compare', 'top:0,left:0,width=820,height=600,resizable=yes,scrollbars=yes');

 event.preventDefault();
});

2014-08-07

Filter categories by store

Getting categories from specific store:
$storeId = 5;
$categoryId = Mage::app()->getStore($storeId)->getRootCategoryId();
$categories = Mage::getModel('catalog/category')->getCollection()
->addFieldToFilter('path', array('like'=> "1/$categoryId/%"))