extra_field.lib.php 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  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 ExtraFieldOption($this->type);
  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. public 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. $options[''] = get_lang('SelectAnOption');
  491. if (!empty($field_details['options'])) {
  492. foreach ($field_details['options'] as $option_details) {
  493. if ($get_lang_variables) {
  494. $options[$option_details['option_value']] = get_lang(
  495. $option_details['option_display_text']
  496. );
  497. } else {
  498. $options[$option_details['option_value']] = $option_details['option_display_text'];
  499. }
  500. }
  501. }
  502. if ($get_lang_variables) {
  503. $field_details['field_display_text'] = get_lang($field_details['field_display_text']);
  504. }
  505. //chzn-select doesn't work for sessions??
  506. $form->addElement(
  507. 'select',
  508. 'extra_'.$field_details['field_variable'],
  509. $field_details['field_display_text'],
  510. $options,
  511. array('id' => 'extra_'.$field_details['field_variable'])
  512. );
  513. if (!$admin_permissions) {
  514. if ($field_details['field_visible'] == 0) {
  515. $form->freeze('extra_'.$field_details['field_variable']);
  516. }
  517. }
  518. break;
  519. case ExtraField::FIELD_TYPE_SELECT_MULTIPLE:
  520. $options = array();
  521. foreach ($field_details['options'] as $option_id => $option_details) {
  522. $options[$option_details['option_value']] = $option_details['option_display_text'];
  523. }
  524. $form->addElement(
  525. 'select',
  526. 'extra_'.$field_details['field_variable'],
  527. $field_details['field_display_text'],
  528. $options,
  529. array('multiple' => 'multiple')
  530. );
  531. if (!$admin_permissions) {
  532. if ($field_details['field_visible'] == 0) {
  533. $form->freeze('extra_'.$field_details['field_variable']);
  534. }
  535. }
  536. break;
  537. case ExtraField::FIELD_TYPE_DATE:
  538. $form->addElement(
  539. 'datepickerdate',
  540. 'extra_'.$field_details['field_variable'],
  541. $field_details['field_display_text'],
  542. array('form_name' => $form_name)
  543. );
  544. $form->_elements[$form->_elementIndex['extra_'.$field_details['field_variable']]]->setLocalOption(
  545. 'minYear',
  546. 1900
  547. );
  548. $defaults['extra_'.$field_details['field_variable']] = date('Y-m-d 12:00:00');
  549. if (!isset($form->_defaultValues['extra_'.$field_details['field_variable']])) {
  550. $form->setDefaults($defaults);
  551. }
  552. if (!$admin_permissions) {
  553. if ($field_details['field_visible'] == 0) {
  554. $form->freeze('extra_'.$field_details['field_variable']);
  555. }
  556. }
  557. $form->applyFilter('theme', 'trim');
  558. break;
  559. case ExtraField::FIELD_TYPE_DATETIME:
  560. $form->addElement(
  561. 'datepicker',
  562. 'extra_'.$field_details['field_variable'],
  563. $field_details['field_display_text'],
  564. array('form_name' => $form_name)
  565. );
  566. $form->_elements[$form->_elementIndex['extra_'.$field_details['field_variable']]]->setLocalOption(
  567. 'minYear',
  568. 1900
  569. );
  570. $defaults['extra_'.$field_details['field_variable']] = date('Y-m-d 12:00:00');
  571. if (!isset($form->_defaultValues['extra_'.$field_details['field_variable']])) {
  572. $form->setDefaults($defaults);
  573. }
  574. if (!$admin_permissions) {
  575. if ($field_details['field_visible'] == 0) {
  576. $form->freeze('extra_'.$field_details['field_variable']);
  577. }
  578. }
  579. $form->applyFilter('theme', 'trim');
  580. break;
  581. case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
  582. $first_select_id = 'first_extra_'.$field_details['field_variable'];
  583. $url = api_get_path(WEB_AJAX_PATH).'extra_field.ajax.php?1=1';
  584. $jquery_ready_content .= '
  585. $("#'.$first_select_id.'").on("change", function() {
  586. var id = $(this).val();
  587. if (id) {
  588. $.ajax({
  589. url: "'.$url.'&a=get_second_select_options",
  590. dataType: "json",
  591. data: "type='.$type.'&field_id='.$field_details['id'].'&option_value_id="+id,
  592. success: function(data) {
  593. $("#second_extra_'.$field_details['field_variable'].'").empty();
  594. $.each(data, function(index, value) {
  595. $("#second_extra_'.$field_details['field_variable'].'").append($("<option/>", {
  596. value: index,
  597. text: value
  598. }));
  599. });
  600. },
  601. });
  602. } else {
  603. $("#second_extra_'.$field_details['field_variable'].'").empty();
  604. }
  605. });';
  606. $first_id = null;
  607. $second_id = null;
  608. if (!empty($extra_data)) {
  609. $first_id = $extra_data['extra_'.$field_details['field_variable']]['extra_'.$field_details['field_variable']];
  610. $second_id = $extra_data['extra_'.$field_details['field_variable']]['extra_'.$field_details['field_variable'].'_second'];
  611. }
  612. $options = ExtraField::extra_field_double_select_convert_array_to_ordered_array(
  613. $field_details['options']
  614. );
  615. $values = array('' => get_lang('Select'));
  616. $second_values = array();
  617. if (!empty($options)) {
  618. foreach ($options as $option) {
  619. foreach ($option as $sub_option) {
  620. if ($sub_option['option_value'] == '0') {
  621. $values[$sub_option['id']] = $sub_option['option_display_text'];
  622. } else {
  623. if ($first_id === $sub_option['option_value']) {
  624. $second_values[$sub_option['id']] = $sub_option['option_display_text'];
  625. }
  626. }
  627. }
  628. }
  629. }
  630. $group = array();
  631. $group[] = $form->createElement(
  632. 'select',
  633. 'extra_'.$field_details['field_variable'],
  634. null,
  635. $values,
  636. array('id' => $first_select_id)
  637. );
  638. $group[] = $form->createElement(
  639. 'select',
  640. 'extra_'.$field_details['field_variable'].'_second',
  641. null,
  642. $second_values,
  643. array('id' => 'second_extra_'.$field_details['field_variable'])
  644. );
  645. $form->addGroup(
  646. $group,
  647. 'extra_'.$field_details['field_variable'],
  648. $field_details['field_display_text'],
  649. '&nbsp;'
  650. );
  651. if (!$admin_permissions) {
  652. if ($field_details['field_visible'] == 0) {
  653. $form->freeze('extra_'.$field_details['field_variable']);
  654. }
  655. }
  656. break;
  657. case ExtraField::FIELD_TYPE_DIVIDER:
  658. $form->addElement(
  659. 'static',
  660. $field_details['field_variable'],
  661. '<br /><strong>'.$field_details['field_display_text'].'</strong>'
  662. );
  663. break;
  664. case ExtraField::FIELD_TYPE_TAG:
  665. //the magic should be here
  666. $user_tags = UserManager::get_user_tags($user_id, $field_details['id']);
  667. $tag_list = '';
  668. if (is_array($user_tags) && count($user_tags) > 0) {
  669. foreach ($user_tags as $tag) {
  670. $tag_list .= '<option value="'.$tag['tag'].'" class="selected">'.$tag['tag'].'</option>';
  671. }
  672. }
  673. $multi_select = '<select id="extra_'.$field_details['field_variable'].'" name="extra_'.$field_details['field_variable'].'">
  674. '.$tag_list.'
  675. </select>';
  676. $form->addElement('label', $field_details['field_display_text'], $multi_select);
  677. $url = api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php';
  678. $complete_text = get_lang('StartToType');
  679. //if cache is set to true the jquery will be called 1 time
  680. $field_variable = $field_details['field_variable'];
  681. $field_id = $field_details['id'];
  682. $jquery_ready_content .= <<<EOF
  683. $("#extra_$field_variable").fcbkcomplete({
  684. json_url: "$url?a=search_tags&field_id=$field_id",
  685. cache: false,
  686. filter_case: true,
  687. filter_hide: true,
  688. complete_text:"$complete_text",
  689. firstselected: false,
  690. filter_selected: true,
  691. newel: true
  692. });
  693. EOF;
  694. break;
  695. case ExtraField::FIELD_TYPE_TIMEZONE:
  696. $form->addElement(
  697. 'select',
  698. 'extra_'.$field_details['field_variable'],
  699. $field_details['field_display_text'],
  700. api_get_timezones(),
  701. ''
  702. );
  703. if ($field_details['field_visible'] == 0) {
  704. $form->freeze(
  705. 'extra_'.$field_details['field_variable']
  706. );
  707. }
  708. break;
  709. case ExtraField::FIELD_TYPE_SOCIAL_PROFILE:
  710. // get the social network's favicon
  711. $icon_path = UserManager::get_favicon_from_url(
  712. $extra_data['extra_'.$field_details['field_variable']],
  713. $field_details['field_default_value']
  714. );
  715. // special hack for hi5
  716. $leftpad = '1.7';
  717. $top = '0.4';
  718. $domain = parse_url($icon_path, PHP_URL_HOST);
  719. if ($domain == 'www.hi5.com' or $domain == 'hi5.com') {
  720. $leftpad = '3';
  721. $top = '0';
  722. }
  723. // print the input field
  724. $form->addElement(
  725. 'text',
  726. 'extra_'.$field_details['field_variable'],
  727. $field_details['field_display_text'],
  728. array(
  729. 'size' => 60,
  730. 'style' => 'background-image: url(\''.$icon_path.'\'); background-repeat: no-repeat; background-position: 0.4em '.$top.'em; padding-left: '.$leftpad.'em; '
  731. )
  732. );
  733. $form->applyFilter('extra_'.$field_details['field_variable'], 'stripslashes');
  734. $form->applyFilter('extra_'.$field_details['field_variable'], 'trim');
  735. if ($field_details['field_visible'] == 0) {
  736. $form->freeze('extra_'.$field_details['field_variable']);
  737. }
  738. break;
  739. }
  740. }
  741. }
  742. $return = array();
  743. $return['jquery_ready_content'] = $jquery_ready_content;
  744. return $return;
  745. }
  746. function setupBreadcrumb(&$breadcrumb, $action)
  747. {
  748. if ($action == 'add') {
  749. $breadcrumb[] = array('url' => $this->pageUrl, 'name' => $this->pageName);
  750. $breadcrumb[] = array('url' => '#', 'name' => get_lang('Add'));
  751. } elseif ($action == 'edit') {
  752. $breadcrumb[] = array('url' => $this->pageUrl, 'name' => $this->pageName);
  753. $breadcrumb[] = array('url' => '#', 'name' => get_lang('Edit'));
  754. } else {
  755. $breadcrumb[] = array('url' => '#', 'name' => $this->pageName);
  756. }
  757. }
  758. /**
  759. * Displays the title + grid
  760. */
  761. public function display()
  762. {
  763. // action links
  764. echo '<div class="actions">';
  765. echo '<a href="../admin/index.php">'.Display::return_icon(
  766. 'back.png',
  767. get_lang('BackTo').' '.get_lang('PlatformAdmin'),
  768. '',
  769. ICON_SIZE_MEDIUM
  770. ).'</a>';
  771. echo '<a href="'.api_get_self().'?action=add&type='.$this->type.'">'.Display::return_icon(
  772. 'add_user_fields.png',
  773. get_lang('Add'),
  774. '',
  775. ICON_SIZE_MEDIUM
  776. ).'</a>';
  777. echo '</div>';
  778. echo Display::grid_html($this->type.'_fields');
  779. }
  780. public function getJqgridColumnNames()
  781. {
  782. return array(
  783. get_lang('Name'),
  784. get_lang('FieldLabel'),
  785. get_lang('Type'),
  786. get_lang('FieldChangeability'),
  787. get_lang('Visibility'),
  788. get_lang('Filter'),
  789. get_lang('FieldOrder'),
  790. get_lang('Actions')
  791. );
  792. }
  793. public function getJqgridColumnModel()
  794. {
  795. return array(
  796. array('name' => 'field_display_text', 'index' => 'field_display_text', 'width' => '180', 'align' => 'left'),
  797. array(
  798. 'name' => 'field_variable',
  799. 'index' => 'field_variable',
  800. 'width' => '',
  801. 'align' => 'left',
  802. 'sortable' => 'true'
  803. ),
  804. array(
  805. 'name' => 'field_type',
  806. 'index' => 'field_type',
  807. 'width' => '',
  808. 'align' => 'left',
  809. 'sortable' => 'true'
  810. ),
  811. array(
  812. 'name' => 'field_changeable',
  813. 'index' => 'field_changeable',
  814. 'width' => '50',
  815. 'align' => 'left',
  816. 'sortable' => 'true'
  817. ),
  818. array(
  819. 'name' => 'field_visible',
  820. 'index' => 'field_visible',
  821. 'width' => '40',
  822. 'align' => 'left',
  823. 'sortable' => 'true'
  824. ),
  825. array(
  826. 'name' => 'field_filter',
  827. 'index' => 'field_filter',
  828. 'width' => '30',
  829. 'align' => 'left',
  830. 'sortable' => 'true'
  831. ),
  832. array(
  833. 'name' => 'field_order',
  834. 'index' => 'field_order',
  835. 'width' => '40',
  836. 'align' => 'left',
  837. 'sortable' => 'true'
  838. ),
  839. array(
  840. 'name' => 'actions',
  841. 'index' => 'actions',
  842. 'width' => '100',
  843. 'align' => 'left',
  844. 'formatter' => 'action_formatter',
  845. 'sortable' => 'false'
  846. )
  847. );
  848. }
  849. public function return_form($url, $action)
  850. {
  851. $form = new FormValidator($this->type.'_field', 'post', $url);
  852. $form->addElement('hidden', 'type', $this->type);
  853. $id = isset($_GET['id']) ? intval($_GET['id']) : null;
  854. $form->addElement('hidden', 'id', $id);
  855. // Setting the form elements
  856. $header = get_lang('Add');
  857. $defaults = array();
  858. if ($action == 'edit') {
  859. $header = get_lang('Modify');
  860. // Setting the defaults
  861. $defaults = $this->get($id);
  862. }
  863. $form->addElement('header', $header);
  864. $form->addElement('text', 'field_display_text', get_lang('Name'), array('class' => 'span5'));
  865. // Field type
  866. $types = self::get_field_types();
  867. $form->addElement(
  868. 'select',
  869. 'field_type',
  870. get_lang('FieldType'),
  871. $types,
  872. array('id' => 'field_type', 'class' => 'chzn-select', 'data-placeholder' => get_lang('Select'))
  873. );
  874. $form->addElement('label', get_lang('Example'), '<div id="example">-</div>');
  875. //$form->addElement('advanced_settings','<a class="btn btn-show" id="advanced_parameters" href="javascript://">'.get_lang('AdvancedParameters').'</a>');
  876. //$form->addElement('html','<div id="options" style="display:none">');
  877. $form->addElement('text', 'field_variable', get_lang('FieldLabel'), array('class' => 'span5'));
  878. $form->addElement(
  879. 'text',
  880. 'field_options',
  881. get_lang('FieldPossibleValues'),
  882. array('id' => 'field_options', 'class' => 'span6')
  883. );
  884. if ($action == 'edit') {
  885. if (in_array(
  886. $defaults['field_type'],
  887. array(ExtraField::FIELD_TYPE_SELECT, ExtraField::FIELD_TYPE_DOUBLE_SELECT)
  888. )
  889. ) {
  890. $url = Display::url(
  891. get_lang('EditExtraFieldOptions'),
  892. 'extra_field_options.php?type='.$this->type.'&field_id='.$id
  893. );
  894. $form->addElement('label', null, $url);
  895. $form->freeze('field_options');
  896. }
  897. }
  898. $form->addElement(
  899. 'text',
  900. 'field_default_value',
  901. get_lang('FieldDefaultValue'),
  902. array('id' => 'field_default_value', 'class' => 'span5')
  903. );
  904. $group = array();
  905. $group[] = $form->createElement('radio', 'field_visible', null, get_lang('Yes'), 1);
  906. $group[] = $form->createElement('radio', 'field_visible', null, get_lang('No'), 0);
  907. $form->addGroup($group, '', get_lang('Visible'), '', false);
  908. $group = array();
  909. $group[] = $form->createElement('radio', 'field_changeable', null, get_lang('Yes'), 1);
  910. $group[] = $form->createElement('radio', 'field_changeable', null, get_lang('No'), 0);
  911. $form->addGroup($group, '', get_lang('FieldChangeability'), '', false);
  912. $group = array();
  913. $group[] = $form->createElement('radio', 'field_filter', null, get_lang('Yes'), 1);
  914. $group[] = $form->createElement('radio', 'field_filter', null, get_lang('No'), 0);
  915. $form->addGroup($group, '', get_lang('FieldFilter'), '', false);
  916. $form->addElement('text', 'field_order', get_lang('FieldOrder'), array('class' => 'span1'));
  917. if ($action == 'edit') {
  918. $option = new ExtraFieldOption($this->type);
  919. if ($defaults['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) {
  920. $form->freeze('field_options');
  921. }
  922. $defaults['field_options'] = $option->get_field_options_by_field_to_string($id);
  923. $form->addElement('button', 'submit', get_lang('Modify'), 'class="save"');
  924. } else {
  925. $defaults['field_visible'] = 0;
  926. $defaults['field_changeable'] = 0;
  927. $defaults['field_filter'] = 0;
  928. $form->addElement('button', 'submit', get_lang('Add'), 'class="save"');
  929. }
  930. /*if (!empty($defaults['created_at'])) {
  931. $defaults['created_at'] = api_convert_and_format_date($defaults['created_at']);
  932. }
  933. if (!empty($defaults['updated_at'])) {
  934. $defaults['updated_at'] = api_convert_and_format_date($defaults['updated_at']);
  935. }*/
  936. $form->setDefaults($defaults);
  937. // Setting the rules
  938. $form->addRule('field_display_text', get_lang('ThisFieldIsRequired'), 'required');
  939. //$form->addRule('field_variable', get_lang('ThisFieldIsRequired'), 'required');
  940. $form->addRule('field_type', get_lang('ThisFieldIsRequired'), 'required');
  941. return $form;
  942. }
  943. public function getJqgridActionLinks($token)
  944. {
  945. //With this function we can add actions to the jgrid (edit, delete, etc)
  946. return 'function action_formatter(cellvalue, options, rowObject) {
  947. return \'<a href="?action=edit&type='.$this->type.'&id=\'+options.rowId+\'">'.Display::return_icon(
  948. 'edit.png',
  949. get_lang('Edit'),
  950. '',
  951. ICON_SIZE_SMALL
  952. ).'</a>'.
  953. '&nbsp;<a onclick="javascript:if(!confirm('."\'".addslashes(
  954. api_htmlentities(get_lang("ConfirmYourChoice"), ENT_QUOTES)
  955. )."\'".')) return false;" href="?sec_token='.$token.'&type='.$this->type.'&action=delete&id=\'+options.rowId+\'">'.Display::return_icon(
  956. 'delete.png',
  957. get_lang('Delete'),
  958. '',
  959. ICON_SIZE_SMALL
  960. ).'</a>'.
  961. '\';
  962. }';
  963. }
  964. public function getRules(&$columns, &$column_model)
  965. {
  966. $fields = $this->get_all(
  967. array('field_visible = ? AND field_filter = ?' => array(1, 1)),
  968. 'option_display_text'
  969. );
  970. $extraFieldOption = new ExtraFieldOption($this->type);
  971. $rules = array();
  972. if (!empty($fields)) {
  973. foreach ($fields as $field) {
  974. $search_options = array();
  975. $type = 'text';
  976. if (in_array($field['field_type'], array(self::FIELD_TYPE_SELECT, self::FIELD_TYPE_DOUBLE_SELECT))) {
  977. $type = 'select';
  978. $search_options['sopt'] = array('eq', 'ne'); //equal not equal
  979. } else {
  980. $search_options['sopt'] = array('cn', 'nc'); //contains not contains
  981. }
  982. $search_options['searchhidden'] = 'true';
  983. $search_options['defaultValue'] = isset($search_options['field_default_value']) ? $search_options['field_default_value'] : null;
  984. if ($field['field_type'] == self::FIELD_TYPE_DOUBLE_SELECT) {
  985. //Add 2 selects
  986. $options = $extraFieldOption->get_field_options_by_field($field['id']);
  987. $options = self::extra_field_double_select_convert_array_to_ordered_array($options);
  988. $first_options = array();
  989. if (!empty($options)) {
  990. foreach ($options as $option) {
  991. foreach ($option as $sub_option) {
  992. if ($sub_option['option_value'] == 0) {
  993. $first_options[] = $sub_option['field_id'].'#'.$sub_option['id'].':'.$sub_option['option_display_text'];
  994. } else {
  995. }
  996. }
  997. }
  998. }
  999. $search_options['value'] = implode(';', $first_options);
  1000. $search_options['dataInit'] = 'fill_second_select';
  1001. //First
  1002. $column_model[] = array(
  1003. 'name' => 'extra_'.$field['field_variable'],
  1004. 'index' => 'extra_'.$field['field_variable'],
  1005. 'width' => '100',
  1006. 'hidden' => 'true',
  1007. 'search' => 'true',
  1008. 'stype' => 'select',
  1009. 'searchoptions' => $search_options
  1010. );
  1011. $columns[] = $field['field_display_text'].' (1)';
  1012. $rules[] = array('field' => 'extra_'.$field['field_variable'], 'op' => 'cn');
  1013. //Second
  1014. $search_options['value'] = $field['id'].':';
  1015. $search_options['dataInit'] = 'register_second_select';
  1016. $column_model[] = array(
  1017. 'name' => 'extra_'.$field['field_variable'].'_second',
  1018. 'index' => 'extra_'.$field['field_variable'].'_second',
  1019. 'width' => '100',
  1020. 'hidden' => 'true',
  1021. 'search' => 'true',
  1022. 'stype' => 'select',
  1023. 'searchoptions' => $search_options
  1024. );
  1025. $columns[] = $field['field_display_text'].' (2)';
  1026. $rules[] = array('field' => 'extra_'.$field['field_variable'].'_second', 'op' => 'cn');
  1027. continue;
  1028. } else {
  1029. $search_options['value'] = $extraFieldOption->get_field_options_to_string(
  1030. $field['id'],
  1031. false,
  1032. 'option_display_text'
  1033. );
  1034. }
  1035. $column_model[] = array(
  1036. 'name' => 'extra_'.$field['field_variable'],
  1037. 'index' => 'extra_'.$field['field_variable'],
  1038. 'width' => '100',
  1039. 'hidden' => 'true',
  1040. 'search' => 'true',
  1041. 'stype' => $type,
  1042. 'searchoptions' => $search_options
  1043. );
  1044. $columns[] = $field['field_display_text'];
  1045. $rules[] = array('field' => 'extra_'.$field['field_variable'], 'op' => 'cn');
  1046. }
  1047. }
  1048. return $rules;
  1049. }
  1050. /**
  1051. * @param array $options
  1052. * @return array
  1053. */
  1054. public function parseConditions($options)
  1055. {
  1056. $inject_extra_fields = null;
  1057. $extraFieldOption = new ExtraFieldOption($this->type);
  1058. $double_fields = array();
  1059. if (isset($options['extra'])) {
  1060. $extra_fields = $options['extra'];
  1061. if (!empty($extra_fields)) {
  1062. $counter = 1;
  1063. foreach ($extra_fields as &$extra) {
  1064. $extra_field_obj = new ExtraField($this->type);
  1065. $extra_field_info = $extra_field_obj->get($extra['id']);
  1066. $extra['extra_field_info'] = $extra_field_info;
  1067. if (isset($extra_field_info['field_type']) && in_array(
  1068. $extra_field_info['field_type'],
  1069. array(
  1070. ExtraField::FIELD_TYPE_SELECT,
  1071. ExtraField::FIELD_TYPE_SELECT,
  1072. ExtraField::FIELD_TYPE_DOUBLE_SELECT
  1073. )
  1074. )
  1075. ) {
  1076. $inject_extra_fields .= " fvo$counter.option_display_text as {$extra['field']}, ";
  1077. } else {
  1078. $inject_extra_fields .= " fv$counter.field_value as {$extra['field']}, ";
  1079. }
  1080. if (isset($extra_fields_info[$extra['id']])) {
  1081. $info = $extra_fields_info[$extra['id']];
  1082. } else {
  1083. $info = $this->get($extra['id']);
  1084. $extra_fields_info[$extra['id']] = $info;
  1085. }
  1086. if (isset($info['field_type']) && $info['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) {
  1087. $double_fields[$info['id']] = $info;
  1088. }
  1089. $counter++;
  1090. }
  1091. }
  1092. }
  1093. $options_by_double = array();
  1094. foreach ($double_fields as $double) {
  1095. $my_options = $extraFieldOption->get_field_options_by_field(
  1096. $double['id'],
  1097. true
  1098. );
  1099. $options_by_double['extra_'.$double['field_variable']] = $my_options;
  1100. }
  1101. $field_value_to_join = array();
  1102. //filter can be all/any = and/or
  1103. $inject_joins = null;
  1104. $inject_where = null;
  1105. $where = null;
  1106. if (!empty($options['where'])) {
  1107. if (!empty($options['extra'])) {
  1108. //Removing double 1=1
  1109. $options['where'] = str_replace(' 1 = 1 AND', '', $options['where']);
  1110. //Always OR
  1111. $counter = 1;
  1112. foreach ($extra_fields as $extra_info) {
  1113. $extra_field_info = $extra_info['extra_field_info'];
  1114. $inject_joins .= " INNER JOIN $this->table_field_values fv$counter ON (s.".$this->primaryKey." = fv$counter.".$this->handler_id.") ";
  1115. //Add options
  1116. if (isset($extra_field_info['field_type']) && in_array(
  1117. $extra_field_info['field_type'],
  1118. array(
  1119. ExtraField::FIELD_TYPE_SELECT,
  1120. ExtraField::FIELD_TYPE_SELECT,
  1121. ExtraField::FIELD_TYPE_DOUBLE_SELECT
  1122. )
  1123. )
  1124. ) {
  1125. $options['where'] = str_replace(
  1126. $extra_info['field'],
  1127. 'fv'.$counter.'.field_id = '.$extra_info['id'].' AND fvo'.$counter.'.option_value',
  1128. $options['where']
  1129. );
  1130. $inject_joins .= " INNER JOIN $this->table_field_options fvo$counter ".
  1131. " ON (fv$counter.field_id = fvo$counter.field_id AND fv$counter.field_value = fvo$counter.option_value) ";
  1132. } else {
  1133. //text, textarea, etc
  1134. $options['where'] = str_replace(
  1135. $extra_info['field'],
  1136. 'fv'.$counter.'.field_id = '.$extra_info['id'].' AND fv'.$counter.'.field_value',
  1137. $options['where']
  1138. );
  1139. }
  1140. $field_value_to_join[] = " fv$counter.$this->handler_id ";
  1141. $counter++;
  1142. }
  1143. if (!empty($field_value_to_join)) {
  1144. //$inject_where .= " AND s.id = ".implode(' = ', $field_value_to_join);
  1145. }
  1146. }
  1147. $where .= ' AND '.$options['where'];
  1148. }
  1149. $order = null;
  1150. if (!empty($options['order'])) {
  1151. $order = " ORDER BY ".$options['order'];
  1152. }
  1153. $limit = null;
  1154. if (!empty($options['limit'])) {
  1155. $limit = " LIMIT ".$options['limit'];
  1156. }
  1157. return array(
  1158. 'order' => $order,
  1159. 'limit' => $limit,
  1160. 'where' => $where,
  1161. 'inject_where' => $inject_where,
  1162. 'inject_joins' => $inject_joins,
  1163. 'field_value_to_join' => $field_value_to_join,
  1164. 'inject_extra_fields' => $inject_extra_fields,
  1165. );
  1166. }
  1167. public function getExtraFieldRules($filters, $stringToSearch = 'extra_')
  1168. {
  1169. $extra_fields = array();
  1170. // Getting double select if exists
  1171. $double_select = array();
  1172. foreach ($filters->rules as $rule) {
  1173. if (strpos($rule->field, '_second') === false) {
  1174. } else {
  1175. $my_field = str_replace('_second', '', $rule->field);
  1176. $double_select[$my_field] = $rule->data;
  1177. }
  1178. }
  1179. $condition_array = array();
  1180. foreach ($filters->rules as $rule) {
  1181. if (strpos($rule->field, $stringToSearch) === false) {
  1182. //normal fields
  1183. $field = $rule->field;
  1184. if (isset($rule->data) && $rule->data != -1) {
  1185. $condition_array[] = get_where_clause($field, $rule->op, $rule->data);
  1186. }
  1187. } else {
  1188. // Extra fields
  1189. if (strpos($rule->field, '_second') === false) {
  1190. //No _second
  1191. $original_field = str_replace($stringToSearch, '', $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 (!isset($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 (isset($rule->data)) {
  1208. if ($rule->data == -1) {
  1209. continue;
  1210. }
  1211. $condition_array[] = ' ('.get_where_clause($rule->field, $rule->op, $rule->data).') ';
  1212. $extra_fields[] = array(
  1213. 'field' => $rule->field,
  1214. 'id' => $field_option['id'],
  1215. 'data' => $rule->data
  1216. );
  1217. }
  1218. }
  1219. } else {
  1220. $my_field = str_replace('_second', '', $rule->field);
  1221. $original_field = str_replace($stringToSearch, '', $my_field);
  1222. $field_option = $this->get_handler_field_info_by_field_variable($original_field);
  1223. $extra_fields[] = array(
  1224. 'field' => $rule->field,
  1225. 'id' => $field_option['id']
  1226. );
  1227. }
  1228. }
  1229. }
  1230. return array(
  1231. 'extra_fields' => $extra_fields,
  1232. 'condition_array' => $condition_array
  1233. );
  1234. }
  1235. }