Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gopalkumar315/75435c67d600a67bc264 to your computer and use it in GitHub Desktop.
Save gopalkumar315/75435c67d600a67bc264 to your computer and use it in GitHub Desktop.

Revisions

  1. @SchumacherFM SchumacherFM revised this gist Dec 4, 2012. 1 changed file with 24 additions and 0 deletions.
    24 changes: 24 additions & 0 deletions Namespace_Module_Block_Adminhtml_Edit_Tab_XXXX.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    <?php
    /* Set up: */

    class Namespace_Module_Block_Adminhtml_News_Edit_Tab_Linking {

    protected function _prepareForm(){

    ...

    $fieldSet->addField('product_categories', 'multiselect',
    array(

    'name' => 'product_categories[]',
    'label' => 'Product Categories',
    'title' => 'Product Categories',
    'required' => FALSE,
    'values' => Mage::getModel('namespace_module/option_productcategories')->getOptionArray()
    ));

    ...

    }

    }
  2. @SchumacherFM SchumacherFM created this gist Dec 4, 2012.
    66 changes: 66 additions & 0 deletions ProductcategoriesGist.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    <?php

    class Namespace_Module_Model_Option_Productcategories extends Varien_Object
    {

    const REPEATER = '_';

    const PREFIX_END = '';

    protected $_options = array();

    /**
    * @param int $parentId
    * @param int $recursionLevel
    *
    * @return array
    */
    public function getOptionArray($parentId = 1, $recursionLevel = 3)
    {
    $recursionLevel = (int)$recursionLevel;
    $parentId = (int)$parentId;

    $category = Mage::getModel('catalog/category');
    /* @var $category Mage_Catalog_Model_Category */
    $storeCategories = $category->getCategories($parentId, $recursionLevel, TRUE, FALSE, TRUE);

    foreach ($storeCategories as $node) {
    /* @var $node Varien_Data_Tree_Node */

    $this->_options[] = array(

    'label' => $node->getName(),
    'value' => $node->getEntityId()
    );
    if ($node->hasChildren()) {
    $this->_getChildOptions($node->getChildren());
    }

    }

    return $this->_options;
    }

    /**
    * @param Varien_Data_Tree_Node_Collection $nodeCollection
    */
    protected function _getChildOptions(Varien_Data_Tree_Node_Collection $nodeCollection)
    {

    foreach ($nodeCollection as $node) {
    /* @var $node Varien_Data_Tree_Node */
    $prefix = str_repeat(self::REPEATER, $node->getLevel() * 1) . self::PREFIX_END;

    $this->_options[] = array(

    'label' => $prefix . $node->getName(),
    'value' => $node->getEntityId()
    );

    if ($node->hasChildren()) {
    $this->_getChildOptions($node->getChildren());
    }
    }
    }

    }