extra_field_option.lib.php 26 KB

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