extra_field.lib.php 66 KB

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