implement TextFormatter class to include media in texts
This commit is contained in:
parent
3fb0b9d074
commit
f723382b93
9 changed files with 137 additions and 7 deletions
|
|
@ -67,6 +67,9 @@
|
|||
// Create linker
|
||||
$this->linker = new \nre\core\Linker($this->request);
|
||||
|
||||
// Create text formatter
|
||||
$this->set('t', new \hhu\z\TextFormatter($this->linker));
|
||||
|
||||
// Create date and time and number formatter
|
||||
$this->set('dateFormatter', new \IntlDateFormatter(
|
||||
\nre\core\Config::getDefault('locale'),
|
||||
|
|
|
|||
127
app/TextFormatter.inc
Normal file
127
app/TextFormatter.inc
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* The Legend of Z
|
||||
*
|
||||
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
|
||||
* @copyright 2014 Heinrich-Heine-Universität Düsseldorf
|
||||
* @license http://www.gnu.org/licenses/gpl.html
|
||||
* @link https://bitbucket.org/coderkun/the-legend-of-z
|
||||
*/
|
||||
|
||||
namespace hhu\z;
|
||||
|
||||
|
||||
/**
|
||||
* Class to format text with different syntax tags.
|
||||
*
|
||||
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
|
||||
*/
|
||||
class TextFormatter
|
||||
{
|
||||
/**
|
||||
* Linker to create links.
|
||||
*
|
||||
* @var Linker
|
||||
*/
|
||||
private $linker;
|
||||
/**
|
||||
* Media-Model to retrieve media data
|
||||
*
|
||||
* @static
|
||||
* @var model
|
||||
*/
|
||||
private static $Media = null;
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Create a new text formatter.
|
||||
*
|
||||
* @param Linker $linker Linker to create links with
|
||||
*/
|
||||
public function __construct(\nre\core\Linker $linker)
|
||||
{
|
||||
$this->linker = $linker;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Format a string.
|
||||
*
|
||||
* @param string $string String to format
|
||||
* @return string Formatted string
|
||||
*/
|
||||
public function t($string)
|
||||
{
|
||||
// Remove chars and replace linefeeds
|
||||
$string = nl2br(htmlspecialchars($string));
|
||||
|
||||
// Handle Seminarymedia
|
||||
preg_match_all('/\[seminarymedia:(\d+)\]/iu', $string, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
|
||||
foreach($matches as &$match)
|
||||
{
|
||||
// Get Seminary media
|
||||
$seminarymediaId = intval($match[1][0]);
|
||||
$htmlImage = null;
|
||||
if(!is_null(\hhu\z\controllers\SeminaryRoleController::$seminary) && $this->loadMediaModel())
|
||||
{
|
||||
try {
|
||||
$medium = self::$Media->getSeminaryMediaById($seminarymediaId);
|
||||
$htmlImage = sprintf(
|
||||
'<img src="%s" alt="%s" />',
|
||||
$this->linker->link(array('media','seminary', \hhu\z\controllers\SeminaryRoleController::$seminary['url'],$medium['url'])),
|
||||
$medium['description']
|
||||
);
|
||||
}
|
||||
catch(\nre\exceptions\IdNotFoundException $e) {
|
||||
}
|
||||
}
|
||||
|
||||
// Replace placholder
|
||||
$startPos = mb_strlen(substr($string, 0, $match[0][1]), 'UTF-8');
|
||||
$endPos = $startPos + mb_strlen($match[0][0]);
|
||||
$string = mb_substr($string, 0, $startPos, 'UTF-8'). $htmlImage . mb_substr($string, $endPos, null, 'UTF-8');
|
||||
}
|
||||
|
||||
|
||||
// Return processed string
|
||||
return $string;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Load the Media-Model if it is not loaded
|
||||
*
|
||||
* @return boolean Whether the Media-Model has been loaded or not
|
||||
*/
|
||||
private function loadMediaModel()
|
||||
{
|
||||
// Do not load Model if it has already been loaded
|
||||
if(!is_null(self::$Media)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
// Load class
|
||||
Model::load('media');
|
||||
|
||||
// Construct Model
|
||||
self::$Media = Model::factory('media');
|
||||
}
|
||||
catch(\Exception $e) {
|
||||
}
|
||||
|
||||
|
||||
// Return whether Media-Model has been loaded or not
|
||||
return !is_null(self::$Media);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
@ -28,7 +28,7 @@
|
|||
* @param string $string String to be masked
|
||||
* @return string Masked string
|
||||
*/
|
||||
static function t($string)
|
||||
public static function t($string)
|
||||
{
|
||||
return nl2br(htmlspecialchars($string));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
<?php endforeach ?>
|
||||
</select>
|
||||
<?php endif ?>
|
||||
<?=\hhu\z\Utils::t($text)?>
|
||||
<?=$t->t($text)?>
|
||||
<?php endforeach ?>
|
||||
|
||||
<br /><br />
|
||||
|
|
|
|||
|
|
@ -7,6 +7,6 @@
|
|||
<?php endforeach ?>
|
||||
</select>
|
||||
<?php endif ?>
|
||||
<?=\hhu\z\Utils::t($text)?>
|
||||
<?=$t->t($text)?>
|
||||
<?php endforeach ?>
|
||||
</form>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<?php foreach($question['answers'] as $i => &$answer) : ?>
|
||||
<li>
|
||||
<input type="checkbox" id="answers[<?=$i?>]" name="answers[<?=$i?>]" value="true" <?=(array_key_exists('useranswer', $answer) && $answer['useranswer']) ? 'checked="checked"' : '' ?> />
|
||||
<label for="answers[<?=$i?>]"><?=\hhu\z\Utils::t($answer['answer'])?></label>
|
||||
<label for="answers[<?=$i?>]"><?=$t->t($answer['answer'])?></label>
|
||||
</li>
|
||||
<?php endforeach ?>
|
||||
</ol>
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
<ol>
|
||||
<?php foreach($questions as $pos => &$question) : ?>
|
||||
<li>
|
||||
<h1><?=\hhu\z\Utils::t($question['question'])?></h1>
|
||||
<h1><?=$t->t($question['question'])?></h1>
|
||||
<ol>
|
||||
<?php foreach($question['answers'] as &$answer) : ?>
|
||||
<li>
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
<?php if($i > 0) : ?>
|
||||
<input type="text" name="answers[<?=$i-1?>]" value="<?=$regexs[$i-1]['answer']?>" />
|
||||
<?php endif ?>
|
||||
<?=\hhu\z\Utils::t($text)?>
|
||||
<?=$t->t($text)?>
|
||||
<?php endforeach ?>
|
||||
|
||||
<br /><br />
|
||||
|
|
|
|||
|
|
@ -3,5 +3,5 @@
|
|||
<span style="background-color:grey"><?=$regexs[$i-1]['answer']?></span>
|
||||
<?php if($regexs[$i-1]['right']) : ?>✓<?php else: ?>✕<?php endif ?>
|
||||
<?php endif ?>
|
||||
<?=\hhu\z\Utils::t($text)?>
|
||||
<?=$t->t($text)?>
|
||||
<?php endforeach ?>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue