extra_field.lib.php 67 KB

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