inital commit
This commit is contained in:
2991
webroot/epicvelocity/lib/BladeOne.php
Normal file
2991
webroot/epicvelocity/lib/BladeOne.php
Normal file
File diff suppressed because it is too large
Load Diff
109
webroot/epicvelocity/lib/BladeOneCache.php
Normal file
109
webroot/epicvelocity/lib/BladeOneCache.php
Normal file
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace eftec\bladeone;
|
||||
|
||||
/**
|
||||
* trait BladeOneCache
|
||||
* Copyright (c) 2016 Jorge Patricio Castro Castillo MIT License. Don't delete this comment, its part of the license.
|
||||
* Extends the tags of the class BladeOne. Its optional
|
||||
* It adds the next tags to the template
|
||||
* <code>
|
||||
* @ cache([cacheid],[duration=86400]). The id is optional. The duration of the cache is in seconds
|
||||
* // content here
|
||||
* @ endcache()
|
||||
* </code>
|
||||
* It also adds a new function (optional) to the business or logic layer
|
||||
* <code>
|
||||
* if ($blade->cacheExpired('hellocache',1,5)) { //'helloonecache' =template, =1 id cache, 5=duration (seconds)
|
||||
* // cache expired, so we should do some stuff (such as read from the database)
|
||||
* }
|
||||
* </code>
|
||||
*
|
||||
* @package BladeOneCache
|
||||
* @version 1.4 2016-06-25
|
||||
* @link https://github.com/EFTEC/BladeOne
|
||||
* @author Jorge Patricio Castro Castillo <jcastro arroba eftec dot cl>
|
||||
*/
|
||||
trait BladeOneCache
|
||||
{
|
||||
protected $curCacheId = 0;
|
||||
protected $curCacheDuration = "";
|
||||
protected $curCachePosition = 0;
|
||||
protected $cacheRunning = false;
|
||||
private $cacheExpired = []; // avoids to compare the file different times.
|
||||
|
||||
public function compileCache($expression)
|
||||
{
|
||||
// get id of template
|
||||
// if the file exists then
|
||||
// compare date.
|
||||
// if the date is too old then re-save.
|
||||
// else
|
||||
// save for the first time.
|
||||
|
||||
return $this->phpTag . "echo \$this->cacheStart{$expression}; if(!\$this->cacheRunning) { ?>";
|
||||
}
|
||||
|
||||
public function compileEndCache($expression)
|
||||
{
|
||||
return $this->phpTag . "} // if cacheRunning\necho \$this->cacheEnd{$expression}; ?>";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the cache expired (or doesn't exist), otherwise false.
|
||||
*
|
||||
* @param string $templateName name of the template to use (such hello for template hello.blade.php)
|
||||
* @param string $id (id of cache, optional, if not id then it adds automatically a number)
|
||||
* @param int $cacheDuration (duration of the cache in seconds)
|
||||
* @return bool (return if the cache expired)
|
||||
*/
|
||||
public function cacheExpired($templateName, $id, $cacheDuration)
|
||||
{
|
||||
if ($this->getMode() & 1) {
|
||||
return true; // forced mode, hence it always expires. (fast mode is ignored).
|
||||
}
|
||||
$compiledFile = $this->getCompiledFile($templateName) . '_cache' . $id;
|
||||
if (isset($this->cacheExpired[$compiledFile])) {
|
||||
// if the information is already in the array then returns it.
|
||||
return $this->cacheExpired[$compiledFile];
|
||||
}
|
||||
$date = @\filemtime($compiledFile);
|
||||
if ($date) {
|
||||
if ($date + $cacheDuration < \time()) {
|
||||
$this->cacheExpired[$compiledFile] = true;
|
||||
return true; // time-out.
|
||||
}
|
||||
} else {
|
||||
$this->cacheExpired[$compiledFile] = true;
|
||||
return true; // no file
|
||||
}
|
||||
$this->cacheExpired[$compiledFile] = false;
|
||||
return false; // cache active.
|
||||
}
|
||||
|
||||
public function cacheStart($id = "", $cacheDuration = 86400)
|
||||
{
|
||||
$this->curCacheId = ($id == "") ? ($this->curCacheId + 1) : $id;
|
||||
$this->curCacheDuration = $cacheDuration;
|
||||
$this->curCachePosition = \strlen(\ob_get_contents());
|
||||
$compiledFile = $this->getCompiledFile() . '_cache' . $this->curCacheId;
|
||||
if ($this->cacheExpired('', $id, $cacheDuration)) {
|
||||
$this->cacheRunning = false;
|
||||
} else {
|
||||
$this->cacheRunning = true;
|
||||
$content = $this->getFile($compiledFile);
|
||||
echo $content;
|
||||
}
|
||||
}
|
||||
|
||||
public function cacheEnd()
|
||||
{
|
||||
if (!$this->cacheRunning) {
|
||||
$txt = \substr(\ob_get_contents(), $this->curCachePosition);
|
||||
$compiledFile = $this->getCompiledFile() . '_cache' . $this->curCacheId;
|
||||
\file_put_contents($compiledFile, $txt);
|
||||
}
|
||||
$this->cacheRunning = false;
|
||||
}
|
||||
}
|
||||
153
webroot/epicvelocity/lib/BladeOneCacheRedis.php
Normal file
153
webroot/epicvelocity/lib/BladeOneCacheRedis.php
Normal file
@@ -0,0 +1,153 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace eftec\bladeone;
|
||||
|
||||
use Redis;
|
||||
|
||||
/**
|
||||
* trait BladeOneCacheRedis
|
||||
* Copyright (c) 2016 Jorge Patricio Castro Castillo MIT License. Don't delete this comment, its part of the license.
|
||||
* Extends the tags of the class BladeOne. Its optional
|
||||
* It adds the next tags to the template
|
||||
* <code>
|
||||
* @ cache([cacheid],[duration=86400]). The id is optional. The duration of the cache is in seconds
|
||||
* // content here
|
||||
* @ endcache()
|
||||
* </code>
|
||||
* It also adds a new function (optional) to the business or logic layer
|
||||
* <code>
|
||||
* if ($blade->cacheExpired('hellocache',1,5)) { //'helloonecache' =template, =1 id cache, 5=duration (seconds)
|
||||
* // cache expired, so we should do some stuff (such as read from the database)
|
||||
* }
|
||||
* </code>
|
||||
*
|
||||
* @package BladeOneCacheRedis
|
||||
* @version 0.1 2017-12-15 NOT YET IMPLEMENTED, ITS A WIP!!!!!!!!
|
||||
* @link https://github.com/EFTEC/BladeOne
|
||||
* @author Jorge Patricio Castro Castillo <jcastro arroba eftec dot cl>
|
||||
*/
|
||||
const CACHEREDIS_SCOPEURL = 1;
|
||||
|
||||
trait BladeOneCacheRedis
|
||||
{
|
||||
protected $curCacheId = 0;
|
||||
protected $curCacheDuration = "";
|
||||
protected $curCachePosition = 0;
|
||||
protected $cacheRunning = false;
|
||||
/** @var \Redis $redis */
|
||||
protected $redis;
|
||||
protected $redisIP = '127.0.0.1';
|
||||
protected $redisPort = 6379;
|
||||
protected $redisTimeOut = 2.5;
|
||||
protected $redisConnected = false;
|
||||
protected $redisNamespace = 'bladeonecache:';
|
||||
protected $redisBase = 0;
|
||||
private $cacheExpired = []; // avoids to compare the file different times.
|
||||
|
||||
//<editor-fold desc="compile">
|
||||
public function compileCache($expression)
|
||||
{
|
||||
// get id of template
|
||||
// if the file exists then
|
||||
// compare date.
|
||||
// if the date is too old then re-save.
|
||||
// else
|
||||
// save for the first time.
|
||||
|
||||
return $this->phpTag . "echo \$this->cacheStart{$expression}; if(!\$this->cacheRunning) { ?>";
|
||||
}
|
||||
|
||||
public function compileEndCache($expression)
|
||||
{
|
||||
return $this->phpTag . "} // if cacheRunning\necho \$this->cacheEnd{$expression}; ?>";
|
||||
}
|
||||
//</editor-fold>
|
||||
|
||||
public function connect($redisIP = null, $redisPort = null, $redisTimeOut = null)
|
||||
{
|
||||
if ($this->redisConnected) {
|
||||
return true;
|
||||
}
|
||||
if (!\class_exists('Redis')) {
|
||||
return false; // it requires redis.
|
||||
}
|
||||
if ($redisIP !== null) {
|
||||
$this->redisIP = $redisIP;
|
||||
$this->redisPort = $redisPort;
|
||||
$this->redisTimeOut = $redisTimeOut;
|
||||
}
|
||||
$this->redis = new Redis();
|
||||
// 2.5 sec timeout.
|
||||
$this->redisConnected = $this->redis->connect($this->redisIP, $this->redisPort, $this->redisTimeOut);
|
||||
|
||||
return $this->redisConnected;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the cache expired (or doesn't exist), otherwise false.
|
||||
*
|
||||
* @param string $templateName name of the template to use (such hello for template hello.blade.php)
|
||||
* @param string $id (id of cache, optional, if not id then it adds automatically a number)
|
||||
* @param int $scope scope of the cache.
|
||||
* @param int $cacheDuration (duration of the cache in seconds)
|
||||
* @return bool (return if the cache expired)
|
||||
*/
|
||||
public function cacheExpired($templateName, $id, $scope, $cacheDuration)
|
||||
{
|
||||
if ($this->getMode() & 1) {
|
||||
return true; // forced mode, hence it always expires. (fast mode is ignored).
|
||||
}
|
||||
$compiledFile = $this->getCompiledFile($templateName) . '_cache' . $id;
|
||||
if (isset($this->cacheExpired[$compiledFile])) {
|
||||
// if the information is already in the array then returns it.
|
||||
return $this->cacheExpired[$compiledFile];
|
||||
}
|
||||
$date = @\filemtime($compiledFile);
|
||||
if ($date) {
|
||||
if ($date + $cacheDuration < \time()) {
|
||||
$this->cacheExpired[$compiledFile] = true;
|
||||
return true; // time-out.
|
||||
}
|
||||
} else {
|
||||
$this->cacheExpired[$compiledFile] = true;
|
||||
return true; // no file
|
||||
}
|
||||
$this->cacheExpired[$compiledFile] = false;
|
||||
return false; // cache active.
|
||||
}
|
||||
|
||||
public function cacheStart($id = "", $cacheDuration = 86400)
|
||||
{
|
||||
$this->curCacheId = ($id == "") ? ($this->curCacheId + 1) : $id;
|
||||
$this->curCacheDuration = $cacheDuration;
|
||||
$this->curCachePosition = \strlen(\ob_get_contents());
|
||||
$compiledFile = $this->getCompiledFile() . '_cache' . $this->curCacheId;
|
||||
if ($this->cacheExpired('', $id, $cacheDuration)) {
|
||||
$this->cacheRunning = false;
|
||||
} else {
|
||||
$this->cacheRunning = true;
|
||||
$content = $this->getFile($compiledFile);
|
||||
echo $content;
|
||||
}
|
||||
// getFile($fileName)
|
||||
}
|
||||
|
||||
public function cacheEnd()
|
||||
{
|
||||
if (!$this->cacheRunning) {
|
||||
$txt = \substr(\ob_get_contents(), $this->curCachePosition);
|
||||
$compiledFile = $this->getCompiledFile() . '_cache' . $this->curCacheId;
|
||||
\file_put_contents($compiledFile, $txt);
|
||||
}
|
||||
$this->cacheRunning = false;
|
||||
}
|
||||
|
||||
private function keyByScope($scope)
|
||||
{
|
||||
$key = '';
|
||||
if ($scope && CACHEREDIS_SCOPEURL) {
|
||||
$key .= $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
|
||||
}
|
||||
}
|
||||
}
|
||||
52
webroot/epicvelocity/lib/BladeOneCustom.php
Normal file
52
webroot/epicvelocity/lib/BladeOneCustom.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace eftec\bladeone;
|
||||
|
||||
/*
|
||||
* Its an example of a custom set of functions for bladeone.
|
||||
* in examples/TestCustom.php there is a working example
|
||||
*/
|
||||
trait BladeOneCustom
|
||||
{
|
||||
private $customItem = []; // indicates the type of the current tag. such as select/selectgroup/etc.
|
||||
|
||||
//<editor-fold desc="compile function">
|
||||
/**
|
||||
* Usage @panel('title',true,true).....@endpanel()
|
||||
*
|
||||
* @param $expression
|
||||
* @return string
|
||||
*/
|
||||
protected function compilePanel($expression)
|
||||
{
|
||||
\array_push($this->customItem, 'Panel');
|
||||
return $this->phpTag . "echo \$this->panel{$expression}; ?>";
|
||||
}
|
||||
|
||||
protected function compileEndPanel()
|
||||
{
|
||||
$r = @\array_pop($this->customItem);
|
||||
if (\is_null($r)) {
|
||||
$this->showError("@endpanel", "Missing @compilepanel or so many @compilepanel", true);
|
||||
}
|
||||
return " </div></section><!-- end panel -->"; // we don't need to create a function for this.
|
||||
}
|
||||
|
||||
//</editor-fold>
|
||||
|
||||
//<editor-fold desc="used function">
|
||||
protected function panel($title = "", $toggle = true, $dismiss = true)
|
||||
{
|
||||
return "<section class='panel'>
|
||||
<header class='panel-heading'>
|
||||
<div class='panel-actions'>
|
||||
" . (($toggle) ? "<a href='#' class='panel-action panel-action-toggle' data-panel-toggle></a>" : "") . "
|
||||
" . (($dismiss) ? "<a href='#' class='panel-action panel-action-dismiss' data-panel-dismiss></a>" : "") . "
|
||||
</div>
|
||||
|
||||
<h2 class='panel-title'>$title</h2>
|
||||
</header>
|
||||
<div class='panel-body'>";
|
||||
}
|
||||
//</editor-fold>
|
||||
}
|
||||
535
webroot/epicvelocity/lib/BladeOneHtml.php
Normal file
535
webroot/epicvelocity/lib/BladeOneHtml.php
Normal file
@@ -0,0 +1,535 @@
|
||||
<?php
|
||||
|
||||
namespace eftec\bladeone;
|
||||
|
||||
/**
|
||||
* trait BladeOneHtml
|
||||
* Copyright (c) 2016 Jorge Patricio Castro Castillo MIT License. Don't delete this comment, its part of the license.
|
||||
* Extends the tags of the class BladeOne. Its optional
|
||||
* It adds the next tags
|
||||
* <code>
|
||||
* select:
|
||||
* @ select('idCountry','value',[,$extra])
|
||||
* @ item('0','--select a country'[,$extra])
|
||||
* @ items($countries,'id','name',$currentCountry[,$extra])
|
||||
* @ endselect()
|
||||
* input:
|
||||
* @ input('iduser',$currentUser,'text'[,$extra])
|
||||
* button:
|
||||
* @ commandbutton('idbutton','value','text'[,$extra])
|
||||
*
|
||||
* </code>
|
||||
* Note. The names of the tags are based in Java Server Faces (JSF)
|
||||
*
|
||||
* @package BladeOneHtml
|
||||
* @version 1.9.1 2018-06-11 (1)
|
||||
* @link https://github.com/EFTEC/BladeOne
|
||||
* @author Jorge Patricio Castro Castillo <jcastro arroba eftec dot cl>
|
||||
*/
|
||||
trait BladeOneHtml
|
||||
{
|
||||
protected $htmlItem = []; // indicates the type of the current tag. such as select/selectgroup/etc.
|
||||
protected $htmlCurrentId = []; //indicates the id of the current tag.
|
||||
|
||||
//<editor-fold desc="compile function">
|
||||
protected function compileSelect($expression)
|
||||
{
|
||||
\array_push($this->htmlItem, 'select');
|
||||
return $this->phpTag . "echo \$this->select{$expression}; ?>";
|
||||
}
|
||||
|
||||
protected function compileListBoxes($expression)
|
||||
{
|
||||
return $this->phpTag . "echo \$this->listboxes{$expression}; ?>";
|
||||
}
|
||||
|
||||
protected function compileLink($expression)
|
||||
{
|
||||
return $this->phpTag . "echo \$this->link{$expression}; ?>";
|
||||
}
|
||||
|
||||
protected function compileSelectGroup($expression)
|
||||
{
|
||||
\array_push($this->htmlItem, 'selectgroup');
|
||||
$this->compilePush('');
|
||||
return $this->phpTag . "echo \$this->select{$expression}; ?>";
|
||||
}
|
||||
|
||||
protected function compileRadio($expression)
|
||||
{
|
||||
\array_push($this->htmlItem, 'radio');
|
||||
return $this->phpTag . "echo \$this->radio{$expression}; ?>";
|
||||
}
|
||||
|
||||
protected function compileCheckbox($expression)
|
||||
{
|
||||
\array_push($this->htmlItem, 'checkbox');
|
||||
return $this->phpTag . "echo \$this->checkbox{$expression}; ?>";
|
||||
}
|
||||
|
||||
protected function compileEndSelect()
|
||||
{
|
||||
$r = @\array_pop($this->htmlItem);
|
||||
if (\is_null($r)) {
|
||||
$this->showError("@endselect", "Missing @select or so many @endselect", true);
|
||||
}
|
||||
return $this->phpTag . "echo '</select>'; ?>";
|
||||
}
|
||||
|
||||
protected function compileEndRadio()
|
||||
{
|
||||
$r = @\array_pop($this->htmlItem);
|
||||
if (\is_null($r)) {
|
||||
return $this->showError("@EndRadio", "Missing @Radio or so many @EndRadio", true);
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
protected function compileEndCheckbox()
|
||||
{
|
||||
$r = @\array_pop($this->htmlItem);
|
||||
if (\is_null($r)) {
|
||||
return $this->showError("@EndCheckbox", "Missing @Checkbox or so many @EndCheckbox", true);
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
protected function compileItem($expression)
|
||||
{
|
||||
// we add a new attribute with the type of the current open tag
|
||||
$r = \end($this->htmlItem);
|
||||
$x = \trim($expression);
|
||||
$x = "('{$r}'," . \substr($x, 1);
|
||||
return $this->phpTag . "echo \$this->item{$x}; ?>";
|
||||
}
|
||||
|
||||
protected function compileItems($expression)
|
||||
{
|
||||
// we add a new attribute with the type of the current open tag
|
||||
$r = \end($this->htmlItem);
|
||||
$x = \trim($expression);
|
||||
$x = "('{$r}'," . \substr($x, 1);
|
||||
return $this->phpTag . "echo \$this->items{$x}; ?>";
|
||||
}
|
||||
|
||||
protected function compileTrio($expression)
|
||||
{
|
||||
// we add a new attribute with the type of the current open tag
|
||||
$r = \end($this->htmlItem);
|
||||
$x = \trim($expression);
|
||||
$x = "('{$r}'," . \substr($x, 1);
|
||||
return $this->phpTag . "echo \$this->trio{$x}; ?>";
|
||||
}
|
||||
|
||||
protected function compileTrios($expression)
|
||||
{
|
||||
// we add a new attribute with the type of the current open tag
|
||||
$r = \end($this->htmlItem);
|
||||
$x = \trim($expression);
|
||||
$x = "('{$r}'," . \substr($x, 1);
|
||||
return $this->phpTag . "echo \$this->trios{$x}; ?>";
|
||||
}
|
||||
|
||||
protected function compileInput($expression)
|
||||
{
|
||||
return $this->phpTag . "echo \$this->input{$expression}; ?>";
|
||||
}
|
||||
|
||||
protected function compileFile($expression)
|
||||
{
|
||||
return $this->phpTag . "echo \$this->file{$expression}; ?>";
|
||||
}
|
||||
|
||||
protected function compileImage($expression)
|
||||
{
|
||||
return $this->phpTag . "echo \$this->image{$expression}; ?>";
|
||||
}
|
||||
|
||||
protected function compileTextArea($expression)
|
||||
{
|
||||
return $this->phpTag . "echo \$this->textArea{$expression}; ?>";
|
||||
}
|
||||
|
||||
protected function compileHidden($expression)
|
||||
{
|
||||
return $this->phpTag . "echo \$this->hidden{$expression}; ?>";
|
||||
}
|
||||
|
||||
protected function compileLabel($expression)
|
||||
{
|
||||
return $this->phpTag . "// {$expression} \n echo \$this->label{$expression}; ?>";
|
||||
}
|
||||
|
||||
protected function compileCommandButton($expression)
|
||||
{
|
||||
return $this->phpTag . "echo \$this->commandButton{$expression}; ?>";
|
||||
}
|
||||
|
||||
protected function compileForm($expression)
|
||||
{
|
||||
return $this->phpTag . "echo \$this->form{$expression}; ?>";
|
||||
}
|
||||
|
||||
protected function compileEndForm()
|
||||
{
|
||||
return $this->phpTag . "echo '</form>'; ?>";
|
||||
}
|
||||
//</editor-fold>
|
||||
|
||||
//<editor-fold desc="used function">
|
||||
public function select($name, $value, $extra = '')
|
||||
{
|
||||
if (\strpos($extra, 'readonly') === false) {
|
||||
return "<select id='" . static::e($name) . "' name='" . static::e($name) . "' {$this->convertArg($extra)}>\n";
|
||||
} else {
|
||||
return "
|
||||
<input id='" . static::e($name) . "' name='" . static::e($name) . "' type='hidden' value='" . static::e($value) . "' />
|
||||
<select id='" . static::e($name) . "_disable' name='" . static::e($name) . "_disable' disabled {$this->convertArg($extra)}>\n";
|
||||
}
|
||||
}
|
||||
|
||||
public function link($url, $label, $extra = '')
|
||||
{
|
||||
return "<a href='{$url}' {$this->convertArg($extra)}>{$label}</a>";
|
||||
}
|
||||
|
||||
/**
|
||||
* Find an element in a array of arrays
|
||||
* If the element doesn't exist in the array then it returns false, otherwise returns true
|
||||
*
|
||||
* @param $find
|
||||
* @param array $array array of primitives or objects
|
||||
* @param array $field field to search
|
||||
* @return bool
|
||||
*/
|
||||
private function listboxesFindArray($find, $array, $field)
|
||||
{
|
||||
if (\count($array) == 0) {
|
||||
return false;
|
||||
}
|
||||
if (!\is_array($array[0])) {
|
||||
return \in_array($find, $array);
|
||||
} else {
|
||||
foreach ($array as $elem) {
|
||||
if ($elem[$field] == $find) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function listboxes($name, $allvalues, $fieldId, $fieldText, $selectedId, $extra = '')
|
||||
{
|
||||
$html = "";
|
||||
$html .= "<table>\n";
|
||||
$html .= " <tr>\n";
|
||||
$html .= " <td>\n";
|
||||
$html .= " <select id='{$name}_noselected' size='6' multiple='multiple' $extra>\n";
|
||||
if (\count($allvalues) == 0) {
|
||||
$allvalues = [];
|
||||
}
|
||||
$html2 = "";
|
||||
foreach ($allvalues as $v) {
|
||||
if (\is_object($v)) {
|
||||
$v = (array)$v;
|
||||
}
|
||||
if (!$this->listboxesFindArray($v[$fieldId], $selectedId, $fieldId)) {
|
||||
$html .= "<option value='" . $v[$fieldId] . "'>" . $v[$fieldText] . "</option>\n";
|
||||
} else {
|
||||
$html2 .= "<option value='" . $v[$fieldId] . "'>" . $v[$fieldText] . "</option>\n";
|
||||
}
|
||||
}
|
||||
$html .= " </select>\n";
|
||||
$html .= " </td>\n";
|
||||
$html .= " <td style='text-align:center;'>\n";
|
||||
$html .= " <input type='button' value='>' id='{$name}_add'/><br>\n";
|
||||
$html .= " <input type='button' value='>>' id='{$name}_addall'/><br>\n";
|
||||
$html .= " <input type='button' value='<' id='{$name}_delete'/><br>\n";
|
||||
$html .= " <input type='button' value='<<' id='{$name}_deleteall'/><br>\n";
|
||||
$html .= " </td>\n";
|
||||
$html .= " <td>\n";
|
||||
$html .= " <select id='{$name}' name='{$name}' size='6' multiple='multiple'>\n";
|
||||
$html .= $html2;
|
||||
$html .= " </select>\n";
|
||||
$html .= " </td>\n";
|
||||
$html .= " </tr>\n";
|
||||
$html .= "</table>\n";
|
||||
return $html;
|
||||
}
|
||||
|
||||
public function selectGroup($name, $extra = '')
|
||||
{
|
||||
return $this->selectGroup($name, $extra);
|
||||
}
|
||||
|
||||
public function radio($id, $value = '', $text = '', $valueSelected = '', $extra = '')
|
||||
{
|
||||
$num = \func_num_args();
|
||||
if ($num > 2) {
|
||||
if ($value == $valueSelected) {
|
||||
if (\is_array($extra)) {
|
||||
$extra['checked'] = 'checked';
|
||||
} else {
|
||||
$extra .= ' checked="checked"';
|
||||
}
|
||||
}
|
||||
return $this->input($id, $value, 'radio', $extra) . ' ' . $text;
|
||||
} else {
|
||||
\array_push($this->htmlCurrentId, $id);
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param string $value
|
||||
* @param string $text
|
||||
* @param string|null $valueSelected
|
||||
* @param string $extra
|
||||
* @return string
|
||||
*/
|
||||
public function checkbox($id, $value = '', $text = '', $valueSelected = '', $extra = '')
|
||||
{
|
||||
$num = \func_num_args();
|
||||
if ($num > 2) {
|
||||
if ($value == $valueSelected) {
|
||||
if (\is_array($extra)) {
|
||||
$extra['checked'] = 'checked';
|
||||
} else {
|
||||
$extra .= ' checked="checked"';
|
||||
}
|
||||
}
|
||||
return $this->input($id, $value, 'checkbox', $extra) . ' ' . $text;
|
||||
} else {
|
||||
\array_push($this->htmlCurrentId, $id);
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type type of the current open tag
|
||||
* @param array|string $valueId if is an array then the first value is used as value, the second is used as
|
||||
* extra
|
||||
* @param $valueText
|
||||
* @param array|string $selectedItem Item selected (optional)
|
||||
* @param string $wrapper Wrapper of the element. For example, <li>%s</li>
|
||||
* @param string $extra
|
||||
* @return string
|
||||
* @internal param string $fieldId Field of the id
|
||||
* @internal param string $fieldText Field of the value visible
|
||||
*/
|
||||
public function item($type, $valueId, $valueText, $selectedItem = '', $wrapper = '', $extra = '')
|
||||
{
|
||||
$id = @\end($this->htmlCurrentId);
|
||||
$wrapper = ($wrapper == '') ? '%s' : $wrapper;
|
||||
if (\is_array($selectedItem)) {
|
||||
$found = \in_array($valueId, $selectedItem);
|
||||
} else {
|
||||
$found = $valueId == $selectedItem;
|
||||
}
|
||||
|
||||
$valueHtml = (!\is_array($valueId)) ? "value='{$valueId}'" : "value='{$valueId[0]}' data='{$valueId[1]}'";
|
||||
switch ($type) {
|
||||
case 'select':
|
||||
$selected = ($found) ? 'selected' : '';
|
||||
return \sprintf($wrapper, "<option $valueHtml $selected " .
|
||||
$this->convertArg($extra) . ">{$valueText}</option>\n");
|
||||
break;
|
||||
case 'radio':
|
||||
$selected = ($found) ? 'checked' : '';
|
||||
return \sprintf($wrapper, "<input type='radio' id='" . static::e($id)
|
||||
. "' name='" . static::e($id) . "' $valueHtml $selected "
|
||||
. $this->convertArg($extra) . "> {$valueText}\n");
|
||||
break;
|
||||
case 'checkbox':
|
||||
$selected = ($found) ? 'checked' : '';
|
||||
return \sprintf($wrapper, "<input type='checkbox' id='" . static::e($id)
|
||||
. "' name='" . static::e($id) . "' $valueHtml $selected "
|
||||
. $this->convertArg($extra) . "> {$valueText}\n");
|
||||
break;
|
||||
|
||||
default:
|
||||
return '???? type undefined: [$type] on @item<br>';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type type of the current open tag
|
||||
* @param array $arrValues Array of objects/arrays to show.
|
||||
* @param string $fieldId Field of the id (for arrValues)
|
||||
* @param string $fieldText Field of the id of selectedItem
|
||||
* @param array|string $selectedItem Item selected (optional)
|
||||
* @param string $selectedFieldId field of the selected item.
|
||||
* @param string $wrapper Wrapper of the element. For example, <li>%s</li>
|
||||
* @param string $extra (optional) is used for add additional information for the html object (such
|
||||
* as class)
|
||||
* @return string
|
||||
* @version 1.1 2017
|
||||
*/
|
||||
public function items(
|
||||
$type,
|
||||
$arrValues,
|
||||
$fieldId,
|
||||
$fieldText,
|
||||
$selectedItem = '',
|
||||
$selectedFieldId = '',
|
||||
$wrapper = '',
|
||||
$extra = ''
|
||||
) {
|
||||
if (\count($arrValues) == 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
if (\is_object(@$arrValues[0])) {
|
||||
$arrValues = (array)$arrValues;
|
||||
}
|
||||
if (\is_array($selectedItem)) {
|
||||
if (\is_object(@$selectedItem[0])) {
|
||||
$primitiveArray = [];
|
||||
foreach ($selectedItem as $v) {
|
||||
$primitiveArray[] = $v->{$selectedFieldId};
|
||||
}
|
||||
$selectedItem = $primitiveArray;
|
||||
}
|
||||
}
|
||||
$result = '';
|
||||
if (\is_object($selectedItem)) {
|
||||
$selectedItem = (array)$selectedItem;
|
||||
}
|
||||
foreach ($arrValues as $v) {
|
||||
if (\is_object($v)) {
|
||||
$v = (array)$v;
|
||||
}
|
||||
$result .= $this->item($type, $v[$fieldId], $v[$fieldText], $selectedItem, $wrapper, $extra);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type type of the current open tag
|
||||
* @param string $valueId value of the trio
|
||||
* @param string $valueText visible value of the trio.
|
||||
* @param string $value3 extra third value for select value or visual
|
||||
* @param array|string $selectedItem Item selected (optional)
|
||||
* @param string $wrapper Wrapper of the element. For example, <li>%s</li>
|
||||
* @param string $extra
|
||||
* @return string
|
||||
* @internal param string $fieldId Field of the id
|
||||
* @internal param string $fieldText Field of the value visible
|
||||
*/
|
||||
public function trio($type, $valueId, $valueText, $value3 = '', $selectedItem = '', $wrapper = '', $extra = '')
|
||||
{
|
||||
$id = @\end($this->htmlCurrentId);
|
||||
$wrapper = ($wrapper == '') ? '%s' : $wrapper;
|
||||
if (\is_array($selectedItem)) {
|
||||
$found = \in_array($valueId, $selectedItem);
|
||||
} else {
|
||||
$found = $valueId == $selectedItem;
|
||||
}
|
||||
switch ($type) {
|
||||
case 'selectgroup':
|
||||
$selected = ($found) ? 'selected' : '';
|
||||
return \sprintf($wrapper, "<option value='{$valueId}' $selected " .
|
||||
$this->convertArg($extra) . ">{$valueText}</option>\n");
|
||||
break;
|
||||
default:
|
||||
return '???? type undefined: [$type] on @item<br>';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type type of the current open tag
|
||||
* @param array $arrValues Array of objects/arrays to show.
|
||||
* @param string $fieldId Field of the id
|
||||
* @param string $fieldText Field of the value visible
|
||||
* @param string $fieldThird
|
||||
* @param array|string $selectedItem Item selected (optional)
|
||||
* @param string $wrapper Wrapper of the element. For example, <li>%s</li>
|
||||
* @param string $extra (optional) is used for add additional information for the html object (such as
|
||||
* class)
|
||||
* @return string
|
||||
* @version 1.0
|
||||
*/
|
||||
public function trios(
|
||||
$type,
|
||||
$arrValues,
|
||||
$fieldId,
|
||||
$fieldText,
|
||||
$fieldThird,
|
||||
$selectedItem = '',
|
||||
$wrapper = '',
|
||||
$extra = ''
|
||||
) {
|
||||
if (\count($arrValues) == 0) {
|
||||
return "";
|
||||
}
|
||||
if (\is_object($arrValues[0])) {
|
||||
$arrValues = (array)$arrValues;
|
||||
}
|
||||
$result = '';
|
||||
$oldV3 = "";
|
||||
foreach ($arrValues as $v) {
|
||||
if (\is_object($v)) {
|
||||
$v = (array)$v;
|
||||
}
|
||||
$v3 = $v[$fieldThird];
|
||||
if ($type == 'selectgroup') {
|
||||
if ($v3 != $oldV3) {
|
||||
if ($oldV3 != "") {
|
||||
$result .= "</optgroup>";
|
||||
}
|
||||
$oldV3 = $v3;
|
||||
$result .= "<optgroup label='{$v3}'>";
|
||||
}
|
||||
}
|
||||
if ($result) {
|
||||
$result .= $this->trio($type, $v[$fieldId], $v[$fieldText], $v3, $selectedItem, $wrapper, $extra);
|
||||
}
|
||||
}
|
||||
if ($type == 'selectgroup' && $oldV3 != "") {
|
||||
$result .= "</optgroup>";
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function input($id, $value = '', $type = 'text', $extra = '')
|
||||
{
|
||||
return "<input id='" . static::e($id) . "' name='" . static::e($id) . "' type='" . $type . "' " . $this->convertArg($extra) . " value='" . static::e($value) . "' />\n";
|
||||
}
|
||||
|
||||
public function file($id, $fullfilepath = '', $file = '', $extra = '')
|
||||
{
|
||||
return "<a href='$fullfilepath'>$file</a>
|
||||
<input id='" . static::e($id) . "_file' name='" . static::e($id) . "_file' type='hidden' value='" . static::e($file) . "' />
|
||||
<input id='" . static::e($id) . "' name='" . static::e($id) . "' type='file' " . $this->convertArg($extra) . " value='" . static::e($fullfilepath) . "' />\n";
|
||||
}
|
||||
|
||||
public function textArea($id, $value = '', $extra = '')
|
||||
{
|
||||
$value = \str_replace('\n', "\n", $value);
|
||||
return "<textarea id='" . static::e($id) . "' name='" . static::e($id) . "' " . $this->convertArg($extra) . " >$value</textarea>\n";
|
||||
}
|
||||
|
||||
public function hidden($id, $value = '', $extra = '')
|
||||
{
|
||||
return $this->input($id, $value, 'hidden', $extra);
|
||||
}
|
||||
|
||||
public function label($id, $value = '', $extra = '')
|
||||
{
|
||||
return "<label for='{$id}' {$this->convertArg($extra)}>{$value}</label>";
|
||||
}
|
||||
|
||||
public function commandButton($id, $value = '', $text = 'Button', $type = 'submit', $extra = '')
|
||||
{
|
||||
return "<button type='{$type}' id='" . static::e($id) . "' name='" . static::e($id) . "' value='" . static::e($value) . "' {$this->convertArg($extra)}>{$text}</button>\n";
|
||||
}
|
||||
|
||||
public function form($action, $method = 'post', $extra = '')
|
||||
{
|
||||
return "<form $action='{$action}' method='{$method}' {$this->convertArg($extra)}>";
|
||||
}
|
||||
|
||||
//</editor-fold>
|
||||
}
|
||||
259
webroot/epicvelocity/lib/BladeOneHtmlBootstrap.php
Normal file
259
webroot/epicvelocity/lib/BladeOneHtmlBootstrap.php
Normal file
@@ -0,0 +1,259 @@
|
||||
<?php
|
||||
|
||||
namespace eftec\bladeone;
|
||||
|
||||
/**
|
||||
* trait BladeOneHtmlBootstrap
|
||||
* Copyright (c) 2016 Jorge Patricio Castro Castillo MIT License. Don't delete this comment, its part of the license.
|
||||
* Extends the tags of the class BladeOne. Its optional
|
||||
* It adds the next tags
|
||||
* <code>
|
||||
* select:
|
||||
* @ select('idCountry','value',[,$extra])
|
||||
* @ item('0','--select a country'[,$extra])
|
||||
* @ items($countries,'id','name',$currentCountry[,$extra])
|
||||
* @ endselect()
|
||||
* input:
|
||||
* @ input('iduser',$currentUser,'text'[,$extra])
|
||||
* button:
|
||||
* @ commandbutton('idbutton','value','text'[,$extra])
|
||||
*
|
||||
* </code>
|
||||
* Note. The names of the tags are based in Java Server Faces (JSF)
|
||||
*
|
||||
* @package BladeOneHtmlBootstrap
|
||||
* @version 1.9.1 2018-06-11 (1)
|
||||
* @link https://github.com/EFTEC/BladeOne
|
||||
* @author Jorge Patricio Castro Castillo <jcastro arroba eftec dot cl>
|
||||
*/
|
||||
trait BladeOneHtmlBootstrap
|
||||
{
|
||||
use BladeOneHtml {
|
||||
BladeOneHtml::select as selectParent;
|
||||
BladeOneHtml::input as inputParent;
|
||||
BladeOneHtml::commandButton as commandButtonParent;
|
||||
BladeOneHtml::textArea as textAreaParent;
|
||||
BladeOneHtml::item as itemParent;
|
||||
BladeOneHtml::checkbox as checkboxParent;
|
||||
BladeOneHtml::compileEndCheckbox as compileEndCheckboxParent;
|
||||
BladeOneHtml::radio as radioParent;
|
||||
BladeOneHtml::compileEndRadio as compileEndRadioParent;
|
||||
}
|
||||
|
||||
//<editor-fold desc="Override methods">
|
||||
public function select($name, $value, $extra = '')
|
||||
{
|
||||
$extra = $this->addClass($extra, 'form-control');
|
||||
return $this->selectParent($name, $value, $extra);
|
||||
}
|
||||
|
||||
public function input($id, $value = '', $type = 'text', $extra = '')
|
||||
{
|
||||
$extra = $this->addClass($extra, 'form-control');
|
||||
return $this->inputParent($id, $value, $type, $extra);
|
||||
}
|
||||
|
||||
public function commandButton($id, $value = '', $text = 'Button', $type = 'submit', $extra = '')
|
||||
{
|
||||
$extra = $this->addClass($extra, 'btn');
|
||||
return $this->commandButtonParent($id, $value, $text, $type, $extra);
|
||||
}
|
||||
|
||||
public function textArea($id, $value = '', $extra = '')
|
||||
{
|
||||
$extra = $this->addClass($extra, 'form-control');
|
||||
return $this->textAreaParent($id, $value, $extra);
|
||||
}
|
||||
|
||||
|
||||
public function file($id, $fullfilepath = '', $file = '', $extra = '')
|
||||
{
|
||||
return "<input id='" . static::e($id) . "_file' name='" . static::e($id) . "_file' type='hidden' value='" . static::e($file) . "' />
|
||||
<div class='input-group'>
|
||||
<label class='input-group-btn'>
|
||||
<span class='btn btn-primary'>
|
||||
Browse…<a href='$fullfilepath' class='afile' ><i class='fa fa-paperclip'></i></a>
|
||||
<input type='file' style='display: none;' id='" . static::e($id) . "' name='" . static::e($id) . "' $extra />
|
||||
</span>
|
||||
</label>
|
||||
<input type='text' class='form-control' readonly></div>";
|
||||
// return "<a href='$fullfilepath'>$file</a>
|
||||
//<input id='".static::e($id)."_file' name='".static::e($id)."_file' type='hidden' value='".static::e($file)."' />
|
||||
// <input id='".static::e($id)."' name='".static::e($id)."' type='file' ".$this->convertArg($extra)." value='".static::e($fullfilepath)."' />\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $id of the field
|
||||
* @param string $fullfilepath full file path of the image
|
||||
* @param string $file filename of the file
|
||||
* @param string $extra extra field of the input file
|
||||
* @return string html
|
||||
*/
|
||||
public function image($id, $fullfilepath = '', $file = '', $extra = '')
|
||||
{
|
||||
return "<input id='" . static::e($id) . "_file' name='" . static::e($id) . "_file' type='hidden' value='" . static::e($file) . "' />
|
||||
<img src='$fullfilepath' class='img-thumbnail' />
|
||||
<div class='input-group'>
|
||||
|
||||
<label class='input-group-btn'>
|
||||
<span class='btn btn-primary'>
|
||||
Browse…
|
||||
<input type='file' style='display: none;' id='" . static::e($id) . "' name='" . static::e($id) . "' $extra />
|
||||
</span>
|
||||
</label>
|
||||
<input type='text' class='form-control' readonly></div>";
|
||||
// return "<a href='$fullfilepath'>$file</a>
|
||||
//<input id='".static::e($id)."_file' name='".static::e($id)."_file' type='hidden' value='".static::e($file)."' />
|
||||
// <input id='".static::e($id)."' name='".static::e($id)."' type='file' ".$this->convertArg($extra)." value='".static::e($fullfilepath)."' />\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type type of the current open tag
|
||||
* @param array|string $valueId if is an array then the first value is used as value, the second is used as
|
||||
* extra
|
||||
* @param $valueText
|
||||
* @param array|string $selectedItem Item selected (optional)
|
||||
* @param string $wrapper Wrapper of the element. For example, <li>%s</li>
|
||||
* @param string $extra
|
||||
* @return string
|
||||
* @internal param string $fieldId Field of the id
|
||||
* @internal param string $fieldText Field of the value visible
|
||||
*/
|
||||
public function item($type, $valueId, $valueText, $selectedItem = '', $wrapper = '', $extra = '')
|
||||
{
|
||||
$id = @\end($this->htmlCurrentId);
|
||||
$wrapper = ($wrapper == '') ? '%s' : $wrapper;
|
||||
|
||||
if (\is_array($selectedItem)) {
|
||||
$found = \in_array($valueId, $selectedItem);
|
||||
} else {
|
||||
if (\is_null($selectedItem)) {
|
||||
// diferentiate null = '' != 0
|
||||
$found = $valueId === '' || $valueId === null;
|
||||
} else {
|
||||
$found = $selectedItem == $valueId;
|
||||
}
|
||||
}
|
||||
$valueHtml = (!\is_array($valueId)) ? "value='{$valueId}'" : "value='{$valueId[0]}' data='{$valueId[1]}'";
|
||||
switch ($type) {
|
||||
case 'select':
|
||||
$selected = ($found) ? 'selected' : '';
|
||||
return \sprintf($wrapper, "<option $valueHtml $selected " .
|
||||
$this->convertArg($extra) . ">{$valueText}</option>\n");
|
||||
break;
|
||||
case 'radio':
|
||||
$selected = ($found) ? 'checked' : '';
|
||||
return \sprintf($wrapper, "<label><input type='radio' id='" . static::e($id)
|
||||
. "' name='" . static::e($id) . "' $valueHtml $selected "
|
||||
. $this->convertArg($extra) . "> {$valueText}</label>\n");
|
||||
break;
|
||||
case 'checkbox':
|
||||
$selected = ($found) ? 'checked' : '';
|
||||
return \sprintf($wrapper, "<label><input type='checkbox' id='" . static::e($id)
|
||||
. "' name='" . static::e($id) . "' $valueHtml $selected "
|
||||
. $this->convertArg($extra) . "> {$valueText}</label>\n");
|
||||
break;
|
||||
|
||||
default:
|
||||
return '???? type undefined: [$type] on @item<br>';
|
||||
}
|
||||
}
|
||||
|
||||
public function checkbox($id, $value = '', $text = '', $valueSelected = '', $extra = '')
|
||||
{
|
||||
$num = \func_num_args();
|
||||
if ($num > 2) {
|
||||
if ($value == $valueSelected) {
|
||||
if (\is_array($extra)) {
|
||||
$extra['checked'] = 'checked';
|
||||
} else {
|
||||
$extra .= ' checked="checked"';
|
||||
}
|
||||
}
|
||||
//return '<div class="checkbox"><label>'.$this->inputParent($id, $value, 'checkbox', $extra) . ' ' . $text.'</label></div>';
|
||||
return '<div><label>' . $this->inputParent($id, $value, 'checkbox', $extra) . ' ' . $text . '</label></div>';
|
||||
} else {
|
||||
\array_push($this->htmlCurrentId, $id);
|
||||
return '<div>';
|
||||
//return '<div class="checkbox">';
|
||||
}
|
||||
}
|
||||
|
||||
public function radio($id, $value = '', $text = '', $valueSelected = '', $extra = '')
|
||||
{
|
||||
$num = \func_num_args();
|
||||
if ($num > 2) {
|
||||
if ($value == $valueSelected) {
|
||||
if (\is_array($extra)) {
|
||||
$extra['checked'] = 'checked';
|
||||
} else {
|
||||
$extra .= ' checked="checked"';
|
||||
}
|
||||
}
|
||||
return '<div class="radio"><label>' . $this->inputParent($id, $value, 'radio', $extra) . ' ' . $text . '</label></div>';
|
||||
} else {
|
||||
\array_push($this->htmlCurrentId, $id);
|
||||
return '<div class="radio">';
|
||||
}
|
||||
}
|
||||
|
||||
public function compileEndCheckbox()
|
||||
{
|
||||
$r = $this->compileEndCheckboxParent();
|
||||
$r .= '</div>';
|
||||
return $r;
|
||||
}
|
||||
|
||||
public function compileEndRadio()
|
||||
{
|
||||
$r = $this->compileEndRadioParent();
|
||||
$r .= '</div>';
|
||||
return $r;
|
||||
}
|
||||
//</editor-fold>
|
||||
|
||||
//<editor-fold desc="Misc members">
|
||||
|
||||
/**
|
||||
* It adds a class to a html tag parameter
|
||||
*
|
||||
* @example addClass('type="text" class="btn","btn-standard")
|
||||
* @param string|array $txt
|
||||
* @param string $newclass The class(es) to add, example "class1" or "class1 class"
|
||||
* @return string|array
|
||||
*/
|
||||
protected function addClass($txt, $newclass)
|
||||
{
|
||||
if (\is_array($txt)) {
|
||||
$txt = \array_change_key_case($txt);
|
||||
@$txt['class'] = ' ' . $newclass;
|
||||
return $txt;
|
||||
}
|
||||
$p0 = \stripos(' ' . $txt, ' class');
|
||||
if ($p0 === false) {
|
||||
// if the content of the tag doesn't contain a class then it adds one.
|
||||
return $txt . ' class="' . $newclass . '"';
|
||||
}
|
||||
// the class tag exists so we found the closes character ' or " and we add the class (or classes) inside it
|
||||
// may be it could duplicates the tag.
|
||||
$p1 = \strpos($txt, "'", $p0);
|
||||
$p2 = \strpos($txt, '"', $p0);
|
||||
$p1 = ($p1 === false) ? 99999 : $p1;
|
||||
$p2 = ($p2 === false) ? 99999 : $p2;
|
||||
|
||||
if ($p1 < $p2) {
|
||||
return \substr_replace($txt, $newclass . ' ', $p1 + 1, 0);
|
||||
} else {
|
||||
echo $p2 . "#";
|
||||
return \substr_replace($txt, $newclass . ' ', $p2 + 1, 0);
|
||||
}
|
||||
}
|
||||
|
||||
protected function separatesParam($txt)
|
||||
{
|
||||
$result = [];
|
||||
\preg_match_all("~\"[^\"]++\"|'[^']++'|\([^)]++\)|[^,]++~", $txt, $result);
|
||||
return $result;
|
||||
}
|
||||
//</editor-fold>
|
||||
}
|
||||
146
webroot/epicvelocity/lib/BladeOneLang.php
Normal file
146
webroot/epicvelocity/lib/BladeOneLang.php
Normal file
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
namespace eftec\bladeone;
|
||||
|
||||
/**
|
||||
* Trait BladeOneLang
|
||||
* It adds the next tags
|
||||
* <code>
|
||||
* select:
|
||||
* @ _e('hello')
|
||||
* @ _n('Product','Products',$n)
|
||||
* @ _ef('hello %s',$user)
|
||||
* </code>
|
||||
*
|
||||
* @package eftec\bladeone
|
||||
* @version 1.0 2017-12-17 (1)
|
||||
* @link https://github.com/EFTEC/BladeOne
|
||||
* @author Jorge Patricio Castro Castillo <jcastro arroba eftec dot cl>
|
||||
* @copyright 2017 Jorge Patricio Castro Castillo MIT License. Don't delete this comment, its part of the license.
|
||||
*/
|
||||
trait BladeOneLang
|
||||
{
|
||||
/** @var string The path to the missing translations log file. If empty then every missing key is not saved. */
|
||||
public $missingLog = '';
|
||||
|
||||
/** @var array Hold dictionary of translations */
|
||||
public static $dictionary = [];
|
||||
|
||||
/**
|
||||
* Tries to translate the word if its in the array defined by BladeOneLang::$dictionary
|
||||
* If the operation fails then, it returns the original expression without translation.
|
||||
*
|
||||
* @param $phrase
|
||||
* @return string
|
||||
*/
|
||||
public function _e($phrase)
|
||||
{
|
||||
if ((!\array_key_exists($phrase, static::$dictionary))) {
|
||||
$this->missingTranslation($phrase);
|
||||
return $phrase;
|
||||
} else {
|
||||
return static::$dictionary[$phrase];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Its the same than @_e, however it parses the text (using sprintf).
|
||||
* If the operation fails then, it returns the original expression without translation.
|
||||
*
|
||||
* @param $phrase
|
||||
* @return string
|
||||
*/
|
||||
public function _ef($phrase)
|
||||
{
|
||||
$argv = \func_get_args();
|
||||
$r = $this->_e($phrase);
|
||||
$argv[0] = $r; // replace the first argument with the translation.
|
||||
$result = @\call_user_func_array("sprintf", $argv);
|
||||
$result = ($result === false) ? $r : $result;
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* if num is more than one then it returns the phrase in plural, otherwise the phrase in singular.
|
||||
* Note: the translation should be as follow: $msg['Person']='Person' $msg=['Person']['p']='People'
|
||||
*
|
||||
* @param string $phrase
|
||||
* @param string $phrases
|
||||
* @param int $num
|
||||
* @return string
|
||||
*/
|
||||
public function _n($phrase, $phrases, $num = 0)
|
||||
{
|
||||
if ((!\array_key_exists($phrase, static::$dictionary))) {
|
||||
$this->missingTranslation($phrase);
|
||||
return ($num <= 1) ? $phrase : $phrases;
|
||||
} else {
|
||||
return ($num <= 1) ? $this->_e($phrase) : $this->_e($phrases);
|
||||
}
|
||||
}
|
||||
|
||||
//<editor-fold desc="compile">
|
||||
|
||||
/**
|
||||
* Used for @_e directive.
|
||||
*
|
||||
* @param $expression
|
||||
* @return string
|
||||
*/
|
||||
protected function compile_e($expression)
|
||||
{
|
||||
return $this->phpTag . "echo \$this->_e{$expression}; ?>";
|
||||
}
|
||||
|
||||
/**
|
||||
* Used for @_ef directive.
|
||||
*
|
||||
* @param $expression
|
||||
* @return string
|
||||
*/
|
||||
protected function compile_ef($expression)
|
||||
{
|
||||
return $this->phpTag . "echo \$this->_ef{$expression}; ?>";
|
||||
}
|
||||
|
||||
/**
|
||||
* Used for @_n directive.
|
||||
*
|
||||
* @param $expression
|
||||
* @return string
|
||||
*/
|
||||
protected function compile_n($expression)
|
||||
{
|
||||
return $this->phpTag . "echo \$this->_n{$expression}; ?>";
|
||||
}
|
||||
|
||||
//</editor-fold>
|
||||
|
||||
/**
|
||||
* Log a missing translation to $this->missingLog.
|
||||
*
|
||||
* @param $txt
|
||||
*/
|
||||
private function missingTranslation($txt)
|
||||
{
|
||||
if (!$this->missingLog) {
|
||||
return; // if there is not a file assigned then it skips saving.
|
||||
}
|
||||
|
||||
$fz = @\filesize($this->missingLog);
|
||||
$mode = 'a';
|
||||
|
||||
if (\is_object($txt) || \is_array($txt)) {
|
||||
$txt = \print_r($txt, true);
|
||||
}
|
||||
|
||||
// Rewrite file if more than 100000 bytes
|
||||
if ($fz > 100000) {
|
||||
$mode = 'w';
|
||||
}
|
||||
|
||||
$fp = \fopen($this->missingLog, 'w');
|
||||
\fwrite($fp, $txt . "\n");
|
||||
\fclose($fp);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user