Advertisement
Guest User

Magento Page Head Sorting

a guest
Sep 26th, 2012
1,329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.92 KB | None | 0 0
  1. ----------------------------------------------------------------------
  2. Package/Module/Helper/Data.php
  3. ----------------------------------------------------------------------
  4.  
  5. class Package_Module_Helper_Data extends Mage_Core_Helper_Abstract
  6. {
  7.     /*
  8.      * Inserts array at given position and shifts elements down
  9.      *
  10.      * @param integer $position
  11.      * @param array $array
  12.      * @param array $element
  13.      */
  14.     public function array_insert($position=null,&$array,$element) {
  15.         if (count($array) == 0) {
  16.             $array[] = $element;
  17.         }
  18.         elseif (is_numeric($position) && $position < 0) {
  19.             if((count($array)+position) < 0) {
  20.                 $array = array_insert($array,$element,0);
  21.             }
  22.             else {
  23.                 $array[count($array)+$position] = $element;
  24.             }
  25.         }
  26.         elseif (is_numeric($position) && isset($array[$position])) {
  27.             $part1 = array_slice($array,0,$position,true);
  28.             $part2 = array_slice($array,$position,null,true);
  29.             $array = array_merge($part1,array($position=>$element),$part2);
  30.             foreach($array as $key=>$item) {
  31.                 if (is_null($item)) {
  32.                     unset($array[$key]);
  33.                 }
  34.             }
  35.         }
  36.         elseif (is_null($position)) {
  37.             $array[] = $element;
  38.         }
  39.         elseif (!isset($array[$position])) {
  40.             $array[$position] = $element;
  41.         }
  42.         $array = array_merge($array);
  43.         return $array;
  44.     }
  45. }
  46.  
  47. ----------------------------------------------------------------------
  48. Package/Module/Block/Page/Html/Head.php
  49. ----------------------------------------------------------------------
  50.  
  51. class Package_Module_Block_Page_Html_Head extends Mage_Page_Block_Html_Head
  52. {
  53.    
  54.     public function addJs($name, $params = "", $sort_order = null)
  55.     {
  56.         $this->addItem('js', $name, $params, null, null, $sort_order);
  57.         return $this;
  58.     }
  59.    
  60.     public function addItem($type, $name, $params=null, $if=null, $cond=null, $sort_order = null)
  61.     {
  62.         if ($type==='skin_css' && empty($params)) {
  63.             $params = 'media="all"';
  64.         }
  65.  
  66.         // If sort order is defined then do it
  67.         if($sort_order>=0) {
  68.             $helper = Mage::helper('jquerylib');
  69.             $helper->array_insert($sort_order, $this->_data['items'],array(
  70.                 'type'   => $type,
  71.                 'name'   => $name,
  72.                 'params' => $params,
  73.                 'if'     => $if,
  74.                 'cond'   => $cond
  75.             ));
  76.  
  77.         // Or else append item
  78.         } else {
  79.             $this->_data['items'][$type.'/'.$name] = array(
  80.                 'type'   => $type,
  81.                 'name'   => $name,
  82.                 'params' => $params,
  83.                 'if'     => $if,
  84.                 'cond'   => $cond,
  85.             );
  86.         }
  87.         return $this;
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement