extra_field.lib.php 71 KB

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