extra_field.lib.php 69 KB

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