extra_field_option.lib.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class ExtraFieldOption
  5. * Handles the extra fields for various objects (users, sessions, courses)
  6. */
  7. class ExtraFieldOption extends Model
  8. {
  9. public $columns = array(
  10. 'id',
  11. 'field_id',
  12. 'option_value',
  13. 'display_text',
  14. 'option_order',
  15. 'priority',
  16. 'priority_message',
  17. 'tms'
  18. );
  19. public $extraField;
  20. /**
  21. * Gets the table for the type of object for which we are using an extra field
  22. * @param string $type Type of object (course, user or session)
  23. */
  24. public function __construct($type)
  25. {
  26. parent::__construct();
  27. $this->type = $type;
  28. $extraField = new ExtraField($this->type);
  29. $this->extraField = $extraField;
  30. $this->table = Database::get_main_table(TABLE_EXTRA_FIELD_OPTIONS);
  31. $this->tableExtraField = Database::get_main_table(TABLE_EXTRA_FIELD);
  32. }
  33. /**
  34. * @return ExtraField
  35. */
  36. public function getExtraField()
  37. {
  38. return $this->extraField;
  39. }
  40. /**
  41. * Gets the number of options already available in the table for this item type
  42. * @return int Number of options available
  43. * @assert () >= 0
  44. */
  45. /*public function get_count()
  46. {
  47. $row = Database::select('count(*) as count', $this->table, array(), 'first');
  48. return $row['count'];
  49. }*/
  50. /**
  51. * Gets the number of options available for this field
  52. * @param int $fieldId
  53. *
  54. * @return int Number of options
  55. * @assert ('') === false
  56. * @assert (-1) == 0
  57. * @assert (0) == 0
  58. */
  59. public function get_count_by_field_id($fieldId)
  60. {
  61. if (empty($fieldId)) {
  62. return false;
  63. }
  64. $extraFieldType = $this->getExtraField()->getExtraFieldType();
  65. $fieldId = intval($fieldId);
  66. $sql = "SELECT count(*) as count
  67. FROM $this->table o
  68. INNER JOIN $this->tableExtraField e
  69. ON o.field_id = e.id
  70. WHERE
  71. o.field_id = $fieldId AND
  72. e.extra_field_type = $extraFieldType ";
  73. $result = Database::query($sql);
  74. $result = Database::fetch_array($result);
  75. return $result['count'];
  76. }
  77. /**
  78. * Returns a list of options for a specific field, separated by ";"
  79. * @param int $field_id
  80. * @param bool $add_id_in_array Indicates whether we want the results to be given with their id
  81. * @param string $ordered_by Order by clause (without the "order by") to be added to the SQL query
  82. * @return string List of options separated by ;
  83. * @assert (-1, false, null) == ''
  84. */
  85. public function getFieldOptionsToString($field_id, $add_id_in_array = false, $ordered_by = null)
  86. {
  87. $options = self::get_field_options_by_field($field_id, $add_id_in_array, $ordered_by);
  88. $new_options = array();
  89. if (!empty($options)) {
  90. foreach ($options as $option) {
  91. $new_options[] = $option['option_value'].':'.$option['display_text'];
  92. }
  93. $string = implode(';', $new_options);
  94. return $string;
  95. }
  96. return '';
  97. }
  98. /**
  99. * Delete all the options of a specific field
  100. * @param int $field_id
  101. *
  102. * @result void
  103. * @assert (-1) === false
  104. */
  105. public function delete_all_options_by_field_id($field_id)
  106. {
  107. $field_id = intval($field_id);
  108. $sql = "DELETE FROM {$this->table} WHERE field_id = $field_id";
  109. $r = Database::query($sql);
  110. return $r;
  111. }
  112. /**
  113. * @param array $params
  114. * @param bool $showQuery
  115. *
  116. * @return int
  117. */
  118. public function saveOptions($params, $showQuery = false)
  119. {
  120. $optionInfo = self::get_field_option_by_field_and_option(
  121. $params['field_id'],
  122. $params['option_value']
  123. );
  124. if ($optionInfo == false) {
  125. $optionValue = api_replace_dangerous_char($params['option_value']);
  126. $order = self::get_max_order($params['field_id']);
  127. $newParams = array(
  128. 'field_id' => $params['field_id'],
  129. 'value' => trim($optionValue),
  130. 'display_text' => trim($params['display_text']),
  131. 'option_order' => $order
  132. );
  133. return parent::save($newParams, $showQuery);
  134. }
  135. return false;
  136. }
  137. /**
  138. * Saves an option into the corresponding *_field_options table
  139. * @param array $params Parameters to be considered for the insertion
  140. * @param bool $showQuery Whether to show the query (sent to the parent save() method)
  141. *
  142. * @return bool True on success, false on error
  143. * @assert (array('field_id'=>0), false) === false
  144. * @assert (array('field_id'=>1), false) === true
  145. */
  146. public function save($params, $showQuery = false)
  147. {
  148. $field_id = intval($params['field_id']);
  149. if (empty($field_id)) {
  150. return false;
  151. }
  152. if (!empty($params['field_options']) &&
  153. in_array(
  154. $params['field_type'],
  155. array(
  156. ExtraField::FIELD_TYPE_RADIO,
  157. ExtraField::FIELD_TYPE_SELECT,
  158. ExtraField::FIELD_TYPE_SELECT_MULTIPLE,
  159. ExtraField::FIELD_TYPE_DOUBLE_SELECT
  160. )
  161. )
  162. ) {
  163. if ($params['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) {
  164. //$params['field_options'] = France:Paris;Bretagne;Marseilles;Lyon|Belgique:Bruxelles;Namur;Liège;Bruges|Peru:Lima;Piura;
  165. $options_parsed = ExtraField::extra_field_double_select_convert_string_to_array(
  166. $params['field_options']
  167. );
  168. if (!empty($options_parsed)) {
  169. foreach ($options_parsed as $key => $option) {
  170. $sub_options = $option['options'];
  171. $new_params = array(
  172. 'field_id' => $field_id,
  173. 'option_value' => 0,
  174. 'display_text' => $option['label'],
  175. 'option_order' => 0,
  176. );
  177. // Looking if option already exists:
  178. $option_info = self::get_field_option_by_field_id_and_option_display_text(
  179. $field_id,
  180. $option['label']
  181. );
  182. if (empty($option_info)) {
  183. $sub_id = parent::save($new_params, $showQuery);
  184. } else {
  185. $sub_id = $option_info['id'];
  186. $new_params['id'] = $sub_id;
  187. parent::update($new_params, $showQuery);
  188. }
  189. foreach ($sub_options as $sub_option) {
  190. if (!empty($sub_option)) {
  191. $new_params = array(
  192. 'field_id' => $field_id,
  193. 'option_value' => $sub_id,
  194. 'display_text' => $sub_option,
  195. 'option_order' => 0,
  196. );
  197. $option_info = self::getFieldOptionByFieldIdAndOptionDisplayTextAndOptionValue(
  198. $field_id,
  199. $sub_option,
  200. $sub_id
  201. );
  202. if (empty($option_info)) {
  203. parent::save($new_params, $showQuery);
  204. } else {
  205. $new_params['id'] = $option_info['id'];
  206. parent::update($new_params, $showQuery);
  207. }
  208. }
  209. }
  210. }
  211. }
  212. $list = array();
  213. } else {
  214. $list = explode(';', $params['field_options']);
  215. }
  216. if (!empty($list)) {
  217. foreach ($list as $option) {
  218. $option_info = self::get_field_option_by_field_and_option($field_id, $option);
  219. // Use URLify only for new items
  220. $optionValue = api_replace_dangerous_char($option);
  221. $option = trim($option);
  222. if ($option_info == false) {
  223. $order = self::get_max_order($field_id);
  224. $new_params = array(
  225. 'field_id' => $field_id,
  226. 'option_value' => trim($optionValue),
  227. 'display_text' => trim($option),
  228. 'option_order' => $order,
  229. );
  230. parent::save($new_params, $showQuery);
  231. }
  232. }
  233. }
  234. }
  235. return true;
  236. }
  237. /**
  238. * Save one option item at a time
  239. * @param array $params Parameters specific to the option
  240. * @param bool $show_query Whether to show the query (sent to parent save() method)
  241. * @param bool $insert_repeated Whether to insert even if the option already exists
  242. * @return bool True on success, false on failure
  243. * @assert (array('field_id'=>0),false) === false
  244. * @assert (array('field_id'=>0),false) === true
  245. */
  246. public function save_one_item($params, $show_query = false, $insert_repeated = true)
  247. {
  248. $field_id = intval($params['field_id']);
  249. if (empty($field_id)) {
  250. return false;
  251. }
  252. if (isset($params['option_value'])) {
  253. $params['option_value'] = trim($params['option_value']);
  254. }
  255. if (isset($params['display_text'])) {
  256. $params['display_text'] = trim($params['display_text']);
  257. }
  258. if (empty($params['option_order'])) {
  259. $order = self::get_max_order($field_id);
  260. $params['option_order'] = $order;
  261. }
  262. if ($insert_repeated) {
  263. parent::save($params, $show_query);
  264. } else {
  265. $check = self::get_field_option_by_field_and_option(
  266. $field_id,
  267. $params['option_value']
  268. );
  269. if ($check == false) {
  270. parent::save($params, $show_query);
  271. }
  272. }
  273. return true;
  274. }
  275. /**
  276. * Get the complete row of a specific option of a specific field
  277. * @param int $field_id
  278. * @param string $option_value Value of the option
  279. * @return mixed The row on success or false on failure
  280. * @assert (0,'') === false
  281. */
  282. public function get_field_option_by_field_and_option($field_id, $option_value)
  283. {
  284. $field_id = intval($field_id);
  285. $option_value = Database::escape_string($option_value);
  286. $extraFieldType = $this->getExtraField()->getExtraFieldType();
  287. $sql = "SELECT s.* FROM {$this->table} s
  288. INNER JOIN {$this->tableExtraField} sf
  289. ON (s.field_id = sf.id)
  290. WHERE
  291. field_id = $field_id AND
  292. option_value = '".$option_value."' AND
  293. sf.extra_field_type = ".$extraFieldType."
  294. ";
  295. $result = Database::query($sql);
  296. if (Database::num_rows($result) > 0) {
  297. return Database::store_result($result, 'ASSOC');
  298. }
  299. return false;
  300. }
  301. /**
  302. * Get the complete row of a specific option's display text of a specific field
  303. * @param int $field_id
  304. * @param string $option_display_text Display value of the option
  305. * @return mixed The row on success or false on failure
  306. * @assert (0, '') === false
  307. */
  308. public function get_field_option_by_field_id_and_option_display_text($field_id, $option_display_text)
  309. {
  310. $field_id = intval($field_id);
  311. $option_display_text = Database::escape_string($option_display_text);
  312. $extraFieldType = $this->getExtraField()->getExtraFieldType();
  313. $sql = "SELECT s.* FROM {$this->table} s
  314. INNER JOIN {$this->tableExtraField} sf
  315. ON (s.field_id = sf.id)
  316. WHERE
  317. field_id = $field_id AND
  318. s.display_text = '".$option_display_text."' AND
  319. sf.extra_field_type = ".$extraFieldType."
  320. ";
  321. $result = Database::query($sql);
  322. if (Database::num_rows($result) > 0) {
  323. return Database::fetch_array($result, 'ASSOC');
  324. }
  325. return false;
  326. }
  327. /**
  328. * Get the complete row of a specific option's display text of a specific field
  329. * @param int $field_id
  330. * @param string $option_display_text Display value of the option
  331. * @param string $option_value Value of the option
  332. *
  333. * @return mixed The row on success or false on failure
  334. * @assert (0, '', '') === false
  335. */
  336. public function getFieldOptionByFieldIdAndOptionDisplayTextAndOptionValue(
  337. $field_id,
  338. $option_display_text,
  339. $option_value
  340. ) {
  341. $field_id = intval($field_id);
  342. $option_display_text = Database::escape_string($option_display_text);
  343. $option_value = Database::escape_string($option_value);
  344. $extraFieldType = $this->getExtraField()->getExtraFieldType();
  345. $sql = "SELECT s.* FROM {$this->table} s
  346. INNER JOIN {$this->tableExtraField} sf
  347. ON (s.field_id = sf.id)
  348. WHERE
  349. field_id = $field_id AND
  350. sf.display_text = '".$option_display_text."' AND
  351. option_value = '$option_value' AND
  352. sf.extra_field_type = ".$extraFieldType."
  353. ";
  354. $result = Database::query($sql);
  355. if (Database::num_rows($result) > 0) {
  356. return Database::fetch_array($result, 'ASSOC');
  357. }
  358. return false;
  359. }
  360. /**
  361. * Gets an array of options for a specific field
  362. * @param int $field_id The field ID
  363. * @param bool $add_id_in_array Whether to add the row ID in the result
  364. * @param null $ordered_by Extra ordering query bit
  365. * @return array The options if they exists. Otherwise return false
  366. */
  367. public function get_field_options_by_field($field_id, $add_id_in_array = false, $ordered_by = null)
  368. {
  369. $field_id = intval($field_id);
  370. $orderBy = null;
  371. switch ($ordered_by) {
  372. case 'id':
  373. $orderBy = ['id' => 'ASC'];
  374. break;
  375. case 'field_id':
  376. $orderBy = ['fieldId' => 'ASC'];
  377. break;
  378. case 'option_value':
  379. $orderBy = ['optionValue' => 'ASC'];
  380. break;
  381. case 'display_text':
  382. $orderBy = ['displayText' => 'ASC'];
  383. break;
  384. case 'priority':
  385. $orderBy = ['priority' => 'ASC'];
  386. break;
  387. case 'priority_message':
  388. $orderBy = ['priorityMessage' => 'ASC'];
  389. break;
  390. case 'option_order':
  391. $orderBy = ['optionOrder' => 'ASC'];
  392. break;
  393. }
  394. $result = Database::getManager()
  395. ->getRepository('ChamiloCoreBundle:ExtraFieldOptions')
  396. ->findBy(['field' => $field_id], $orderBy);
  397. if (!$result) {
  398. return false;
  399. }
  400. $options = [];
  401. foreach ($result as $row) {
  402. $option = [
  403. 'id' => $row->getId(),
  404. 'field_id' => $row->getField()->getId(),
  405. 'option_value' => $row->getValue(),
  406. 'display_text' => self::translateDisplayName($row->getDisplayText()),
  407. 'priority' => $row->getPriority(),
  408. 'priority_message' => $row->getPriorityMessage(),
  409. 'option_order' => $row->getOptionOrder()
  410. ];
  411. if ($add_id_in_array) {
  412. $options[$row->getId()] = $option;
  413. continue;
  414. }
  415. $options[] = $option;
  416. }
  417. return $options;
  418. }
  419. /**
  420. * Get options for a specific field as array or in JSON format suited for the double-select format
  421. * @param int $option_value_id Option value ID
  422. * @param bool $to_json Return format (whether it should be formatted to JSON or not)
  423. * @return mixed Row/JSON on success
  424. */
  425. public function get_second_select_field_options_by_field($option_value_id, $to_json = false)
  426. {
  427. $em = Database::getManager();
  428. $option = $em->find('ChamiloCoreBundle:ExtraFieldOptions', intval($option_value_id));
  429. if (!$option) {
  430. return !$to_json ? [] : '{}';
  431. }
  432. $subOptions = $em
  433. ->getRepository('ChamiloCoreBundle:ExtraFieldOptions')
  434. ->findSecondaryOptions($option);
  435. $optionsInfo = [];
  436. foreach ($subOptions as $subOption) {
  437. $optionsInfo[] = [
  438. 'id' => $subOption->getId(),
  439. 'field_id' => $subOption->getField()->getId(),
  440. 'option_value' => $subOption->getValue(),
  441. 'display_text' => $subOption->getDisplayText(),
  442. 'priority' => $subOption->getPriority(),
  443. 'priority_message' => $subOption->getPriorityMessage(),
  444. 'option_order' => $subOption->getOptionOrder()
  445. ];
  446. }
  447. if (!$to_json) {
  448. return $optionsInfo;
  449. }
  450. $json = [];
  451. foreach ($optionsInfo as $optionInfo) {
  452. $json[$optionInfo['id']] = $optionInfo['display_text'];
  453. }
  454. return json_encode($json);
  455. }
  456. /**
  457. * Get options for a specific field as string split by ;
  458. * @param int $field_id
  459. * @param string $ordered_by Extra query bit for reordering
  460. * @return string HTML string of options
  461. * @assert (0, '') === null
  462. */
  463. public function get_field_options_by_field_to_string($field_id, $ordered_by = null)
  464. {
  465. $field = new ExtraField($this->type);
  466. $field_info = $field->get($field_id);
  467. $options = self::get_field_options_by_field($field_id, false, $ordered_by);
  468. $elements = array();
  469. if (!empty($options)) {
  470. switch ($field_info['field_type']) {
  471. case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
  472. $html = ExtraField::extra_field_double_select_convert_array_to_string($options);
  473. break;
  474. default:
  475. foreach ($options as $option) {
  476. $elements[] = $option['option_value'];
  477. }
  478. $html = implode(';', $elements);
  479. break;
  480. }
  481. return $html;
  482. }
  483. return null;
  484. }
  485. /**
  486. * Get the maximum order value for a specific field
  487. * @param int $field_id
  488. * @return int Current max ID + 1 (we start from 0)
  489. * @assert (0, '') === 1
  490. */
  491. public function get_max_order($field_id)
  492. {
  493. $field_id = intval($field_id);
  494. $sql = "SELECT MAX(option_order)
  495. FROM {$this->table}
  496. WHERE field_id = $field_id";
  497. $res = Database::query($sql);
  498. $max = 1;
  499. if (Database::num_rows($res) > 0) {
  500. $row = Database::fetch_array($res);
  501. $max = $row[0] + 1;
  502. }
  503. return $max;
  504. }
  505. /**
  506. * Update the option using the given params
  507. * @param array $params data to be saved
  508. */
  509. public function update($params)
  510. {
  511. parent::update($params);
  512. }
  513. /**
  514. * Display a form with the options for the field_id given in REQUEST
  515. * @return void Prints output
  516. */
  517. public function display()
  518. {
  519. // action links
  520. echo '<div class="actions">';
  521. $field_id = isset($_REQUEST['field_id']) ? intval($_REQUEST['field_id']) : null;
  522. echo '<a href="'.api_get_self().'?action=add&type='.$this->type.'&field_id='.$field_id.'">'.
  523. Display::return_icon('add_user_fields.png', get_lang('Add'), '', ICON_SIZE_MEDIUM).'</a>';
  524. echo '</div>';
  525. echo Display::grid_html('extra_field_options');
  526. }
  527. /**
  528. * @return array
  529. */
  530. public function getPriorityOptions()
  531. {
  532. return array(
  533. '' => get_lang('SelectAnOption'),
  534. 1 => get_lang('Success'),
  535. 2 => get_lang('Info'),
  536. 3 => get_lang('Warning'),
  537. 4 => get_lang('Error'),
  538. );
  539. }
  540. /**
  541. * @param $priority
  542. * @return null|string
  543. */
  544. public function getPriorityMessageType($priority)
  545. {
  546. switch ($priority) {
  547. case 1:
  548. return 'success';
  549. case 2:
  550. return 'info';
  551. case 3:
  552. return 'warning';
  553. case 4:
  554. return 'error';
  555. }
  556. return null;
  557. }
  558. /**
  559. * Returns an HTML form for the current field
  560. * @param string URL to send the form to (action=...)
  561. * @param string Type of action to offer through the form (edit, usually)
  562. *
  563. * @return FormValidator
  564. */
  565. public function return_form($url, $action)
  566. {
  567. $form_name = $this->type.'_field';
  568. $form = new FormValidator($form_name, 'post', $url);
  569. // Setting the form elements
  570. $header = get_lang('Add');
  571. if ($action == 'edit') {
  572. $header = get_lang('Modify');
  573. }
  574. $form->addElement('header', $header);
  575. $id = isset($_GET['id']) ? intval($_GET['id']) : '';
  576. $form->addElement('hidden', 'id', $id);
  577. $form->addElement('hidden', 'type', $this->type);
  578. $form->addElement('hidden', 'field_id', $this->field_id);
  579. if ($action == 'edit') {
  580. $translateUrl = api_get_path(WEB_CODE_PATH) . 'extrafield/translate.php?' . http_build_query([
  581. 'extra_field_option' => $id
  582. ]);
  583. $translateButton = Display::toolbarButton(get_lang('TranslateThisTerm'), $translateUrl, 'language', 'link');
  584. $form->addText(
  585. 'display_text',
  586. [get_lang('Name'), $translateButton]
  587. );
  588. } else {
  589. $form->addElement('text', 'display_text', get_lang('Name'));
  590. }
  591. $form->addElement('text', 'option_value', get_lang('Value'));
  592. $form->addElement('text', 'option_order', get_lang('Order'));
  593. $form->addElement('select', 'priority', get_lang('Priority'), $this->getPriorityOptions());
  594. $form->addElement('textarea', 'priority_message', get_lang('PriorityOfMessage'));
  595. $defaults = array();
  596. if ($action == 'edit') {
  597. // Setting the defaults
  598. $defaults = $this->get($id, false);
  599. $form->freeze('option_value');
  600. $form->addButtonUpdate(get_lang('Modify'));
  601. } else {
  602. $form->addButtonCreate(get_lang('Add'));
  603. }
  604. $form->setDefaults($defaults);
  605. // Setting the rules
  606. $form->addRule('display_text', get_lang('ThisFieldIsRequired'), 'required');
  607. $form->addRule('option_value', get_lang('ThisFieldIsRequired'), 'required');
  608. return $form;
  609. }
  610. /**
  611. * @param string $tag
  612. * @param int $field_id
  613. * @param int $limit
  614. * @return array
  615. */
  616. public function searchByField($tag, $field_id, $limit = 10)
  617. {
  618. $field_id = intval($field_id);
  619. $limit = intval($limit);
  620. $tag = Database::escape_string($tag);
  621. $sql = "SELECT DISTINCT id, option_display_text
  622. FROM {$this->table}
  623. WHERE
  624. field_id = '".$field_id."' AND
  625. option_value LIKE '%$tag%'
  626. ORDER BY option_value
  627. LIMIT 0, $limit
  628. ";
  629. $result = Database::query($sql);
  630. $values = array();
  631. if (Database::num_rows($result)) {
  632. $values = Database::store_result($result, 'ASSOC');
  633. }
  634. return $values;
  635. }
  636. /**
  637. * @param string $tag
  638. * @param int $field_id
  639. * @param int $limit
  640. *
  641. * @return string
  642. */
  643. public function getSearchOptionsByField($tag, $field_id, $limit = 10)
  644. {
  645. $result = $this->searchByField($tag, $field_id, $limit = 10);
  646. $values = array();
  647. $json = null;
  648. if (!empty($result)) {
  649. foreach ($result as $item) {
  650. $values[] = array(
  651. 'value' => $item['id'],
  652. 'caption' => $item['option_display_text'],
  653. );
  654. }
  655. $json = json_encode($values);
  656. }
  657. return $json;
  658. }
  659. /**
  660. * Gets an element
  661. * @param int $id
  662. * @param bool $translateDisplayText Optional
  663. * @return array
  664. */
  665. public function get($id, $translateDisplayText = true)
  666. {
  667. $info = parent::get($id);
  668. if ($translateDisplayText) {
  669. $info['display_text'] = self::translateDisplayName($info['display_text']);
  670. }
  671. return $info;
  672. }
  673. /**
  674. * Translate the display text for a extra field option
  675. * @param string $defaultDisplayText
  676. * @return string
  677. */
  678. public static function translateDisplayName($defaultDisplayText)
  679. {
  680. $variableLanguage = self::getLanguageVariable($defaultDisplayText);
  681. return isset($GLOBALS[$variableLanguage]) ? $GLOBALS[$variableLanguage] : $defaultDisplayText;
  682. }
  683. /**
  684. * @param $defaultDisplayText
  685. * @return mixed|string
  686. */
  687. public static function getLanguageVariable($defaultDisplayText)
  688. {
  689. $variableLanguage = api_replace_dangerous_char($defaultDisplayText);
  690. $variableLanguage = str_replace('-', '_', $variableLanguage);
  691. $variableLanguage = api_underscore_to_camel_case($variableLanguage);
  692. return $variableLanguage;
  693. }
  694. /**
  695. * @param null $options
  696. * @return array
  697. */
  698. public function get_all($options = null)
  699. {
  700. $result = parent::get_all($options);
  701. foreach ($result as &$row) {
  702. $row['display_text'] = self::translateDisplayName($row['display_text']);
  703. }
  704. return $result;
  705. }
  706. }