panels_extra_layouts.module 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * @file panels_extra_layouts.module
  4. * @author António P. P. Almeida <appa@perusio.net>
  5. * @date Wed Aug 3 04:36:20 2011
  6. *
  7. * @brief A module providing a series of extra layouts for Panels. Namely two
  8. * adaptive layouts.
  9. *
  10. *
  11. */
  12. /**
  13. * Implementation of hook_ctools_plugin_directory().
  14. *
  15. * It simply tells panels where to find the .inc files that define various
  16. * types of plugins: layouts, styles, args, contexts, content_types.
  17. *
  18. */
  19. function panels_extra_layouts_ctools_plugin_directory($owner, $plugin_type) {
  20. // We're just providing layout plugins.
  21. if ($owner == 'panels' && $plugin_type == 'layouts') {
  22. return "plugins/$plugin_type";
  23. }
  24. } // panels_extra_layouts_ctools_plugin_directory
  25. /**
  26. * The mapper function for computing the hash.
  27. *
  28. * @param $x integer
  29. * The index of the content array.
  30. * @return integer
  31. * The sum of all the 2nd digits in a given row of the content array.
  32. */
  33. function panels_extra_layouts_adaptive_get_digit($x) {
  34. // array_pop requires a variable. Doh no love for FP on PHP :(.
  35. $last_digit = sscanf($x, "%*1d%d");
  36. return empty($x) ? 0 : array_pop($last_digit);
  37. } // panels_extra_layouts_adaptive_get_digit
  38. /**
  39. * The hashing function is quite simple, hence imperfect. There are some
  40. * exceptions that are handled below to make the way the grid is filled
  41. * consistent.
  42. *
  43. * h(i) = \sum_{i}_{j = 1,n} get_digit(c(i,j))
  44. *
  45. * where i is the row number, j is the column number, c(i,j) is the content of
  46. * entry (i,j) of the content matrix and get_digit is a function that
  47. * returns m given a number nm, e.g., 41, returns 1.
  48. *
  49. * @param $indexes array
  50. * The indexes of a given row of the content array.
  51. * @param $columns integer
  52. * The number of columns in the current 'submatrix'.
  53. * @param $mapper_f string
  54. * The mapper function name.
  55. *
  56. * @return integer
  57. * The hash of a given row.
  58. */
  59. function panels_extra_layouts_adaptive_hash($indexes = array(), $columns = 3, $mapper_f) {
  60. // Bail out if there are no elements.
  61. if (empty($indexes)) return 0;
  62. // Count the number of elements in the given row.
  63. $n = count($indexes);
  64. // Compute the hash.
  65. $h = $n == 1 ? 1 : array_sum(array_map($mapper_f, $indexes));
  66. // For 4 columns there are a couple of special cases where the hash is
  67. // ambiguous.
  68. if ($h != 0 && $columns == 4) {
  69. // If we are in a special situation correct it. The hashing function is quite
  70. // naive. It's the sum of the column indexes in each row.
  71. // Some cases we get an incorrect value that needs to be corrected.
  72. if ($n == 2 && $h == 6) return 4;
  73. if ($n == 2 && ($h == 7 || ($h == 5 && panels_extra_layouts_adaptive_get_digit($indexes[1]) == 3)))
  74. return 3;
  75. }
  76. return $h;
  77. } // panels_extra_layouts_adaptive_hash