extra_field.lib.php 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. class ExtraField extends Model
  4. {
  5. public $columns = array(
  6. 'id',
  7. 'field_type',
  8. 'field_variable',
  9. 'field_display_text',
  10. 'field_default_value',
  11. 'field_order',
  12. 'field_visible',
  13. 'field_changeable',
  14. 'field_filter',
  15. 'tms'
  16. );
  17. CONST FIELD_TYPE_TEXT = 1;
  18. CONST FIELD_TYPE_TEXTAREA = 2;
  19. CONST FIELD_TYPE_RADIO = 3;
  20. CONST FIELD_TYPE_SELECT = 4;
  21. CONST FIELD_TYPE_SELECT_MULTIPLE = 5;
  22. CONST FIELD_TYPE_DATE = 6;
  23. CONST FIELD_TYPE_DATETIME = 7;
  24. CONST FIELD_TYPE_DOUBLE_SELECT = 8;
  25. CONST FIELD_TYPE_DIVIDER = 9;
  26. CONST FIELD_TYPE_TAG = 10;
  27. CONST FIELD_TYPE_TIMEZONE = 11;
  28. CONST FIELD_TYPE_SOCIAL_PROFILE = 12;
  29. CONST FIELD_TYPE_CHECKBOX = 13;
  30. public $type = 'user'; //or session or course
  31. public $handler_id = 'user_id';
  32. public $pageName;
  33. public $pageUrl;
  34. function __construct($type)
  35. {
  36. $this->type = $type;
  37. switch ($this->type) {
  38. case 'course':
  39. $this->table_field_options = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_OPTIONS);
  40. $this->table_field_values = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
  41. //Used for the model
  42. $this->table = Database::get_main_table(TABLE_MAIN_COURSE_FIELD);
  43. $this->handler_id = 'course_code';
  44. $this->primaryKey = 'id';
  45. break;
  46. case 'user':
  47. $this->table_field_options = Database::get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS);
  48. $this->table_field_values = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  49. //Used for the model
  50. $this->table = Database::get_main_table(TABLE_MAIN_USER_FIELD);
  51. $this->handler_id = 'user_id';
  52. $this->primaryKey = 'user_id';
  53. break;
  54. case 'session':
  55. $this->table_field_options = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_OPTIONS);
  56. $this->table_field_values = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES);
  57. //Used for the model
  58. $this->table = Database::get_main_table(TABLE_MAIN_SESSION_FIELD);
  59. $this->handler_id = 'session_id';
  60. $this->primaryKey = 'id';
  61. break;
  62. case 'question':
  63. $this->table_field_options = Database::get_main_table(TABLE_MAIN_QUESTION_FIELD_OPTIONS);
  64. $this->table_field_values = Database::get_main_table(TABLE_MAIN_QUESTION_FIELD_VALUES);
  65. //Used for the model
  66. $this->table = Database::get_main_table(TABLE_MAIN_QUESTION_FIELD);
  67. $this->handler_id = 'question_id';
  68. $this->primaryKey = 'iid';
  69. break;
  70. }
  71. $this->pageUrl = 'extra_fields.php?type='.$this->type;
  72. $this->pageName = get_lang(ucwords($this->type).'Fields'); // Example QuestionFields
  73. }
  74. static function getValidExtraFieldTypes()
  75. {
  76. return array(
  77. 'user',
  78. 'course',
  79. 'session',
  80. 'question'
  81. );
  82. }
  83. public function get_count()
  84. {
  85. $row = Database::select('count(*) as count', $this->table, array(), 'first');
  86. return $row['count'];
  87. }
  88. public function get_all($where_conditions = array(), $order_field_options_by = null)
  89. {
  90. $options = Database::select(
  91. '*',
  92. $this->table,
  93. array('where' => $where_conditions, 'order' => 'field_order ASC')
  94. );
  95. $field_option = new ExtraFieldOption($this->type);
  96. if (!empty($options)) {
  97. foreach ($options as &$option) {
  98. $option['options'] = $field_option->get_field_options_by_field(
  99. $option['id'],
  100. false,
  101. $order_field_options_by
  102. );
  103. }
  104. }
  105. return $options;
  106. }
  107. public function get_handler_field_info_by_field_variable($field_variable)
  108. {
  109. $field_variable = Database::escape_string($field_variable);
  110. $sql_field = "SELECT * FROM {$this->table} WHERE field_variable = '$field_variable'";
  111. $result = Database::query($sql_field);
  112. if (Database::num_rows($result)) {
  113. $r_field = Database::fetch_array($result, 'ASSOC');
  114. return $r_field;
  115. } else {
  116. return false;
  117. }
  118. }
  119. public function get_max_field_order()
  120. {
  121. $sql = "SELECT MAX(field_order) FROM {$this->table}";
  122. $res = Database::query($sql);
  123. $order = 0;
  124. if (Database::num_rows($res) > 0) {
  125. $row = Database::fetch_row($res);
  126. $order = $row[0] + 1;
  127. }
  128. return $order;
  129. }
  130. public static function get_extra_fields_by_handler($handler)
  131. {
  132. $types = array();
  133. $types[self::FIELD_TYPE_TEXT] = get_lang('FieldTypeText');
  134. $types[self::FIELD_TYPE_TEXTAREA] = get_lang('FieldTypeTextarea');
  135. $types[self::FIELD_TYPE_RADIO] = get_lang('FieldTypeRadio');
  136. $types[self::FIELD_TYPE_SELECT] = get_lang('FieldTypeSelect');
  137. $types[self::FIELD_TYPE_SELECT_MULTIPLE] = get_lang('FieldTypeSelectMultiple');
  138. $types[self::FIELD_TYPE_DATE] = get_lang('FieldTypeDate');
  139. $types[self::FIELD_TYPE_DATETIME] = get_lang('FieldTypeDatetime');
  140. $types[self::FIELD_TYPE_DOUBLE_SELECT] = get_lang('FieldTypeDoubleSelect');
  141. $types[self::FIELD_TYPE_DIVIDER] = get_lang('FieldTypeDivider');
  142. $types[self::FIELD_TYPE_TAG] = get_lang('FieldTypeTag');
  143. $types[self::FIELD_TYPE_TIMEZONE] = get_lang('FieldTypeTimezone');
  144. $types[self::FIELD_TYPE_SOCIAL_PROFILE] = get_lang('FieldTypeSocialProfile');
  145. switch ($handler) {
  146. case 'course':
  147. case 'session':
  148. unset($types[self::FIELD_TYPE_TAG]);
  149. unset($types[self::FIELD_TYPE_SOCIAL_PROFILE]);
  150. break;
  151. case 'user':
  152. break;
  153. }
  154. return $types;
  155. }
  156. /**
  157. * @param FormValidator $form
  158. * @param int $item_id
  159. * @return array|bool
  160. */
  161. public function add_elements($form, $item_id = null)
  162. {
  163. if (empty($form)) {
  164. return false;
  165. }
  166. $extra_data = false;
  167. if (!empty($item_id)) {
  168. $extra_data = self::get_handler_extra_data($item_id);
  169. if ($form) {
  170. $form->setDefaults($extra_data);
  171. }
  172. }
  173. $extra_fields = self::get_all();
  174. $extra = ExtraField::set_extra_fields_in_form(
  175. $form,
  176. $extra_data,
  177. $this->type.'_field',
  178. false,
  179. false,
  180. $this->type,
  181. $extra_fields
  182. );
  183. return $extra;
  184. }
  185. public function get_handler_extra_data($item_id)
  186. {
  187. if (empty($item_id)) {
  188. return array();
  189. }
  190. $extra_data = array();
  191. $fields = self::get_all();
  192. $field_values = new ExtraFieldValue($this->type);
  193. if (!empty($fields) > 0) {
  194. foreach ($fields as $field) {
  195. $field_value = $field_values->get_values_by_handler_and_field_id($item_id, $field['id']);
  196. if ($field_value) {
  197. $field_value = $field_value['field_value'];
  198. switch ($field['field_type']) {
  199. case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
  200. $selected_options = explode(
  201. '::',
  202. $field_value
  203. );
  204. $extra_data['extra_'.$field['field_variable']]['extra_'.$field['field_variable']] = $selected_options[0];
  205. $extra_data['extra_'.$field['field_variable']]['extra_'.$field['field_variable'].'_second'] = $selected_options[1];
  206. break;
  207. case ExtraField::FIELD_TYPE_SELECT_MULTIPLE:
  208. $field_value = explode(';', $field_value);
  209. case ExtraField::FIELD_TYPE_RADIO:
  210. $extra_data['extra_'.$field['field_variable']]['extra_'.$field['field_variable']] = $field_value;
  211. break;
  212. /*case ExtraField::FIELD_TYPE_DATETIME:
  213. break;*/
  214. default:
  215. $extra_data['extra_'.$field['field_variable']] = $field_value;
  216. break;
  217. }
  218. }
  219. }
  220. }
  221. return $extra_data;
  222. }
  223. public function get_all_extra_field_by_type($field_type)
  224. {
  225. // all the information of the field
  226. $sql = "SELECT * FROM {$this->table} WHERE field_type='".Database::escape_string($field_type)."'";
  227. $result = Database::query($sql);
  228. $return = array();
  229. while ($row = Database::fetch_array($result)) {
  230. $return[] = $row['id'];
  231. }
  232. return $return;
  233. }
  234. public function get_field_types()
  235. {
  236. return self::get_extra_fields_by_handler($this->type);
  237. }
  238. public function get_field_type_by_id($id)
  239. {
  240. $types = self::get_field_types();
  241. if (isset($types[$id])) {
  242. return $types[$id];
  243. }
  244. return null;
  245. }
  246. /**
  247. * Converts a string like this:
  248. * France:Paris;Bretagne;Marseilles;Lyon|Belgique:Bruxelles;Namur;Liège;Bruges|Peru:Lima;Piura;
  249. * into
  250. * array('France' => array('Paris', 'Bregtane', 'Marseilles'), 'Belgique' => array('Namur', 'Liège', etc
  251. * @param string $string
  252. * @return array
  253. */
  254. static function extra_field_double_select_convert_string_to_array($string)
  255. {
  256. $options = explode('|', $string);
  257. $options_parsed = array();
  258. $id = 0;
  259. if (!empty($options)) {
  260. foreach ($options as $sub_options) {
  261. $options = explode(':', $sub_options);
  262. $sub_sub_options = explode(';', $options[1]);
  263. $options_parsed[$id] = array('label' => $options[0], 'options' => $sub_sub_options);
  264. $id++;
  265. }
  266. }
  267. return $options_parsed;
  268. }
  269. static function extra_field_double_select_convert_array_to_ordered_array($options)
  270. {
  271. $options_parsed = array();
  272. if (!empty($options)) {
  273. foreach ($options as $option) {
  274. if ($option['option_value'] == 0) {
  275. $options_parsed[$option['id']][] = $option;
  276. } else {
  277. $options_parsed[$option['option_value']][] = $option;
  278. }
  279. }
  280. }
  281. return $options_parsed;
  282. }
  283. /**
  284. * @param array options the result of the get_field_options_by_field() array
  285. */
  286. static function extra_field_double_select_convert_array_to_string($options)
  287. {
  288. $string = null;
  289. $options_parsed = self::extra_field_double_select_convert_array_to_ordered_array($options);
  290. if (!empty($options_parsed)) {
  291. foreach ($options_parsed as $option) {
  292. foreach ($option as $key => $item) {
  293. $string .= $item['option_display_text'];
  294. if ($key == 0) {
  295. $string .= ':';
  296. } else {
  297. if (isset($option[$key + 1])) {
  298. $string .= ';';
  299. }
  300. }
  301. }
  302. $string .= '|';
  303. }
  304. }
  305. if (!empty($string)) {
  306. $string = substr($string, 0, strlen($string) - 1);
  307. }
  308. return $string;
  309. }
  310. function clean_parameters($params)
  311. {
  312. if (!isset($params['field_variable']) || empty($params['field_variable'])) {
  313. $params['field_variable'] = trim(strtolower(str_replace(" ", "_", $params['field_display_text'])));
  314. }
  315. if (!isset($params['field_order'])) {
  316. $max_order = self::get_max_field_order();
  317. $params['field_order'] = $max_order;
  318. }
  319. return $params;
  320. }
  321. public function save($params, $show_query = false)
  322. {
  323. $session_field_info = self::get_handler_field_info_by_field_variable($params['field_variable']);
  324. $params = self::clean_parameters($params);
  325. if ($session_field_info) {
  326. return $session_field_info['id'];
  327. } else {
  328. if (!isset($params['tms'])) {
  329. $params['tms'] = api_get_utc_datetime();
  330. }
  331. $id = parent::save($params, $show_query);
  332. if ($id) {
  333. $session_field_option = new SessionFieldOption();
  334. $params['field_id'] = $id;
  335. $session_field_option->save($params);
  336. }
  337. return $id;
  338. }
  339. }
  340. public function update($params)
  341. {
  342. $params = self::clean_parameters($params);
  343. if (isset($params['id'])) {
  344. $field_option = new ExtraFieldOption($this->type);
  345. $params['field_id'] = $params['id'];
  346. $field_option->save($params);
  347. }
  348. parent::update($params);
  349. }
  350. public function delete($id)
  351. {
  352. parent::delete($id);
  353. $field_option = new ExtraFieldOption($this->type);
  354. $field_option->delete_all_options_by_field_id($id);
  355. $session_field_values = new ExtraFieldValue($this->type);
  356. $session_field_values->delete_all_values_by_field_id($id);
  357. }
  358. static function set_extra_fields_in_form(
  359. $form,
  360. $extra_data,
  361. $form_name,
  362. $admin_permissions = false,
  363. $user_id = null,
  364. $type = 'user',
  365. $extra = null
  366. ) {
  367. $user_id = intval($user_id);
  368. // User extra fields
  369. if ($type == 'user') {
  370. $extra = UserManager::get_extra_fields(0, 50, 5, 'ASC', true, null, true);
  371. }
  372. $jquery_ready_content = null;
  373. if (!empty($extra)) {
  374. foreach ($extra as $field_details) {
  375. if (!$admin_permissions) {
  376. if ($field_details['field_visible'] == 0) {
  377. continue;
  378. }
  379. }
  380. switch ($field_details['field_type']) {
  381. case ExtraField::FIELD_TYPE_TEXT:
  382. $form->addElement(
  383. 'text',
  384. 'extra_'.$field_details['field_variable'],
  385. $field_details['field_display_text'],
  386. array('class' => 'span4')
  387. );
  388. $form->applyFilter('extra_'.$field_details['field_variable'], 'stripslashes');
  389. $form->applyFilter('extra_'.$field_details['field_variable'], 'trim');
  390. if (!$admin_permissions) {
  391. if ($field_details['field_visible'] == 0) {
  392. $form->freeze(
  393. 'extra_'.$field_details['field_variable']
  394. );
  395. }
  396. }
  397. break;
  398. case ExtraField::FIELD_TYPE_TEXTAREA:
  399. $form->add_html_editor(
  400. 'extra_'.$field_details['field_variable'],
  401. $field_details['field_display_text'],
  402. false,
  403. false,
  404. array('ToolbarSet' => 'Profile', 'Width' => '100%', 'Height' => '130')
  405. );
  406. $form->applyFilter('extra_'.$field_details['field_variable'], 'stripslashes');
  407. $form->applyFilter('extra_'.$field_details['field_variable'], 'trim');
  408. if (!$admin_permissions) {
  409. if ($field_details['field_visible'] == 0) {
  410. $form->freeze(
  411. 'extra_'.$field_details['field_variable']
  412. );
  413. }
  414. }
  415. break;
  416. case ExtraField::FIELD_TYPE_RADIO:
  417. $group = array();
  418. foreach ($field_details['options'] as $option_details) {
  419. $options[$option_details['option_value']] = $option_details['option_display_text'];
  420. $group[] = $form->createElement(
  421. 'radio',
  422. 'extra_'.$field_details['field_variable'],
  423. $option_details['option_value'],
  424. $option_details['option_display_text'].'<br />',
  425. $option_details['option_value']
  426. );
  427. }
  428. $form->addGroup(
  429. $group,
  430. 'extra_'.$field_details['field_variable'],
  431. $field_details['field_display_text'],
  432. ''
  433. );
  434. if (!$admin_permissions) {
  435. if ($field_details['field_visible'] == 0) {
  436. $form->freeze(
  437. 'extra_'.$field_details['field_variable']
  438. );
  439. }
  440. }
  441. break;
  442. case ExtraField::FIELD_TYPE_CHECKBOX:
  443. $group = array();
  444. if (isset($field_details['options']) && !empty($field_details['options'])) {
  445. foreach ($field_details['options'] as $option_details) {
  446. $options[$option_details['option_value']] = $option_details['option_display_text'];
  447. $group[] = $form->createElement(
  448. 'checkbox',
  449. 'extra_'.$field_details['field_variable'],
  450. $option_details['option_value'],
  451. $option_details['option_display_text'].'<br />',
  452. $option_details['option_value']
  453. );
  454. }
  455. } else {
  456. // We asume that is a switch on/off with 1 and 0 as values
  457. $group[] = $form->createElement(
  458. 'checkbox',
  459. 'extra_'.$field_details['field_variable'],
  460. null,
  461. //$field_details['field_display_text'].'<br />',
  462. 'Yes <br />',
  463. null
  464. );
  465. }
  466. $form->addGroup(
  467. $group,
  468. 'extra_'.$field_details['field_variable'],
  469. $field_details['field_display_text'],
  470. ''
  471. );
  472. if (!$admin_permissions) {
  473. if ($field_details['field_visible'] == 0) {
  474. $form->freeze(
  475. 'extra_'.$field_details['field_variable']
  476. );
  477. }
  478. }
  479. break;
  480. case ExtraField::FIELD_TYPE_SELECT:
  481. $get_lang_variables = false;
  482. if (in_array(
  483. $field_details['field_variable'],
  484. array('mail_notify_message', 'mail_notify_invitation', 'mail_notify_group_message')
  485. )
  486. ) {
  487. $get_lang_variables = true;
  488. }
  489. $options = array();
  490. if (!empty($field_details['options'])) {
  491. foreach ($field_details['options'] as $option_id => $option_details) {
  492. if ($get_lang_variables) {
  493. $options[$option_details['option_value']] = get_lang(
  494. $option_details['option_display_text']
  495. );
  496. } else {
  497. $options[$option_details['option_value']] = $option_details['option_display_text'];
  498. }
  499. }
  500. }
  501. if ($get_lang_variables) {
  502. $field_details['field_display_text'] = get_lang($field_details['field_display_text']);
  503. }
  504. //chzn-select doesn't work for sessions??
  505. $form->addElement(
  506. 'select',
  507. 'extra_'.$field_details['field_variable'],
  508. $field_details['field_display_text'],
  509. $options,
  510. array('class' => '', 'id' => 'extra_'.$field_details['field_variable'])
  511. );
  512. if (!$admin_permissions) {
  513. if ($field_details['field_visible'] == 0) {
  514. $form->freeze('extra_'.$field_details['field_variable']);
  515. }
  516. }
  517. break;
  518. case ExtraField::FIELD_TYPE_SELECT_MULTIPLE:
  519. $options = array();
  520. foreach ($field_details['options'] as $option_id => $option_details) {
  521. $options[$option_details['option_value']] = $option_details['option_display_text'];
  522. }
  523. $form->addElement(
  524. 'select',
  525. 'extra_'.$field_details['field_variable'],
  526. $field_details['field_display_text'],
  527. $options,
  528. array('multiple' => 'multiple')
  529. );
  530. if (!$admin_permissions) {
  531. if ($field_details['field_visible'] == 0) {
  532. $form->freeze('extra_'.$field_details['field_variable']);
  533. }
  534. }
  535. break;
  536. case ExtraField::FIELD_TYPE_DATE:
  537. $form->addElement(
  538. 'datepickerdate',
  539. 'extra_'.$field_details['field_variable'],
  540. $field_details['field_display_text'],
  541. array('form_name' => $form_name)
  542. );
  543. $form->_elements[$form->_elementIndex['extra_'.$field_details['field_variable']]]->setLocalOption(
  544. 'minYear',
  545. 1900
  546. );
  547. $defaults['extra_'.$field_details['field_variable']] = date('Y-m-d 12:00:00');
  548. if (!isset($form->_defaultValues['extra_'.$field_details['field_variable']])) {
  549. $form->setDefaults($defaults);
  550. }
  551. if (!$admin_permissions) {
  552. if ($field_details['field_visible'] == 0) {
  553. $form->freeze('extra_'.$field_details['field_variable']);
  554. }
  555. }
  556. $form->applyFilter('theme', 'trim');
  557. break;
  558. case ExtraField::FIELD_TYPE_DATETIME:
  559. $form->addElement(
  560. 'datepicker',
  561. 'extra_'.$field_details['field_variable'],
  562. $field_details['field_display_text'],
  563. array('form_name' => $form_name)
  564. );
  565. $form->_elements[$form->_elementIndex['extra_'.$field_details['field_variable']]]->setLocalOption(
  566. 'minYear',
  567. 1900
  568. );
  569. $defaults['extra_'.$field_details['field_variable']] = date('Y-m-d 12:00:00');
  570. if (!isset($form->_defaultValues['extra_'.$field_details['field_variable']])) {
  571. $form->setDefaults($defaults);
  572. }
  573. if (!$admin_permissions) {
  574. if ($field_details['field_visible'] == 0) {
  575. $form->freeze('extra_'.$field_details['field_variable']);
  576. }
  577. }
  578. $form->applyFilter('theme', 'trim');
  579. break;
  580. case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
  581. $first_select_id = 'first_extra_'.$field_details['field_variable'];
  582. $url = api_get_path(WEB_AJAX_PATH).'extra_field.ajax.php?1=1';
  583. $jquery_ready_content .= '
  584. $("#'.$first_select_id.'").on("change", function() {
  585. var id = $(this).val();
  586. if (id) {
  587. $.ajax({
  588. url: "'.$url.'&a=get_second_select_options",
  589. dataType: "json",
  590. data: "type='.$type.'&field_id='.$field_details['id'].'&option_value_id="+id,
  591. success: function(data) {
  592. $("#second_extra_'.$field_details['field_variable'].'").empty();
  593. $.each(data, function(index, value) {
  594. $("#second_extra_'.$field_details['field_variable'].'").append($("<option/>", {
  595. value: index,
  596. text: value
  597. }));
  598. });
  599. },
  600. });
  601. } else {
  602. $("#second_extra_'.$field_details['field_variable'].'").empty();
  603. }
  604. });';
  605. $first_id = null;
  606. $second_id = null;
  607. if (!empty($extra_data)) {
  608. $first_id = $extra_data['extra_'.$field_details['field_variable']]['extra_'.$field_details['field_variable']];
  609. $second_id = $extra_data['extra_'.$field_details['field_variable']]['extra_'.$field_details['field_variable'].'_second'];
  610. }
  611. $options = ExtraField::extra_field_double_select_convert_array_to_ordered_array(
  612. $field_details['options']
  613. );
  614. $values = array('' => get_lang('Select'));
  615. $second_values = array();
  616. if (!empty($options)) {
  617. foreach ($options as $option) {
  618. foreach ($option as $sub_option) {
  619. if ($sub_option['option_value'] == '0') {
  620. $values[$sub_option['id']] = $sub_option['option_display_text'];
  621. } else {
  622. if ($first_id === $sub_option['option_value']) {
  623. $second_values[$sub_option['id']] = $sub_option['option_display_text'];
  624. }
  625. }
  626. }
  627. }
  628. }
  629. $group = array();
  630. $group[] = $form->createElement(
  631. 'select',
  632. 'extra_'.$field_details['field_variable'],
  633. null,
  634. $values,
  635. array('id' => $first_select_id)
  636. );
  637. $group[] = $form->createElement(
  638. 'select',
  639. 'extra_'.$field_details['field_variable'].'_second',
  640. null,
  641. $second_values,
  642. array('id' => 'second_extra_'.$field_details['field_variable'])
  643. );
  644. $form->addGroup(
  645. $group,
  646. 'extra_'.$field_details['field_variable'],
  647. $field_details['field_display_text'],
  648. '&nbsp;'
  649. );
  650. if (!$admin_permissions) {
  651. if ($field_details['field_visible'] == 0) {
  652. $form->freeze('extra_'.$field_details['field_variable']);
  653. }
  654. }
  655. break;
  656. case ExtraField::FIELD_TYPE_DIVIDER:
  657. $form->addElement(
  658. 'static',
  659. $field_details['field_variable'],
  660. '<br /><strong>'.$field_details['field_display_text'].'</strong>'
  661. );
  662. break;
  663. case ExtraField::FIELD_TYPE_TAG:
  664. //the magic should be here
  665. $user_tags = UserManager::get_user_tags($user_id, $field_details['id']);
  666. $tag_list = '';
  667. if (is_array($user_tags) && count($user_tags) > 0) {
  668. foreach ($user_tags as $tag) {
  669. $tag_list .= '<option value="'.$tag['tag'].'" class="selected">'.$tag['tag'].'</option>';
  670. }
  671. }
  672. $multi_select = '<select id="extra_'.$field_details['field_variable'].'" name="extra_'.$field_details['field_variable'].'">
  673. '.$tag_list.'
  674. </select>';
  675. $form->addElement('label', $field_details['field_display_text'], $multi_select);
  676. $url = api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php';
  677. $complete_text = get_lang('StartToType');
  678. //if cache is set to true the jquery will be called 1 time
  679. $field_variable = $field_details['field_variable'];
  680. $field_id = $field_details['id'];
  681. $jquery_ready_content .= <<<EOF
  682. $("#extra_$field_variable").fcbkcomplete({
  683. json_url: "$url?a=search_tags&field_id=$field_id",
  684. cache: false,
  685. filter_case: true,
  686. filter_hide: true,
  687. complete_text:"$complete_text",
  688. firstselected: false,
  689. filter_selected: true,
  690. newel: true
  691. });
  692. EOF;
  693. break;
  694. case ExtraField::FIELD_TYPE_TIMEZONE:
  695. $form->addElement(
  696. 'select',
  697. 'extra_'.$field_details['field_variable'],
  698. $field_details['field_display_text'],
  699. api_get_timezones(),
  700. ''
  701. );
  702. if ($field_details['field_visible'] == 0) {
  703. $form->freeze(
  704. 'extra_'.$field_details['field_variable']
  705. );
  706. }
  707. break;
  708. case ExtraField::FIELD_TYPE_SOCIAL_PROFILE:
  709. // get the social network's favicon
  710. $icon_path = UserManager::get_favicon_from_url(
  711. $extra_data['extra_'.$field_details['field_variable']],
  712. $field_details['field_default_value']
  713. );
  714. // special hack for hi5
  715. $leftpad = '1.7';
  716. $top = '0.4';
  717. $domain = parse_url($icon_path, PHP_URL_HOST);
  718. if ($domain == 'www.hi5.com' or $domain == 'hi5.com') {
  719. $leftpad = '3';
  720. $top = '0';
  721. }
  722. // print the input field
  723. $form->addElement(
  724. 'text',
  725. 'extra_'.$field_details['field_variable'],
  726. $field_details['field_display_text'],
  727. array(
  728. 'size' => 60,
  729. 'style' => 'background-image: url(\''.$icon_path.'\'); background-repeat: no-repeat; background-position: 0.4em '.$top.'em; padding-left: '.$leftpad.'em; '
  730. )
  731. );
  732. $form->applyFilter('extra_'.$field_details['field_variable'], 'stripslashes');
  733. $form->applyFilter('extra_'.$field_details['field_variable'], 'trim');
  734. if ($field_details['field_visible'] == 0) {
  735. $form->freeze('extra_'.$field_details['field_variable']);
  736. }
  737. break;
  738. }
  739. }
  740. }
  741. $return = array();
  742. $return['jquery_ready_content'] = $jquery_ready_content;
  743. return $return;
  744. }
  745. function setupBreadcrumb(&$breadcrumb, $action)
  746. {
  747. if ($action == 'add') {
  748. $breadcrumb[] = array('url' => $this->pageUrl, 'name' => $this->pageName);
  749. $breadcrumb[] = array('url' => '#', 'name' => get_lang('Add'));
  750. } elseif ($action == 'edit') {
  751. $breadcrumb[] = array('url' => $this->pageUrl, 'name' => $this->pageName);
  752. $breadcrumb[] = array('url' => '#', 'name' => get_lang('Edit'));
  753. } else {
  754. $breadcrumb[] = array('url' => '#', 'name' => $this->pageName);
  755. }
  756. }
  757. /**
  758. * Displays the title + grid
  759. */
  760. public function display()
  761. {
  762. // action links
  763. echo '<div class="actions">';
  764. echo '<a href="../admin/index.php">'.Display::return_icon(
  765. 'back.png',
  766. get_lang('BackTo').' '.get_lang('PlatformAdmin'),
  767. '',
  768. ICON_SIZE_MEDIUM
  769. ).'</a>';
  770. echo '<a href="'.api_get_self().'?action=add&type='.$this->type.'">'.Display::return_icon(
  771. 'add_user_fields.png',
  772. get_lang('Add'),
  773. '',
  774. ICON_SIZE_MEDIUM
  775. ).'</a>';
  776. echo '</div>';
  777. echo Display::grid_html($this->type.'_fields');
  778. }
  779. public function getJqgridColumnNames()
  780. {
  781. return array(
  782. get_lang('Name'),
  783. get_lang('FieldLabel'),
  784. get_lang('Type'),
  785. get_lang('FieldChangeability'),
  786. get_lang('Visibility'),
  787. get_lang('Filter'),
  788. get_lang('FieldOrder'),
  789. get_lang('Actions')
  790. );
  791. }
  792. public function getJqgridColumnModel()
  793. {
  794. return array(
  795. array('name' => 'field_display_text', 'index' => 'field_display_text', 'width' => '180', 'align' => 'left'),
  796. array(
  797. 'name' => 'field_variable',
  798. 'index' => 'field_variable',
  799. 'width' => '',
  800. 'align' => 'left',
  801. 'sortable' => 'true'
  802. ),
  803. array(
  804. 'name' => 'field_type',
  805. 'index' => 'field_type',
  806. 'width' => '',
  807. 'align' => 'left',
  808. 'sortable' => 'true'
  809. ),
  810. array(
  811. 'name' => 'field_changeable',
  812. 'index' => 'field_changeable',
  813. 'width' => '50',
  814. 'align' => 'left',
  815. 'sortable' => 'true'
  816. ),
  817. array(
  818. 'name' => 'field_visible',
  819. 'index' => 'field_visible',
  820. 'width' => '40',
  821. 'align' => 'left',
  822. 'sortable' => 'true'
  823. ),
  824. array(
  825. 'name' => 'field_filter',
  826. 'index' => 'field_filter',
  827. 'width' => '30',
  828. 'align' => 'left',
  829. 'sortable' => 'true'
  830. ),
  831. array(
  832. 'name' => 'field_order',
  833. 'index' => 'field_order',
  834. 'width' => '40',
  835. 'align' => 'left',
  836. 'sortable' => 'true'
  837. ),
  838. array(
  839. 'name' => 'actions',
  840. 'index' => 'actions',
  841. 'width' => '100',
  842. 'align' => 'left',
  843. 'formatter' => 'action_formatter',
  844. 'sortable' => 'false'
  845. )
  846. );
  847. }
  848. public function return_form($url, $action)
  849. {
  850. $form = new FormValidator($this->type.'_field', 'post', $url);
  851. $form->addElement('hidden', 'type', $this->type);
  852. $id = isset($_GET['id']) ? intval($_GET['id']) : null;
  853. $form->addElement('hidden', 'id', $id);
  854. // Setting the form elements
  855. $header = get_lang('Add');
  856. $defaults = array();
  857. if ($action == 'edit') {
  858. $header = get_lang('Modify');
  859. // Setting the defaults
  860. $defaults = $this->get($id);
  861. }
  862. $form->addElement('header', $header);
  863. $form->addElement('text', 'field_display_text', get_lang('Name'), array('class' => 'span5'));
  864. // Field type
  865. $types = self::get_field_types();
  866. $form->addElement(
  867. 'select',
  868. 'field_type',
  869. get_lang('FieldType'),
  870. $types,
  871. array('id' => 'field_type', 'class' => 'chzn-select', 'data-placeholder' => get_lang('Select'))
  872. );
  873. $form->addElement('label', get_lang('Example'), '<div id="example">-</div>');
  874. //$form->addElement('advanced_settings','<a class="btn btn-show" id="advanced_parameters" href="javascript://">'.get_lang('AdvancedParameters').'</a>');
  875. //$form->addElement('html','<div id="options" style="display:none">');
  876. $form->addElement('text', 'field_variable', get_lang('FieldLabel'), array('class' => 'span5'));
  877. $form->addElement(
  878. 'text',
  879. 'field_options',
  880. get_lang('FieldPossibleValues'),
  881. array('id' => 'field_options', 'class' => 'span6')
  882. );
  883. if ($action == 'edit') {
  884. if (in_array(
  885. $defaults['field_type'],
  886. array(ExtraField::FIELD_TYPE_SELECT, ExtraField::FIELD_TYPE_DOUBLE_SELECT)
  887. )
  888. ) {
  889. $url = Display::url(
  890. get_lang('EditExtraFieldOptions'),
  891. 'extra_field_options.php?type='.$this->type.'&field_id='.$id
  892. );
  893. $form->addElement('label', null, $url);
  894. $form->freeze('field_options');
  895. }
  896. }
  897. $form->addElement(
  898. 'text',
  899. 'field_default_value',
  900. get_lang('FieldDefaultValue'),
  901. array('id' => 'field_default_value', 'class' => 'span5')
  902. );
  903. $group = array();
  904. $group[] = $form->createElement('radio', 'field_visible', null, get_lang('Yes'), 1);
  905. $group[] = $form->createElement('radio', 'field_visible', null, get_lang('No'), 0);
  906. $form->addGroup($group, '', get_lang('Visible'), '', false);
  907. $group = array();
  908. $group[] = $form->createElement('radio', 'field_changeable', null, get_lang('Yes'), 1);
  909. $group[] = $form->createElement('radio', 'field_changeable', null, get_lang('No'), 0);
  910. $form->addGroup($group, '', get_lang('FieldChangeability'), '', false);
  911. $group = array();
  912. $group[] = $form->createElement('radio', 'field_filter', null, get_lang('Yes'), 1);
  913. $group[] = $form->createElement('radio', 'field_filter', null, get_lang('No'), 0);
  914. $form->addGroup($group, '', get_lang('FieldFilter'), '', false);
  915. $form->addElement('text', 'field_order', get_lang('FieldOrder'), array('class' => 'span1'));
  916. if ($action == 'edit') {
  917. $option = new ExtraFieldOption($this->type);
  918. if ($defaults['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) {
  919. $form->freeze('field_options');
  920. }
  921. $defaults['field_options'] = $option->get_field_options_by_field_to_string($id);
  922. $form->addElement('button', 'submit', get_lang('Modify'), 'class="save"');
  923. } else {
  924. $defaults['field_visible'] = 0;
  925. $defaults['field_changeable'] = 0;
  926. $defaults['field_filter'] = 0;
  927. $form->addElement('button', 'submit', get_lang('Add'), 'class="save"');
  928. }
  929. /*if (!empty($defaults['created_at'])) {
  930. $defaults['created_at'] = api_convert_and_format_date($defaults['created_at']);
  931. }
  932. if (!empty($defaults['updated_at'])) {
  933. $defaults['updated_at'] = api_convert_and_format_date($defaults['updated_at']);
  934. }*/
  935. $form->setDefaults($defaults);
  936. // Setting the rules
  937. $form->addRule('field_display_text', get_lang('ThisFieldIsRequired'), 'required');
  938. //$form->addRule('field_variable', get_lang('ThisFieldIsRequired'), 'required');
  939. $form->addRule('field_type', get_lang('ThisFieldIsRequired'), 'required');
  940. return $form;
  941. }
  942. public function getJqgridActionLinks($token)
  943. {
  944. //With this function we can add actions to the jgrid (edit, delete, etc)
  945. return 'function action_formatter(cellvalue, options, rowObject) {
  946. return \'<a href="?action=edit&type='.$this->type.'&id=\'+options.rowId+\'">'.Display::return_icon(
  947. 'edit.png',
  948. get_lang('Edit'),
  949. '',
  950. ICON_SIZE_SMALL
  951. ).'</a>'.
  952. '&nbsp;<a onclick="javascript:if(!confirm('."\'".addslashes(
  953. api_htmlentities(get_lang("ConfirmYourChoice"), ENT_QUOTES)
  954. )."\'".')) return false;" href="?sec_token='.$token.'&type='.$this->type.'&action=delete&id=\'+options.rowId+\'">'.Display::return_icon(
  955. 'delete.png',
  956. get_lang('Delete'),
  957. '',
  958. ICON_SIZE_SMALL
  959. ).'</a>'.
  960. '\';
  961. }';
  962. }
  963. public function getRules(&$columns, &$column_model)
  964. {
  965. $fields = $this->get_all(
  966. array('field_visible = ? AND field_filter = ?' => array(1, 1)),
  967. 'option_display_text'
  968. );
  969. $extraFieldOption = new ExtraFieldOption($this->type);
  970. $rules = array();
  971. if (!empty($fields)) {
  972. foreach ($fields as $field) {
  973. $search_options = array();
  974. $type = 'text';
  975. if (in_array($field['field_type'], array(self::FIELD_TYPE_SELECT, self::FIELD_TYPE_DOUBLE_SELECT))) {
  976. $type = 'select';
  977. $search_options['sopt'] = array('eq', 'ne'); //equal not equal
  978. } else {
  979. $search_options['sopt'] = array('cn', 'nc'); //contains not contains
  980. }
  981. $search_options['searchhidden'] = 'true';
  982. $search_options['defaultValue'] = isset($search_options['field_default_value']) ? $search_options['field_default_value'] : null;
  983. if ($field['field_type'] == self::FIELD_TYPE_DOUBLE_SELECT) {
  984. //Add 2 selects
  985. $options = $extraFieldOption->get_field_options_by_field($field['id']);
  986. $options = self::extra_field_double_select_convert_array_to_ordered_array($options);
  987. $first_options = array();
  988. if (!empty($options)) {
  989. foreach ($options as $option) {
  990. foreach ($option as $sub_option) {
  991. if ($sub_option['option_value'] == 0) {
  992. $first_options[] = $sub_option['field_id'].'#'.$sub_option['id'].':'.$sub_option['option_display_text'];
  993. } else {
  994. }
  995. }
  996. }
  997. }
  998. $search_options['value'] = implode(';', $first_options);
  999. $search_options['dataInit'] = 'fill_second_select';
  1000. //First
  1001. $column_model[] = array(
  1002. 'name' => 'extra_'.$field['field_variable'],
  1003. 'index' => 'extra_'.$field['field_variable'],
  1004. 'width' => '100',
  1005. 'hidden' => 'true',
  1006. 'search' => 'true',
  1007. 'stype' => 'select',
  1008. 'searchoptions' => $search_options
  1009. );
  1010. $columns[] = $field['field_display_text'].' (1)';
  1011. $rules[] = array('field' => 'extra_'.$field['field_variable'], 'op' => 'cn');
  1012. //Second
  1013. $search_options['value'] = $field['id'].':';
  1014. $search_options['dataInit'] = 'register_second_select';
  1015. $column_model[] = array(
  1016. 'name' => 'extra_'.$field['field_variable'].'_second',
  1017. 'index' => 'extra_'.$field['field_variable'].'_second',
  1018. 'width' => '100',
  1019. 'hidden' => 'true',
  1020. 'search' => 'true',
  1021. 'stype' => 'select',
  1022. 'searchoptions' => $search_options
  1023. );
  1024. $columns[] = $field['field_display_text'].' (2)';
  1025. $rules[] = array('field' => 'extra_'.$field['field_variable'].'_second', 'op' => 'cn');
  1026. continue;
  1027. } else {
  1028. $search_options['value'] = $extraFieldOption->get_field_options_to_string(
  1029. $field['id'],
  1030. false,
  1031. 'option_display_text'
  1032. );
  1033. }
  1034. $column_model[] = array(
  1035. 'name' => 'extra_'.$field['field_variable'],
  1036. 'index' => 'extra_'.$field['field_variable'],
  1037. 'width' => '100',
  1038. 'hidden' => 'true',
  1039. 'search' => 'true',
  1040. 'stype' => $type,
  1041. 'searchoptions' => $search_options
  1042. );
  1043. $columns[] = $field['field_display_text'];
  1044. $rules[] = array('field' => 'extra_'.$field['field_variable'], 'op' => 'cn');
  1045. }
  1046. }
  1047. return $rules;
  1048. }
  1049. /**
  1050. * @param array $options
  1051. * @return array
  1052. */
  1053. function parseConditions($options)
  1054. {
  1055. $inject_extra_fields = null;
  1056. $extraFieldOption = new ExtraFieldOption($this->type);
  1057. $double_fields = array();
  1058. if (isset($options['extra'])) {
  1059. $extra_fields = $options['extra'];
  1060. if (!empty($extra_fields)) {
  1061. $counter = 1;
  1062. foreach ($extra_fields as &$extra) {
  1063. $extra_field_obj = new ExtraField($this->type);
  1064. $extra_field_info = $extra_field_obj->get($extra['id']);
  1065. $extra['extra_field_info'] = $extra_field_info;
  1066. if (in_array(
  1067. $extra_field_info['field_type'],
  1068. array(
  1069. ExtraField::FIELD_TYPE_SELECT,
  1070. ExtraField::FIELD_TYPE_SELECT,
  1071. ExtraField::FIELD_TYPE_DOUBLE_SELECT
  1072. )
  1073. )
  1074. ) {
  1075. $inject_extra_fields .= " fvo$counter.option_display_text as {$extra['field']}, ";
  1076. } else {
  1077. $inject_extra_fields .= " fv$counter.field_value as {$extra['field']}, ";
  1078. }
  1079. if (isset($extra_fields_info[$extra['id']])) {
  1080. $info = $extra_fields_info[$extra['id']];
  1081. } else {
  1082. $info = $this->get($extra['id']);
  1083. $extra_fields_info[$extra['id']] = $info;
  1084. }
  1085. if ($info['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) {
  1086. $double_fields[$info['id']] = $info;
  1087. }
  1088. $counter++;
  1089. }
  1090. }
  1091. }
  1092. $options_by_double = array();
  1093. foreach ($double_fields as $double) {
  1094. $my_options = $extraFieldOption->get_field_options_by_field(
  1095. $double['id'],
  1096. true
  1097. );
  1098. $options_by_double['extra_'.$double['field_variable']] = $my_options;
  1099. }
  1100. $field_value_to_join = array();
  1101. //filter can be all/any = and/or
  1102. $inject_joins = null;
  1103. $inject_where = null;
  1104. $where = null;
  1105. if (!empty($options['where'])) {
  1106. if (!empty($options['extra'])) {
  1107. //Removing double 1=1
  1108. $options['where'] = str_replace(' 1 = 1 AND', '', $options['where']);
  1109. //Always OR
  1110. $counter = 1;
  1111. foreach ($extra_fields as $extra_info) {
  1112. $extra_field_info = $extra_info['extra_field_info'];
  1113. $inject_joins .= " INNER JOIN $this->table_field_values fv$counter ON (s.".$this->primaryKey." = fv$counter.".$this->handler_id.") ";
  1114. //Add options
  1115. if (in_array(
  1116. $extra_field_info['field_type'],
  1117. array(
  1118. ExtraField::FIELD_TYPE_SELECT,
  1119. ExtraField::FIELD_TYPE_SELECT,
  1120. ExtraField::FIELD_TYPE_DOUBLE_SELECT
  1121. )
  1122. )
  1123. ) {
  1124. $options['where'] = str_replace(
  1125. $extra_info['field'],
  1126. 'fv'.$counter.'.field_id = '.$extra_info['id'].' AND fvo'.$counter.'.option_value',
  1127. $options['where']
  1128. );
  1129. $inject_joins .= " INNER JOIN $this->table_field_options fvo$counter ".
  1130. " ON (fv$counter.field_id = fvo$counter.field_id AND fv$counter.field_value = fvo$counter.option_value) ";
  1131. } else {
  1132. //text, textarea, etc
  1133. $options['where'] = str_replace(
  1134. $extra_info['field'],
  1135. 'fv'.$counter.'.field_id = '.$extra_info['id'].' AND fv'.$counter.'.field_value',
  1136. $options['where']
  1137. );
  1138. }
  1139. $field_value_to_join[] = " fv$counter.$this->handler_id ";
  1140. $counter++;
  1141. }
  1142. if (!empty($field_value_to_join)) {
  1143. //$inject_where .= " AND s.id = ".implode(' = ', $field_value_to_join);
  1144. }
  1145. }
  1146. $where .= ' AND '.$options['where'];
  1147. }
  1148. $order = null;
  1149. if (!empty($options['order'])) {
  1150. $order = " ORDER BY ".$options['order'];
  1151. }
  1152. $limit = null;
  1153. if (!empty($options['limit'])) {
  1154. $limit = " LIMIT ".$options['limit'];
  1155. }
  1156. return array(
  1157. 'order' => $order,
  1158. 'limit' => $limit,
  1159. 'where' => $where,
  1160. 'inject_where' => $inject_where,
  1161. 'inject_joins' => $inject_joins,
  1162. 'field_value_to_join' => $field_value_to_join,
  1163. 'inject_extra_fields' => $inject_extra_fields,
  1164. );
  1165. }
  1166. public function getExtraFieldRules($filters)
  1167. {
  1168. $extra_fields = array();
  1169. // Getting double select if exists
  1170. $double_select = array();
  1171. foreach ($filters->rules as $rule) {
  1172. if (strpos($rule->field, '_second') === false) {
  1173. } else {
  1174. $my_field = str_replace('_second', '', $rule->field);
  1175. $double_select[$my_field] = $rule->data;
  1176. }
  1177. }
  1178. $condition_array = array();
  1179. foreach ($filters->rules as $rule) {
  1180. if (strpos($rule->field, 'extra_') === false) {
  1181. //normal fields
  1182. $field = $rule->field;
  1183. if (!empty($rule->data)) {
  1184. $condition_array[] = get_where_clause($field, $rule->op, $rule->data);
  1185. }
  1186. } else {
  1187. //Extra fields
  1188. //normal
  1189. if (strpos($rule->field, '_second') === false) {
  1190. //No _second
  1191. $original_field = str_replace('extra_', '', $rule->field);
  1192. $field_option = $this->get_handler_field_info_by_field_variable($original_field);
  1193. if ($field_option['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) {
  1194. if (isset($double_select[$rule->field])) {
  1195. $data = explode('#', $rule->data);
  1196. $rule->data = $data[1].'::'.$double_select[$rule->field];
  1197. } else {
  1198. // only was sent 1 select
  1199. $data = explode('#', $rule->data);
  1200. $rule->data = $data[1];
  1201. }
  1202. if (!empty($rule->data)) {
  1203. $condition_array[] = ' ('.get_where_clause($rule->field, $rule->op, $rule->data).') ';
  1204. $extra_fields[] = array('field' => $rule->field, 'id' => $field_option['id']);
  1205. }
  1206. } else {
  1207. if (!empty($rule->data)) {
  1208. $condition_array[] = ' ('.get_where_clause($rule->field, $rule->op, $rule->data).') ';
  1209. $extra_fields[] = array('field' => $rule->field, 'id' => $field_option['id']);
  1210. }
  1211. }
  1212. } else {
  1213. $my_field = str_replace('_second', '', $rule->field);
  1214. $original_field = str_replace('extra_', '', $my_field);
  1215. $field_option = $this->get_handler_field_info_by_field_variable($original_field);
  1216. $extra_fields[] = array('field' => $rule->field, 'id' => $field_option['id']);
  1217. }
  1218. }
  1219. }
  1220. return array(
  1221. 'extra_fields' => $extra_fields,
  1222. 'condition_array' => $condition_array
  1223. );
  1224. }
  1225. }