extra_field.lib.php 70 KB

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