public function testArraySortKeys()
 {
     // test array
     $testArray = array(-2 => 'Davy Hellemans', 1 => 'Tijs Verkoyen', 4 => 'Dave Lens');
     // expected result
     $expectedArray = array('Davy Hellemans', 'Tijs Verkoyen', 'Dave Lens');
     // perform test
     $this->assertEquals($expectedArray, SpoonFilter::arraySortKeys($testArray));
 }
Example #2
0
 /**
  * Builds & returns the pagination.
  *
  * @return	string
  * @param	string $URL
  * @param	int $offset
  * @param	string $order
  * @param	string $sort
  * @param	int $numResults
  * @param	int $numPerPage
  * @param	bool[optional] $debug
  * @param	string[optional] $compileDirectory
  */
 public static function getContent($URL, $offset, $order, $sort, $numResults, $numPerPage, $debug = true, $compileDirectory = null)
 {
     // current page
     $currentPage = ceil($offset / $numPerPage) + 1;
     // number of pages
     $numPages = ceil($numResults / $numPerPage);
     // load template
     $tpl = new SpoonTemplate();
     // compile directory
     if ($compileDirectory !== null) {
         $tpl->setCompileDirectory($compileDirectory);
     } else {
         $tpl->setCompileDirectory(dirname(__FILE__));
     }
     // force compiling
     $tpl->setForceCompile((bool) $debug);
     // previous url
     if ($currentPage > 1) {
         // label & url
         $previousLabel = self::$previous;
         $previousURL = str_replace(array('[offset]', '[order]', '[sort]'), array($offset - $numPerPage, $order, $sort), $URL);
         $tpl->assign('previousLabel', $previousLabel);
         $tpl->assign('previousURL', $previousURL);
     }
     // next url
     if ($currentPage < $numPages) {
         // label & url
         $nextLabel = self::$next;
         $nextURL = str_replace(array('[offset]', '[order]', '[sort]'), array($offset + $numPerPage, $order, $sort), $URL);
         $tpl->assign('nextLabel', $nextLabel);
         $tpl->assign('nextURL', $nextURL);
     }
     // limit
     $limit = 7;
     $breakpoint = 4;
     $items = array();
     /**
      * Less than or 7 pages. We know all the keys, and we put them in the array
      * that we will use to generate the actual pagination.
      */
     if ($numPages <= $limit) {
         for ($i = 1; $i <= $numPages; $i++) {
             $items[$i] = $i;
         }
     } else {
         // first page
         if ($currentPage == 1) {
             // [1] 2 3 4 5 6 7 8 9 10 11 12 13
             for ($i = 1; $i <= $limit; $i++) {
                 $items[$i] = $i;
             }
             $items[$limit + 1] = '...';
         } elseif ($currentPage == $numPages) {
             // 1 2 3 4 5 6 7 8 9 10 11 12 [13]
             $items[$numPages - $limit - 1] = '...';
             for ($i = $numPages - $limit; $i <= $numPages; $i++) {
                 $items[$i] = $i;
             }
         } else {
             // 1 2 3 [4] 5 6 7 8 9 10 11 12 13
             // define min & max
             $min = $currentPage - $breakpoint + 1;
             $max = $currentPage + $breakpoint - 1;
             // minimum doesnt exist
             while ($min <= 0) {
                 $min++;
                 $max++;
             }
             // maximum doesnt exist
             while ($max > $numPages) {
                 $min--;
                 $max--;
             }
             // create the list
             if ($min != 1) {
                 $items[$min - 1] = '...';
             }
             for ($i = $min; $i <= $max; $i++) {
                 $items[$i] = $i;
             }
             if ($max != $numPages) {
                 $items[$max + 1] = '...';
             }
         }
     }
     // init var
     $pages = array();
     // loop pages
     foreach ($items as $item) {
         // counter
         if (!isset($i)) {
             $i = 0;
         }
         // base details
         $pages[$i]['page'] = false;
         $pages[$i]['currentPage'] = false;
         $pages[$i]['otherPage'] = false;
         $pages[$i]['noPage'] = false;
         $pages[$i]['url'] = '';
         $pages[$i]['pageNumber'] = $item;
         // hellips
         if ($item == '...') {
             $pages[$i]['noPage'] = true;
         } else {
             // show page
             $pages[$i]['page'] = true;
             // current page ?
             if ($item == $currentPage) {
                 $pages[$i]['currentPage'] = true;
             } else {
                 // show the page
                 $pages[$i]['otherPage'] = true;
                 // url to this page
                 $pages[$i]['url'] = str_replace(array('[offset]', '[order]', '[sort]'), array($numPerPage * $item - $numPerPage, $order, $sort), $URL);
             }
         }
         // update counter
         $i++;
     }
     // first key needs to be zero
     $pages = SpoonFilter::arraySortKeys($pages);
     // assign pages
     $tpl->assign('pages', $pages);
     // cough it up
     ob_start();
     $tpl->display(dirname(__FILE__) . '/paging.tpl');
     return ob_get_clean();
 }
