Skip to content

Commit

Permalink
add support of forms, debug report and admin panel in Pico CMS themes…
Browse files Browse the repository at this point in the history
…, see #17

P01contact: separate parse() and newForm(), to create a new form directly
P01contact: return debugReport() and admin panel(), to allow placement by user
P01contact: separate php reporting (auto when debug enabled) and debugReport()
  • Loading branch information
nliautaud committed Oct 22, 2017
1 parent cd378dc commit 38de7b5
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 45 deletions.
64 changes: 56 additions & 8 deletions p01-contact/P01contact_pico.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @link https://github.com/nliautaud/p01contact
* @author Nicolas Liautaud
* @package p01-contact
* @version 1.0.0
* @version 1.1
*/

require_once 'src/P01contact.php';
Expand All @@ -20,9 +20,9 @@ class P01contact_pico extends AbstractPicoPlugin
private $P01contact;

/**
* Triggered after Pico has read its configuration
* Initialize P01contact and set the default language from Pico settings
*
* Initialize P01contact and get default language in settings
* Triggered after Pico has read its configuration
*
* @see Pico::getConfig()
* @param array &$config array of config variables
Expand All @@ -31,19 +31,67 @@ class P01contact_pico extends AbstractPicoPlugin
public function onConfigLoaded(array &$config)
{
$this->P01contact = new P01C\P01contact();

if(!empty($config['default_language'])) {
$this->P01contact->default_lang = $config['default_language'];
}
}
/**
* Parse pages content
* Replace (% contact %) tags and contact_admin tags in pages content
*
* Triggered after Pico has prepared the raw file contents for parsing
*
* @see Pico::parseFileContent()
* @see DummyPlugin::onContentParsed()
* @param string &$content prepared file contents for parsing
* @return void
*/
public function onContentPrepared(&$content)
{
// replace forms (% contact ... %)
$content = $this->P01contact->parse($content);

if ($this->P01contact->config('debug')) {
$this->P01contact->debug();
}

// replace config panel (% contact_admin_config %)
$content = preg_replace_callback('`(%\s*contact_admin_config\s*%)`', function () {
return $this->P01contact->panel();
}, $content, 1);

// replace debug report (% contact_admin_debug %)
$content = preg_replace_callback('`(%\s*contact_admin_debug\s*%)`', function () {
return $this->P01contact->config('debug') ? $this->P01contact->debugReport() : '';
}, $content, 1);
}
/**
* Add {{ contact() }} and {{ contact_admin() }} twig functions
* For outputing forms and admin panels from themes templates
*
* Triggered before Pico renders the page
*
* @see Pico::getTwig()
* @see DummyPlugin::onPageRendered()
* @param Twig_Environment &$twig twig template engine
* @param array &$twigVariables template variables
* @param string &$templateName file name of the template
* @return void
*/
public function onPageRendering(Twig_Environment &$twig, array &$twigVariables, &$templateName)
{
// {{ contact() }} output the default form
// {{ contact('parameters') }} custom parameters
// {{ contact('fr', 'parameters') }} custom parameters and form-specific language
// {{ contact('fr', null) }} default form with form-specific language
$twig->addFunction(new Twig_SimpleFunction('contact', function ($a, $b) {
if (isset($b)) return $this->P01contact->newForm($b, $a);
return $this->P01contact->newForm($a);
}));

// {{ contact_admin('debug') }} output the debug report
// {{ contact_admin('config') }} output the config panel
$twig->addFunction(new Twig_SimpleFunction('contact_admin', function ($type) {
if ($type == 'debug' && $this->P01contact->config('debug'))
return $this->P01contact->debugReport();
if ($type == 'config')
return $this->P01contact->panel();
}));
}
}
92 changes: 57 additions & 35 deletions p01-contact/src/P01contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class P01contact

public function __construct()
{
define('P01C\VERSION', '1.0.1');
define('P01C\VERSION', '1.1.0');
$this->version = VERSION;

define('P01C\SERVERNAME', $_SERVER['SERVER_NAME']);
Expand All @@ -47,6 +47,9 @@ public function __construct()

$this->loadConfig();
$this->loadLangs();

if ($this->config('debug'))
$this->enablePHPdebug();
}

