PicoContact.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * p01-contact for Pico CMS - Simply add contact forms in your pages
  4. *
  5. * This plugin let you add contact forms in your pages by writing simple tags.
  6. * You can also define recipients or create your own complex forms.
  7. *
  8. * This file is the handle of p01-contact for Pico 2.
  9. *
  10. * @link https://github.com/nliautaud/p01contact
  11. * @author Nicolas Liautaud
  12. * @package p01contact
  13. * @version 1.1
  14. */
  15. require_once 'src/P01contact.php';
  16. class PicoContact extends AbstractPicoPlugin
  17. {
  18. const API_VERSION = 2;
  19. private $P01contact;
  20. protected $enabled = false;
  21. protected $doContact = false;
  22. protected $forAll = false;
  23. protected $ContactStyle = '/plugins/PicoContact/style.css';
  24. /**
  25. * Initialize P01contact and set the default language from Pico settings
  26. *
  27. * Triggered after Pico has read its configuration
  28. *
  29. * @see Pico::getConfig()
  30. * @param array &$config array of config variables
  31. * @return void
  32. */
  33. public function onConfigLoaded(array &$config)
  34. {
  35. if (!empty($config['default_language'])) {
  36. $this->default_lang = $config['default_language'];
  37. }
  38. $this->forAll=$this->getPluginConfig('forall',false);
  39. }
  40. //~ public function generateRandomString($length = 6)
  41. //~ {
  42. //~ $characters = '123456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ'; // lookalike characters excluded
  43. //~ $randomString = '';
  44. //~ for ($i = 0; $i < $length; $i++) {
  45. //~ $randomString .= $characters[rand(0, strlen($characters) - 1)];
  46. //~ }
  47. //~ return $randomString;
  48. //~ }
  49. public function onMetaParsed(array &$meta)
  50. {
  51. if($this->forAll || (!empty($meta['contact']['enabled']) && $meta['contact']['enabled']) ) {
  52. $this->doContact = true;
  53. if(!empty($meta['contact']['style'])) $this->ContactStyle=$meta['contact']['style'];
  54. }
  55. }
  56. /**
  57. * Replace (% contact %) tags and contact_admin tags in pages content
  58. *
  59. * Triggered after Pico has prepared the raw file contents for parsing
  60. *
  61. * @see Pico::parseFileContent()
  62. * @see DummyPlugin::onContentParsed()
  63. * @param string &$content prepared file contents for parsing
  64. * @return void
  65. */
  66. public function onContentPrepared(&$content)
  67. {
  68. if($this->doContact) {
  69. $this->P01contact = new P01C\P01contact();
  70. if (!empty($this->default_lang)) {
  71. $this->P01contact->default_lang = $this->default_lang;
  72. }
  73. $this->P01contact->ContactStyle = $this->ContactStyle;
  74. //~ $pwd = $this->generateRandomString();
  75. //~ file_put_contents(__DIR__ . '/src/pwd',$pwd);
  76. // replace config panel (% contact_admin_config %)
  77. $content = preg_replace_callback('`\(%\s*contact_admin_config\s*%\)`', function () {
  78. return '<div>' . $this->P01contact->panel(). '</div>';
  79. }, $content, 1);
  80. // replace debug report (% contact_admin_debug %)
  81. $content = preg_replace_callback('`\(%\s*contact_admin_debug\s*%\)`', function () {
  82. if (!$this->P01contact->config('debug')) {
  83. return '';
  84. }
  85. return '<div>' . $this->P01contact->debugReport() .'</div>';
  86. }, $content, 1);
  87. // replace forms (% contact ... %)
  88. $content = $this->P01contact->parse($content);
  89. }
  90. }
  91. /**
  92. * Add {{ contact() }} and {{ contact_admin() }} twig functions
  93. * For outputing forms and admin panels from themes templates
  94. *
  95. * Triggered when Pico registers the twig template engine
  96. *
  97. * @see Pico::getTwig()
  98. * @param Twig_Environment &$twig Twig instance
  99. * @return void
  100. */
  101. public function onTwigRegistered(Twig_Environment &$twig)
  102. {
  103. if($this->doContact) {
  104. // {{ contact() }} output the default form
  105. // {{ contact('parameters') }} custom parameters
  106. // {{ contact('fr', 'parameters') }} custom parameters and form-specific language
  107. // {{ contact('fr', null) }} default form with form-specific language
  108. $twig->addFunction(new Twig_SimpleFunction('contact', function ($a = null, $b = null) {
  109. if ($b) {
  110. return $this->P01contact->newForm($b, $a);
  111. }
  112. return $this->P01contact->newForm($a);
  113. }));
  114. // {{ contact_admin('debug') }} output the debug report
  115. // {{ contact_admin('config') }} output the config panel
  116. $twig->addFunction(new Twig_SimpleFunction('contact_admin', function ($type) {
  117. if ($type == 'debug' && $this->P01contact->config('debug')) {
  118. return $this->P01contact->debugReport();
  119. }
  120. if ($type == 'config') {
  121. return $this->P01contact->panel();
  122. }
  123. }));
  124. }
  125. }
  126. }