PageRenderTime 24ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/pi.pipebomb.php

https://github.com/stedman/sd.pipebomb.ee_addon
PHP | 88 lines | 51 code | 16 blank | 21 comment | 6 complexity | 4c8ad9b4765f7a6ebdd6bb73ef1da95d MD5 | raw file
  1. <?php
  2. /*
  3. CHANGELOG:
  4. 1.0 2010-02-26
  5. * renamed parameters
  6. 0.3 2010-02-25
  7. * added cookie_switch parameter
  8. 0.2 2010-02-24
  9. * changed name from pi.language_pipe.php to pi.pipebomp.php
  10. * moved hard-coded attributes into plugin paramters
  11. 0.1 2010-02-09
  12. */
  13. $plugin_info = array(
  14. 'pi_name' => 'PipeBomb',
  15. 'pi_version' => '1.0',
  16. 'pi_author' => 'Steve Stedman',
  17. 'pi_author_url' => 'http://stedmandesign.com/',
  18. 'pi_description' => 'Run simple switch/case expressions within a small tag footprint.',
  19. 'pi_usage' => PipeBomb::usage()
  20. );
  21. class PipeBomb {
  22. var $return_data;
  23. function PipeBomb()
  24. {
  25. global $IN, $TMPL;
  26. $switch = $TMPL->fetch_param('switch');
  27. $switch_type = strtolower($TMPL->fetch_param('switch_type'));
  28. // here's where the name 'pipebomb' comes from:
  29. $cases = explode('|', $TMPL->fetch_param('cases') );
  30. $values = explode('|', $TMPL->fetch_param('values') );
  31. // extract the switch from a cookie
  32. if ($switch_type == 'cookie') {
  33. // toss the EE prefix, if there is one
  34. $switch = str_replace('exp_', '', $switch);
  35. $switch = $IN->GBL($switch, 'COOKIE');
  36. }
  37. // save switch position in cases as an array key
  38. $key = array_search($switch, $cases);
  39. // if no key found, default to the first item in the array
  40. $key = ($key === FALSE) ? 0 : $key;
  41. // if the array value at the provided key location is empty, default to the first item in the array
  42. $this->return_data = empty($values[$key]) ? $values[0] : $values[$key];
  43. }
  44. // ----------------------------------------
  45. // Plugin Usage
  46. // ----------------------------------------
  47. function usage()
  48. {
  49. ob_start();
  50. ?>
  51. SAMPLES
  52. --------------------
  53. Common usage:
  54. {exp:pipebomb switch="{dynamic_variable}" cases="1|2|3|4" values="Number One|Number Two|Number Three|Number Four"}
  55. In the example above, if the dynamic variable is 3, then "Number Three" shall be returned.
  56. Switch on ExpressionEngine cookies:
  57. {exp:pipebomb switch="language" switch_type="cookie" cases="|_es|_pg" values="Display English|Indicar Español|Mostrar Português"}
  58. If you need to get a cookie variable set by ExpressionEngine, set the switch_type to "cookie" and enter the cookie name in the switch parameter.
  59. PARAMETERS
  60. --------------------
  61. switch: the variable expression that will be used to search for a match in "cases"
  62. switch_type="cookie": [OPTIONAL] to get the value of an ExpressionEngine cookie (the value of "switch" should be the cookie name)
  63. cases: the piped fields for the "switch" to match
  64. values: the piped output that corresponds to the "cases" fields
  65. <?php
  66. $buffer = ob_get_contents();
  67. ob_end_clean();
  68. return $buffer;
  69. }
  70. // END
  71. }
  72. ?>