1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
- /**
- * @file panels_extra_layouts.module
- * @author António P. P. Almeida <appa@perusio.net>
- * @date Wed Aug 3 04:36:20 2011
- *
- * @brief A module providing a series of extra layouts for Panels. Namely two
- * adaptive layouts.
- *
- *
- */
- /**
- * Implementation of hook_ctools_plugin_directory().
- *
- * It simply tells panels where to find the .inc files that define various
- * types of plugins: layouts, styles, args, contexts, content_types.
- *
- */
- function panels_extra_layouts_ctools_plugin_directory($owner, $plugin_type) {
- // We're just providing layout plugins.
- if ($owner == 'panels' && $plugin_type == 'layouts') {
- return "plugins/$plugin_type";
- }
- } // panels_extra_layouts_ctools_plugin_directory
- /**
- * The mapper function for computing the hash.
- *
- * @param $x integer
- * The index of the content array.
- * @return integer
- * The sum of all the 2nd digits in a given row of the content array.
- */
- function panels_extra_layouts_adaptive_get_digit($x) {
- // array_pop requires a variable. Doh no love for FP on PHP :(.
- $last_digit = sscanf($x, "%*1d%d");
- return empty($x) ? 0 : array_pop($last_digit);
- } // panels_extra_layouts_adaptive_get_digit
- /**
- * The hashing function is quite simple, hence imperfect. There are some
- * exceptions that are handled below to make the way the grid is filled
- * consistent.
- *
- * h(i) = \sum_{i}_{j = 1,n} get_digit(c(i,j))
- *
- * where i is the row number, j is the column number, c(i,j) is the content of
- * entry (i,j) of the content matrix and get_digit is a function that
- * returns m given a number nm, e.g., 41, returns 1.
- *
- * @param $indexes array
- * The indexes of a given row of the content array.
- * @param $columns integer
- * The number of columns in the current 'submatrix'.
- * @param $mapper_f string
- * The mapper function name.
- *
- * @return integer
- * The hash of a given row.
- */
- function panels_extra_layouts_adaptive_hash($indexes = array(), $columns = 3, $mapper_f) {
- // Bail out if there are no elements.
- if (empty($indexes)) return 0;
- // Count the number of elements in the given row.
- $n = count($indexes);
- // Compute the hash.
- $h = $n == 1 ? 1 : array_sum(array_map($mapper_f, $indexes));
- // For 4 columns there are a couple of special cases where the hash is
- // ambiguous.
- if ($h != 0 && $columns == 4) {
- // If we are in a special situation correct it. The hashing function is quite
- // naive. It's the sum of the column indexes in each row.
- // Some cases we get an incorrect value that needs to be corrected.
- if ($n == 2 && $h == 6) return 4;
- if ($n == 2 && ($h == 7 || ($h == 5 && panels_extra_layouts_adaptive_get_digit($indexes[1]) == 3)))
- return 3;
- }
- return $h;
- } // panels_extra_layouts_adaptive_hash
|