From f723382b936050ba6418126e33578c25b655dc53 Mon Sep 17 00:00:00 2001 From: coderkun Date: Fri, 18 Apr 2014 18:25:52 +0200 Subject: [PATCH] implement TextFormatter class to include media in texts --- app/Controller.inc | 3 + app/TextFormatter.inc | 127 ++++++++++++++++++ app/Utils.inc | 2 +- questtypes/choiceinput/html/quest.tpl | 2 +- questtypes/choiceinput/html/submission.tpl | 2 +- questtypes/multiplechoice/html/quest.tpl | 2 +- questtypes/multiplechoice/html/submission.tpl | 2 +- questtypes/textinput/html/quest.tpl | 2 +- questtypes/textinput/html/submission.tpl | 2 +- 9 files changed, 137 insertions(+), 7 deletions(-) create mode 100644 app/TextFormatter.inc diff --git a/app/Controller.inc b/app/Controller.inc index 5731aeed..461c9fd1 100644 --- a/app/Controller.inc +++ b/app/Controller.inc @@ -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'), diff --git a/app/TextFormatter.inc b/app/TextFormatter.inc new file mode 100644 index 00000000..0bdf1ef9 --- /dev/null +++ b/app/TextFormatter.inc @@ -0,0 +1,127 @@ + + * @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 + */ + 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( + '%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); + } + + } + +?> diff --git a/app/Utils.inc b/app/Utils.inc index a445dcf5..6f0475df 100644 --- a/app/Utils.inc +++ b/app/Utils.inc @@ -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)); } diff --git a/questtypes/choiceinput/html/quest.tpl b/questtypes/choiceinput/html/quest.tpl index 08029501..489cbc2f 100644 --- a/questtypes/choiceinput/html/quest.tpl +++ b/questtypes/choiceinput/html/quest.tpl @@ -7,7 +7,7 @@ - + t($text)?>

diff --git a/questtypes/choiceinput/html/submission.tpl b/questtypes/choiceinput/html/submission.tpl index 200b3c7f..40f60505 100644 --- a/questtypes/choiceinput/html/submission.tpl +++ b/questtypes/choiceinput/html/submission.tpl @@ -7,6 +7,6 @@ - + t($text)?> diff --git a/questtypes/multiplechoice/html/quest.tpl b/questtypes/multiplechoice/html/quest.tpl index c2c32595..3ac7a0d8 100644 --- a/questtypes/multiplechoice/html/quest.tpl +++ b/questtypes/multiplechoice/html/quest.tpl @@ -6,7 +6,7 @@ &$answer) : ?>
  • /> - +
  • diff --git a/questtypes/multiplechoice/html/submission.tpl b/questtypes/multiplechoice/html/submission.tpl index ae14f199..ebbbd493 100644 --- a/questtypes/multiplechoice/html/submission.tpl +++ b/questtypes/multiplechoice/html/submission.tpl @@ -1,7 +1,7 @@
      &$question) : ?>
    1. -

      +

      t($question['question'])?>

      1. diff --git a/questtypes/textinput/html/quest.tpl b/questtypes/textinput/html/quest.tpl index 53b7bd08..d074596f 100644 --- a/questtypes/textinput/html/quest.tpl +++ b/questtypes/textinput/html/quest.tpl @@ -3,7 +3,7 @@ 0) : ?> - + t($text)?>

        diff --git a/questtypes/textinput/html/submission.tpl b/questtypes/textinput/html/submission.tpl index dbc453c3..b3d606f4 100644 --- a/questtypes/textinput/html/submission.tpl +++ b/questtypes/textinput/html/submission.tpl @@ -3,5 +3,5 @@ - +t($text)?>