extra_field.lib.php 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\ExtraField as EntityExtraField;
  4. /**
  5. * Class ExtraField
  6. */
  7. class ExtraField extends Model
  8. {
  9. public $columns = array(
  10. 'id',
  11. 'field_type',
  12. 'variable',
  13. 'display_text',
  14. 'default_value',
  15. 'field_order',
  16. 'visible',
  17. 'changeable',
  18. 'filter',
  19. 'extra_field_type',
  20. /* Enable this when field_loggeable is introduced as a table field (2.0)
  21. 'field_loggeable',
  22. */
  23. 'created_at'
  24. );
  25. public $ops = array(
  26. 'eq' => '=', //equal
  27. 'ne' => '<>', //not equal
  28. 'lt' => '<', //less than
  29. 'le' => '<=', //less than or equal
  30. 'gt' => '>', //greater than
  31. 'ge' => '>=', //greater than or equal
  32. 'bw' => 'LIKE', //begins with
  33. 'bn' => 'NOT LIKE', //doesn't begin with
  34. 'in' => 'LIKE', //is in
  35. 'ni' => 'NOT LIKE', //is not in
  36. 'ew' => 'LIKE', //ends with
  37. 'en' => 'NOT LIKE', //doesn't end with
  38. 'cn' => 'LIKE', //contains
  39. 'nc' => 'NOT LIKE' //doesn't contain
  40. );
  41. const FIELD_TYPE_TEXT = 1;
  42. const FIELD_TYPE_TEXTAREA = 2;
  43. const FIELD_TYPE_RADIO = 3;
  44. const FIELD_TYPE_SELECT = 4;
  45. const FIELD_TYPE_SELECT_MULTIPLE = 5;
  46. const FIELD_TYPE_DATE = 6;
  47. const FIELD_TYPE_DATETIME = 7;
  48. const FIELD_TYPE_DOUBLE_SELECT = 8;
  49. const FIELD_TYPE_DIVIDER = 9;
  50. const FIELD_TYPE_TAG = 10;
  51. const FIELD_TYPE_TIMEZONE = 11;
  52. const FIELD_TYPE_SOCIAL_PROFILE = 12;
  53. const FIELD_TYPE_CHECKBOX = 13;
  54. const FIELD_TYPE_MOBILE_PHONE_NUMBER = 14;
  55. const FIELD_TYPE_INTEGER = 15;
  56. const FIELD_TYPE_FILE_IMAGE = 16;
  57. const FIELD_TYPE_FLOAT = 17;
  58. const FIELD_TYPE_FILE = 18;
  59. const FIELD_TYPE_VIDEO_URL = 19;
  60. const FIELD_TYPE_LETTERS_ONLY = 20;
  61. const FIELD_TYPE_ALPHANUMERIC = 21;
  62. const FIELD_TYPE_LETTERS_SPACE = 22;
  63. const FIELD_TYPE_ALPHANUMERIC_SPACE = 23;
  64. public $type = 'user';
  65. public $pageName;
  66. public $pageUrl;
  67. public $extraFieldType = 0;
  68. /**
  69. * @param string $type
  70. */
  71. public function __construct($type)
  72. {
  73. $this->type = $type;
  74. $this->table = Database::get_main_table(TABLE_EXTRA_FIELD);
  75. $this->table_field_options = Database::get_main_table(TABLE_EXTRA_FIELD_OPTIONS);
  76. $this->table_field_values = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
  77. switch ($this->type) {
  78. case 'calendar_event':
  79. $this->extraFieldType = EntityExtraField::CALENDAR_FIELD_TYPE;
  80. break;
  81. case 'course':
  82. $this->extraFieldType = EntityExtraField::COURSE_FIELD_TYPE;
  83. break;
  84. case 'user':
  85. $this->extraFieldType = EntityExtraField::USER_FIELD_TYPE;
  86. break;
  87. case 'session':
  88. $this->extraFieldType = EntityExtraField::SESSION_FIELD_TYPE;
  89. break;
  90. case 'question':
  91. $this->extraFieldType = EntityExtraField::QUESTION_FIELD_TYPE;
  92. break;
  93. case 'lp':
  94. $this->extraFieldType = EntityExtraField::LP_FIELD_TYPE;
  95. break;
  96. case 'lp_item':
  97. $this->extraFieldType = EntityExtraField::LP_ITEM_FIELD_TYPE;
  98. break;
  99. }
  100. $this->pageUrl = 'extra_fields.php?type='.$this->type;
  101. // Example QuestionFields
  102. $this->pageName = get_lang(ucwords($this->type).'Fields');
  103. }
  104. /**
  105. * @return int
  106. */
  107. public function getExtraFieldType()
  108. {
  109. return $this->extraFieldType;
  110. }
  111. /**
  112. * @return array
  113. */
  114. public static function getValidExtraFieldTypes()
  115. {
  116. return array(
  117. 'user',
  118. 'course',
  119. 'session',
  120. 'question',
  121. 'lp',
  122. 'calendar_event',
  123. 'lp_item'
  124. );
  125. }
  126. /**
  127. * @return int
  128. */
  129. public function get_count()
  130. {
  131. $em = Database::getManager();
  132. $query = $em->getRepository('ChamiloCoreBundle:ExtraField')->createQueryBuilder('e');
  133. $query->select('count(e.id)');
  134. $query->where('e.extraFieldType = :type');
  135. $query->setParameter('type', $this->getExtraFieldType());
  136. return $query->getQuery()->getScalarResult();
  137. }
  138. /**
  139. * @param string $sidx
  140. * @param string $sord
  141. * @param int $start
  142. * @param int $limit
  143. *
  144. * @return array
  145. */
  146. public function getAllGrid($sidx, $sord, $start, $limit)
  147. {
  148. switch ($sidx) {
  149. case 'field_order':
  150. $sidx = 'e.fieldOrder';
  151. break;
  152. case 'variable':
  153. $sidx = 'e.variable';
  154. break;
  155. case 'display_text':
  156. $sidx = 'e.displayText';
  157. break;
  158. case 'changeable':
  159. $sidx = 'e.changeable';
  160. break;
  161. case 'visible':
  162. $sidx = 'e.visible';
  163. break;
  164. case 'filter':
  165. $sidx = 'e.filter';
  166. break;
  167. case 'display_text':
  168. $sidx = 'e.fieldType';
  169. break;
  170. }
  171. $em = Database::getManager();
  172. $query = $em->getRepository('ChamiloCoreBundle:ExtraField')->createQueryBuilder('e');
  173. $query->select('e')
  174. ->where('e.extraFieldType = :type')
  175. ->setParameter('type', $this->getExtraFieldType())
  176. ->orderBy($sidx, $sord)
  177. ->setFirstResult($start)
  178. ->setMaxResults($limit);
  179. //echo $query->getQuery()->getSQL();
  180. return $query->getQuery()->getArrayResult();
  181. }
  182. /**
  183. * @param array $conditions
  184. * @param null $order_field_options_by
  185. *
  186. * @return array
  187. */
  188. public function get_all($conditions = array(), $order_field_options_by = null)
  189. {
  190. $conditions = Database::parse_conditions(array('where' => $conditions));
  191. if (empty($conditions)) {
  192. $conditions .= " WHERE extra_field_type = ".$this->extraFieldType;
  193. } else {
  194. $conditions .= " AND extra_field_type = ".$this->extraFieldType;
  195. }
  196. $sql = "SELECT * FROM $this->table
  197. $conditions
  198. ORDER BY field_order ASC
  199. ";
  200. $result = Database::query($sql);
  201. $extraFields = Database::store_result($result, 'ASSOC');
  202. $option = new ExtraFieldOption($this->type);
  203. if (!empty($extraFields)) {
  204. foreach ($extraFields as &$extraField) {
  205. $extraField['options'] = $option->get_field_options_by_field(
  206. $extraField['id'],
  207. false,
  208. $order_field_options_by
  209. );
  210. }
  211. }
  212. return $extraFields;
  213. }
  214. /**
  215. * @param string $variable
  216. *
  217. * @return array|bool
  218. */
  219. public function get_handler_field_info_by_field_variable($variable)
  220. {
  221. $variable = Database::escape_string($variable);
  222. $sql = "SELECT * FROM {$this->table}
  223. WHERE
  224. variable = '$variable' AND
  225. extra_field_type = $this->extraFieldType";
  226. $result = Database::query($sql);
  227. if (Database::num_rows($result)) {
  228. $row = Database::fetch_array($result, 'ASSOC');
  229. // All the options of the field
  230. $sql = "SELECT * FROM $this->table_field_options
  231. WHERE field_id='".intval($row['id'])."'
  232. ORDER BY option_order ASC";
  233. $result = Database::query($sql);
  234. while ($option = Database::fetch_array($result)) {
  235. $row['options'][$option['id']] = $option;
  236. }
  237. return $row;
  238. } else {
  239. return false;
  240. }
  241. }
  242. /**
  243. * @param int $fieldId
  244. *
  245. * @return array|bool
  246. */
  247. public function getFieldInfoByFieldId($fieldId)
  248. {
  249. $fieldId = intval($fieldId);
  250. $sql = "SELECT * FROM {$this->table}
  251. WHERE
  252. field_id = '$fieldId' AND
  253. extra_field_type = $this->extraFieldType";
  254. $result = Database::query($sql);
  255. if (Database::num_rows($result)) {
  256. $row = Database::fetch_array($result, 'ASSOC');
  257. // All the options of the field
  258. $sql = "SELECT * FROM $this->table_field_options
  259. WHERE field_id='".$fieldId."'
  260. ORDER BY option_order ASC";
  261. $result = Database::query($sql);
  262. while ($option = Database::fetch_array($result)) {
  263. $row['options'][$option['id']] = $option;
  264. }
  265. return $row;
  266. } else {
  267. return false;
  268. }
  269. }
  270. /**
  271. * @return int
  272. */
  273. public function get_max_field_order()
  274. {
  275. $sql = "SELECT MAX(field_order)
  276. FROM {$this->table}
  277. WHERE
  278. extra_field_type = '.$this->extraFieldType.'";
  279. $res = Database::query($sql);
  280. $order = 0;
  281. if (Database::num_rows($res) > 0) {
  282. $row = Database::fetch_row($res);
  283. $order = $row[0] + 1;
  284. }
  285. return $order;
  286. }
  287. /**
  288. * @param string $handler
  289. *
  290. * @return array
  291. */
  292. public static function get_extra_fields_by_handler($handler)
  293. {
  294. $types= array();
  295. $types[self::FIELD_TYPE_TEXT] = get_lang('FieldTypeText');
  296. $types[self::FIELD_TYPE_TEXTAREA] = get_lang('FieldTypeTextarea');
  297. $types[self::FIELD_TYPE_RADIO] = get_lang('FieldTypeRadio');
  298. $types[self::FIELD_TYPE_SELECT] = get_lang('FieldTypeSelect');
  299. $types[self::FIELD_TYPE_SELECT_MULTIPLE] = get_lang('FieldTypeSelectMultiple');
  300. $types[self::FIELD_TYPE_DATE] = get_lang('FieldTypeDate');
  301. $types[self::FIELD_TYPE_DATETIME] = get_lang('FieldTypeDatetime');
  302. $types[self::FIELD_TYPE_DOUBLE_SELECT] = get_lang('FieldTypeDoubleSelect');
  303. $types[self::FIELD_TYPE_DIVIDER] = get_lang('FieldTypeDivider');
  304. $types[self::FIELD_TYPE_TAG] = get_lang('FieldTypeTag');
  305. $types[self::FIELD_TYPE_TIMEZONE] = get_lang('FieldTypeTimezone');
  306. $types[self::FIELD_TYPE_SOCIAL_PROFILE] = get_lang('FieldTypeSocialProfile');
  307. $types[self::FIELD_TYPE_MOBILE_PHONE_NUMBER] = get_lang('FieldTypeMobilePhoneNumber');
  308. $types[self::FIELD_TYPE_CHECKBOX] = get_lang('FieldTypeCheckbox');
  309. $types[self::FIELD_TYPE_INTEGER] = get_lang('FieldTypeInteger');
  310. $types[self::FIELD_TYPE_FILE_IMAGE] = get_lang('FieldTypeFileImage');
  311. $types[self::FIELD_TYPE_FLOAT] = get_lang('FieldTypeFloat');
  312. $types[self::FIELD_TYPE_FILE] = get_lang('FieldTypeFile');
  313. $types[self::FIELD_TYPE_VIDEO_URL] = get_lang('FieldTypeVideoUrl');
  314. $types[self::FIELD_TYPE_LETTERS_ONLY] = get_lang('FieldTypeOnlyLetters');
  315. $types[self::FIELD_TYPE_ALPHANUMERIC] = get_lang('FieldTypeAlphanumeric');
  316. $types[self::FIELD_TYPE_LETTERS_SPACE] = get_lang(
  317. 'FieldTypeLettersSpaces'
  318. );
  319. $types[self::FIELD_TYPE_ALPHANUMERIC_SPACE] = get_lang(
  320. 'FieldTypeAlphanumericSpaces'
  321. );
  322. switch ($handler) {
  323. case 'course':
  324. case 'session':
  325. case 'user':
  326. break;
  327. }
  328. return $types;
  329. }
  330. /**
  331. * Add elements to a form
  332. *
  333. * @param FormValidator $form
  334. * @param int $itemId
  335. * @param array $exclude variables of extra field to exclude
  336. * @return array|bool
  337. */
  338. public function addElements($form, $itemId = 0, $exclude = [])
  339. {
  340. if (empty($form)) {
  341. return false;
  342. }
  343. $itemId = intval($itemId);
  344. $form->addHidden('item_id', $itemId);
  345. $extraData = false;
  346. if (!empty($itemId)) {
  347. $extraData = self::get_handler_extra_data($itemId);
  348. if ($form) {
  349. $form->setDefaults($extraData);
  350. }
  351. }
  352. $extraFields = $this->get_all(null, 'option_order');
  353. $extra = $this->set_extra_fields_in_form(
  354. $form,
  355. $extraData,
  356. false,
  357. $extraFields,
  358. $itemId,
  359. $exclude
  360. );
  361. return $extra;
  362. }
  363. /**
  364. * @param int $itemId (session_id, question_id, course id)
  365. *
  366. * @return array
  367. */
  368. public function get_handler_extra_data($itemId)
  369. {
  370. if (empty($itemId)) {
  371. return array();
  372. }
  373. $extra_data = array();
  374. $fields = self::get_all();
  375. $field_values = new ExtraFieldValue($this->type);
  376. if (!empty($fields) > 0) {
  377. foreach ($fields as $field) {
  378. $field_value = $field_values->get_values_by_handler_and_field_id(
  379. $itemId, $field['id']
  380. );
  381. if ($field['field_type'] == ExtraField::FIELD_TYPE_TAG) {
  382. $tags = UserManager::get_user_tags_to_string($itemId, $field['id'], false);
  383. $extra_data['extra_'.$field['variable']] = $tags;
  384. continue;
  385. }
  386. if ($field_value) {
  387. $field_value = $field_value['value'];
  388. switch ($field['field_type']) {
  389. case ExtraField::FIELD_TYPE_TAG:
  390. $tags = UserManager::get_user_tags_to_string($itemId, $field['id'], false);
  391. $extra_data['extra_'.$field['variable']] = $tags;
  392. break;
  393. case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
  394. $selected_options = explode(
  395. '::',
  396. $field_value
  397. );
  398. $firstOption = isset($selected_options[0]) ? $selected_options[0] : '';
  399. $secondOption = isset($selected_options[1]) ? $selected_options[1] : '';
  400. $extra_data['extra_'.$field['variable']]['extra_'.$field['variable']] = $firstOption;
  401. $extra_data['extra_'.$field['variable']]['extra_'.$field['variable'].'_second'] = $secondOption;
  402. break;
  403. case ExtraField::FIELD_TYPE_SELECT_MULTIPLE:
  404. $field_value = explode(';', $field_value);
  405. $extra_data['extra_'.$field['variable']] = $field_value;
  406. break;
  407. case ExtraField::FIELD_TYPE_RADIO:
  408. $extra_data['extra_'.$field['variable']]['extra_'.$field['variable']] = $field_value;
  409. break;
  410. default:
  411. $extra_data['extra_'.$field['variable']] = $field_value;
  412. break;
  413. }
  414. } else {
  415. // Set default values
  416. if (isset($field['field_default_value']) && !empty($field['field_default_value'])) {
  417. $extra_data['extra_'.$field['variable']] = $field['field_default_value'];
  418. }
  419. }
  420. }
  421. }
  422. return $extra_data;
  423. }
  424. /**
  425. * @param string $field_type
  426. *
  427. * @return array
  428. */
  429. public function get_all_extra_field_by_type($field_type)
  430. {
  431. // all the information of the field
  432. $sql = "SELECT * FROM {$this->table}
  433. WHERE
  434. field_type = '".Database::escape_string($field_type)."' AND
  435. extra_field_type = $this->extraFieldType
  436. ";
  437. $result = Database::query($sql);
  438. $return = array();
  439. while ($row = Database::fetch_array($result)) {
  440. $return[] = $row['id'];
  441. }
  442. return $return;
  443. }
  444. /**
  445. * @return array
  446. */
  447. public function get_field_types()
  448. {
  449. return self::get_extra_fields_by_handler($this->type);
  450. }
  451. /**
  452. * @param int $id
  453. *
  454. * @return null
  455. */
  456. public function get_field_type_by_id($id)
  457. {
  458. $types = self::get_field_types();
  459. if (isset($types[$id])) {
  460. return $types[$id];
  461. }
  462. return null;
  463. }
  464. /**
  465. * Converts a string like this:
  466. * France:Paris;Bretagne;Marseilles;Lyon|Belgique:Bruxelles;Namur;Liège;Bruges|Peru:Lima;Piura;
  467. * into
  468. * array(
  469. * 'France' =>
  470. * array('Paris', 'Bregtane', 'Marseilles'),
  471. * 'Belgique' =>
  472. * array('Namur', 'Liège')
  473. * ), etc
  474. * @param string $string
  475. *
  476. * @return array
  477. */
  478. public static function extra_field_double_select_convert_string_to_array($string)
  479. {
  480. $options = explode('|', $string);
  481. $options_parsed = array();
  482. $id = 0;
  483. if (!empty($options)) {
  484. foreach ($options as $sub_options) {
  485. $options = explode(':', $sub_options);
  486. $sub_sub_options = explode(';', $options[1]);
  487. $options_parsed[$id] = array(
  488. 'label' => $options[0],
  489. 'options' => $sub_sub_options,
  490. );
  491. $id++;
  492. }
  493. }
  494. return $options_parsed;
  495. }
  496. /**
  497. * @param array $options
  498. *
  499. * @return array
  500. */
  501. public static function extra_field_double_select_convert_array_to_ordered_array($options)
  502. {
  503. $options_parsed = array();
  504. if (!empty($options)) {
  505. foreach ($options as $option) {
  506. if ($option['option_value'] == 0) {
  507. $options_parsed[$option['id']][] = $option;
  508. } else {
  509. $options_parsed[$option['option_value']][] = $option;
  510. }
  511. }
  512. }
  513. return $options_parsed;
  514. }
  515. /**
  516. * @param array $options the result of the get_field_options_by_field() array
  517. *
  518. * @return string
  519. */
  520. public static function extra_field_double_select_convert_array_to_string($options)
  521. {
  522. $string = null;
  523. $options_parsed = self::extra_field_double_select_convert_array_to_ordered_array($options);
  524. if (!empty($options_parsed)) {
  525. foreach ($options_parsed as $option) {
  526. foreach ($option as $key => $item) {
  527. $string .= $item['display_text'];
  528. if ($key == 0) {
  529. $string .= ':';
  530. } else {
  531. if (isset($option[$key + 1])) {
  532. $string .= ';';
  533. }
  534. }
  535. }
  536. $string .= '|';
  537. }
  538. }
  539. if (!empty($string)) {
  540. $string = substr($string, 0, strlen($string) - 1);
  541. }
  542. return $string;
  543. }
  544. /**
  545. * @param array $params
  546. *
  547. * @return array
  548. */
  549. public function clean_parameters($params)
  550. {
  551. if (!isset($params['variable']) || empty($params['variable'])) {
  552. $params['variable'] = $params['display_text'];
  553. }
  554. $params['variable'] = trim(strtolower(str_replace(" ", "_", $params['variable'])));
  555. if (!isset($params['field_order'])) {
  556. $max_order = self::get_max_field_order();
  557. $params['field_order'] = $max_order;
  558. }
  559. return $params;
  560. }
  561. /**
  562. * @param array $params
  563. * @param bool $show_query
  564. *
  565. * @return bool
  566. */
  567. public function save($params, $show_query = false)
  568. {
  569. $fieldInfo = self::get_handler_field_info_by_field_variable($params['variable']);
  570. $params = self::clean_parameters($params);
  571. $params['extra_field_type'] = $this->extraFieldType;
  572. if ($fieldInfo) {
  573. return $fieldInfo['id'];
  574. } else {
  575. $id = parent::save($params, $show_query);
  576. if ($id) {
  577. $session_field_option = new ExtraFieldOption($this->type);
  578. $params['field_id'] = $id;
  579. $session_field_option->save($params);
  580. }
  581. return $id;
  582. }
  583. }
  584. /**
  585. * @param array $params
  586. *
  587. * @return bool|void
  588. */
  589. public function update($params)
  590. {
  591. $params = self::clean_parameters($params);
  592. if (isset($params['id'])) {
  593. $field_option = new ExtraFieldOption($this->type);
  594. $params['field_id'] = $params['id'];
  595. $field_option->save($params);
  596. }
  597. parent::update($params);
  598. }
  599. /**
  600. * @param $id
  601. *
  602. * @return bool|void
  603. */
  604. public function delete($id)
  605. {
  606. $field_option = new ExtraFieldOption($this->type);
  607. $field_option->delete_all_options_by_field_id($id);
  608. $session_field_values = new ExtraFieldValue($this->type);
  609. $session_field_values->delete_all_values_by_field_id($id);
  610. parent::delete($id);
  611. }
  612. /**
  613. * @param FormValidator $form
  614. * @param array $extraData
  615. * @param bool $admin_permissions
  616. * @param int $user_id
  617. * @param array $extra
  618. * @param int $itemId
  619. * @param array $exclude variables of extra field to exclude
  620. * @return array
  621. */
  622. public function set_extra_fields_in_form(
  623. $form,
  624. $extraData,
  625. $admin_permissions = false,
  626. $extra = array(),
  627. $itemId = null,
  628. $exclude = []
  629. ) {
  630. $type = $this->type;
  631. $jquery_ready_content = null;
  632. if (!empty($extra)) {
  633. foreach ($extra as $field_details) {
  634. // Getting default value id if is set
  635. $defaultValueId = null;
  636. if (isset($field_details['options']) && !empty($field_details['options'])) {
  637. $valueToFind = null;
  638. if (isset($field_details['field_default_value'])) {
  639. $valueToFind = $field_details['field_default_value'];
  640. }
  641. // If a value is found we override the default value
  642. if (isset($extraData['extra_'.$field_details['variable']])) {
  643. $valueToFind = $extraData['extra_'.$field_details['variable']];
  644. }
  645. foreach ($field_details['options'] as $option) {
  646. if ($option['option_value'] == $valueToFind) {
  647. $defaultValueId = $option['id'];
  648. }
  649. }
  650. }
  651. if (!$admin_permissions) {
  652. if ($field_details['visible'] == 0) {
  653. continue;
  654. }
  655. if (in_array($field_details['variable'], $exclude)) {
  656. continue;
  657. }
  658. }
  659. switch ($field_details['field_type']) {
  660. case ExtraField::FIELD_TYPE_TEXT:
  661. $form->addElement(
  662. 'text',
  663. 'extra_'.$field_details['variable'],
  664. $field_details['display_text'],
  665. array()
  666. );
  667. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  668. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  669. if (!$admin_permissions) {
  670. if ($field_details['visible'] == 0) {
  671. $form->freeze(
  672. 'extra_'.$field_details['variable']
  673. );
  674. }
  675. }
  676. break;
  677. case ExtraField::FIELD_TYPE_TEXTAREA:
  678. $form->addHtmlEditor(
  679. 'extra_'.$field_details['variable'],
  680. $field_details['display_text'],
  681. false,
  682. false,
  683. array('ToolbarSet' => 'Profile', 'Width' => '100%', 'Height' => '130')
  684. );
  685. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  686. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  687. if (!$admin_permissions) {
  688. if ($field_details['visible'] == 0) {
  689. $form->freeze(
  690. 'extra_'.$field_details['variable']
  691. );
  692. }
  693. }
  694. break;
  695. case ExtraField::FIELD_TYPE_RADIO:
  696. $group = array();
  697. if (isset($field_details['options']) && !empty($field_details['options'])) {
  698. foreach ($field_details['options'] as $option_details) {
  699. $options[$option_details['option_value']] = $option_details['display_text'];
  700. $group[] = $form->createElement(
  701. 'radio',
  702. 'extra_'.$field_details['variable'],
  703. $option_details['option_value'],
  704. $option_details['display_text'].'<br />',
  705. $option_details['option_value']
  706. );
  707. }
  708. }
  709. $form->addGroup(
  710. $group,
  711. 'extra_'.$field_details['variable'],
  712. $field_details['display_text'],
  713. ''
  714. );
  715. if (!$admin_permissions) {
  716. if ($field_details['visible'] == 0) {
  717. $form->freeze(
  718. 'extra_'.$field_details['variable']
  719. );
  720. }
  721. }
  722. break;
  723. case ExtraField::FIELD_TYPE_CHECKBOX:
  724. $group = array();
  725. if (isset($field_details['options']) && !empty($field_details['options'])) {
  726. foreach ($field_details['options'] as $option_details) {
  727. $options[$option_details['option_value']] = $option_details['display_text'];
  728. $group[] = $form->createElement(
  729. 'checkbox',
  730. 'extra_'.$field_details['variable'],
  731. $option_details['option_value'],
  732. $option_details['display_text'].'<br />',
  733. $option_details['option_value']
  734. );
  735. }
  736. } else {
  737. $fieldVariable = "extra_{$field_details['variable']}";
  738. $checkboxAttributes = array();
  739. if (is_array($extraData) && array_key_exists($fieldVariable, $extraData)) {
  740. $checkboxAttributes['checked'] = true;
  741. }
  742. // We assume that is a switch on/off with 1 and 0 as values
  743. $group[] = $form->createElement(
  744. 'checkbox',
  745. 'extra_'.$field_details['variable'],
  746. null,
  747. //$field_details['display_text'].'<br />',
  748. get_lang('Yes'),
  749. $checkboxAttributes
  750. );
  751. }
  752. $form->addGroup(
  753. $group,
  754. 'extra_'.$field_details['variable'],
  755. $field_details['display_text'],
  756. ''
  757. );
  758. if (!$admin_permissions) {
  759. if ($field_details['visible'] == 0) {
  760. $form->freeze(
  761. 'extra_'.$field_details['variable']
  762. );
  763. }
  764. }
  765. break;
  766. case ExtraField::FIELD_TYPE_SELECT:
  767. $get_lang_variables = false;
  768. if (in_array(
  769. $field_details['variable'],
  770. array('mail_notify_message', 'mail_notify_invitation', 'mail_notify_group_message')
  771. )
  772. ) {
  773. $get_lang_variables = true;
  774. }
  775. // Get extra field workflow
  776. $userInfo = api_get_user_info();
  777. $addOptions = array();
  778. $optionsExists = false;
  779. global $app;
  780. // Check if exist $app['orm.em'] object
  781. if (isset($app['orm.em']) && is_object($app['orm.em'])) {
  782. $optionsExists = $app['orm.em']
  783. ->getRepository('ChamiloLMS\Entity\ExtraFieldOptionRelFieldOption')
  784. ->findOneBy(array('fieldId' => $field_details['id']));
  785. }
  786. if ($optionsExists) {
  787. if (isset($userInfo['status']) && !empty($userInfo['status'])) {
  788. $fieldWorkFlow = $app['orm.em']->getRepository('ChamiloLMS\Entity\ExtraFieldOptionRelFieldOption')
  789. ->findBy(
  790. array(
  791. 'fieldId' => $field_details['id'],
  792. 'relatedFieldOptionId' => $defaultValueId,
  793. 'roleId' => $userInfo['status']
  794. )
  795. );
  796. foreach ($fieldWorkFlow as $item) {
  797. $addOptions[] = $item->getFieldOptionId();
  798. }
  799. }
  800. }
  801. $options = array();
  802. if (empty($defaultValueId)) {
  803. $options[''] = get_lang('SelectAnOption');
  804. }
  805. $optionList = array();
  806. if (!empty($field_details['options'])) {
  807. foreach ($field_details['options'] as $option_details) {
  808. $optionList[$option_details['id']] = $option_details;
  809. if ($get_lang_variables) {
  810. $options[$option_details['option_value']] = get_lang($option_details['display_text']);
  811. } else {
  812. if ($optionsExists) {
  813. // Adding always the default value
  814. if ($option_details['id'] == $defaultValueId) {
  815. $options[$option_details['option_value']] = $option_details['display_text'];
  816. } else {
  817. if (isset($addOptions) && !empty($addOptions)) {
  818. // Parsing filters
  819. if (in_array($option_details['id'], $addOptions)) {
  820. $options[$option_details['option_value']] = $option_details['display_text'];
  821. }
  822. }
  823. }
  824. } else {
  825. // Normal behaviour
  826. $options[$option_details['option_value']] = $option_details['display_text'];
  827. }
  828. }
  829. }
  830. if (isset($optionList[$defaultValueId])) {
  831. if (isset($optionList[$defaultValueId]['option_value']) &&
  832. $optionList[$defaultValueId]['option_value'] == 'aprobada'
  833. ) {
  834. // @todo function don't exists api_is_question_manager
  835. /*if (api_is_question_manager() == false) {
  836. $form->freeze();
  837. }*/
  838. }
  839. }
  840. // Setting priority message
  841. if (isset($optionList[$defaultValueId]) &&
  842. isset($optionList[$defaultValueId]['priority'])
  843. ) {
  844. if (!empty($optionList[$defaultValueId]['priority'])) {
  845. $priorityId = $optionList[$defaultValueId]['priority'];
  846. $option = new ExtraFieldOption($this->type);
  847. $messageType = $option->getPriorityMessageType($priorityId);
  848. $form->addElement(
  849. 'label',
  850. null,
  851. Display::return_message(
  852. $optionList[$defaultValueId]['priority_message'],
  853. $messageType
  854. )
  855. );
  856. }
  857. }
  858. }
  859. if ($get_lang_variables) {
  860. $field_details['display_text'] = get_lang($field_details['display_text']);
  861. }
  862. // chzn-select doesn't work for sessions??
  863. $form->addElement(
  864. 'select',
  865. 'extra_' . $field_details['variable'],
  866. $field_details['display_text'],
  867. $options,
  868. array('id' => 'extra_'.$field_details['variable'])
  869. );
  870. /* Enable this when field_loggeable is introduced as a table field (2.0)
  871. if ($optionsExists && $field_details['field_loggeable'] && !empty($defaultValueId)) {
  872. $form->addElement(
  873. 'textarea',
  874. 'extra_' . $field_details['variable'] . '_comment',
  875. $field_details['display_text'] . ' ' . get_lang('Comment')
  876. );
  877. $extraFieldValue = new ExtraFieldValue($this->type);
  878. $repo = $app['orm.em']->getRepository($extraFieldValue->entityName);
  879. $repoLog = $app['orm.em']->getRepository('Gedmo\Loggable\Entity\LogEntry');
  880. $newEntity = $repo->findOneBy(
  881. array(
  882. $this->handlerEntityId => $itemId,
  883. 'fieldId' => $field_details['id']
  884. )
  885. );
  886. // @todo move this in a function inside the class
  887. if ($newEntity) {
  888. $logs = $repoLog->getLogEntries($newEntity);
  889. if (!empty($logs)) {
  890. $html = '<b>' . get_lang('LatestChanges') . '</b><br /><br />';
  891. $table = new HTML_Table(array('class' => 'data_table'));
  892. $table->setHeaderContents(0, 0, get_lang('Value'));
  893. $table->setHeaderContents(0, 1, get_lang('Comment'));
  894. $table->setHeaderContents(0, 2, get_lang('ModifyDate'));
  895. $table->setHeaderContents(0, 3, get_lang('Username'));
  896. $row = 1;
  897. foreach ($logs as $log) {
  898. $column = 0;
  899. $data = $log->getData();
  900. $fieldValue = isset($data['fieldValue']) ? $data['fieldValue'] : null;
  901. $comment = isset($data['comment']) ? $data['comment'] : null;
  902. $table->setCellContents($row, $column, $fieldValue);
  903. $column++;
  904. $table->setCellContents($row, $column, $comment);
  905. $column++;
  906. $table->setCellContents($row, $column, api_get_local_time($log->getLoggedAt()->format('Y-m-d H:i:s')));
  907. $column++;
  908. $table->setCellContents($row, $column, $log->getUsername());
  909. $row++;
  910. }
  911. $form->addElement('label', null, $html.$table->toHtml());
  912. }
  913. }
  914. }
  915. */
  916. if (!$admin_permissions) {
  917. if ($field_details['visible'] == 0) {
  918. $form->freeze('extra_' . $field_details['variable']);
  919. }
  920. }
  921. break;
  922. case ExtraField::FIELD_TYPE_SELECT_MULTIPLE:
  923. $options = array();
  924. foreach ($field_details['options'] as $option_id => $option_details) {
  925. $options[$option_details['option_value']] = $option_details['display_text'];
  926. }
  927. $form->addElement(
  928. 'select',
  929. 'extra_'.$field_details['variable'],
  930. $field_details['display_text'],
  931. $options,
  932. array('multiple' => 'multiple')
  933. );
  934. if (!$admin_permissions) {
  935. if ($field_details['visible'] == 0) {
  936. $form->freeze('extra_'.$field_details['variable']);
  937. }
  938. }
  939. break;
  940. case ExtraField::FIELD_TYPE_DATE:
  941. $form->addDatePicker('extra_'.$field_details['variable'], $field_details['display_text']);
  942. if (!$admin_permissions) {
  943. if ($field_details['visible'] == 0) {
  944. $form->freeze('extra_'.$field_details['variable']);
  945. }
  946. }
  947. $form->applyFilter('theme', 'trim');
  948. break;
  949. case ExtraField::FIELD_TYPE_DATETIME:
  950. $form->addDateTimePicker(
  951. 'extra_'.$field_details['variable'],
  952. $field_details['display_text']
  953. );
  954. $defaults['extra_'.$field_details['variable']] = api_get_local_time();
  955. if (!isset($form->_defaultValues['extra_'.$field_details['variable']])) {
  956. $form->setDefaults($defaults);
  957. }
  958. if (!$admin_permissions) {
  959. if ($field_details['visible'] == 0) {
  960. $form->freeze('extra_'.$field_details['variable']);
  961. }
  962. }
  963. $form->applyFilter('theme', 'trim');
  964. break;
  965. case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
  966. $first_select_id = 'first_extra_'.$field_details['variable'];
  967. $url = api_get_path(WEB_AJAX_PATH).'extra_field.ajax.php?1=1';
  968. $jquery_ready_content .= '
  969. $("#'.$first_select_id.'").on("change", function() {
  970. var id = $(this).val();
  971. if (id) {
  972. $.ajax({
  973. url: "'.$url.'&a=get_second_select_options",
  974. dataType: "json",
  975. data: "type='.$type.'&field_id='.$field_details['id'].'&option_value_id="+id,
  976. success: function(data) {
  977. $("#second_extra_'.$field_details['variable'].'").empty();
  978. $.each(data, function(index, value) {
  979. $("#second_extra_'.$field_details['variable'].'").append($("<option/>", {
  980. value: index,
  981. text: value
  982. }));
  983. });
  984. },
  985. });
  986. } else {
  987. $("#second_extra_'.$field_details['variable'].'").empty();
  988. }
  989. });';
  990. $first_id = null;
  991. $second_id = null;
  992. if (!empty($extraData)) {
  993. $first_id = $extraData['extra_'.$field_details['variable']]['extra_'.$field_details['variable']];
  994. $second_id = $extraData['extra_'.$field_details['variable']]['extra_'.$field_details['variable'].'_second'];
  995. }
  996. $options = ExtraField::extra_field_double_select_convert_array_to_ordered_array(
  997. $field_details['options']
  998. );
  999. $values = array('' => get_lang('Select'));
  1000. $second_values = array();
  1001. if (!empty($options)) {
  1002. foreach ($options as $option) {
  1003. foreach ($option as $sub_option) {
  1004. if ($sub_option['option_value'] == '0') {
  1005. $values[$sub_option['id']] = $sub_option['display_text'];
  1006. } else {
  1007. if ($first_id === $sub_option['option_value']) {
  1008. $second_values[$sub_option['id']] = $sub_option['display_text'];
  1009. }
  1010. }
  1011. }
  1012. }
  1013. }
  1014. $group = array();
  1015. $group[] = $form->createElement(
  1016. 'select',
  1017. 'extra_'.$field_details['variable'],
  1018. null,
  1019. $values,
  1020. array('id' => $first_select_id)
  1021. );
  1022. $group[] = $form->createElement(
  1023. 'select',
  1024. 'extra_'.$field_details['variable'].'_second',
  1025. null,
  1026. $second_values,
  1027. array('id' => 'second_extra_'.$field_details['variable'])
  1028. );
  1029. $form->addGroup(
  1030. $group,
  1031. 'extra_'.$field_details['variable'],
  1032. $field_details['display_text'],
  1033. '&nbsp;'
  1034. );
  1035. if (!$admin_permissions) {
  1036. if ($field_details['visible'] == 0) {
  1037. $form->freeze('extra_'.$field_details['variable']);
  1038. }
  1039. }
  1040. break;
  1041. case ExtraField::FIELD_TYPE_DIVIDER:
  1042. $form->addElement(
  1043. 'static',
  1044. $field_details['variable'],
  1045. '<br /><strong>'.$field_details['display_text'].'</strong>'
  1046. );
  1047. break;
  1048. case ExtraField::FIELD_TYPE_TAG:
  1049. $variable = $field_details['variable'];
  1050. $field_id = $field_details['id'];
  1051. if ($this->type == 'user') {
  1052. /* //the magic should be here
  1053. $user_tags = UserManager::get_user_tags($user_id, $field_details[0]);
  1054. $tag_list = '';
  1055. if (is_array($user_tags) && count($user_tags) > 0) {
  1056. foreach ($user_tags as $tag) {
  1057. $tag_list .= '<option value="'.$tag['tag'].'" class="selected">'.$tag['tag'].'</option>';
  1058. }
  1059. }
  1060. $multi_select = '<select id="extra_'.$field_details[1].'" name="extra_'.$field_details[1].'">
  1061. '.$tag_list.'
  1062. </select>';
  1063. $form->addElement('label', $field_details[3], $multi_select);
  1064. $url = api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php';
  1065. $complete_text = get_lang('StartToType');
  1066. //if cache is set to true the jquery will be called 1 time
  1067. $jquery_ready_content = <<<EOF
  1068. $("#extra_$field_details[1]").fcbkcomplete({
  1069. json_url: "$url?a=search_tags&field_id=$field_details[0]",
  1070. cache: false,
  1071. filter_case: true,
  1072. filter_hide: true,
  1073. complete_text:"$complete_text",
  1074. firstselected: true,
  1075. //onremove: "testme",
  1076. //onselect: "testme",
  1077. filter_selected: true,
  1078. newel: true
  1079. });
  1080. EOF;
  1081. break;*/
  1082. // The magic should be here
  1083. $user_tags = UserManager::get_user_tags($itemId, $field_details['id']);
  1084. $tag_list = '';
  1085. if (is_array($user_tags) && count($user_tags) > 0) {
  1086. foreach ($user_tags as $tag) {
  1087. $tag_list .= '<option value="'.$tag['tag'].'" class="selected">'.$tag['tag'].'</option>';
  1088. }
  1089. }
  1090. $url = api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php';
  1091. } else {
  1092. $tag_list = '';
  1093. $em = Database::getManager();
  1094. $fieldTags = $em
  1095. ->getRepository('ChamiloCoreBundle:ExtraFieldRelTag')
  1096. ->findBy([
  1097. 'fieldId' => $field_id,
  1098. 'itemId' => $itemId
  1099. ]);
  1100. foreach ($fieldTags as $fieldTag) {
  1101. $tag = $em->find('ChamiloCoreBundle:Tag', $fieldTag->getTagId());
  1102. if (empty($tag)) {
  1103. continue;
  1104. }
  1105. $tag_list .= Display::tag(
  1106. 'option',
  1107. $tag->getTag(),
  1108. [
  1109. 'value' => $tag->getTag(),
  1110. 'class' => 'selected'
  1111. ]
  1112. );
  1113. }
  1114. $url = api_get_path(WEB_AJAX_PATH).'extra_field.ajax.php';
  1115. }
  1116. $form->addElement('hidden', 'extra_'.$field_details['variable'].'__persist__', 1);
  1117. $multiSelect = '<select id="extra_'.$field_details['variable'].'" name="extra_'.$field_details['variable'].'">
  1118. '.$tag_list.'
  1119. </select>';
  1120. $form->addElement('label', $field_details['display_text'], $multiSelect);
  1121. $complete_text = get_lang('StartToType');
  1122. //if cache is set to true the jquery will be called 1 time
  1123. $jquery_ready_content .= <<<EOF
  1124. $("#extra_$variable").fcbkcomplete({
  1125. json_url: "$url?a=search_tags&field_id=$field_id&type={$this->type}",
  1126. cache: false,
  1127. filter_case: true,
  1128. filter_hide: true,
  1129. complete_text:"$complete_text",
  1130. firstselected: false,
  1131. filter_selected: true,
  1132. newel: true
  1133. });
  1134. EOF;
  1135. break;
  1136. case ExtraField::FIELD_TYPE_TIMEZONE:
  1137. $form->addElement(
  1138. 'select',
  1139. 'extra_'.$field_details['variable'],
  1140. $field_details['display_text'],
  1141. api_get_timezones(),
  1142. ''
  1143. );
  1144. if ($field_details['visible'] == 0) {
  1145. $form->freeze(
  1146. 'extra_'.$field_details['variable']
  1147. );
  1148. }
  1149. break;
  1150. case ExtraField::FIELD_TYPE_SOCIAL_PROFILE:
  1151. // get the social network's favicon
  1152. $icon_path = UserManager::get_favicon_from_url(
  1153. $extraData['extra_'.$field_details['variable']],
  1154. $field_details['field_default_value']
  1155. );
  1156. // special hack for hi5
  1157. $leftpad = '1.7';
  1158. $top = '0.4';
  1159. $domain = parse_url($icon_path, PHP_URL_HOST);
  1160. if ($domain == 'www.hi5.com' or $domain == 'hi5.com') {
  1161. $leftpad = '3';
  1162. $top = '0';
  1163. }
  1164. // print the input field
  1165. $form->addElement(
  1166. 'text',
  1167. 'extra_'.$field_details['variable'],
  1168. $field_details['display_text'],
  1169. array(
  1170. 'size' => 60,
  1171. 'style' => 'background-image: url(\''.$icon_path.'\'); background-repeat: no-repeat; background-position: 0.4em '.$top.'em; padding-left: '.$leftpad.'em; '
  1172. )
  1173. );
  1174. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  1175. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  1176. if ($field_details['visible'] == 0) {
  1177. $form->freeze('extra_'.$field_details['variable']);
  1178. }
  1179. break;
  1180. case ExtraField::FIELD_TYPE_MOBILE_PHONE_NUMBER:
  1181. $form->addElement(
  1182. 'text',
  1183. 'extra_'.$field_details[1],
  1184. $field_details[3]." (".get_lang('CountryDialCode').")",
  1185. array('size' => 40, 'placeholder' => '(xx)xxxxxxxxx')
  1186. );
  1187. $form->applyFilter('extra_'.$field_details[1], 'stripslashes');
  1188. $form->applyFilter('extra_'.$field_details[1], 'trim');
  1189. $form->applyFilter('extra_'.$field_details[1], 'mobile_phone_number_filter');
  1190. $form->addRule(
  1191. 'extra_'.$field_details[1],
  1192. get_lang('MobilePhoneNumberWrong'),
  1193. 'mobile_phone_number'
  1194. );
  1195. if ($field_details['visible'] == 0) {
  1196. $form->freeze('extra_'.$field_details['variable']);
  1197. }
  1198. break;
  1199. case ExtraField::FIELD_TYPE_INTEGER:
  1200. $form->addElement(
  1201. 'number',
  1202. 'extra_'.$field_details['variable'],
  1203. $field_details['display_text'],
  1204. array('class' => 'span1', 'step' => 1)
  1205. );
  1206. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  1207. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  1208. $form->applyFilter('extra_'.$field_details['variable'], 'intval');
  1209. if (!$admin_permissions) {
  1210. if ($field_details['visible'] == 0) {
  1211. $form->freeze(
  1212. 'extra_'.$field_details['variable']
  1213. );
  1214. }
  1215. }
  1216. break;
  1217. case ExtraField::FIELD_TYPE_FILE_IMAGE:
  1218. $fieldVariable = "extra_{$field_details['variable']}";
  1219. $fieldTexts = array(
  1220. $field_details['display_text']
  1221. );
  1222. if (is_array($extraData) && array_key_exists($fieldVariable, $extraData)) {
  1223. if (file_exists(api_get_path(SYS_UPLOAD_PATH) . $extraData[$fieldVariable])) {
  1224. $fieldTexts[] = Display::img(
  1225. api_get_path(WEB_UPLOAD_PATH) . $extraData[$fieldVariable],
  1226. $field_details['display_text'],
  1227. array('width' => '300')
  1228. );
  1229. }
  1230. }
  1231. $form->addElement(
  1232. 'file',
  1233. $fieldVariable,
  1234. $fieldTexts,
  1235. array('accept' => 'image/*')
  1236. );
  1237. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  1238. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  1239. $allowed_picture_types = array ('jpg', 'jpeg', 'png', 'gif');
  1240. $form->addRule(
  1241. 'extra_'.$field_details['variable'],
  1242. get_lang('OnlyImagesAllowed') . ' ('.implode(',', $allowed_picture_types).')',
  1243. 'filetype',
  1244. $allowed_picture_types
  1245. );
  1246. if (!$admin_permissions) {
  1247. if ($field_details['visible'] == 0) {
  1248. $form->freeze(
  1249. 'extra_'.$field_details['variable']
  1250. );
  1251. }
  1252. }
  1253. break;
  1254. case ExtraField::FIELD_TYPE_FLOAT:
  1255. $form->addElement(
  1256. 'number',
  1257. 'extra_'.$field_details['variable'],
  1258. $field_details['display_text'],
  1259. array('class' => 'span1', 'step' => '0.01')
  1260. );
  1261. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  1262. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  1263. $form->applyFilter('extra_'.$field_details['variable'], 'floatval');
  1264. if (!$admin_permissions) {
  1265. if ($field_details['visible'] == 0) {
  1266. $form->freeze(
  1267. 'extra_'.$field_details['variable']
  1268. );
  1269. }
  1270. }
  1271. break;
  1272. case ExtraField::FIELD_TYPE_FILE:
  1273. $fieldVariable = "extra_{$field_details['variable']}";
  1274. $fieldTexts = array(
  1275. $field_details['display_text']
  1276. );
  1277. if (is_array($extraData) &&
  1278. array_key_exists($fieldVariable, $extraData)
  1279. ) {
  1280. if (file_exists(api_get_path(SYS_UPLOAD_PATH) . $extraData[$fieldVariable])) {
  1281. $fieldTexts[] = Display::url(
  1282. api_get_path(WEB_UPLOAD_PATH) . $extraData[$fieldVariable],
  1283. api_get_path(WEB_UPLOAD_PATH) . $extraData[$fieldVariable],
  1284. array(
  1285. 'title' => $field_details['display_text'],
  1286. 'target' => '_blank'
  1287. )
  1288. );
  1289. }
  1290. }
  1291. $form->addElement(
  1292. 'file',
  1293. $fieldVariable,
  1294. $fieldTexts,
  1295. array()
  1296. );
  1297. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  1298. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  1299. if (!$admin_permissions) {
  1300. if ($field_details['visible'] == 0) {
  1301. $form->freeze(
  1302. 'extra_'.$field_details['variable']
  1303. );
  1304. }
  1305. }
  1306. break;
  1307. case ExtraField::FIELD_TYPE_VIDEO_URL:
  1308. $form->addUrl(
  1309. "extra_{$field_details['variable']}",
  1310. $field_details['display_text'],
  1311. false,
  1312. ['placeholder' => 'https://']
  1313. );
  1314. break;
  1315. case ExtraField::FIELD_TYPE_LETTERS_ONLY:
  1316. $form->addTextLettersOnly(
  1317. "extra_{$field_details['variable']}",
  1318. $field_details['display_text']
  1319. );
  1320. $form->applyFilter('extra_' . $field_details['variable'], 'stripslashes');
  1321. if (!$admin_permissions) {
  1322. if ($field_details['visible'] == 0) {
  1323. $form->freeze(
  1324. 'extra_' . $field_details['variable']
  1325. );
  1326. }
  1327. }
  1328. break;
  1329. case ExtraField::FIELD_TYPE_ALPHANUMERIC:
  1330. $form->addTextAlphanumeric(
  1331. "extra_{$field_details['variable']}",
  1332. $field_details['display_text']
  1333. );
  1334. $form->applyFilter(
  1335. 'extra_' . $field_details['variable'],
  1336. 'stripslashes'
  1337. );
  1338. if (!$admin_permissions) {
  1339. if ($field_details['visible'] == 0) {
  1340. $form->freeze(
  1341. 'extra_' . $field_details['variable']
  1342. );
  1343. }
  1344. }
  1345. break;
  1346. case ExtraField::FIELD_TYPE_LETTERS_SPACE:
  1347. $form->addTextLettersAndSpaces(
  1348. "extra_{$field_details['variable']}",
  1349. $field_details['display_text']
  1350. );
  1351. $form->applyFilter('extra_' . $field_details['variable'], 'stripslashes');
  1352. if (!$admin_permissions) {
  1353. if ($field_details['visible'] == 0) {
  1354. $form->freeze(
  1355. 'extra_' . $field_details['variable']
  1356. );
  1357. }
  1358. }
  1359. break;
  1360. case ExtraField::FIELD_TYPE_ALPHANUMERIC_SPACE:
  1361. $form->addTextAlphanumericAndSpaces(
  1362. "extra_{$field_details['variable']}",
  1363. $field_details['display_text']
  1364. );
  1365. $form->applyFilter(
  1366. 'extra_' . $field_details['variable'],
  1367. 'stripslashes'
  1368. );
  1369. if (!$admin_permissions) {
  1370. if ($field_details['visible'] == 0) {
  1371. $form->freeze(
  1372. 'extra_' . $field_details['variable']
  1373. );
  1374. }
  1375. }
  1376. break;
  1377. }
  1378. }
  1379. }
  1380. $return = array();
  1381. $return['jquery_ready_content'] = $jquery_ready_content;
  1382. return $return;
  1383. }
  1384. function setupBreadcrumb(&$breadcrumb, $action)
  1385. {
  1386. if ($action == 'add') {
  1387. $breadcrumb[] = array('url' => $this->pageUrl, 'name' => $this->pageName);
  1388. $breadcrumb[] = array('url' => '#', 'name' => get_lang('Add'));
  1389. } elseif ($action == 'edit') {
  1390. $breadcrumb[] = array('url' => $this->pageUrl, 'name' => $this->pageName);
  1391. $breadcrumb[] = array('url' => '#', 'name' => get_lang('Edit'));
  1392. } else {
  1393. $breadcrumb[] = array('url' => '#', 'name' => $this->pageName);
  1394. }
  1395. }
  1396. /**
  1397. * Displays the title + grid
  1398. */
  1399. public function display()
  1400. {
  1401. // action links
  1402. echo '<div class="actions">';
  1403. echo '<a href="../admin/index.php">'.Display::return_icon(
  1404. 'back.png',
  1405. get_lang('BackTo').' '.get_lang('PlatformAdmin'),
  1406. '',
  1407. ICON_SIZE_MEDIUM
  1408. ).'</a>';
  1409. echo '<a href="'.api_get_self().'?action=add&type='.$this->type.'">'.Display::return_icon(
  1410. 'add_user_fields.png',
  1411. get_lang('Add'),
  1412. '',
  1413. ICON_SIZE_MEDIUM
  1414. ).'</a>';
  1415. echo '</div>';
  1416. echo Display::grid_html($this->type.'_fields');
  1417. }
  1418. /**
  1419. * @return array
  1420. */
  1421. public function getJqgridColumnNames()
  1422. {
  1423. return array(
  1424. get_lang('Name'),
  1425. get_lang('FieldLabel'),
  1426. get_lang('Type'),
  1427. get_lang('FieldChangeability'),
  1428. get_lang('Visibility'),
  1429. get_lang('Filter'),
  1430. get_lang('FieldOrder'),
  1431. get_lang('Actions')
  1432. );
  1433. }
  1434. /**
  1435. * @return array
  1436. */
  1437. public function getJqgridColumnModel()
  1438. {
  1439. return array(
  1440. array(
  1441. 'name' => 'display_text',
  1442. 'index' => 'display_text',
  1443. 'width' => '180',
  1444. 'align' => 'left',
  1445. ),
  1446. array(
  1447. 'name' => 'variable',
  1448. 'index' => 'variable',
  1449. 'width' => '',
  1450. 'align' => 'left',
  1451. 'sortable' => 'true'
  1452. ),
  1453. array(
  1454. 'name' => 'field_type',
  1455. 'index' => 'field_type',
  1456. 'width' => '',
  1457. 'align' => 'left',
  1458. 'sortable' => 'true'
  1459. ),
  1460. array(
  1461. 'name' => 'changeable',
  1462. 'index' => 'changeable',
  1463. 'width' => '50',
  1464. 'align' => 'left',
  1465. 'sortable' => 'true'
  1466. ),
  1467. array(
  1468. 'name' => 'visible',
  1469. 'index' => 'visible',
  1470. 'width' => '40',
  1471. 'align' => 'left',
  1472. 'sortable' => 'true'
  1473. ),
  1474. array(
  1475. 'name' => 'filter',
  1476. 'index' => 'filter',
  1477. 'width' => '30',
  1478. 'align' => 'left',
  1479. 'sortable' => 'true'
  1480. ),
  1481. array(
  1482. 'name' => 'field_order',
  1483. 'index' => 'field_order',
  1484. 'width' => '40',
  1485. 'align' => 'left',
  1486. 'sortable' => 'true'
  1487. ),
  1488. array(
  1489. 'name' => 'actions',
  1490. 'index' => 'actions',
  1491. 'width' => '100',
  1492. 'align' => 'left',
  1493. 'formatter' => 'action_formatter',
  1494. 'sortable' => 'false'
  1495. )
  1496. );
  1497. }
  1498. /**
  1499. * @param string $url
  1500. * @param string $action
  1501. * @return FormValidator
  1502. */
  1503. public function return_form($url, $action)
  1504. {
  1505. $form = new FormValidator($this->type.'_field', 'post', $url);
  1506. $form->addElement('hidden', 'type', $this->type);
  1507. $id = isset($_GET['id']) ? intval($_GET['id']) : null;
  1508. $form->addElement('hidden', 'id', $id);
  1509. // Setting the form elements
  1510. $header = get_lang('Add');
  1511. $defaults = array();
  1512. if ($action == 'edit') {
  1513. $header = get_lang('Modify');
  1514. // Setting the defaults
  1515. $defaults = $this->get($id);
  1516. }
  1517. $form->addElement('header', $header);
  1518. $form->addElement('text', 'display_text', get_lang('Name'), array('class' => 'span5'));
  1519. // Field type
  1520. $types = self::get_field_types();
  1521. $form->addElement(
  1522. 'select',
  1523. 'field_type',
  1524. get_lang('FieldType'),
  1525. $types,
  1526. array('id' => 'field_type', 'class' => 'chzn-select', 'data-placeholder' => get_lang('Select'))
  1527. );
  1528. $form->addElement('label', get_lang('Example'), '<div id="example">-</div>');
  1529. $form->addElement('text', 'variable', get_lang('FieldLabel'), array('class' => 'span5'));
  1530. $form->addElement(
  1531. 'text',
  1532. 'field_options',
  1533. get_lang('FieldPossibleValues'),
  1534. array('id' => 'field_options', 'class' => 'span6')
  1535. );
  1536. $fieldWithOptions = array(
  1537. ExtraField::FIELD_TYPE_RADIO,
  1538. ExtraField::FIELD_TYPE_SELECT_MULTIPLE,
  1539. ExtraField::FIELD_TYPE_SELECT,
  1540. ExtraField::FIELD_TYPE_TAG,
  1541. ExtraField::FIELD_TYPE_DOUBLE_SELECT,
  1542. );
  1543. if ($action == 'edit') {
  1544. if (in_array($defaults['field_type'], $fieldWithOptions)) {
  1545. $url = Display::url(
  1546. get_lang('EditExtraFieldOptions'),
  1547. 'extra_field_options.php?type='.$this->type.'&field_id='.$id
  1548. );
  1549. $form->addElement('label', null, $url);
  1550. if ($defaults['field_type'] == ExtraField::FIELD_TYPE_SELECT) {
  1551. $urlWorkFlow = Display::url(
  1552. get_lang('EditExtraFieldWorkFlow'),
  1553. 'extra_field_workflow.php?type='.$this->type.'&field_id='.$id
  1554. );
  1555. $form->addElement('label', null, $urlWorkFlow);
  1556. }
  1557. $form->freeze('field_options');
  1558. }
  1559. }
  1560. $form->addElement(
  1561. 'text',
  1562. 'default_value',
  1563. get_lang('FieldDefaultValue'),
  1564. array('id' => 'default_value')
  1565. );
  1566. $group = array();
  1567. $group[] = $form->createElement('radio', 'visible', null, get_lang('Yes'), 1);
  1568. $group[] = $form->createElement('radio', 'visible', null, get_lang('No'), 0);
  1569. $form->addGroup($group, '', get_lang('Visible'), '', false);
  1570. $group = array();
  1571. $group[] = $form->createElement('radio', 'changeable', null, get_lang('Yes'), 1);
  1572. $group[] = $form->createElement('radio', 'changeable', null, get_lang('No'), 0);
  1573. $form->addGroup($group, '', get_lang('FieldChangeability'), '', false);
  1574. $group = array();
  1575. $group[] = $form->createElement('radio', 'filter', null, get_lang('Yes'), 1);
  1576. $group[] = $form->createElement('radio', 'filter', null, get_lang('No'), 0);
  1577. $form->addGroup($group, '', get_lang('FieldFilter'), '', false);
  1578. /* Enable this when field_loggeable is introduced as a table field (2.0)
  1579. $group = array();
  1580. $group[] = $form->createElement('radio', 'field_loggeable', null, get_lang('Yes'), 1);
  1581. $group[] = $form->createElement('radio', 'field_loggeable', null, get_lang('No'), 0);
  1582. $form->addGroup($group, '', get_lang('FieldLoggeable'), '', false);
  1583. */
  1584. $form->addElement('text', 'field_order', get_lang('FieldOrder'), array('class' => 'span1'));
  1585. if ($action == 'edit') {
  1586. $option = new ExtraFieldOption($this->type);
  1587. if ($defaults['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) {
  1588. $form->freeze('field_options');
  1589. }
  1590. $defaults['field_options'] = $option->get_field_options_by_field_to_string($id);
  1591. $form->addButtonUpdate(get_lang('Modify'));
  1592. } else {
  1593. $defaults['visible'] = 0;
  1594. $defaults['changeable'] = 0;
  1595. $defaults['filter'] = 0;
  1596. $form->addButtonCreate(get_lang('Add'));
  1597. }
  1598. /*if (!empty($defaults['created_at'])) {
  1599. $defaults['created_at'] = api_convert_and_format_date($defaults['created_at']);
  1600. }
  1601. if (!empty($defaults['updated_at'])) {
  1602. $defaults['updated_at'] = api_convert_and_format_date($defaults['updated_at']);
  1603. }*/
  1604. $form->setDefaults($defaults);
  1605. // Setting the rules
  1606. $form->addRule('display_text', get_lang('ThisFieldIsRequired'), 'required');
  1607. $form->addRule('field_type', get_lang('ThisFieldIsRequired'), 'required');
  1608. return $form;
  1609. }
  1610. /**
  1611. * @param $token
  1612. * @return string
  1613. */
  1614. public function getJqgridActionLinks($token)
  1615. {
  1616. //With this function we can add actions to the jgrid (edit, delete, etc)
  1617. return 'function action_formatter(cellvalue, options, rowObject) {
  1618. return \'<a href="?action=edit&type='.$this->type.'&id=\'+options.rowId+\'">'.Display::return_icon(
  1619. 'edit.png',
  1620. get_lang('Edit'),
  1621. '',
  1622. ICON_SIZE_SMALL
  1623. ).'</a>'.
  1624. '&nbsp;<a onclick="javascript:if(!confirm('."\'".addslashes(
  1625. api_htmlentities(get_lang("ConfirmYourChoice"), ENT_QUOTES)
  1626. )."\'".')) return false;" href="?sec_token='.$token.'&type='.$this->type.'&action=delete&id=\'+options.rowId+\'">'.Display::return_icon(
  1627. 'delete.png',
  1628. get_lang('Delete'),
  1629. '',
  1630. ICON_SIZE_SMALL
  1631. ).'</a>'.
  1632. '\';
  1633. }';
  1634. }
  1635. /**
  1636. * @param array $columns
  1637. * @param array $column_model
  1638. * @param array $extraFields
  1639. * @return array
  1640. */
  1641. public function getRules(&$columns, &$column_model, $extraFields = array(), $checkExtraFieldExistence = false)
  1642. {
  1643. $fields = $this->get_all(
  1644. array(
  1645. 'visible = ? AND filter = ?' => array(1, 1)
  1646. ),
  1647. 'display_text'
  1648. );
  1649. $extraFieldOption = new ExtraFieldOption($this->type);
  1650. $rules = array();
  1651. if (!empty($fields)) {
  1652. foreach ($fields as $field) {
  1653. $search_options = array();
  1654. $type = 'text';
  1655. if (in_array($field['field_type'], array(self::FIELD_TYPE_SELECT, self::FIELD_TYPE_DOUBLE_SELECT))) {
  1656. $type = 'select';
  1657. $search_options['sopt'] = array('eq', 'ne'); //equal not equal
  1658. } else {
  1659. $search_options['sopt'] = array('cn', 'nc'); //contains not contains
  1660. }
  1661. $search_options['searchhidden'] = 'true';
  1662. $search_options['defaultValue'] = isset($search_options['field_default_value']) ? $search_options['field_default_value'] : null;
  1663. if ($field['field_type'] == self::FIELD_TYPE_DOUBLE_SELECT) {
  1664. //Add 2 selects
  1665. $options = $extraFieldOption->get_field_options_by_field($field['id']);
  1666. $options = self::extra_field_double_select_convert_array_to_ordered_array($options);
  1667. $first_options = array();
  1668. if (!empty($options)) {
  1669. foreach ($options as $option) {
  1670. foreach ($option as $sub_option) {
  1671. if ($sub_option['option_value'] == 0) {
  1672. $first_options[] = $sub_option['field_id'].'#'.$sub_option['id'].':'.$sub_option['display_text'];
  1673. }
  1674. }
  1675. }
  1676. }
  1677. $search_options['value'] = implode(';', $first_options);
  1678. $search_options['dataInit'] = 'fill_second_select';
  1679. // First
  1680. $column_model[] = array(
  1681. 'name' => 'extra_'.$field['variable'],
  1682. 'index' => 'extra_'.$field['variable'],
  1683. 'width' => '100',
  1684. 'hidden' => 'true',
  1685. 'search' => 'true',
  1686. 'stype' => 'select',
  1687. 'searchoptions' => $search_options,
  1688. );
  1689. $columns[] = $field['display_text'].' (1)';
  1690. $rules[] = array(
  1691. 'field' => 'extra_'.$field['variable'],
  1692. 'op' => 'cn',
  1693. );
  1694. //Second
  1695. $search_options['value'] = $field['id'].':';
  1696. $search_options['dataInit'] = 'register_second_select';
  1697. $column_model[] = array(
  1698. 'name' => 'extra_'.$field['variable'].'_second',
  1699. 'index' => 'extra_'.$field['variable'].'_second',
  1700. 'width' => '100',
  1701. 'hidden' => 'true',
  1702. 'search' => 'true',
  1703. 'stype' => 'select',
  1704. 'searchoptions' => $search_options,
  1705. );
  1706. $columns[] = $field['display_text'].' (2)';
  1707. $rules[] = array('field' => 'extra_'.$field['variable'].'_second', 'op' => 'cn');
  1708. continue;
  1709. } else {
  1710. $search_options['value'] = $extraFieldOption->getFieldOptionsToString(
  1711. $field['id'],
  1712. false,
  1713. 'display_text'
  1714. );
  1715. }
  1716. $column_model[] = array(
  1717. 'name' => 'extra_'.$field['variable'],
  1718. 'index' => 'extra_'.$field['variable'],
  1719. 'width' => '100',
  1720. 'hidden' => 'true',
  1721. 'search' => 'true',
  1722. 'stype' => $type,
  1723. 'searchoptions' => $search_options
  1724. );
  1725. $columns[] = $field['display_text'];
  1726. $rules[] = array('field' => 'extra_'.$field['variable'], 'op' => 'cn');
  1727. }
  1728. }
  1729. return $rules;
  1730. }
  1731. /**
  1732. * @param array $options
  1733. * @return array
  1734. */
  1735. public function parseConditions($options)
  1736. {
  1737. $inject_extra_fields = null;
  1738. $extraFieldOption = new ExtraFieldOption($this->type);
  1739. $double_fields = array();
  1740. if (isset($options['extra'])) {
  1741. $extra_fields = $options['extra'];
  1742. if (!empty($extra_fields)) {
  1743. $counter = 1;
  1744. foreach ($extra_fields as &$extra) {
  1745. $extra_field_obj = new ExtraField($this->type);
  1746. $extra_field_info = $extra_field_obj->get($extra['id']);
  1747. $extra['extra_field_info'] = $extra_field_info;
  1748. if (isset($extra_field_info['field_type']) && in_array(
  1749. $extra_field_info['field_type'],
  1750. array(
  1751. ExtraField::FIELD_TYPE_SELECT,
  1752. ExtraField::FIELD_TYPE_SELECT,
  1753. ExtraField::FIELD_TYPE_DOUBLE_SELECT
  1754. )
  1755. )
  1756. ) {
  1757. $inject_extra_fields .= " fvo$counter.display_text as {$extra['field']}, ";
  1758. } else {
  1759. $inject_extra_fields .= " fv$counter.value as {$extra['field']}, ";
  1760. }
  1761. if (isset($extra_fields_info[$extra['id']])) {
  1762. $info = $extra_fields_info[$extra['id']];
  1763. } else {
  1764. $info = $this->get($extra['id']);
  1765. $extra_fields_info[$extra['id']] = $info;
  1766. }
  1767. if (isset($info['field_type']) && $info['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) {
  1768. $double_fields[$info['id']] = $info;
  1769. }
  1770. $counter++;
  1771. }
  1772. }
  1773. }
  1774. $options_by_double = array();
  1775. foreach ($double_fields as $double) {
  1776. $my_options = $extraFieldOption->get_field_options_by_field(
  1777. $double['id'],
  1778. true
  1779. );
  1780. $options_by_double['extra_'.$double['variable']] = $my_options;
  1781. }
  1782. $field_value_to_join = array();
  1783. //filter can be all/any = and/or
  1784. $inject_joins = null;
  1785. $inject_where = null;
  1786. $where = null;
  1787. if (!empty($options['where'])) {
  1788. if (!empty($options['extra'])) {
  1789. // Removing double 1=1
  1790. $options['where'] = str_replace(' 1 = 1 AND', '', $options['where']);
  1791. // Always OR
  1792. $counter = 1;
  1793. foreach ($extra_fields as $extra_info) {
  1794. $extra_field_info = $extra_info['extra_field_info'];
  1795. $inject_joins .= " INNER JOIN $this->table_field_values fv$counter
  1796. ON (s.".$this->primaryKey." = fv$counter.".$this->handler_id.") ";
  1797. // Add options
  1798. if (isset($extra_field_info['field_type']) && in_array(
  1799. $extra_field_info['field_type'],
  1800. array(
  1801. ExtraField::FIELD_TYPE_SELECT,
  1802. ExtraField::FIELD_TYPE_SELECT,
  1803. ExtraField::FIELD_TYPE_DOUBLE_SELECT
  1804. )
  1805. )
  1806. ) {
  1807. $options['where'] = str_replace(
  1808. $extra_info['field'],
  1809. 'fv'.$counter.'.field_id = '.$extra_info['id'].' AND fvo'.$counter.'.option_value',
  1810. $options['where']
  1811. );
  1812. $inject_joins .= " INNER JOIN $this->table_field_options fvo$counter ".
  1813. " ON (fv$counter.field_id = fvo$counter.field_id AND fv$counter.value = fvo$counter.option_value) ";
  1814. } else {
  1815. //text, textarea, etc
  1816. $options['where'] = str_replace(
  1817. $extra_info['field'],
  1818. 'fv'.$counter.'.field_id = '.$extra_info['id'].' AND fv'.$counter.'.value',
  1819. $options['where']
  1820. );
  1821. }
  1822. $field_value_to_join[] = " fv$counter.$this->handler_id ";
  1823. $counter++;
  1824. }
  1825. if (!empty($field_value_to_join)) {
  1826. //$inject_where .= " AND s.id = ".implode(' = ', $field_value_to_join);
  1827. }
  1828. }
  1829. $where .= ' AND '.$options['where'];
  1830. }
  1831. $order = null;
  1832. if (!empty($options['order'])) {
  1833. $order = " ORDER BY ".$options['order'];
  1834. }
  1835. $limit = null;
  1836. if (!empty($options['limit'])) {
  1837. $limit = " LIMIT ".$options['limit'];
  1838. }
  1839. return array(
  1840. 'order' => $order,
  1841. 'limit' => $limit,
  1842. 'where' => $where,
  1843. 'inject_where' => $inject_where,
  1844. 'inject_joins' => $inject_joins,
  1845. 'field_value_to_join' => $field_value_to_join,
  1846. 'inject_extra_fields' => $inject_extra_fields,
  1847. );
  1848. }
  1849. //@todo move this in the display_class or somewhere else
  1850. /**
  1851. * @param $col
  1852. * @param $oper
  1853. * @param $val
  1854. * @return string
  1855. */
  1856. public function get_where_clause($col, $oper, $val)
  1857. {
  1858. if (empty($col)) {
  1859. return '';
  1860. }
  1861. if ($oper == 'bw' || $oper == 'bn') {
  1862. $val .= '%';
  1863. }
  1864. if ($oper == 'ew' || $oper == 'en') {
  1865. $val = '%'.$val;
  1866. }
  1867. if ($oper == 'cn' || $oper == 'nc' || $oper == 'in' || $oper == 'ni') {
  1868. $val = '%'.$val.'%';
  1869. }
  1870. $val = \Database::escape_string($val);
  1871. return " $col {$this->ops[$oper]} '$val' ";
  1872. }
  1873. /**
  1874. * @param $filters
  1875. * @param string $stringToSearch
  1876. * @return array
  1877. */
  1878. public function getExtraFieldRules($filters, $stringToSearch = 'extra_')
  1879. {
  1880. $extra_fields = array();
  1881. // Getting double select if exists
  1882. $double_select = array();
  1883. foreach ($filters->rules as $rule) {
  1884. if (strpos($rule->field, '_second') === false) {
  1885. } else {
  1886. $my_field = str_replace('_second', '', $rule->field);
  1887. $double_select[$my_field] = $rule->data;
  1888. }
  1889. }
  1890. $condition_array = array();
  1891. foreach ($filters->rules as $rule) {
  1892. if (strpos($rule->field, $stringToSearch) === false) {
  1893. //normal fields
  1894. $field = $rule->field;
  1895. if (isset($rule->data) && $rule->data != -1) {
  1896. $condition_array[] = $this->get_where_clause($field, $rule->op, $rule->data);
  1897. }
  1898. } else {
  1899. // Extra fields
  1900. if (strpos($rule->field, '_second') === false) {
  1901. //No _second
  1902. $original_field = str_replace($stringToSearch, '', $rule->field);
  1903. $field_option = $this->get_handler_field_info_by_variable($original_field);
  1904. if ($field_option['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) {
  1905. if (isset($double_select[$rule->field])) {
  1906. $data = explode('#', $rule->data);
  1907. $rule->data = $data[1].'::'.$double_select[$rule->field];
  1908. } else {
  1909. // only was sent 1 select
  1910. $data = explode('#', $rule->data);
  1911. $rule->data = $data[1];
  1912. }
  1913. if (!isset($rule->data)) {
  1914. $condition_array[] = ' ('.$this->get_where_clause($rule->field, $rule->op, $rule->data).') ';
  1915. $extra_fields[] = array('field' => $rule->field, 'id' => $field_option['id']);
  1916. }
  1917. } else {
  1918. if (isset($rule->data)) {
  1919. if ($rule->data == -1) {
  1920. continue;
  1921. }
  1922. $condition_array[] = ' ('.$this->get_where_clause($rule->field, $rule->op, $rule->data).') ';
  1923. $extra_fields[] = array(
  1924. 'field' => $rule->field,
  1925. 'id' => $field_option['id'],
  1926. 'data' => $rule->data
  1927. );
  1928. }
  1929. }
  1930. } else {
  1931. $my_field = str_replace('_second', '', $rule->field);
  1932. $original_field = str_replace($stringToSearch, '', $my_field);
  1933. $field_option = $this->get_handler_field_info_by_field_variable($original_field);
  1934. $extra_fields[] = array(
  1935. 'field' => $rule->field,
  1936. 'id' => $field_option['id']
  1937. );
  1938. }
  1939. }
  1940. }
  1941. return array(
  1942. 'extra_fields' => $extra_fields,
  1943. 'condition_array' => $condition_array
  1944. );
  1945. }
  1946. /**
  1947. * Get the extra fields and their formatted values
  1948. * @param int|string $itemId The item ID (It could be a session_id, course_id or user_id)
  1949. * @return array The extra fields data
  1950. */
  1951. public function getDataAndFormattedValues($itemId)
  1952. {
  1953. $valuesData = array();
  1954. $fields = $this->get_all();
  1955. foreach ($fields as $field) {
  1956. if ($field['visible'] != '1') {
  1957. continue;
  1958. }
  1959. $fieldValue = new ExtraFieldValue($this->type);
  1960. $valueData = $fieldValue->get_values_by_handler_and_field_id($itemId, $field['id'], true);
  1961. if (!$valueData) {
  1962. continue;
  1963. }
  1964. $displayedValue = get_lang('None');
  1965. switch ($field['field_type']) {
  1966. case ExtraField::FIELD_TYPE_CHECKBOX:
  1967. if ($valueData !== false && $valueData['value'] == '1') {
  1968. $displayedValue = get_lang('Yes');
  1969. } else {
  1970. $displayedValue = get_lang('No');
  1971. }
  1972. break;
  1973. case ExtraField::FIELD_TYPE_DATE:
  1974. if ($valueData !== false && !empty($valueData['value'])) {
  1975. $displayedValue = api_format_date($valueData['value'], DATE_FORMAT_LONG_NO_DAY);
  1976. }
  1977. break;
  1978. case ExtraField::FIELD_TYPE_FILE_IMAGE:
  1979. if ($valueData === false || empty($valueData['value'])) {
  1980. break;
  1981. }
  1982. if (!file_exists(api_get_path(SYS_UPLOAD_PATH) . $valueData['value'])) {
  1983. break;
  1984. }
  1985. $image = Display::img(
  1986. api_get_path(WEB_UPLOAD_PATH) . $valueData['value'],
  1987. $field['display_text'],
  1988. array('width' => '300')
  1989. );
  1990. $displayedValue = Display::url(
  1991. $image,
  1992. api_get_path(WEB_UPLOAD_PATH) . $valueData['value'],
  1993. array('target' => '_blank')
  1994. );
  1995. break;
  1996. case ExtraField::FIELD_TYPE_FILE:
  1997. if ($valueData === false || empty($valueData['value'])) {
  1998. break;
  1999. }
  2000. if (!file_exists(api_get_path(SYS_UPLOAD_PATH) . $valueData['value'])) {
  2001. break;
  2002. }
  2003. $displayedValue = Display::url(
  2004. get_lang('Download'),
  2005. api_get_path(WEB_UPLOAD_PATH) . $valueData['value'],
  2006. array(
  2007. 'title' => $field['display_text'],
  2008. 'target' => '_blank'
  2009. )
  2010. );
  2011. break;
  2012. default:
  2013. $displayedValue = $valueData['value'];
  2014. break;
  2015. }
  2016. $valuesData[] = array(
  2017. 'text' => $field['display_text'],
  2018. 'value' => $displayedValue
  2019. );
  2020. }
  2021. return $valuesData;
  2022. }
  2023. }