/**
Expand Down Expand Up @@ -81,57 +84,76 @@ public function parse($contents)
$pattern = "`(?<!<code>)\(%\s*contact\s*(\w*)\s*:?$sp(.*?)$sp%\)`s";
preg_match_all($pattern, $contents, $tags, PREG_SET_ORDER);

foreach ($tags as $tag) {
$form = $this->newForm($tag[2], $tag[1]);
$contents = preg_replace($pattern, $form, $contents, 1);
}
return $contents;
}
/**
* Return a form based on the given parameters and lang
*
* @param string $params the parameters string, according to the syntax
* @param string $lang form-specific language code
* @return string the html form
*/
public function newForm($params = '', $lang = null)
{
$defaultStyle = '';
static $once;
if (!$once) {
$inc = '<link rel="stylesheet" href="'.SERVER.RELPATH.'style.css"/>';
$contents = $inc . $contents;
$defaultStyle = '<link rel="stylesheet" href="'.SERVER.RELPATH.'style.css"/>';
$once = true;
}
$form = new P01contactForm($this);
$form->parseTag($params);
if ($lang) $form->lang = $lang;
$form->post();

foreach ($tags as $tag) {
$form = new P01contactForm($this);
$form->parseTag($tag[2]);
$form->lang = $tag[1];
$form->post();
$contents = preg_replace($pattern, $form->html(), $contents, 1);
}
$_SESSION['p01-contact']['last_page_load'] = time();

return $contents;
return $defaultStyle . $form->html();
}

/**
* Enable PHP error reporting and display system and p01-contact infos.
* Display system and P01contact infos.
*
* @return string the html report
*/
public function debug()
public function debugReport()
{
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$out = '<h2 style="color:#c33">p01-contact debug</h2>';

$out.= '<h3>Health :</h3>';
$health = 'PHP version : '.phpversion()."\n";
$health.= 'PHP mbstring (UTF-8) : '.(extension_loaded('mbstring') ? 'OK' : 'MISSING');
$out.= preint($health, true);

echo'<h2 style="color:#c33">p01-contact debug</h2>';

echo'<h3>Health :</h3>';
preint($health);

echo'<h3>Constants :</h3>';
preint(array_filter(get_defined_constants(true)['user'], function ($n) {
$out.= '<h3>Constants :</h3>';
$out.= preint(array_filter(get_defined_constants(true)['user'], function ($n) {
return 0 === strpos($n, __namespace__);
}, ARRAY_FILTER_USE_KEY));
}, ARRAY_FILTER_USE_KEY), true);

if (!empty($_SESSION)) {
echo'<h3>$_SESSION :</h3>';
preint($_SESSION);
$out.= '<h3>$_SESSION :</h3>';
$out.= preint($_SESSION, true);
}
if (!empty($_POST)) {
echo'<h3>$_POST :</h3>';
preint($_POST);
$out.= '<h3>$_POST :</h3>';
$out.= preint($_POST, true);
}
echo'<h3>$p01contact :</h3>';
preint($this);
$out.= '<h3>$p01contact :</h3>';
$out.= preint($this, true);
return $out;
}
/**
* Enable PHP error reporting
*/
public function enablePHPdebug()
{
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
}


Expand Down Expand Up @@ -347,13 +369,13 @@ public function panel()
$this->loadConfig();

if ($success) {
echo '<div class="updated">' . $this->lang('config_updated') . '</div>';
$msg = '<div class="updated">' . $this->lang('config_updated') . '</div>';
} else {
echo '<div class="error">'.$this->lang('config_error_modify');
echo '<pre>'.CONFIGPATH.'</pre></div>';
$msg = '<div class="error">'.$this->lang('config_error_modify');
$msg.= '<pre>'.CONFIGPATH.'</pre></div>';
}
}
echo $this->panelContent();
return $msg . $this->panelContent();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions p01-contact_gs.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @link https://github.com/nliautaud/p01contact
* @author Nicolas Liautaud
* @package p01-contact
* @version 1.0.0
* @version 1.1
*/

require_once GSPLUGINPATH . 'p01-contact/src/P01contact.php';
Expand Down Expand Up @@ -43,7 +43,7 @@ function p01contact_filter($contents)
$contents = $p01contact->parse($contents);

if ($p01contact->config('debug')) {
$p01contact->debug();
echo $p01contact->debugReport();
}
return $contents;
}
Expand Down

0 comments on commit 38de7b5

Please sign in to comment.