extra_field.lib.php 69 KB

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