extra_field.lib.php 72 KB

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