extra_field.lib.php 68 KB

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