extra_field.lib.php 72 KB

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