/plugins/bricks/tags/0.7-DP3/bricks.plugin.php

https://github.com/somefool/habari-extras · PHP · 155 lines · 73 code · 19 blank · 63 comment · 17 complexity · b9c69f6ab8e778a5430e615f3bb6a631 MD5 · raw file

  1. <?php
  2. /**
  3. * A plugin to use predefined and user-generated text bricks.
  4. *
  5. **/
  6. class Bricks extends Plugin
  7. {
  8. private $bricks_found;
  9. private $post;
  10. private $bricks = array();
  11. /**
  12. * Set up some default options
  13. *
  14. **/
  15. public function action_plugin_activation($file)
  16. {
  17. }
  18. /**
  19. * Create plugin configuration menu entry
  20. **/
  21. public function filter_plugin_config( $actions, $plugin_id )
  22. {
  23. if ( $plugin_id == $this->plugin_id() ) {
  24. $actions[] = _t('Configure');
  25. }
  26. return $actions;
  27. }
  28. /**
  29. * Create plugin configuration
  30. **/
  31. public function action_plugin_ui($plugin_id, $action)
  32. {
  33. if ($plugin_id == $this->plugin_id()) {
  34. switch($action) {
  35. case _t('Configure'):
  36. $ui = new FormUI('postfields');
  37. $ui->append('static', 'typelabel', _t('Add bricks', array('keine ahnung','unsicher')));
  38. $ui->append('textmulti', 'bricks', 'bricks_bricks', 'Available bricks:');
  39. $ui->append('submit', 'submit', 'Submit');
  40. $ui->out();
  41. }
  42. }
  43. }
  44. // DEBUGGING CRAP
  45. // Displays the available bricks in the publish form
  46. // No functionality, only for viewing
  47. // Will be replaced by something better later
  48. /*
  49. public function action_form_publish($form, $post)
  50. {
  51. $bricks = Options::get('bricks_bricks');
  52. if(!is_array($bricks) || count($bricks) == 0) {
  53. return;
  54. }
  55. $output = '';
  56. $control_id = 0;
  57. $postbricks = $form->publish_controls->append('fieldset', 'postbricks', 'Available bricks');
  58. foreach($bricks as $brick)
  59. {
  60. //skip invalid bricks
  61. if(!strpos($brick,'=')) continue;
  62. list($bricktext, $brickvalue) = explode("=",$brick,2);
  63. $control_id = md5($bricktext);
  64. $fieldname = "postbrick_{$control_id}";
  65. $custombrick = $postbricks->append('text', $fieldname, 'null:null', $bricktext);
  66. $custombrick->value = $brickvalue;
  67. $custombrick->template = 'tabcontrol_text';
  68. }
  69. }*/
  70. /**
  71. * Add help text to plugin configuration page
  72. **/
  73. public function help()
  74. {
  75. // eeh? How is this used to display sourcecode?
  76. $help = _t( 'Usage: Enter brickname=brickvalue in one of the fields in the configuration. Brickname can contain a-Z, 0-9, underscores and dashes. Brickvalue can be anything. Be careful with html, Habari sometimes messes it up. In post content, use {brickname} and it will be replaced with brickvalue automatically when the post is displayed. {i} is a predefined brick that inserts your current Habari base path so you can create internal links using {i}slug.', 'bricks' );
  77. return $help;
  78. }
  79. /**
  80. * Nothing to do here atm
  81. **/
  82. public function action_init()
  83. {
  84. }
  85. /**
  86. * This is where the stuff happens.
  87. * All bricks in the post content will be replaced with their values.
  88. **/
  89. public function filter_post_content( $content, $post )
  90. {
  91. // If we're on the publish page, replacement will be destructive.
  92. // We don't want that, so return here.
  93. $handler = Controller::get_handler();
  94. if ( isset( $handler->action ) && $handler->action == 'admin' && isset($handler->handler_vars['page']) && $handler->handler_vars['page'] == 'publish' ) {
  95. return $content;
  96. }
  97. // Get available bricks
  98. $bricklist = Options::get('bricks_bricks');
  99. foreach($bricklist as $brickentry)
  100. {
  101. //skip invalid bricks (bricks without value)
  102. if(!strpos($brickentry,'=')) continue;
  103. list($key, $value) = explode('=', $brickentry, 2);
  104. $this->bricks[$key] = $value;
  105. }
  106. // Add predefined bricks
  107. $this->bricks['i'] = Site::get_url('habari') . "/";
  108. if(!is_array($this->bricks) || count($this->bricks) == 0) {
  109. return $content;
  110. }
  111. $this->post = $post;
  112. // Scan for bricks and replace matches with their values
  113. $return = preg_replace_callback( '/({[a-zA-Z0-9\-_]+})/Us', array($this, 'brickit'), $content );
  114. if ( !$this->bricks_found ) {
  115. return $content;
  116. }
  117. return $return;
  118. }
  119. // This is called for each found brick in the post
  120. // $matches contains the brick twice because it is found with a subexp in (), we will only use the direct match
  121. private function brickit( $matches )
  122. {
  123. // Get brickname without {}
  124. $brickname = substr($matches[1], 1, strlen($matches[1])-2);
  125. if(array_key_exists($brickname, $this->bricks))
  126. {
  127. $this->bricks_found = true;
  128. return $this->bricks[$brickname];
  129. }
  130. else return $matches[1];
  131. }
  132. // public function action_update_check () {
  133. // Update::add( 'bricks', '', $this->info->version );
  134. // }
  135. }
  136. ?>