Example #3
0
 /**
  * Clear all (or a specific) elements in the breadcrumb
  *
  * @return	void
  * @param	int[optional] $key	If the key is provided it will be removed from the array, otherwise the whole array will be cleared.
  */
 public function clear($key = null)
 {
     // key given?
     if ($key !== null) {
         // remove specific key
         unset($this->items[(int) $key]);
         // resort, to avoid shit when parsing
         $this->items = SpoonFilter::arraySortKeys($this->items);
     } else {
         $this->items = array();
     }
 }
Example #4
0
 /**
  * Retrieve the columns sequence.
  *
  * @return	array
  */
 private function getColumnsSequence()
 {
     // init var
     $columns = array();
     // loop all the columns
     foreach ($this->columns as $column) {
         $columns[$column->getSequence()] = $column->getName();
     }
     // reindex
     return !empty($columns) ? SpoonFilter::arraySortKeys($columns) : $columns;
 }
Example #5
0
    /**
     * Get all the available extra's
     *
     * @return array
     */
    public static function getExtrasData()
    {
        // get all extras
        $extras = (array) BackendModel::getDB()->getRecords('SELECT i.id, i.module, i.type, i.label, i.data
			 FROM modules_extras AS i
			 INNER JOIN modules AS m ON i.module = m.name
			 WHERE i.hidden = ?
			 ORDER BY i.module, i.sequence', array('N'));
        // build array
        $values = array();
        // init var
        $itemsToRemove = array();
        // loop extras
        foreach ($extras as $id => $row) {
            // unserialize data
            $row['data'] = @unserialize($row['data']);
            // remove items that are not for the current language
            if (isset($row['data']['language']) && $row['data']['language'] != BackendLanguage::getWorkingLanguage()) {
                continue;
            }
            // set URL if needed
            if (!isset($row['data']['url'])) {
                $row['data']['url'] = BackendModel::createURLForAction('index', $row['module']);
            }
            // build name
            $name = SpoonFilter::ucfirst(BL::lbl($row['label']));
            if (isset($row['data']['extra_label'])) {
                $name = $row['data']['extra_label'];
            }
            if (isset($row['data']['label_variables'])) {
                $name = vsprintf($name, $row['data']['label_variables']);
            }
            // create modulename
            $moduleName = SpoonFilter::ucfirst(BL::lbl(SpoonFilter::toCamelCase($row['module'])));
            // build array
            if (!isset($values[$row['module']])) {
                $values[$row['module']] = array('value' => $row['module'], 'name' => $moduleName, 'items' => array());
            }
            // add real extra
            $values[$row['module']]['items'][$row['type']][$name] = array('id' => $row['id'], 'label' => $name);
        }
        // loop
        foreach ($values as &$row) {
            if (!empty($row['items']['widget'])) {
                $row['items']['widget'] = SpoonFilter::arraySortKeys($row['items']['widget']);
            }
            if (!empty($row['items']['block'])) {
                $row['items']['block'] = SpoonFilter::arraySortKeys($row['items']['block']);
            }
        }
        return $values;
    }