PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/core/Tracker/Field/TextArea.php

https://gitlab.com/ElvisAns/tiki
PHP | 377 lines | 328 code | 37 blank | 12 comment | 29 complexity | 0216710098809b0428983b34ab603f6e MD5 | raw file
  1. <?php
  2. // (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
  3. //
  4. // All Rights Reserved. See copyright.txt for details and a complete list of authors.
  5. // Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
  6. // $Id$
  7. /**
  8. * Handler class for TextArea
  9. *
  10. * Letter key: ~a~
  11. *
  12. */
  13. class Tracker_Field_TextArea extends Tracker_Field_Text
  14. {
  15. public static function getTypes()
  16. {
  17. return [
  18. 'a' => [
  19. 'name' => tr('Text Area'),
  20. 'description' => tr('Multi-line text input'),
  21. 'help' => 'Textarea-Tracker-Field',
  22. 'prefs' => ['trackerfield_textarea'],
  23. 'tags' => ['basic'],
  24. 'default' => 'y',
  25. 'params' => [
  26. 'toolbars' => [
  27. 'name' => tr('Toolbars'),
  28. 'description' => tr('Enable the toolbars as syntax helpers.'),
  29. 'filter' => 'int',
  30. 'options' => [
  31. 0 => tr('Disable'),
  32. 1 => tr('Enable'),
  33. ],
  34. 'legacy_index' => 0,
  35. ],
  36. 'width' => [
  37. 'name' => tr('Width'),
  38. 'description' => tr('Size of the text area, in characters.'),
  39. 'filter' => 'int',
  40. 'legacy_index' => 1,
  41. ],
  42. 'height' => [
  43. 'name' => tr('Height'),
  44. 'description' => tr('Size of the text area, in lines.'),
  45. 'filter' => 'int',
  46. 'legacy_index' => 2,
  47. ],
  48. 'max' => [
  49. 'name' => tr('Character Limit'),
  50. 'description' => tr('Maximum number of characters to be stored.'),
  51. 'filter' => 'int',
  52. 'legacy_index' => 3,
  53. ],
  54. 'listmax' => [
  55. 'name' => tr('Display Limit (List)'),
  56. 'description' => tr('Maximum number of characters to be displayed in list mode before the value gets truncated.'),
  57. 'filter' => 'int',
  58. 'legacy_index' => 4,
  59. ],
  60. 'wordmax' => [
  61. 'name' => tr('Word Count'),
  62. 'description' => tr('Limit the length of the text, in number of words.'),
  63. 'filter' => 'int',
  64. 'legacy_index' => 5,
  65. ],
  66. 'distinct' => [
  67. 'name' => tr('Distinct Values'),
  68. 'description' => tr('All values in the field must be different.'),
  69. 'filter' => 'alpha',
  70. 'default' => 'n',
  71. 'options' => [
  72. 'n' => tr('No'),
  73. 'y' => tr('Yes'),
  74. ],
  75. 'legacy_index' => 6,
  76. ],
  77. 'wysiwyg' => [
  78. 'name' => tr('Use WYSIWYG'),
  79. 'description' => tr('Use a rich text editor instead of inputting plain text.'),
  80. 'default' => 'n',
  81. 'filter' => 'alpha',
  82. 'options' => [
  83. 'n' => tr('No'),
  84. 'y' => tr('Yes'),
  85. ],
  86. 'legacy_index' => 7,
  87. ],
  88. 'samerow' => [
  89. 'name' => tr('Same Row'),
  90. 'description' => tr('Display the field name and input on the same row.'),
  91. 'deprecated' => false,
  92. 'filter' => 'int',
  93. 'default' => 1,
  94. 'options' => [
  95. 0 => tr('No'),
  96. 1 => tr('Yes'),
  97. ],
  98. 'legacy_index' => 8,
  99. ],
  100. ],
  101. ],
  102. ];
  103. }
  104. public function postSaveHook($value)
  105. {
  106. $itemId = (int)$this->getItemId();
  107. $fieldId = (int)$this->getFieldId();
  108. $permName = $this->getConfiguration('permName');
  109. $wikilib = TikiLib::lib('wiki');
  110. $wikilib->update_wikicontent_relations($value, 'trackeritemfield', sprintf("%d:%d", $itemId, $fieldId));
  111. $wikilib->update_wikicontent_links($value, 'trackeritemfield', sprintf("%d:%d", $itemId, $fieldId));
  112. }
  113. public function getFieldData(array $requestData = [])
  114. {
  115. $ins_id = $this->getInsertId();
  116. $data = $this->processMultilingual($requestData, $ins_id);
  117. global $user, $prefs;
  118. $language = $prefs['language'];
  119. $c = 0;
  120. if (isset($requestData[$ins_id])) {
  121. $value = (array) $data['value'];
  122. foreach ($value as $key => $val) {
  123. $newvalue = TikiLib::lib('parser')->process_save_plugins(
  124. $val,
  125. [
  126. 'type' => 'trackeritem',
  127. 'itemId' => $this->getItemId(),
  128. 'user' => $user,
  129. ]
  130. );
  131. if ($newvalue !== $val) {
  132. if (isset($data['lingualvalue'][$c])) {
  133. $data['lingualvalue'][$c]['value'] = $newvalue;
  134. $data['lingualpvalue'][$c]['value'] = $this->attemptParse($newvalue);
  135. $data['value'][$data['lingualvalue'][$c]['lang']] = $newvalue;
  136. if ($data['lingualvalue'][$c]['lang'] === $language) {
  137. $data['pvalue'] = $data['lingualpvalue'][$c]['value'];
  138. }
  139. } else {
  140. $data['value'] = $newvalue;
  141. $data['pvalue'] = $this->attemptParse($newvalue);
  142. }
  143. }
  144. $c++;
  145. }
  146. }
  147. return $data;
  148. }
  149. public function renderInput($context = [])
  150. {
  151. static $firstTime = true;
  152. $cols = $this->getOption('width');
  153. $rows = $this->getOption('height');
  154. $data = [
  155. 'toolbar' => $this->getOption('toolbars') ? 'y' : 'n',
  156. 'cols' => ($cols >= 1) ? $cols : 80,
  157. 'rows' => ($rows >= 1) ? $rows : 6,
  158. 'keyup' => '',
  159. ];
  160. if ($this->getOption('wordmax')) {
  161. $data['keyup'] = "wordCount({$this->getOption('wordmax')}, this, 'cpt_{$this->getConfiguration('fieldId')}', '" . addcslashes(tr('Word Limit Exceeded'), "'") . "')";
  162. } elseif ($this->getOption('max')) {
  163. $data['keyup'] = "charCount({$this->getOption('max')}, this, 'cpt_{$this->getConfiguration('fieldId')}', '" . addcslashes(tr('Character Limit Exceeded'), "'") . "')";
  164. }
  165. $data['element_id'] = 'area_' . uniqid();
  166. if ($firstTime && $this->getOption('wysiwyg') === 'y') { // wysiwyg
  167. $is_html = '<input type="hidden" id="allowhtml" value="1" />';
  168. $firstTime = false;
  169. } else {
  170. $is_html = '';
  171. }
  172. return $this->renderTemplate('trackerinput/textarea.tpl', $context, $data) . $is_html;
  173. }
  174. public function renderInnerOutput($context = [])
  175. {
  176. $output = parent::renderInnerOutput($context);
  177. if (! empty($context['list_mode']) && $context['list_mode'] === 'y' && $this->getOption('listmax')) {
  178. TikiLib::lib('smarty')->loadPlugin('smarty_modifier_truncate');
  179. return smarty_modifier_truncate(strip_tags($output), $this->getOption('listmax'));
  180. } else {
  181. return $output;
  182. }
  183. }
  184. public function getDocumentPart(Search_Type_Factory_Interface $typeFactory)
  185. {
  186. $value = $this->getValue();
  187. $fieldType = $this->getIndexableType();
  188. $baseKey = $this->getBaseKey();
  189. if ($this->getConfiguration('isMultilingual') == 'y') {
  190. if (! empty($value)) {
  191. $decoded = json_decode($value, true);
  192. if (is_array($decoded)) {
  193. $value = implode("\n", $decoded);
  194. } else {
  195. $decoded = [];
  196. }
  197. } else {
  198. $decoded = [];
  199. }
  200. $data = [$baseKey => $typeFactory->$fieldType($value)];
  201. foreach ($decoded as $lang => $content) {
  202. $data["{$baseKey}_{$lang}"] = $typeFactory->$fieldType($content);
  203. $data["{$baseKey}_{$lang}_raw"] = $typeFactory->identifier($content);
  204. }
  205. return $data;
  206. } else {
  207. $data = [
  208. $baseKey => $typeFactory->$fieldType($value),
  209. "{$baseKey}_raw" => $typeFactory->identifier($value),
  210. ];
  211. return $data;
  212. }
  213. }
  214. public function getProvidedFields()
  215. {
  216. global $prefs;
  217. $baseKey = $this->getBaseKey();
  218. $data = [$baseKey, "{$baseKey}_raw"];
  219. if ($this->getConfiguration('isMultilingual') == 'y') {
  220. foreach ($prefs['available_languages'] as $lang) {
  221. $data[] = "{$baseKey}_{$lang}";
  222. $data[] = "{$baseKey}_{$lang}_raw";
  223. }
  224. }
  225. return $data;
  226. }
  227. public function getGlobalFields()
  228. {
  229. global $prefs;
  230. $baseKey = $this->getBaseKey();
  231. $data = [$baseKey => true];
  232. if ($this->getConfiguration('isMultilingual') == 'y') {
  233. foreach ($prefs['available_languages'] as $lang) {
  234. $data[$baseKey . '_' . $lang] = true;
  235. }
  236. }
  237. return $data;
  238. }
  239. public function getTabularSchema()
  240. {
  241. global $prefs;
  242. $schema = new Tracker\Tabular\Schema($this->getTrackerDefinition());
  243. $permName = $this->getConfiguration('permName');
  244. $baseKey = $this->getBaseKey();
  245. $name = $this->getConfiguration('name');
  246. $plain = function ($lang) {
  247. return function ($value, $extra) use ($lang) {
  248. if (isset($extra['text'])) {
  249. $value = $extra['text'];
  250. } elseif ($lang && isset($value[$lang])) {
  251. $value = $lang;
  252. }
  253. return $value;
  254. };
  255. };
  256. $render = function ($lang) use ($plain) {
  257. $f = $plain($lang);
  258. return function ($value, $extra) use ($f) {
  259. $value = $f($value, $extra);
  260. return $this->attemptParse($value);
  261. };
  262. };
  263. if ('y' !== $this->getConfiguration('isMultilingual', 'n')) {
  264. $schema->addNew($permName, "default")
  265. ->setLabel($name)
  266. ->setReadOnly(true)
  267. ->setPlainReplacement('default-raw')
  268. ->addQuerySource('text', "{$baseKey}_raw")
  269. ->setRenderTransform($render(null))
  270. ;
  271. $schema->addNew($permName, 'default-raw')
  272. ->setLabel($name)
  273. ->addQuerySource('text', "{$baseKey}_raw")
  274. ->setRenderTransform($plain(null))
  275. ->setParseIntoTransform(function (&$info, $value) use ($permName) {
  276. $info['fields'][$permName] = $value;
  277. })
  278. ;
  279. // convert incoming html to wiki syntax and the opposite on export
  280. $schema->addNew($permName, 'wiki-html')
  281. ->setLabel($name)
  282. ->addQuerySource('text', "{$baseKey}_raw")
  283. ->setRenderTransform($render(null))
  284. ->setParseIntoTransform(function (&$info, $value) use ($permName) {
  285. $info['fields'][$permName] = TikiLib::lib('edit')->parseToWiki($value);
  286. });
  287. } else {
  288. $lang = $prefs['language'];
  289. $schema->addNew($permName, "current")
  290. ->setLabel($name)
  291. ->setReadOnly(true)
  292. ->setPlainReplacement('current-raw')
  293. ->addQuerySource('text', "{$baseKey}_{$lang}_raw")
  294. ->setRenderTransform($render($lang))
  295. ;
  296. $schema->addNew($permName, 'current-raw')
  297. ->setLabel(tr('%0 (%1)', $name, $lang))
  298. ->setReadOnly(true)
  299. ->addIncompatibility($permName, 'current')
  300. ->addQuerySource('text', "{$baseKey}_{$lang}_raw")
  301. ->setRenderTransform($plain($lang))
  302. ;
  303. foreach ($prefs['available_languages'] as $lang) {
  304. $schema->addNew($permName, $lang)
  305. ->setLabel($name)
  306. ->setPlainReplacement("$lang-raw")
  307. ->addQuerySource('text', "{$baseKey}_{$lang}_raw")
  308. ->addIncompatibility($permName, 'current')
  309. ->addIncompatibility($permName, 'current-raw')
  310. ->setRenderTransform($render($lang))
  311. ;
  312. $schema->addNew($permName, "$lang-raw")
  313. ->setLabel(tr('%0 (%1)', $name, $lang))
  314. ->addQuerySource('text', "{$baseKey}_{$lang}_raw")
  315. ->addIncompatibility($permName, 'current')
  316. ->addIncompatibility($permName, 'current-raw')
  317. ->addIncompatibility($permName, $lang)
  318. ->setRenderTransform($plain($lang))
  319. ->setParseIntoTransform(function (&$info, $value) use ($permName, $lang) {
  320. $info['fields'][$permName][$lang] = $value;
  321. })
  322. ;
  323. }
  324. }
  325. return $schema;
  326. }
  327. protected function attemptParse($text)
  328. {
  329. global $prefs;
  330. $parseOptions = [];
  331. if ($this->getOption('wysiwyg') === 'y') {
  332. $parseOptions['is_html'] = ($prefs['wysiwyg_htmltowiki'] !== 'y');
  333. }
  334. return TikiLib::lib('parser')->parse_data($text, $parseOptions);
  335. }
  336. protected function getIndexableType()
  337. {
  338. return 'wikitext';
  339. }
  340. }