extra_field_option.lib.php 26 KB

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