SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/RichText.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Helpers;
  15.  
  16. use Exception;
  17.  
  18. use Kohana;
  19.  
  20.  
  21. /**
  22. * Interface the display of a richtext field
  23. **/
  24. abstract class RichText {
  25.  
  26. /**
  27.   * Draw a rich text field
  28.   *
  29.   * Field type should be a class name sans 'RichText', e.g. 'TinyMCE4' for the 'TinyMCE4RichText' class.
  30.   * Optionally, the name can be bollowed by : and a config group name, e.g. 'TinyMCE4:Lite' for the 'Lite' config.
  31.   *
  32.   * @param $field_name The name of the field to draw.
  33.   * @param $content The content of the field, in HTML, but not escaped in any way.
  34.   * @param $width The width of the field, in pixels. Default = 600
  35.   * @param $height The height of the field, in pixels. Default = 300
  36.   * @param $type Override the field type. Default as per configuration and $_GET/$_SESSION overrides
  37.   **/
  38. static public function draw($field_name, $content, $width = 600, $height = 300, $type = null)
  39. {
  40. if ($type == null) $type = trim(@$_GET['_richtext']);
  41. if ($type == null) $type = Kohana::config('sprout.rich_text_type');
  42.  
  43. // Parse config group from 'type' var
  44. $matches = array();
  45. if (preg_match('/^([_a-zA-Z0-9]+):([-_a-zA-Z0-9]+)$/', $type, $matches)) {
  46. $type = $matches[1];
  47. $config_group = $matches[2];
  48. } else {
  49. $config_group = null;
  50. }
  51.  
  52. $class_name = $type . 'RichText';
  53. if (strpos($class_name, '\\') === false) {
  54. $class_name = 'Sprout\\Helpers\\' . $class_name;
  55. }
  56.  
  57. if (! class_exists($class_name)) {
  58. throw new Exception ("Unknown rich text type '{$type}'.");
  59. }
  60.  
  61. $rich = new $class_name;
  62. return $rich->drawInternal($field_name, $content, $width, $height, $config_group);
  63. }
  64.  
  65.  
  66. /**
  67.   * Shows a richtext field. Should output content directly
  68.   *
  69.   * @param string $field_name The name of the field
  70.   * @param string $content The content of the richtext field, in HTML
  71.   * @param int $width The width of the field, in pixels
  72.   * @param int $height The height of the field, in pixels
  73.   * @param string $config_group Specific configuration to use instead of the default
  74.   **/
  75. abstract protected function drawInternal($field_name, $content, $width = 600, $height = 300, $config_group = null);
  76.  
  77. }
  78.  
  79.  
  80.