extra_field.lib.php 132 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145
  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_to_self',
  17. 'visible_to_others',
  18. 'changeable',
  19. 'filter',
  20. 'extra_field_type',
  21. /* Enable this when field_loggeable is introduced as a table field (2.0)
  22. 'field_loggeable',
  23. */
  24. 'created_at'
  25. );
  26. public $ops = array(
  27. 'eq' => '=', //equal
  28. 'ne' => '<>', //not equal
  29. 'lt' => '<', //less than
  30. 'le' => '<=', //less than or equal
  31. 'gt' => '>', //greater than
  32. 'ge' => '>=', //greater than or equal
  33. 'bw' => 'LIKE', //begins with
  34. 'bn' => 'NOT LIKE', //doesn't begin with
  35. 'in' => 'LIKE', //is in
  36. 'ni' => 'NOT LIKE', //is not in
  37. 'ew' => 'LIKE', //ends with
  38. 'en' => 'NOT LIKE', //doesn't end with
  39. 'cn' => 'LIKE', //contains
  40. 'nc' => 'NOT LIKE' //doesn't contain
  41. );
  42. const FIELD_TYPE_TEXT = 1;
  43. const FIELD_TYPE_TEXTAREA = 2;
  44. const FIELD_TYPE_RADIO = 3;
  45. const FIELD_TYPE_SELECT = 4;
  46. const FIELD_TYPE_SELECT_MULTIPLE = 5;
  47. const FIELD_TYPE_DATE = 6;
  48. const FIELD_TYPE_DATETIME = 7;
  49. const FIELD_TYPE_DOUBLE_SELECT = 8;
  50. const FIELD_TYPE_DIVIDER = 9;
  51. const FIELD_TYPE_TAG = 10;
  52. const FIELD_TYPE_TIMEZONE = 11;
  53. const FIELD_TYPE_SOCIAL_PROFILE = 12;
  54. const FIELD_TYPE_CHECKBOX = 13;
  55. const FIELD_TYPE_MOBILE_PHONE_NUMBER = 14;
  56. const FIELD_TYPE_INTEGER = 15;
  57. const FIELD_TYPE_FILE_IMAGE = 16;
  58. const FIELD_TYPE_FLOAT = 17;
  59. const FIELD_TYPE_FILE = 18;
  60. const FIELD_TYPE_VIDEO_URL = 19;
  61. const FIELD_TYPE_LETTERS_ONLY = 20;
  62. const FIELD_TYPE_ALPHANUMERIC = 21;
  63. const FIELD_TYPE_LETTERS_SPACE = 22;
  64. const FIELD_TYPE_ALPHANUMERIC_SPACE = 23;
  65. const FIELD_TYPE_GEOLOCALIZATION = 24;
  66. const FIELD_TYPE_GEOLOCALIZATION_COORDINATES = 25;
  67. public $type = 'user';
  68. public $pageName;
  69. public $pageUrl;
  70. public $extraFieldType = 0;
  71. public $table_field_options;
  72. public $table_field_values;
  73. public $table_field_tag;
  74. public $table_field_rel_tag;
  75. public $handler_id;
  76. public $primaryKey;
  77. /**
  78. * @param string $type
  79. */
  80. public function __construct($type)
  81. {
  82. parent::__construct();
  83. $this->type = $type;
  84. $this->table = Database::get_main_table(TABLE_EXTRA_FIELD);
  85. $this->table_field_options = Database::get_main_table(TABLE_EXTRA_FIELD_OPTIONS);
  86. $this->table_field_values = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
  87. $this->table_field_tag = Database::get_main_table(TABLE_MAIN_TAG);
  88. $this->table_field_rel_tag = Database::get_main_table(TABLE_MAIN_EXTRA_FIELD_REL_TAG);
  89. $this->handler_id = 'item_id';
  90. switch ($this->type) {
  91. case 'calendar_event':
  92. $this->extraFieldType = EntityExtraField::CALENDAR_FIELD_TYPE;
  93. break;
  94. case 'course':
  95. $this->extraFieldType = EntityExtraField::COURSE_FIELD_TYPE;
  96. $this->primaryKey = 'id';
  97. break;
  98. case 'user':
  99. $this->extraFieldType = EntityExtraField::USER_FIELD_TYPE;
  100. $this->primaryKey = 'id';
  101. break;
  102. case 'session':
  103. $this->extraFieldType = EntityExtraField::SESSION_FIELD_TYPE;
  104. $this->primaryKey = 'id';
  105. break;
  106. case 'question':
  107. $this->extraFieldType = EntityExtraField::QUESTION_FIELD_TYPE;
  108. break;
  109. case 'lp':
  110. $this->extraFieldType = EntityExtraField::LP_FIELD_TYPE;
  111. break;
  112. case 'lp_item':
  113. $this->extraFieldType = EntityExtraField::LP_ITEM_FIELD_TYPE;
  114. break;
  115. case 'skill':
  116. $this->extraFieldType = EntityExtraField::SKILL_FIELD_TYPE;
  117. break;
  118. case 'work':
  119. $this->extraFieldType = EntityExtraField::WORK_FIELD_TYPE;
  120. break;
  121. }
  122. $this->pageUrl = 'extra_fields.php?type='.$this->type;
  123. // Example QuestionFields
  124. $this->pageName = get_lang(ucwords($this->type).'Fields');
  125. }
  126. /**
  127. * @return int
  128. */
  129. public function getExtraFieldType()
  130. {
  131. return (int) $this->extraFieldType;
  132. }
  133. /**
  134. * @return array
  135. */
  136. public static function getValidExtraFieldTypes()
  137. {
  138. return array(
  139. 'user',
  140. 'course',
  141. 'session',
  142. 'question',
  143. 'lp',
  144. 'calendar_event',
  145. 'lp_item',
  146. 'skill',
  147. 'work'
  148. );
  149. }
  150. /**
  151. * @return int
  152. */
  153. public function get_count()
  154. {
  155. $em = Database::getManager();
  156. $query = $em->getRepository('ChamiloCoreBundle:ExtraField')->createQueryBuilder('e');
  157. $query->select('count(e.id)');
  158. $query->where('e.extraFieldType = :type');
  159. $query->setParameter('type', $this->getExtraFieldType());
  160. return $query->getQuery()->getScalarResult();
  161. }
  162. /**
  163. * @param string $sidx
  164. * @param string $sord
  165. * @param int $start
  166. * @param int $limit
  167. *
  168. * @return array
  169. */
  170. public function getAllGrid($sidx, $sord, $start, $limit)
  171. {
  172. switch ($sidx) {
  173. case 'field_order':
  174. $sidx = 'e.fieldOrder';
  175. break;
  176. case 'variable':
  177. $sidx = 'e.variable';
  178. break;
  179. case 'display_text':
  180. $sidx = 'e.displayText';
  181. break;
  182. case 'changeable':
  183. $sidx = 'e.changeable';
  184. break;
  185. case 'visible_to_self':
  186. $sidx = 'e.visibleToSelf';
  187. break;
  188. case 'visible_to_others':
  189. $sidx = 'e.visibleToOthers';
  190. break;
  191. case 'filter':
  192. $sidx = 'e.filter';
  193. break;
  194. case 'display_text':
  195. $sidx = 'e.fieldType';
  196. break;
  197. }
  198. $em = Database::getManager();
  199. $query = $em->getRepository('ChamiloCoreBundle:ExtraField')->createQueryBuilder('e');
  200. $query->select('e')
  201. ->where('e.extraFieldType = :type')
  202. ->setParameter('type', $this->getExtraFieldType())
  203. ->orderBy($sidx, $sord)
  204. ->setFirstResult($start)
  205. ->setMaxResults($limit);
  206. //echo $query->getQuery()->getSQL();
  207. return $query->getQuery()->getArrayResult();
  208. }
  209. /**
  210. * @param array $conditions
  211. * @param null $order_field_options_by
  212. *
  213. * @return array
  214. */
  215. public function get_all($conditions = array(), $order_field_options_by = null)
  216. {
  217. $conditions = Database::parse_conditions(array('where' => $conditions));
  218. if (empty($conditions)) {
  219. $conditions .= " WHERE extra_field_type = ".$this->extraFieldType;
  220. } else {
  221. $conditions .= " AND extra_field_type = ".$this->extraFieldType;
  222. }
  223. $sql = "SELECT * FROM $this->table
  224. $conditions
  225. ORDER BY field_order ASC
  226. ";
  227. $result = Database::query($sql);
  228. $extraFields = Database::store_result($result, 'ASSOC');
  229. $option = new ExtraFieldOption($this->type);
  230. if (!empty($extraFields)) {
  231. foreach ($extraFields as &$extraField) {
  232. $extraField['display_text'] = self::translateDisplayName(
  233. $extraField['variable'],
  234. $extraField['display_text']
  235. );
  236. $extraField['options'] = $option->get_field_options_by_field(
  237. $extraField['id'],
  238. false,
  239. $order_field_options_by
  240. );
  241. }
  242. }
  243. return $extraFields;
  244. }
  245. /**
  246. * @param string $variable
  247. *
  248. * @return array|bool
  249. */
  250. public function get_handler_field_info_by_field_variable($variable)
  251. {
  252. $variable = Database::escape_string($variable);
  253. $sql = "SELECT * FROM {$this->table}
  254. WHERE
  255. variable = '$variable' AND
  256. extra_field_type = $this->extraFieldType";
  257. $result = Database::query($sql);
  258. if (Database::num_rows($result)) {
  259. $row = Database::fetch_array($result, 'ASSOC');
  260. $row['display_text'] = self::translateDisplayName($row['variable'], $row['display_text']);
  261. // All the options of the field
  262. $sql = "SELECT * FROM $this->table_field_options
  263. WHERE field_id='".intval($row['id'])."'
  264. ORDER BY option_order ASC";
  265. $result = Database::query($sql);
  266. while ($option = Database::fetch_array($result)) {
  267. $row['options'][$option['id']] = $option;
  268. }
  269. return $row;
  270. } else {
  271. return false;
  272. }
  273. }
  274. /**
  275. * Get all the field info for tags
  276. * @param string $variable
  277. *
  278. * @return array|bool
  279. */
  280. public function get_handler_field_info_by_tags($variable)
  281. {
  282. $variable = Database::escape_string($variable);
  283. $sql = "SELECT * FROM {$this->table}
  284. WHERE
  285. variable = '$variable' AND
  286. extra_field_type = $this->extraFieldType";
  287. $result = Database::query($sql);
  288. if (Database::num_rows($result)) {
  289. $row = Database::fetch_array($result, 'ASSOC');
  290. $row['display_text'] = self::translateDisplayName(
  291. $row['variable'],
  292. $row['display_text']
  293. );
  294. // All the tags of the field
  295. $sql = "SELECT * FROM $this->table_field_tag
  296. WHERE field_id='".intval($row['id'])."'
  297. ORDER BY id ASC";
  298. $result = Database::query($sql);
  299. while ($option = Database::fetch_array($result, 'ASSOC')) {
  300. $row['options'][$option['id']] = $option;
  301. }
  302. return $row;
  303. } else {
  304. return false;
  305. }
  306. }
  307. /**
  308. * @param int $fieldId
  309. *
  310. * @return array|bool
  311. */
  312. public function getFieldInfoByFieldId($fieldId)
  313. {
  314. $fieldId = intval($fieldId);
  315. $sql = "SELECT * FROM {$this->table}
  316. WHERE
  317. id = '$fieldId' AND
  318. extra_field_type = $this->extraFieldType";
  319. $result = Database::query($sql);
  320. if (Database::num_rows($result)) {
  321. $row = Database::fetch_array($result, 'ASSOC');
  322. // All the options of the field
  323. $sql = "SELECT * FROM $this->table_field_options
  324. WHERE field_id='".$fieldId."'
  325. ORDER BY option_order ASC";
  326. $result = Database::query($sql);
  327. while ($option = Database::fetch_array($result)) {
  328. $row['options'][$option['id']] = $option;
  329. }
  330. return $row;
  331. } else {
  332. return false;
  333. }
  334. }
  335. /**
  336. * @return int
  337. */
  338. public function get_max_field_order()
  339. {
  340. $sql = "SELECT MAX(field_order)
  341. FROM {$this->table}
  342. WHERE
  343. extra_field_type = '.$this->extraFieldType.'";
  344. $res = Database::query($sql);
  345. $order = 0;
  346. if (Database::num_rows($res) > 0) {
  347. $row = Database::fetch_row($res);
  348. $order = $row[0] + 1;
  349. }
  350. return $order;
  351. }
  352. /**
  353. * @param string $handler
  354. *
  355. * @return array
  356. */
  357. public static function get_extra_fields_by_handler($handler)
  358. {
  359. $types = array();
  360. $types[self::FIELD_TYPE_TEXT] = get_lang('FieldTypeText');
  361. $types[self::FIELD_TYPE_TEXTAREA] = get_lang('FieldTypeTextarea');
  362. $types[self::FIELD_TYPE_RADIO] = get_lang('FieldTypeRadio');
  363. $types[self::FIELD_TYPE_SELECT] = get_lang('FieldTypeSelect');
  364. $types[self::FIELD_TYPE_SELECT_MULTIPLE] = get_lang('FieldTypeSelectMultiple');
  365. $types[self::FIELD_TYPE_DATE] = get_lang('FieldTypeDate');
  366. $types[self::FIELD_TYPE_DATETIME] = get_lang('FieldTypeDatetime');
  367. $types[self::FIELD_TYPE_DOUBLE_SELECT] = get_lang('FieldTypeDoubleSelect');
  368. $types[self::FIELD_TYPE_DIVIDER] = get_lang('FieldTypeDivider');
  369. $types[self::FIELD_TYPE_TAG] = get_lang('FieldTypeTag');
  370. $types[self::FIELD_TYPE_TIMEZONE] = get_lang('FieldTypeTimezone');
  371. $types[self::FIELD_TYPE_SOCIAL_PROFILE] = get_lang('FieldTypeSocialProfile');
  372. $types[self::FIELD_TYPE_MOBILE_PHONE_NUMBER] = get_lang('FieldTypeMobilePhoneNumber');
  373. $types[self::FIELD_TYPE_CHECKBOX] = get_lang('FieldTypeCheckbox');
  374. $types[self::FIELD_TYPE_INTEGER] = get_lang('FieldTypeInteger');
  375. $types[self::FIELD_TYPE_FILE_IMAGE] = get_lang('FieldTypeFileImage');
  376. $types[self::FIELD_TYPE_FLOAT] = get_lang('FieldTypeFloat');
  377. $types[self::FIELD_TYPE_FILE] = get_lang('FieldTypeFile');
  378. $types[self::FIELD_TYPE_VIDEO_URL] = get_lang('FieldTypeVideoUrl');
  379. $types[self::FIELD_TYPE_LETTERS_ONLY] = get_lang('FieldTypeOnlyLetters');
  380. $types[self::FIELD_TYPE_ALPHANUMERIC] = get_lang('FieldTypeAlphanumeric');
  381. $types[self::FIELD_TYPE_LETTERS_SPACE] = get_lang(
  382. 'FieldTypeLettersSpaces'
  383. );
  384. $types[self::FIELD_TYPE_ALPHANUMERIC_SPACE] = get_lang(
  385. 'FieldTypeAlphanumericSpaces'
  386. );
  387. $types[self::FIELD_TYPE_GEOLOCALIZATION] = get_lang(
  388. 'Geolocalization'
  389. );
  390. $types[self::FIELD_TYPE_GEOLOCALIZATION_COORDINATES] = get_lang(
  391. 'GeolocalizationCoordinates'
  392. );
  393. switch ($handler) {
  394. case 'course':
  395. // no break
  396. case 'session':
  397. // no break
  398. case 'user':
  399. // no break
  400. case 'skill':
  401. break;
  402. }
  403. return $types;
  404. }
  405. /**
  406. * Add elements to a form
  407. *
  408. * @param FormValidator $form
  409. * @param int $itemId
  410. * @param array $exclude variables of extra field to exclude
  411. * @param bool $filter
  412. * @param bool $useTagAsSelect
  413. * @param array $showOnlyTheseFields
  414. * @param array $orderFields
  415. * @param bool $adminPermissions
  416. *
  417. * @return array|bool
  418. */
  419. public function addElements(
  420. $form,
  421. $itemId = 0,
  422. $exclude = [],
  423. $filter = false,
  424. $useTagAsSelect = false,
  425. $showOnlyThisFields = [],
  426. $orderFields = [],
  427. $extraData = [],
  428. $specialUrlList = [],
  429. $orderDependingDefaults = false,
  430. $forceShowFields = false,
  431. $separateExtraMultipleSelect = [],
  432. $customLabelsExtraMultipleSelect = [],
  433. $fieldsToFreeze = [],
  434. $addEmptyOptionSelects = false,
  435. $introductionTextList = []
  436. ) {
  437. if (empty($form)) {
  438. return false;
  439. }
  440. $itemId = intval($itemId);
  441. $form->addHidden('item_id', $itemId);
  442. if (empty($extraData)) {
  443. if (!empty($itemId)) {
  444. $extraData = self::get_handler_extra_data($itemId);
  445. if ($form) {
  446. if (!empty($showOnlyThisFields)) {
  447. $setData = [];
  448. foreach ($showOnlyThisFields as $variable) {
  449. $extraName = 'extra_'.$variable;
  450. if (in_array($extraName, array_keys($extraData))) {
  451. $setData[$extraName] = $extraData[$extraName];
  452. };
  453. }
  454. $form->setDefaults($setData);
  455. } else {
  456. $form->setDefaults($extraData);
  457. }
  458. }
  459. }
  460. }
  461. $conditions = [];
  462. if ($filter) {
  463. $conditions = ['filter = ?' => 1];
  464. }
  465. $extraFields = $this->get_all($conditions, 'option_order');
  466. $extra = $this->set_extra_fields_in_form(
  467. $form,
  468. $extraData,
  469. $forceShowFields,
  470. $extraFields,
  471. $itemId,
  472. $exclude,
  473. $useTagAsSelect,
  474. $showOnlyThisFields,
  475. $orderFields,
  476. $specialUrlList,
  477. $orderDependingDefaults,
  478. $separateExtraMultipleSelect,
  479. $customLabelsExtraMultipleSelect,
  480. $fieldsToFreeze,
  481. $addEmptyOptionSelects,
  482. $introductionTextList
  483. );
  484. return $extra;
  485. }
  486. /**
  487. * @param int $itemId (session_id, question_id, course id)
  488. *
  489. * @return array
  490. */
  491. public function get_handler_extra_data($itemId)
  492. {
  493. if (empty($itemId)) {
  494. return array();
  495. }
  496. $extra_data = array();
  497. $fields = self::get_all();
  498. $field_values = new ExtraFieldValue($this->type);
  499. if (!empty($fields) > 0) {
  500. foreach ($fields as $field) {
  501. $field_value = $field_values->get_values_by_handler_and_field_id(
  502. $itemId,
  503. $field['id']
  504. );
  505. if ($field['field_type'] == self::FIELD_TYPE_TAG) {
  506. $tags = UserManager::get_user_tags_to_string($itemId, $field['id'], false);
  507. $extra_data['extra_'.$field['variable']] = $tags;
  508. continue;
  509. }
  510. if ($field_value) {
  511. $field_value = $field_value['value'];
  512. switch ($field['field_type']) {
  513. case self::FIELD_TYPE_TAG:
  514. $tags = UserManager::get_user_tags_to_string($itemId, $field['id'], false);
  515. $extra_data['extra_'.$field['variable']] = $tags;
  516. break;
  517. case self::FIELD_TYPE_DOUBLE_SELECT:
  518. $selected_options = explode(
  519. '::',
  520. $field_value
  521. );
  522. $firstOption = isset($selected_options[0]) ? $selected_options[0] : '';
  523. $secondOption = isset($selected_options[1]) ? $selected_options[1] : '';
  524. $extra_data['extra_'.$field['variable']]['extra_'.$field['variable']] = $firstOption;
  525. $extra_data['extra_'.$field['variable']]['extra_'.$field['variable'].'_second'] = $secondOption;
  526. break;
  527. case self::FIELD_TYPE_SELECT_MULTIPLE:
  528. $field_value = explode(';', $field_value);
  529. $extra_data['extra_'.$field['variable']] = $field_value;
  530. break;
  531. case self::FIELD_TYPE_RADIO:
  532. $extra_data['extra_'.$field['variable']]['extra_'.$field['variable']] = $field_value;
  533. break;
  534. default:
  535. $extra_data['extra_'.$field['variable']] = $field_value;
  536. break;
  537. }
  538. } else {
  539. // Set default values
  540. if (isset($field['field_default_value']) && !empty($field['field_default_value'])) {
  541. $extra_data['extra_'.$field['variable']] = $field['field_default_value'];
  542. }
  543. }
  544. }
  545. }
  546. return $extra_data;
  547. }
  548. /**
  549. * @param string $field_type
  550. *
  551. * @return array
  552. */
  553. public function get_all_extra_field_by_type($field_type)
  554. {
  555. // all the information of the field
  556. $sql = "SELECT * FROM {$this->table}
  557. WHERE
  558. field_type = '".Database::escape_string($field_type)."' AND
  559. extra_field_type = $this->extraFieldType
  560. ";
  561. $result = Database::query($sql);
  562. $return = array();
  563. while ($row = Database::fetch_array($result)) {
  564. $return[] = $row['id'];
  565. }
  566. return $return;
  567. }
  568. /**
  569. * @return array
  570. */
  571. public function get_field_types()
  572. {
  573. return self::get_extra_fields_by_handler($this->type);
  574. }
  575. /**
  576. * @param int $id
  577. *
  578. * @return null
  579. */
  580. public function get_field_type_by_id($id)
  581. {
  582. $types = self::get_field_types();
  583. if (isset($types[$id])) {
  584. return $types[$id];
  585. }
  586. return null;
  587. }
  588. /**
  589. * Converts a string like this:
  590. * France:Paris;Bretagne;Marseille;Lyon|Belgique:Bruxelles;Namur;Liège;Bruges|Peru:Lima;Piura;
  591. * into
  592. * array(
  593. * 'France' =>
  594. * array('Paris', 'Bretagne', 'Marseille'),
  595. * 'Belgique' =>
  596. * array('Namur', 'Liège')
  597. * ), etc
  598. * @param string $string
  599. *
  600. * @return array
  601. */
  602. public static function extra_field_double_select_convert_string_to_array($string)
  603. {
  604. $options = explode('|', $string);
  605. $options_parsed = array();
  606. $id = 0;
  607. if (!empty($options)) {
  608. foreach ($options as $sub_options) {
  609. $options = explode(':', $sub_options);
  610. $sub_sub_options = explode(';', $options[1]);
  611. $options_parsed[$id] = array(
  612. 'label' => $options[0],
  613. 'options' => $sub_sub_options,
  614. );
  615. $id++;
  616. }
  617. }
  618. return $options_parsed;
  619. }
  620. /**
  621. * @param array $options
  622. *
  623. * @return array
  624. */
  625. public static function extra_field_double_select_convert_array_to_ordered_array($options)
  626. {
  627. $options_parsed = array();
  628. if (!empty($options)) {
  629. foreach ($options as $option) {
  630. if ($option['option_value'] == 0) {
  631. $options_parsed[$option['id']][] = $option;
  632. } else {
  633. $options_parsed[$option['option_value']][] = $option;
  634. }
  635. }
  636. }
  637. return $options_parsed;
  638. }
  639. /**
  640. * @param array $options the result of the get_field_options_by_field() array
  641. *
  642. * @return string
  643. */
  644. public static function extra_field_double_select_convert_array_to_string($options)
  645. {
  646. $string = null;
  647. $options_parsed = self::extra_field_double_select_convert_array_to_ordered_array($options);
  648. if (!empty($options_parsed)) {
  649. foreach ($options_parsed as $option) {
  650. foreach ($option as $key => $item) {
  651. $string .= $item['display_text'];
  652. if ($key == 0) {
  653. $string .= ':';
  654. } else {
  655. if (isset($option[$key + 1])) {
  656. $string .= ';';
  657. }
  658. }
  659. }
  660. $string .= '|';
  661. }
  662. }
  663. if (!empty($string)) {
  664. $string = substr($string, 0, strlen($string) - 1);
  665. }
  666. return $string;
  667. }
  668. /**
  669. * @param array $params
  670. *
  671. * @return array
  672. */
  673. public function clean_parameters($params)
  674. {
  675. if (!isset($params['variable']) || empty($params['variable'])) {
  676. $params['variable'] = $params['display_text'];
  677. }
  678. $params['variable'] = trim(strtolower(str_replace(" ", "_", $params['variable'])));
  679. if (!isset($params['field_order'])) {
  680. $max_order = self::get_max_field_order();
  681. $params['field_order'] = $max_order;
  682. } else {
  683. $params['field_order'] = (int) $params['field_order'];
  684. }
  685. return $params;
  686. }
  687. /**
  688. * @param array $params
  689. * @param bool $show_query
  690. *
  691. * @return bool
  692. */
  693. public function save($params, $show_query = false)
  694. {
  695. $fieldInfo = self::get_handler_field_info_by_field_variable($params['variable']);
  696. $params = self::clean_parameters($params);
  697. $params['extra_field_type'] = $this->extraFieldType;
  698. if ($fieldInfo) {
  699. return $fieldInfo['id'];
  700. } else {
  701. $id = parent::save($params, $show_query);
  702. if ($id) {
  703. $session_field_option = new ExtraFieldOption($this->type);
  704. $params['field_id'] = $id;
  705. $session_field_option->save($params);
  706. }
  707. return $id;
  708. }
  709. }
  710. /**
  711. * @param array $params
  712. *
  713. * @return bool|void
  714. */
  715. public function update($params)
  716. {
  717. $params = self::clean_parameters($params);
  718. if (isset($params['id'])) {
  719. $field_option = new ExtraFieldOption($this->type);
  720. $params['field_id'] = $params['id'];
  721. $field_option->save($params);
  722. }
  723. parent::update($params);
  724. }
  725. /**
  726. * @param $id
  727. *
  728. * @return bool|void
  729. */
  730. public function delete($id)
  731. {
  732. $em = Database::getManager();
  733. $items = $em->getRepository('ChamiloCoreBundle:ExtraFieldSavedSearch')->findBy(['field' => $id]);
  734. if ($items) {
  735. foreach ($items as $item) {
  736. $em->remove($item);
  737. }
  738. $em->flush();
  739. }
  740. $field_option = new ExtraFieldOption($this->type);
  741. $field_option->delete_all_options_by_field_id($id);
  742. $session_field_values = new ExtraFieldValue($this->type);
  743. $session_field_values->delete_all_values_by_field_id($id);
  744. parent::delete($id);
  745. }
  746. /**
  747. * Add an element that matches the given extra field to the given $form object
  748. * @param FormValidator $form
  749. * @param array $extraData
  750. * @param bool $admin_permissions
  751. * @param int $user_id
  752. * @param array $extra
  753. * @param int $itemId
  754. * @param array $exclude variables of extra field to exclude
  755. * @param array
  756. *
  757. * @return array
  758. */
  759. public function set_extra_fields_in_form(
  760. $form,
  761. $extraData,
  762. $admin_permissions = false,
  763. $extra = array(),
  764. $itemId = null,
  765. $exclude = [],
  766. $useTagAsSelect = false,
  767. $showOnlyThisFields = [],
  768. $orderFields = [],
  769. $specialUrlList = [],
  770. $orderDependingDefaults = false,
  771. $separateExtraMultipleSelect = [],
  772. $customLabelsExtraMultipleSelect = [],
  773. $fieldsToFreeze = [],
  774. $addEmptyOptionSelects = false,
  775. $introductionTextList = []
  776. ) {
  777. $type = $this->type;
  778. $jquery_ready_content = '';
  779. if (!empty($extra)) {
  780. $newOrder = [];
  781. if (!empty($orderFields)) {
  782. foreach ($orderFields as $order) {
  783. foreach ($extra as $field_details) {
  784. if ($order === $field_details['variable']) {
  785. $newOrder[] = $field_details;
  786. }
  787. }
  788. }
  789. $extra = $newOrder;
  790. }
  791. foreach ($extra as $field_details) {
  792. if (!empty($showOnlyThisFields)) {
  793. if (!in_array($field_details['variable'], $showOnlyThisFields)) {
  794. continue;
  795. }
  796. }
  797. // Getting default value id if is set
  798. $defaultValueId = null;
  799. if (isset($field_details['options']) && !empty($field_details['options'])) {
  800. $valueToFind = null;
  801. if (isset($field_details['field_default_value'])) {
  802. $valueToFind = $field_details['field_default_value'];
  803. }
  804. // If a value is found we override the default value
  805. if (isset($extraData['extra_'.$field_details['variable']])) {
  806. $valueToFind = $extraData['extra_'.$field_details['variable']];
  807. }
  808. foreach ($field_details['options'] as $option) {
  809. if ($option['option_value'] == $valueToFind) {
  810. $defaultValueId = $option['id'];
  811. }
  812. }
  813. }
  814. if (!$admin_permissions) {
  815. if ($field_details['visible_to_self'] == 0) {
  816. continue;
  817. }
  818. if (in_array($field_details['variable'], $exclude)) {
  819. continue;
  820. }
  821. }
  822. if (!empty($introductionTextList) &&
  823. in_array($field_details['variable'], array_keys($introductionTextList))
  824. ) {
  825. $form->addHtml($introductionTextList[$field_details['variable']]);
  826. }
  827. switch ($field_details['field_type']) {
  828. case ExtraField::FIELD_TYPE_TEXT:
  829. $form->addElement(
  830. 'text',
  831. 'extra_'.$field_details['variable'],
  832. $field_details['display_text'],
  833. array()
  834. );
  835. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  836. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  837. if (!$admin_permissions) {
  838. if ($field_details['visible_to_self'] == 0) {
  839. $form->freeze(
  840. 'extra_'.$field_details['variable']
  841. );
  842. }
  843. }
  844. break;
  845. case ExtraField::FIELD_TYPE_TEXTAREA:
  846. $form->addHtmlEditor(
  847. 'extra_'.$field_details['variable'],
  848. $field_details['display_text'],
  849. false,
  850. false,
  851. array('ToolbarSet' => 'Profile', 'Width' => '100%', 'Height' => '130')
  852. );
  853. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  854. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  855. if (!$admin_permissions) {
  856. if ($field_details['visible_to_self'] == 0) {
  857. $form->freeze(
  858. 'extra_'.$field_details['variable']
  859. );
  860. }
  861. }
  862. break;
  863. case ExtraField::FIELD_TYPE_RADIO:
  864. $group = array();
  865. if (isset($field_details['options']) && !empty($field_details['options'])) {
  866. foreach ($field_details['options'] as $option_details) {
  867. $options[$option_details['option_value']] = $option_details['display_text'];
  868. $group[] = $form->createElement(
  869. 'radio',
  870. 'extra_'.$field_details['variable'],
  871. $option_details['option_value'],
  872. $option_details['display_text'].'<br />',
  873. $option_details['option_value']
  874. );
  875. }
  876. }
  877. $form->addGroup(
  878. $group,
  879. 'extra_'.$field_details['variable'],
  880. $field_details['display_text']
  881. );
  882. if (!$admin_permissions) {
  883. if ($field_details['visible_to_self'] == 0) {
  884. $form->freeze(
  885. 'extra_'.$field_details['variable']
  886. );
  887. }
  888. }
  889. break;
  890. case ExtraField::FIELD_TYPE_CHECKBOX:
  891. $group = array();
  892. if (isset($field_details['options']) && !empty($field_details['options'])) {
  893. foreach ($field_details['options'] as $option_details) {
  894. $options[$option_details['option_value']] = $option_details['display_text'];
  895. $group[] = $form->createElement(
  896. 'checkbox',
  897. 'extra_'.$field_details['variable'],
  898. $option_details['option_value'],
  899. $option_details['display_text'].'<br />',
  900. $option_details['option_value']
  901. );
  902. }
  903. } else {
  904. $fieldVariable = "extra_{$field_details['variable']}";
  905. $checkboxAttributes = array();
  906. if (is_array($extraData) && array_key_exists($fieldVariable, $extraData)) {
  907. if (!empty($extraData[$fieldVariable])) {
  908. $checkboxAttributes['checked'] = 1;
  909. }
  910. }
  911. // We assume that is a switch on/off with 1 and 0 as values
  912. $group[] = $form->createElement(
  913. 'checkbox',
  914. 'extra_'.$field_details['variable'],
  915. null,
  916. //$field_details['display_text'].'<br />',
  917. get_lang('Yes'),
  918. $checkboxAttributes
  919. );
  920. }
  921. $form->addGroup(
  922. $group,
  923. 'extra_'.$field_details['variable'],
  924. $field_details['display_text']
  925. );
  926. if (!$admin_permissions) {
  927. if ($field_details['visible_to_self'] == 0) {
  928. $form->freeze(
  929. 'extra_'.$field_details['variable']
  930. );
  931. }
  932. }
  933. break;
  934. case ExtraField::FIELD_TYPE_SELECT:
  935. $get_lang_variables = false;
  936. if (in_array(
  937. $field_details['variable'],
  938. array('mail_notify_message', 'mail_notify_invitation', 'mail_notify_group_message')
  939. )
  940. ) {
  941. $get_lang_variables = true;
  942. }
  943. // Get extra field workflow
  944. $userInfo = api_get_user_info();
  945. $addOptions = array();
  946. $optionsExists = false;
  947. global $app;
  948. // Check if exist $app['orm.em'] object
  949. if (isset($app['orm.em']) && is_object($app['orm.em'])) {
  950. $optionsExists = $app['orm.em']
  951. ->getRepository('ChamiloLMS\Entity\ExtraFieldOptionRelFieldOption')
  952. ->findOneBy(array('fieldId' => $field_details['id']));
  953. }
  954. if ($optionsExists) {
  955. if (isset($userInfo['status']) && !empty($userInfo['status'])) {
  956. $fieldWorkFlow = $app['orm.em']->getRepository('ChamiloLMS\Entity\ExtraFieldOptionRelFieldOption')
  957. ->findBy(
  958. array(
  959. 'fieldId' => $field_details['id'],
  960. 'relatedFieldOptionId' => $defaultValueId,
  961. 'roleId' => $userInfo['status']
  962. )
  963. );
  964. foreach ($fieldWorkFlow as $item) {
  965. $addOptions[] = $item->getFieldOptionId();
  966. }
  967. }
  968. }
  969. $options = array();
  970. if (empty($defaultValueId)) {
  971. $options[''] = get_lang('SelectAnOption');
  972. }
  973. $optionList = array();
  974. if (!empty($field_details['options'])) {
  975. foreach ($field_details['options'] as $option_details) {
  976. $optionList[$option_details['id']] = $option_details;
  977. if ($get_lang_variables) {
  978. $options[$option_details['option_value']] = $option_details['display_text'];
  979. } else {
  980. if ($optionsExists) {
  981. // Adding always the default value
  982. if ($option_details['id'] == $defaultValueId) {
  983. $options[$option_details['option_value']] = $option_details['display_text'];
  984. } else {
  985. if (isset($addOptions) && !empty($addOptions)) {
  986. // Parsing filters
  987. if (in_array($option_details['id'], $addOptions)) {
  988. $options[$option_details['option_value']] = $option_details['display_text'];
  989. }
  990. }
  991. }
  992. } else {
  993. // Normal behaviour
  994. $options[$option_details['option_value']] = $option_details['display_text'];
  995. }
  996. }
  997. }
  998. if (isset($optionList[$defaultValueId])) {
  999. if (isset($optionList[$defaultValueId]['option_value']) &&
  1000. $optionList[$defaultValueId]['option_value'] == 'aprobada'
  1001. ) {
  1002. // @todo function don't exists api_is_question_manager
  1003. /*if (api_is_question_manager() == false) {
  1004. $form->freeze();
  1005. }*/
  1006. }
  1007. }
  1008. // Setting priority message
  1009. if (isset($optionList[$defaultValueId]) &&
  1010. isset($optionList[$defaultValueId]['priority'])
  1011. ) {
  1012. if (!empty($optionList[$defaultValueId]['priority'])) {
  1013. $priorityId = $optionList[$defaultValueId]['priority'];
  1014. $option = new ExtraFieldOption($this->type);
  1015. $messageType = $option->getPriorityMessageType($priorityId);
  1016. $form->addElement(
  1017. 'label',
  1018. null,
  1019. Display::return_message(
  1020. $optionList[$defaultValueId]['priority_message'],
  1021. $messageType
  1022. )
  1023. );
  1024. }
  1025. }
  1026. }
  1027. // chzn-select doesn't work for sessions??
  1028. $form->addElement(
  1029. 'select',
  1030. 'extra_'.$field_details['variable'],
  1031. $field_details['display_text'],
  1032. $options,
  1033. array('id' => 'extra_'.$field_details['variable'])
  1034. );
  1035. /* Enable this when field_loggeable is introduced as a table field (2.0)
  1036. if ($optionsExists && $field_details['field_loggeable'] && !empty($defaultValueId)) {
  1037. $form->addElement(
  1038. 'textarea',
  1039. 'extra_' . $field_details['variable'] . '_comment',
  1040. $field_details['display_text'] . ' ' . get_lang('Comment')
  1041. );
  1042. $extraFieldValue = new ExtraFieldValue($this->type);
  1043. $repo = $app['orm.em']->getRepository($extraFieldValue->entityName);
  1044. $repoLog = $app['orm.em']->getRepository('Gedmo\Loggable\Entity\LogEntry');
  1045. $newEntity = $repo->findOneBy(
  1046. array(
  1047. $this->handlerEntityId => $itemId,
  1048. 'fieldId' => $field_details['id']
  1049. )
  1050. );
  1051. // @todo move this in a function inside the class
  1052. if ($newEntity) {
  1053. $logs = $repoLog->getLogEntries($newEntity);
  1054. if (!empty($logs)) {
  1055. $html = '<b>' . get_lang('LatestChanges') . '</b><br /><br />';
  1056. $table = new HTML_Table(array('class' => 'data_table'));
  1057. $table->setHeaderContents(0, 0, get_lang('Value'));
  1058. $table->setHeaderContents(0, 1, get_lang('Comment'));
  1059. $table->setHeaderContents(0, 2, get_lang('ModifyDate'));
  1060. $table->setHeaderContents(0, 3, get_lang('Username'));
  1061. $row = 1;
  1062. foreach ($logs as $log) {
  1063. $column = 0;
  1064. $data = $log->getData();
  1065. $fieldValue = isset($data['fieldValue']) ? $data['fieldValue'] : null;
  1066. $comment = isset($data['comment']) ? $data['comment'] : null;
  1067. $table->setCellContents($row, $column, $fieldValue);
  1068. $column++;
  1069. $table->setCellContents($row, $column, $comment);
  1070. $column++;
  1071. $table->setCellContents($row, $column, api_get_local_time($log->getLoggedAt()->format('Y-m-d H:i:s')));
  1072. $column++;
  1073. $table->setCellContents($row, $column, $log->getUsername());
  1074. $row++;
  1075. }
  1076. $form->addElement('label', null, $html.$table->toHtml());
  1077. }
  1078. }
  1079. }
  1080. */
  1081. if (!$admin_permissions) {
  1082. if ($field_details['visible_to_self'] == 0) {
  1083. $form->freeze('extra_' . $field_details['variable']);
  1084. }
  1085. }
  1086. break;
  1087. case ExtraField::FIELD_TYPE_SELECT_MULTIPLE:
  1088. $options = array();
  1089. foreach ($field_details['options'] as $option_id => $option_details) {
  1090. $options[$option_details['option_value']] = $option_details['display_text'];
  1091. }
  1092. if ($orderDependingDefaults) {
  1093. $defaultOptions = $extraData['extra_'.$field_details['variable']];
  1094. if (!empty($defaultOptions)) {
  1095. $firstList = [];
  1096. if ($addEmptyOptionSelects) {
  1097. $firstList[] = '';
  1098. }
  1099. foreach ($defaultOptions as $key) {
  1100. if (isset($options[$key])) {
  1101. $firstList[$key] = $options[$key];
  1102. }
  1103. }
  1104. if (!empty($firstList)) {
  1105. $options = array_merge($firstList, $options);
  1106. }
  1107. } else {
  1108. $firstList = [];
  1109. if ($addEmptyOptionSelects) {
  1110. $firstList[] = '&nbsp;';
  1111. $options = array_merge($firstList, $options);
  1112. }
  1113. }
  1114. }
  1115. // OFAJ
  1116. $separateValue = 0;
  1117. if (isset($separateExtraMultipleSelect[$field_details['variable']])) {
  1118. $separateValue = $separateExtraMultipleSelect[$field_details['variable']];
  1119. }
  1120. if ($separateValue > 0) {
  1121. for($i = 0; $i < $separateValue; $i++) {
  1122. $form->addElement(
  1123. 'select',
  1124. 'extra_'.$field_details['variable'].'['.$i.']',
  1125. $customLabelsExtraMultipleSelect[$field_details['variable']][$i], //$field_details['display_text'],
  1126. $options,
  1127. array('id' => 'extra_'.$field_details['variable'].'_'.$i)
  1128. );
  1129. }
  1130. } else {
  1131. // Ofaj
  1132. $attributes = array('multiple' => 'multiple', 'id' => 'extra_'.$field_details['variable']);
  1133. $chosenSelect = [
  1134. 'ecouter',
  1135. 'lire',
  1136. 'participer_a_une_conversation',
  1137. 's_exprimer_oralement_en_continu',
  1138. 'ecrire',
  1139. ];
  1140. if (in_array($field_details['variable'], $chosenSelect)) {
  1141. $attributes['select_chosen'] = true;
  1142. }
  1143. // default behaviour
  1144. $form->addElement(
  1145. 'select',
  1146. 'extra_'.$field_details['variable'],
  1147. $field_details['display_text'],
  1148. $options,
  1149. $attributes
  1150. );
  1151. }
  1152. if (!$admin_permissions) {
  1153. if ($field_details['visible_to_self'] == 0) {
  1154. $form->freeze('extra_'.$field_details['variable']);
  1155. }
  1156. }
  1157. break;
  1158. case ExtraField::FIELD_TYPE_DATE:
  1159. $form->addDatePicker('extra_'.$field_details['variable'], $field_details['display_text']);
  1160. if (!$admin_permissions) {
  1161. if ($field_details['visible_to_self'] == 0) {
  1162. $form->freeze('extra_'.$field_details['variable']);
  1163. }
  1164. }
  1165. break;
  1166. case ExtraField::FIELD_TYPE_DATETIME:
  1167. $form->addDateTimePicker(
  1168. 'extra_'.$field_details['variable'],
  1169. $field_details['display_text']
  1170. );
  1171. $defaults['extra_'.$field_details['variable']] = api_get_local_time();
  1172. if (!isset($form->_defaultValues['extra_'.$field_details['variable']])) {
  1173. $form->setDefaults($defaults);
  1174. }
  1175. if (!$admin_permissions) {
  1176. if ($field_details['visible_to_self'] == 0) {
  1177. $form->freeze('extra_'.$field_details['variable']);
  1178. }
  1179. }
  1180. break;
  1181. case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
  1182. $first_select_id = 'first_extra_'.$field_details['variable'];
  1183. $url = api_get_path(WEB_AJAX_PATH).'extra_field.ajax.php?1=1';
  1184. $jquery_ready_content .= '
  1185. $("#'.$first_select_id.'").on("change", function() {
  1186. var id = $(this).val();
  1187. if (id) {
  1188. $.ajax({
  1189. url: "'.$url.'&a=get_second_select_options",
  1190. dataType: "json",
  1191. data: "type='.$type.'&field_id='.$field_details['id'].'&option_value_id="+id,
  1192. success: function(data) {
  1193. $("#second_extra_'.$field_details['variable'].'").empty();
  1194. $.each(data, function(index, value) {
  1195. $("#second_extra_'.$field_details['variable'].'").append($("<option/>", {
  1196. value: index,
  1197. text: value
  1198. }));
  1199. });
  1200. $("#second_extra_'.$field_details['variable'].'").selectpicker("refresh");
  1201. },
  1202. });
  1203. } else {
  1204. $("#second_extra_'.$field_details['variable'].'").empty();
  1205. }
  1206. });';
  1207. $first_id = null;
  1208. if (!empty($extraData)) {
  1209. if (isset($extraData['extra_'.$field_details['variable']])) {
  1210. $first_id = $extraData['extra_'.$field_details['variable']]['extra_'.$field_details['variable']];
  1211. }
  1212. }
  1213. $options = self::extra_field_double_select_convert_array_to_ordered_array(
  1214. $field_details['options']
  1215. );
  1216. $values = array('' => get_lang('Select'));
  1217. $second_values = array();
  1218. if (!empty($options)) {
  1219. foreach ($options as $option) {
  1220. foreach ($option as $sub_option) {
  1221. if ($sub_option['option_value'] == '0') {
  1222. $values[$sub_option['id']] = $sub_option['display_text'];
  1223. } else {
  1224. if ($first_id === $sub_option['option_value']) {
  1225. $second_values[$sub_option['id']] = $sub_option['display_text'];
  1226. }
  1227. }
  1228. }
  1229. }
  1230. }
  1231. $group = array();
  1232. $group[] = $form->createElement(
  1233. 'select',
  1234. 'extra_'.$field_details['variable'],
  1235. null,
  1236. $values,
  1237. array('id' => $first_select_id)
  1238. );
  1239. $group[] = $form->createElement(
  1240. 'select',
  1241. 'extra_'.$field_details['variable'].'_second',
  1242. null,
  1243. $second_values,
  1244. array('id' => 'second_extra_'.$field_details['variable'])
  1245. );
  1246. $form->addGroup(
  1247. $group,
  1248. 'extra_'.$field_details['variable'],
  1249. $field_details['display_text']
  1250. );
  1251. if (!$admin_permissions) {
  1252. if ($field_details['visible_to_self'] == 0) {
  1253. $form->freeze('extra_'.$field_details['variable']);
  1254. }
  1255. }
  1256. break;
  1257. case self::FIELD_TYPE_DIVIDER:
  1258. $form->addHtml('
  1259. <div class="form-group ">
  1260. <div class="col-sm-12">
  1261. <div class="panel-separator">
  1262. <h4 id="' . $field_details['variable'].'" class="form-separator">'.$field_details['display_text'].'</h4>
  1263. </div>
  1264. </div>
  1265. </div>
  1266. ');
  1267. break;
  1268. case self::FIELD_TYPE_TAG:
  1269. $variable = $field_details['variable'];
  1270. $field_id = $field_details['id'];
  1271. $separateValue = 0;
  1272. if (isset($separateExtraMultipleSelect[$field_details['variable']])) {
  1273. $separateValue = $separateExtraMultipleSelect[$field_details['variable']];
  1274. }
  1275. if ($separateValue > 0) {
  1276. $em = Database::getManager();
  1277. $fieldTags = $em
  1278. ->getRepository('ChamiloCoreBundle:ExtraFieldRelTag')
  1279. ->findBy(
  1280. [
  1281. 'fieldId' => $field_id,
  1282. 'itemId' => $itemId,
  1283. ]
  1284. );
  1285. /*if (!empty($extraData) && isset($extraData['extra_'.$field_details['variable']])) {
  1286. $data = $extraData['extra_'.$field_details['variable']];
  1287. foreach ($data as $option) {
  1288. $tagsSelect->addOption(
  1289. $option,
  1290. $option,
  1291. ['selected' => 'selected', 'class' => 'selected']
  1292. );
  1293. }
  1294. }*/
  1295. // ofaj
  1296. for ($i = 0; $i < $separateValue; $i++) {
  1297. $tagsSelect = $form->addElement(
  1298. 'select',
  1299. 'extra_'.$field_details['variable'].'['.$i.']',
  1300. $customLabelsExtraMultipleSelect[$field_details['variable']][$i], //$field_details['display_text'],
  1301. null,
  1302. array('id' => 'extra_'.$field_details['variable'].'_'.$i)
  1303. );
  1304. if ($addEmptyOptionSelects) {
  1305. $tagsSelect->addOption(
  1306. '',
  1307. ''
  1308. );
  1309. }
  1310. /*if (!empty($extraData) && isset($extraData['extra_'.$field_details['variable']])) {
  1311. $data = $extraData['extra_'.$field_details['variable']];
  1312. foreach ($data as $option) {
  1313. $tagsSelect->addOption(
  1314. $option,
  1315. $option,
  1316. [
  1317. 'selected' => 'selected',
  1318. 'class' => 'selected'
  1319. ]
  1320. );
  1321. }
  1322. }*/
  1323. foreach ($fieldTags as $fieldTag) {
  1324. $tag = $em->find('ChamiloCoreBundle:Tag', $fieldTag->getTagId());
  1325. if (empty($tag)) {
  1326. continue;
  1327. }
  1328. $tagsSelect->addOption(
  1329. $tag->getTag(),
  1330. $tag->getTag()
  1331. );
  1332. }
  1333. }
  1334. } else {
  1335. $tagsSelect = $form->addSelect(
  1336. "extra_{$field_details['variable']}",
  1337. $field_details['display_text']
  1338. );
  1339. if ($useTagAsSelect === false) {
  1340. $tagsSelect->setAttribute('class', null);
  1341. }
  1342. $tagsSelect->setAttribute(
  1343. 'id',
  1344. "extra_{$field_details['variable']}"
  1345. );
  1346. $tagsSelect->setMultiple(true);
  1347. if ($this->type === 'user') {
  1348. // The magic should be here
  1349. $user_tags = UserManager::get_user_tags(
  1350. $itemId,
  1351. $field_details['id']
  1352. );
  1353. if (is_array($user_tags) && count(
  1354. $user_tags
  1355. ) > 0
  1356. ) {
  1357. foreach ($user_tags as $tag) {
  1358. $tagsSelect->addOption(
  1359. $tag['tag'],
  1360. $tag['tag'],
  1361. [
  1362. 'selected' => 'selected',
  1363. 'class' => 'selected',
  1364. ]
  1365. );
  1366. }
  1367. }
  1368. $url = api_get_path(
  1369. WEB_AJAX_PATH
  1370. ).'user_manager.ajax.php';
  1371. } else {
  1372. $em = Database::getManager();
  1373. $fieldTags = $em->getRepository(
  1374. 'ChamiloCoreBundle:ExtraFieldRelTag'
  1375. )
  1376. ->findBy(
  1377. [
  1378. 'fieldId' => $field_id,
  1379. 'itemId' => $itemId,
  1380. ]
  1381. );
  1382. foreach ($fieldTags as $fieldTag) {
  1383. $tag = $em->find(
  1384. 'ChamiloCoreBundle:Tag',
  1385. $fieldTag->getTagId()
  1386. );
  1387. if (empty($tag)) {
  1388. continue;
  1389. }
  1390. $tagsSelect->addOption(
  1391. $tag->getTag(),
  1392. $tag->getTag(),
  1393. [
  1394. 'selected' => 'selected',
  1395. 'class' => 'selected',
  1396. ]
  1397. );
  1398. }
  1399. if (!empty($extraData) && isset($extraData['extra_'.$field_details['variable']])) {
  1400. $data = $extraData['extra_'.$field_details['variable']];
  1401. foreach ($data as $option) {
  1402. $tagsSelect->addOption(
  1403. $option,
  1404. $option,
  1405. [
  1406. 'selected' => 'selected',
  1407. 'class' => 'selected',
  1408. ]
  1409. );
  1410. }
  1411. }
  1412. if ($useTagAsSelect) {
  1413. $fieldTags = $em->getRepository('ChamiloCoreBundle:ExtraFieldRelTag')
  1414. ->findBy(
  1415. [
  1416. 'fieldId' => $field_id,
  1417. ]
  1418. );
  1419. $tagsAdded = [];
  1420. foreach ($fieldTags as $fieldTag) {
  1421. $tag = $em->find(
  1422. 'ChamiloCoreBundle:Tag',
  1423. $fieldTag->getTagId()
  1424. );
  1425. if (empty($tag)) {
  1426. continue;
  1427. }
  1428. $tagText = $tag->getTag();
  1429. if (in_array($tagText, $tagsAdded)) {
  1430. continue;
  1431. }
  1432. $tagsSelect->addOption(
  1433. $tag->getTag(),
  1434. $tag->getTag(),
  1435. []
  1436. );
  1437. $tagsAdded[] = $tagText;
  1438. }
  1439. }
  1440. $url = api_get_path(WEB_AJAX_PATH).'extra_field.ajax.php';
  1441. }
  1442. $url = $url."?a=search_tags&field_id=$field_id&type={$this->type}";
  1443. if (isset($specialUrlList[$field_details['variable']])) {
  1444. //$url = $specialUrlList[$field_details['variable']]."&field_id=$field_id&type={$this->type}";
  1445. }
  1446. if ($useTagAsSelect == false) {
  1447. $complete_text = get_lang('StartToType');
  1448. //if cache is set to true the jquery will be called 1 time
  1449. $jquery_ready_content .= <<<EOF
  1450. $("#extra_$variable").fcbkcomplete({
  1451. json_url: "$url",
  1452. cache: false,
  1453. filter_case: true,
  1454. filter_hide: true,
  1455. complete_text:"$complete_text",
  1456. firstselected: false,
  1457. filter_selected: true,
  1458. newel: true
  1459. });
  1460. EOF;
  1461. }
  1462. }
  1463. break;
  1464. case ExtraField::FIELD_TYPE_TIMEZONE:
  1465. $form->addElement(
  1466. 'select',
  1467. 'extra_'.$field_details['variable'],
  1468. $field_details['display_text'],
  1469. api_get_timezones(),
  1470. ''
  1471. );
  1472. if ($field_details['visible_to_self'] == 0) {
  1473. $form->freeze(
  1474. 'extra_'.$field_details['variable']
  1475. );
  1476. }
  1477. break;
  1478. case ExtraField::FIELD_TYPE_SOCIAL_PROFILE:
  1479. // get the social network's favicon
  1480. $extra_data_variable = isset($extraData['extra_'.$field_details['variable']]) ? $extraData['extra_'.$field_details['variable']] : null;
  1481. $field_default_value = isset($field_details['field_default_value']) ? $field_details['field_default_value'] : null;
  1482. $icon_path = UserManager::get_favicon_from_url(
  1483. $extra_data_variable,
  1484. $field_default_value
  1485. );
  1486. // special hack for hi5
  1487. $leftpad = '1.7';
  1488. $top = '0.4';
  1489. $domain = parse_url($icon_path, PHP_URL_HOST);
  1490. if ($domain == 'www.hi5.com' or $domain == 'hi5.com') {
  1491. $leftpad = '3';
  1492. $top = '0';
  1493. }
  1494. // print the input field
  1495. $form->addElement(
  1496. 'text',
  1497. 'extra_'.$field_details['variable'],
  1498. $field_details['display_text'],
  1499. array(
  1500. 'size' => 60,
  1501. 'style' => 'background-image: url(\''.$icon_path.'\'); background-repeat: no-repeat; background-position: 0.4em '.$top.'em; padding-left: '.$leftpad.'em; ',
  1502. )
  1503. );
  1504. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  1505. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  1506. if ($field_details['visible_to_self'] == 0) {
  1507. $form->freeze('extra_'.$field_details['variable']);
  1508. }
  1509. break;
  1510. case ExtraField::FIELD_TYPE_MOBILE_PHONE_NUMBER:
  1511. $form->addElement(
  1512. 'text',
  1513. 'extra_'.$field_details[1],
  1514. $field_details[3]." (".get_lang('CountryDialCode').")",
  1515. array('size' => 40, 'placeholder' => '(xx)xxxxxxxxx')
  1516. );
  1517. $form->applyFilter('extra_'.$field_details[1], 'stripslashes');
  1518. $form->applyFilter('extra_'.$field_details[1], 'trim');
  1519. $form->applyFilter('extra_'.$field_details[1], 'mobile_phone_number_filter');
  1520. $form->addRule(
  1521. 'extra_'.$field_details[1],
  1522. get_lang('MobilePhoneNumberWrong'),
  1523. 'mobile_phone_number'
  1524. );
  1525. if ($field_details['visible_to_self'] == 0) {
  1526. $form->freeze('extra_'.$field_details['variable']);
  1527. }
  1528. break;
  1529. case ExtraField::FIELD_TYPE_INTEGER:
  1530. $form->addElement(
  1531. 'number',
  1532. 'extra_'.$field_details['variable'],
  1533. $field_details['display_text'],
  1534. array('class' => 'span1', 'step' => 1)
  1535. );
  1536. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  1537. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  1538. $form->applyFilter('extra_'.$field_details['variable'], 'intval');
  1539. if (!$admin_permissions) {
  1540. if ($field_details['visible_to_self'] == 0) {
  1541. $form->freeze(
  1542. 'extra_'.$field_details['variable']
  1543. );
  1544. }
  1545. }
  1546. break;
  1547. case ExtraField::FIELD_TYPE_FILE_IMAGE:
  1548. $fieldVariable = "extra_{$field_details['variable']}";
  1549. $fieldTexts = [
  1550. $field_details['display_text'],
  1551. ];
  1552. if (is_array($extraData) && array_key_exists($fieldVariable, $extraData)) {
  1553. if (file_exists(api_get_path(SYS_UPLOAD_PATH) . $extraData[$fieldVariable])) {
  1554. $fieldTexts[] = Display::img(
  1555. api_get_path(WEB_UPLOAD_PATH) . $extraData[$fieldVariable],
  1556. $field_details['display_text'],
  1557. ['width' => '300']
  1558. );
  1559. }
  1560. }
  1561. if ($fieldTexts[0] === 'Image') {
  1562. $fieldTexts[0] = get_lang($fieldTexts[0]);
  1563. }
  1564. $form->addFile(
  1565. $fieldVariable,
  1566. $fieldTexts,
  1567. ['accept' => 'image/*', 'id' => 'extra_image', 'crop_image' => 'true']
  1568. );
  1569. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  1570. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  1571. $allowed_picture_types = ['jpg', 'jpeg', 'png', 'gif'];
  1572. $form->addRule(
  1573. 'extra_'.$field_details['variable'],
  1574. get_lang('OnlyImagesAllowed') . ' ('.implode(',', $allowed_picture_types).')',
  1575. 'filetype',
  1576. $allowed_picture_types
  1577. );
  1578. if (!$admin_permissions) {
  1579. if ($field_details['visible_to_self'] == 0) {
  1580. $form->freeze(
  1581. 'extra_'.$field_details['variable']
  1582. );
  1583. }
  1584. }
  1585. break;
  1586. case self::FIELD_TYPE_FLOAT:
  1587. $form->addElement(
  1588. 'number',
  1589. 'extra_'.$field_details['variable'],
  1590. $field_details['display_text'],
  1591. array('class' => 'span1', 'step' => '0.01')
  1592. );
  1593. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  1594. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  1595. $form->applyFilter('extra_'.$field_details['variable'], 'floatval');
  1596. if (!$admin_permissions) {
  1597. if ($field_details['visible_to_self'] == 0) {
  1598. $form->freeze(
  1599. 'extra_'.$field_details['variable']
  1600. );
  1601. }
  1602. }
  1603. break;
  1604. case self::FIELD_TYPE_FILE:
  1605. $fieldVariable = "extra_{$field_details['variable']}";
  1606. $fieldTexts = array(
  1607. $field_details['display_text']
  1608. );
  1609. if (is_array($extraData) &&
  1610. array_key_exists($fieldVariable, $extraData)
  1611. ) {
  1612. if (file_exists(api_get_path(SYS_UPLOAD_PATH) . $extraData[$fieldVariable])) {
  1613. $fieldTexts[] = Display::url(
  1614. api_get_path(WEB_UPLOAD_PATH) . $extraData[$fieldVariable],
  1615. api_get_path(WEB_UPLOAD_PATH) . $extraData[$fieldVariable],
  1616. array(
  1617. 'title' => $field_details['display_text'],
  1618. 'target' => '_blank'
  1619. )
  1620. );
  1621. }
  1622. }
  1623. $form->addElement(
  1624. 'file',
  1625. $fieldVariable,
  1626. $fieldTexts,
  1627. array()
  1628. );
  1629. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  1630. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  1631. if (!$admin_permissions) {
  1632. if ($field_details['visible_to_self'] == 0) {
  1633. $form->freeze(
  1634. 'extra_'.$field_details['variable']
  1635. );
  1636. }
  1637. }
  1638. break;
  1639. case self::FIELD_TYPE_VIDEO_URL:
  1640. $form->addUrl(
  1641. "extra_{$field_details['variable']}",
  1642. $field_details['display_text'],
  1643. false,
  1644. ['placeholder' => 'https://']
  1645. );
  1646. break;
  1647. case self::FIELD_TYPE_LETTERS_ONLY:
  1648. $form->addTextLettersOnly(
  1649. "extra_{$field_details['variable']}",
  1650. $field_details['display_text']
  1651. );
  1652. $form->applyFilter('extra_' . $field_details['variable'], 'stripslashes');
  1653. if (!$admin_permissions) {
  1654. if ($field_details['visible_to_self'] == 0) {
  1655. $form->freeze(
  1656. 'extra_' . $field_details['variable']
  1657. );
  1658. }
  1659. }
  1660. break;
  1661. case self::FIELD_TYPE_ALPHANUMERIC:
  1662. $form->addTextAlphanumeric(
  1663. "extra_{$field_details['variable']}",
  1664. $field_details['display_text']
  1665. );
  1666. $form->applyFilter(
  1667. 'extra_'.$field_details['variable'],
  1668. 'stripslashes'
  1669. );
  1670. if (!$admin_permissions) {
  1671. if ($field_details['visible_to_self'] == 0) {
  1672. $form->freeze(
  1673. 'extra_' . $field_details['variable']
  1674. );
  1675. }
  1676. }
  1677. break;
  1678. case self::FIELD_TYPE_LETTERS_SPACE:
  1679. $form->addTextLettersAndSpaces(
  1680. "extra_{$field_details['variable']}",
  1681. $field_details['display_text']
  1682. );
  1683. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  1684. if (!$admin_permissions) {
  1685. if ($field_details['visible_to_self'] == 0) {
  1686. $form->freeze(
  1687. 'extra_' . $field_details['variable']
  1688. );
  1689. }
  1690. }
  1691. break;
  1692. case self::FIELD_TYPE_ALPHANUMERIC_SPACE:
  1693. $form->addTextAlphanumericAndSpaces(
  1694. "extra_{$field_details['variable']}",
  1695. $field_details['display_text']
  1696. );
  1697. $form->applyFilter(
  1698. 'extra_'.$field_details['variable'],
  1699. 'stripslashes'
  1700. );
  1701. if (!$admin_permissions) {
  1702. if ($field_details['visible_to_self'] == 0) {
  1703. $form->freeze(
  1704. 'extra_' . $field_details['variable']
  1705. );
  1706. }
  1707. }
  1708. break;
  1709. case self::FIELD_TYPE_GEOLOCALIZATION:
  1710. $dataValue = isset($extraData['extra_'.$field_details['variable']])
  1711. ? $extraData['extra_'.$field_details['variable']]
  1712. : '';
  1713. $form->addElement(
  1714. 'text',
  1715. 'extra_'.$field_details['variable'],
  1716. $field_details['display_text'],
  1717. ['id' => 'extra_'.$field_details['variable']]
  1718. );
  1719. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  1720. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  1721. if (!$admin_permissions) {
  1722. if ($field_details['visible_to_self'] == 0) {
  1723. $form->freeze(
  1724. 'extra_'.$field_details['variable']
  1725. );
  1726. }
  1727. }
  1728. $form->addHtml(
  1729. '<script>
  1730. $(document).ready(function() {
  1731. if (typeof google === "object") {
  1732. var address = "' . $dataValue . '";
  1733. initializeGeo'.$field_details['variable'].'(address, false);
  1734. $("#geolocalization_extra_'.$field_details['variable'].'").on("click", function() {
  1735. var address = $("#extra_'.$field_details['variable'].'").val();
  1736. initializeGeo'.$field_details['variable'].'(address, false);
  1737. return false;
  1738. });
  1739. $("#myLocation_extra_'.$field_details['variable'].'").on("click", function() {
  1740. myLocation'.$field_details['variable'].'();
  1741. return false;
  1742. });
  1743. $("#extra_'.$field_details['variable'].'").keypress(function (event) {
  1744. if (event.which == 13) {
  1745. $("#geolocalization_extra_'.$field_details['variable'].'").click();
  1746. return false;
  1747. }
  1748. });
  1749. } else {
  1750. $("#map_extra_'.$field_details['variable'].'").html("<div class=\"alert alert-info\">'.get_lang('YouNeedToActivateTheGoogleMapsPluginInAdminPlatformToSeeTheMap').'</div>");
  1751. }
  1752. });
  1753. function myLocation'.$field_details['variable'].'() {
  1754. if (navigator.geolocation) {
  1755. var geoPosition = function(position) {
  1756. var lat = position.coords.latitude;
  1757. var lng = position.coords.longitude;
  1758. var latLng = new google.maps.LatLng(lat, lng);
  1759. initializeGeo'.$field_details['variable'].'(false, latLng)
  1760. };
  1761. var geoError = function(error) {
  1762. alert("Geocode ' . get_lang('Error').': " + error);
  1763. };
  1764. var geoOptions = {
  1765. enableHighAccuracy: true
  1766. };
  1767. navigator.geolocation.getCurrentPosition(geoPosition, geoError, geoOptions);
  1768. }
  1769. }
  1770. function initializeGeo'.$field_details['variable'].'(address, latLng) {
  1771. var geocoder = new google.maps.Geocoder();
  1772. var latlng = new google.maps.LatLng(-34.397, 150.644);
  1773. var myOptions = {
  1774. zoom: 15,
  1775. center: latlng,
  1776. mapTypeControl: true,
  1777. mapTypeControlOptions: {
  1778. style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
  1779. },
  1780. navigationControl: true,
  1781. mapTypeId: google.maps.MapTypeId.ROADMAP
  1782. };
  1783. map_'.$field_details['variable'].' = new google.maps.Map(document.getElementById("map_extra_'.$field_details['variable'].'"), myOptions);
  1784. var parameter = address ? { "address": address } : latLng ? { "latLng": latLng } : false;
  1785. if (geocoder && parameter) {
  1786. geocoder.geocode(parameter, function(results, status) {
  1787. if (status == google.maps.GeocoderStatus.OK) {
  1788. if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
  1789. map_'.$field_details['variable'].'.setCenter(results[0].geometry.location);
  1790. if (!address) {
  1791. $("#extra_'.$field_details['variable'].'").val(results[0].formatted_address);
  1792. }
  1793. var infowindow = new google.maps.InfoWindow({
  1794. content: "<b>" + $("#extra_'.$field_details['variable'].'").val() + "</b>",
  1795. size: new google.maps.Size(150, 50)
  1796. });
  1797. var marker = new google.maps.Marker({
  1798. position: results[0].geometry.location,
  1799. map: map_'.$field_details['variable'].',
  1800. title: $("#extra_'.$field_details['variable'].'").val()
  1801. });
  1802. google.maps.event.addListener(marker, "click", function() {
  1803. infowindow.open(map_'.$field_details['variable'].', marker);
  1804. });
  1805. } else {
  1806. alert("' . get_lang("NotFound").'");
  1807. }
  1808. } else {
  1809. alert("Geocode ' . get_lang('Error').': '.get_lang("AddressField").' '.get_lang("NotFound").'");
  1810. }
  1811. });
  1812. }
  1813. }
  1814. </script>
  1815. ');
  1816. $form->addHtml('
  1817. <div class="form-group">
  1818. <label for="geolocalization_extra_'.$field_details['variable'].'" class="col-sm-2 control-label"></label>
  1819. <div class="col-sm-8">
  1820. <button class="null btn btn-default " id="geolocalization_extra_'.$field_details['variable'].'" name="geolocalization_extra_'.$field_details['variable'].'" type="submit"><em class="fa fa-map-marker"></em> '.get_lang('Geolocalization').'</button>
  1821. <button class="null btn btn-default " id="myLocation_extra_'.$field_details['variable'].'" name="myLocation_extra_'.$field_details['variable'].'" type="submit"><em class="fa fa-crosshairs"></em> '.get_lang('MyLocation').'</button>
  1822. </div>
  1823. </div>
  1824. ');
  1825. $form->addHtml('
  1826. <div class="form-group">
  1827. <label for="map_extra_'.$field_details['variable'].'" class="col-sm-2 control-label">
  1828. '.$field_details['display_text'].' - '.get_lang('Map').'
  1829. </label>
  1830. <div class="col-sm-8">
  1831. <div name="map_extra_'.$field_details['variable'].'" id="map_extra_'.$field_details['variable'].'" style="width:100%; height:300px;">
  1832. </div>
  1833. </div>
  1834. </div>
  1835. ');
  1836. break;
  1837. case self::FIELD_TYPE_GEOLOCALIZATION_COORDINATES:
  1838. $dataValue = isset($extraData['extra_'.$field_details['variable']])
  1839. ? $extraData['extra_'.$field_details['variable']]
  1840. : '';
  1841. $form->addElement(
  1842. 'text',
  1843. 'extra_'.$field_details['variable'],
  1844. $field_details['display_text'],
  1845. ['id' => 'extra_'.$field_details['variable']]
  1846. );
  1847. $form->applyFilter('extra_'.$field_details['variable'], 'stripslashes');
  1848. $form->applyFilter('extra_'.$field_details['variable'], 'trim');
  1849. if (!$admin_permissions) {
  1850. if ($field_details['visible_to_self'] == 0) {
  1851. $form->freeze(
  1852. 'extra_'.$field_details['variable']
  1853. );
  1854. }
  1855. }
  1856. $latLag = explode(",", $dataValue);
  1857. // if no value, set default coordinates value
  1858. if (empty($dataValue)) {
  1859. $lat = '-34.397';
  1860. $lng = '150.644';
  1861. } else {
  1862. $lat = $latLag[0];
  1863. $lng = $latLag[1];
  1864. }
  1865. $form->addHtml(
  1866. '<script>
  1867. $(document).ready(function() {
  1868. if (typeof google === "object") {
  1869. var lat = "' . $lat.'";
  1870. var lng = "' . $lng.'";
  1871. var latLng = new google.maps.LatLng(lat, lng);
  1872. initializeGeo'.$field_details['variable'].'(false, latLng);
  1873. $("#geolocalization_extra_'.$field_details['variable'].'").on("click", function() {
  1874. var latLng = $("#extra_'.$field_details['variable'].'").val().split(",");
  1875. var lat = latLng[0];
  1876. var lng = latLng[1];
  1877. var latLng = new google.maps.LatLng(lat, lng);
  1878. initializeGeo'.$field_details['variable'].'(false, latLng);
  1879. return false;
  1880. });
  1881. $("#myLocation_extra_'.$field_details['variable'].'").on("click", function() {
  1882. myLocation'.$field_details['variable'].'();
  1883. return false;
  1884. });
  1885. $("#extra_'.$field_details['variable'].'").keypress(function (event) {
  1886. if (event.which == 13) {
  1887. $("#geolocalization_extra_'.$field_details['variable'].'").click();
  1888. return false;
  1889. }
  1890. });
  1891. } else {
  1892. $("#map_extra_'.$field_details['variable'].'").html("<div class=\"alert alert-info\">'.get_lang('YouNeedToActivateTheGoogleMapsPluginInAdminPlatformToSeeTheMap').'</div>");
  1893. }
  1894. });
  1895. function myLocation'.$field_details['variable'].'() {
  1896. if (navigator.geolocation) {
  1897. var geoPosition = function(position) {
  1898. var lat = position.coords.latitude;
  1899. var lng = position.coords.longitude;
  1900. var latLng = new google.maps.LatLng(lat, lng);
  1901. initializeGeo'.$field_details['variable'].'(false, latLng)
  1902. };
  1903. var geoError = function(error) {
  1904. alert("Geocode ' . get_lang('Error').': " + error);
  1905. };
  1906. var geoOptions = {
  1907. enableHighAccuracy: true
  1908. };
  1909. navigator.geolocation.getCurrentPosition(geoPosition, geoError, geoOptions);
  1910. }
  1911. }
  1912. function initializeGeo'.$field_details['variable'].'(address, latLng) {
  1913. var geocoder = new google.maps.Geocoder();
  1914. var latlng = new google.maps.LatLng(-34.397, 150.644);
  1915. var myOptions = {
  1916. zoom: 15,
  1917. center: latlng,
  1918. mapTypeControl: true,
  1919. mapTypeControlOptions: {
  1920. style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
  1921. },
  1922. navigationControl: true,
  1923. mapTypeId: google.maps.MapTypeId.ROADMAP
  1924. };
  1925. map_'.$field_details['variable'].' = new google.maps.Map(document.getElementById("map_extra_'.$field_details['variable'].'"), myOptions);
  1926. var parameter = address ? { "address": address } : latLng ? { "latLng": latLng } : false;
  1927. if (geocoder && parameter) {
  1928. geocoder.geocode(parameter, function(results, status) {
  1929. if (status == google.maps.GeocoderStatus.OK) {
  1930. if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
  1931. map_'.$field_details['variable'].'.setCenter(results[0].geometry.location);
  1932. $("#extra_'.$field_details['variable'].'").val(results[0].geometry.location.lat() + "," + results[0].geometry.location.lng());
  1933. var infowindow = new google.maps.InfoWindow({
  1934. content: "<b>" + $("#extra_'.$field_details['variable'].'").val() + "</b>",
  1935. size: new google.maps.Size(150, 50)
  1936. });
  1937. var marker = new google.maps.Marker({
  1938. position: results[0].geometry.location,
  1939. map: map_'.$field_details['variable'].',
  1940. title: $("#extra_'.$field_details['variable'].'").val()
  1941. });
  1942. google.maps.event.addListener(marker, "click", function() {
  1943. infowindow.open(map_'.$field_details['variable'].', marker);
  1944. });
  1945. } else {
  1946. alert("' . get_lang("NotFound").'");
  1947. }
  1948. } else {
  1949. alert("Geocode ' . get_lang('Error').': " + status);
  1950. }
  1951. });
  1952. }
  1953. }
  1954. </script>
  1955. ');
  1956. $form->addHtml('
  1957. <div class="form-group">
  1958. <label for="geolocalization_extra_'.$field_details['variable'].'" class="col-sm-2 control-label"></label>
  1959. <div class="col-sm-8">
  1960. <button class="null btn btn-default " id="geolocalization_extra_'.$field_details['variable'].'" name="geolocalization_extra_'.$field_details['variable'].'" type="submit"><em class="fa fa-map-marker"></em> '.get_lang('Geolocalization').'</button>
  1961. <button class="null btn btn-default " id="myLocation_extra_'.$field_details['variable'].'" name="myLocation_extra_'.$field_details['variable'].'" type="submit"><em class="fa fa-crosshairs"></em> '.get_lang('MyLocation').'</button>
  1962. </div>
  1963. </div>
  1964. ');
  1965. $form->addHtml('
  1966. <div class="form-group">
  1967. <label for="map_extra_'.$field_details['variable'].'" class="col-sm-2 control-label">
  1968. '.$field_details['display_text'].' - '.get_lang('Map').'
  1969. </label>
  1970. <div class="col-sm-8">
  1971. <div name="map_extra_'.$field_details['variable'].'" id="map_extra_'.$field_details['variable'].'" style="width:100%; height:300px;">
  1972. </div>
  1973. </div>
  1974. </div>
  1975. ');
  1976. break;
  1977. }
  1978. if (in_array($field_details['variable'], $fieldsToFreeze)) {
  1979. $element = $form->getElement('extra_'.$field_details['variable']);
  1980. $element->freezeSeeOnlySelected = true;
  1981. $form->freeze(
  1982. 'extra_'.$field_details['variable']
  1983. );
  1984. }
  1985. }
  1986. }
  1987. $return = array();
  1988. $return['jquery_ready_content'] = $jquery_ready_content;
  1989. return $return;
  1990. }
  1991. /**
  1992. * @param $breadcrumb
  1993. * @param $action
  1994. */
  1995. public function setupBreadcrumb(&$breadcrumb, $action)
  1996. {
  1997. if ($action == 'add') {
  1998. $breadcrumb[] = array('url' => $this->pageUrl, 'name' => $this->pageName);
  1999. $breadcrumb[] = array('url' => '#', 'name' => get_lang('Add'));
  2000. } elseif ($action == 'edit') {
  2001. $breadcrumb[] = array('url' => $this->pageUrl, 'name' => $this->pageName);
  2002. $breadcrumb[] = array('url' => '#', 'name' => get_lang('Edit'));
  2003. } else {
  2004. $breadcrumb[] = array('url' => '#', 'name' => $this->pageName);
  2005. }
  2006. }
  2007. /**
  2008. * Displays the title + grid
  2009. */
  2010. public function display()
  2011. {
  2012. // action links
  2013. echo '<div class="actions">';
  2014. echo '<a href="../admin/index.php">'.Display::return_icon(
  2015. 'back.png',
  2016. get_lang('BackTo').' '.get_lang('PlatformAdmin'),
  2017. '',
  2018. ICON_SIZE_MEDIUM
  2019. ).'</a>';
  2020. echo '<a href="'.api_get_self().'?action=add&type='.$this->type.'">'.Display::return_icon(
  2021. 'add_user_fields.png',
  2022. get_lang('Add'),
  2023. '',
  2024. ICON_SIZE_MEDIUM
  2025. ).'</a>';
  2026. echo '</div>';
  2027. echo Display::grid_html($this->type.'_fields');
  2028. }
  2029. /**
  2030. * @return array
  2031. */
  2032. public function getJqgridColumnNames()
  2033. {
  2034. return array(
  2035. get_lang('Name'),
  2036. get_lang('FieldLabel'),
  2037. get_lang('Type'),
  2038. get_lang('FieldChangeability'),
  2039. get_lang('VisibleToSelf'),
  2040. get_lang('VisibleToOthers'),
  2041. get_lang('Filter'),
  2042. get_lang('FieldOrder'),
  2043. get_lang('Actions')
  2044. );
  2045. }
  2046. /**
  2047. * @return array
  2048. */
  2049. public function getJqgridColumnModel()
  2050. {
  2051. return array(
  2052. array(
  2053. 'name' => 'display_text',
  2054. 'index' => 'display_text',
  2055. 'width' => '140',
  2056. 'align' => 'left',
  2057. ),
  2058. array(
  2059. 'name' => 'variable',
  2060. 'index' => 'variable',
  2061. 'width' => '90',
  2062. 'align' => 'left',
  2063. 'sortable' => 'true',
  2064. ),
  2065. array(
  2066. 'name' => 'field_type',
  2067. 'index' => 'field_type',
  2068. 'width' => '70',
  2069. 'align' => 'left',
  2070. 'sortable' => 'true',
  2071. ),
  2072. array(
  2073. 'name' => 'changeable',
  2074. 'index' => 'changeable',
  2075. 'width' => '35',
  2076. 'align' => 'left',
  2077. 'sortable' => 'true',
  2078. ),
  2079. array(
  2080. 'name' => 'visible_to_self',
  2081. 'index' => 'visible_to_self',
  2082. 'width' => '45',
  2083. 'align' => 'left',
  2084. 'sortable' => 'true',
  2085. ),
  2086. array(
  2087. 'name' => 'visible_to_others',
  2088. 'index' => 'visible_to_others',
  2089. 'width' => '35',
  2090. 'align' => 'left',
  2091. 'sortable' => 'true',
  2092. ),
  2093. array(
  2094. 'name' => 'filter',
  2095. 'index' => 'filter',
  2096. 'width' => '30',
  2097. 'align' => 'left',
  2098. 'sortable' => 'true',
  2099. ),
  2100. array(
  2101. 'name' => 'field_order',
  2102. 'index' => 'field_order',
  2103. 'width' => '25',
  2104. 'align' => 'left',
  2105. 'sortable' => 'true',
  2106. ),
  2107. array(
  2108. 'name' => 'actions',
  2109. 'index' => 'actions',
  2110. 'width' => '40',
  2111. 'align' => 'left',
  2112. 'formatter' => 'action_formatter',
  2113. 'sortable' => 'false',
  2114. ),
  2115. );
  2116. }
  2117. /**
  2118. * @param string $url
  2119. * @param string $action
  2120. * @return FormValidator
  2121. */
  2122. public function return_form($url, $action)
  2123. {
  2124. $form = new FormValidator($this->type.'_field', 'post', $url);
  2125. $form->addElement('hidden', 'type', $this->type);
  2126. $id = isset($_GET['id']) ? intval($_GET['id']) : null;
  2127. $form->addElement('hidden', 'id', $id);
  2128. // Setting the form elements
  2129. $header = get_lang('Add');
  2130. $defaults = array();
  2131. if ($action == 'edit') {
  2132. $header = get_lang('Modify');
  2133. // Setting the defaults
  2134. $defaults = $this->get($id, false);
  2135. }
  2136. $form->addElement('header', $header);
  2137. if ($action == 'edit') {
  2138. $translateUrl = api_get_path(WEB_CODE_PATH).'extrafield/translate.php?'.http_build_query([
  2139. 'extra_field' => $id
  2140. ]);
  2141. $translateButton = Display::toolbarButton(get_lang('TranslateThisTerm'), $translateUrl, 'language', 'link');
  2142. $form->addText(
  2143. 'display_text',
  2144. [get_lang('Name'), $translateButton]
  2145. );
  2146. } else {
  2147. $form->addElement('text', 'display_text', get_lang('Name'));
  2148. }
  2149. // Field type
  2150. $types = self::get_field_types();
  2151. $form->addElement(
  2152. 'select',
  2153. 'field_type',
  2154. get_lang('FieldType'),
  2155. $types,
  2156. array('id' => 'field_type')
  2157. );
  2158. $form->addElement('label', get_lang('Example'), '<div id="example">-</div>');
  2159. $form->addElement('text', 'variable', get_lang('FieldLabel'), array('class' => 'span5'));
  2160. $form->addElement(
  2161. 'text',
  2162. 'field_options',
  2163. get_lang('FieldPossibleValues'),
  2164. array('id' => 'field_options', 'class' => 'span6')
  2165. );
  2166. $fieldWithOptions = array(
  2167. self::FIELD_TYPE_RADIO,
  2168. self::FIELD_TYPE_SELECT_MULTIPLE,
  2169. self::FIELD_TYPE_SELECT,
  2170. self::FIELD_TYPE_TAG,
  2171. self::FIELD_TYPE_DOUBLE_SELECT,
  2172. );
  2173. if ($action == 'edit') {
  2174. if (in_array($defaults['field_type'], $fieldWithOptions)) {
  2175. $url = Display::url(
  2176. get_lang('EditExtraFieldOptions'),
  2177. 'extra_field_options.php?type='.$this->type.'&field_id='.$id
  2178. );
  2179. $form->addElement('label', null, $url);
  2180. if ($defaults['field_type'] == self::FIELD_TYPE_SELECT) {
  2181. $urlWorkFlow = Display::url(
  2182. get_lang('EditExtraFieldWorkFlow'),
  2183. 'extra_field_workflow.php?type='.$this->type.'&field_id='.$id
  2184. );
  2185. $form->addElement('label', null, $urlWorkFlow);
  2186. }
  2187. $form->freeze('field_options');
  2188. }
  2189. }
  2190. $form->addElement(
  2191. 'text',
  2192. 'default_value',
  2193. get_lang('FieldDefaultValue'),
  2194. array('id' => 'default_value')
  2195. );
  2196. $group = array();
  2197. $group[] = $form->createElement('radio', 'visible_to_self', null, get_lang('Yes'), 1);
  2198. $group[] = $form->createElement('radio', 'visible_to_self', null, get_lang('No'), 0);
  2199. $form->addGroup($group, '', get_lang('VisibleToSelf'), null, false);
  2200. $group = array();
  2201. $group[] = $form->createElement('radio', 'visible_to_others', null, get_lang('Yes'), 1);
  2202. $group[] = $form->createElement('radio', 'visible_to_others', null, get_lang('No'), 0);
  2203. $form->addGroup($group, '', get_lang('VisibleToOthers'), null, false);
  2204. $group = array();
  2205. $group[] = $form->createElement('radio', 'changeable', null, get_lang('Yes'), 1);
  2206. $group[] = $form->createElement('radio', 'changeable', null, get_lang('No'), 0);
  2207. $form->addGroup($group, '', get_lang('FieldChangeability'), null, false);
  2208. $group = array();
  2209. $group[] = $form->createElement('radio', 'filter', null, get_lang('Yes'), 1);
  2210. $group[] = $form->createElement('radio', 'filter', null, get_lang('No'), 0);
  2211. $form->addGroup($group, '', get_lang('FieldFilter'), null, false);
  2212. /* Enable this when field_loggeable is introduced as a table field (2.0)
  2213. $group = array();
  2214. $group[] = $form->createElement('radio', 'field_loggeable', null, get_lang('Yes'), 1);
  2215. $group[] = $form->createElement('radio', 'field_loggeable', null, get_lang('No'), 0);
  2216. $form->addGroup($group, '', get_lang('FieldLoggeable'), '', false);
  2217. */
  2218. $form->addElement('text', 'field_order', get_lang('FieldOrder'));
  2219. if ($action == 'edit') {
  2220. $option = new ExtraFieldOption($this->type);
  2221. if ($defaults['field_type'] == self::FIELD_TYPE_DOUBLE_SELECT) {
  2222. $form->freeze('field_options');
  2223. }
  2224. $defaults['field_options'] = $option->get_field_options_by_field_to_string($id);
  2225. $form->addButtonUpdate(get_lang('Modify'));
  2226. } else {
  2227. $defaults['visible_to_self'] = 0;
  2228. $defaults['visible_to_others'] = 0;
  2229. $defaults['changeable'] = 0;
  2230. $defaults['filter'] = 0;
  2231. $form->addButtonCreate(get_lang('Add'));
  2232. }
  2233. /*if (!empty($defaults['created_at'])) {
  2234. $defaults['created_at'] = api_convert_and_format_date($defaults['created_at']);
  2235. }
  2236. if (!empty($defaults['updated_at'])) {
  2237. $defaults['updated_at'] = api_convert_and_format_date($defaults['updated_at']);
  2238. }*/
  2239. $form->setDefaults($defaults);
  2240. // Setting the rules
  2241. $form->addRule('display_text', get_lang('ThisFieldIsRequired'), 'required');
  2242. $form->addRule('field_type', get_lang('ThisFieldIsRequired'), 'required');
  2243. return $form;
  2244. }
  2245. /**
  2246. * @param $token
  2247. * @return string
  2248. */
  2249. public function getJqgridActionLinks($token)
  2250. {
  2251. //With this function we can add actions to the jgrid (edit, delete, etc)
  2252. $editIcon = Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL);
  2253. $deleteIcon = Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL);
  2254. $confirmMessage = addslashes(
  2255. api_htmlentities(get_lang("ConfirmYourChoice"), ENT_QUOTES)
  2256. );
  2257. $editButton = <<<JAVASCRIPT
  2258. <a href="?action=edit&type={$this->type}&id=' + options.rowId + '" class="btn btn-link btn-xs">\
  2259. $editIcon\
  2260. </a>
  2261. JAVASCRIPT;
  2262. $deleteButton = <<<JAVASCRIPT
  2263. <a \
  2264. onclick="if (!confirm(\'$confirmMessage\')) {return false;}" \
  2265. href="?sec_token=$token&type={$this->type}&id=' + options.rowId + '&action=delete" \
  2266. class="btn btn-link btn-xs">\
  2267. $deleteIcon\
  2268. </a>
  2269. JAVASCRIPT;
  2270. return "function action_formatter(cellvalue, options, rowObject) {
  2271. return '$editButton $deleteButton';
  2272. }";
  2273. }
  2274. /**
  2275. * @param array $columns
  2276. * @param array $column_model
  2277. * @param array $extraFields
  2278. * @return array
  2279. */
  2280. public function getRules(&$columns, &$column_model, $extraFields = array(), $checkExtraFieldExistence = false)
  2281. {
  2282. $fields = $this->get_all(
  2283. array(
  2284. 'visible_to_self = ? AND filter = ?' => array(1, 1)
  2285. ),
  2286. 'display_text'
  2287. );
  2288. $extraFieldOption = new ExtraFieldOption($this->type);
  2289. $rules = array();
  2290. if (!empty($fields)) {
  2291. foreach ($fields as $field) {
  2292. $search_options = array();
  2293. $type = 'text';
  2294. if (in_array($field['field_type'], array(self::FIELD_TYPE_SELECT, self::FIELD_TYPE_DOUBLE_SELECT))) {
  2295. $type = 'select';
  2296. $search_options['sopt'] = array('eq', 'ne'); //equal not equal
  2297. } else {
  2298. $search_options['sopt'] = array('cn', 'nc'); //contains not contains
  2299. }
  2300. $search_options['searchhidden'] = 'true';
  2301. $search_options['defaultValue'] = isset($search_options['field_default_value']) ? $search_options['field_default_value'] : null;
  2302. if ($field['field_type'] == self::FIELD_TYPE_DOUBLE_SELECT) {
  2303. //Add 2 selects
  2304. $options = $extraFieldOption->get_field_options_by_field($field['id']);
  2305. $options = self::extra_field_double_select_convert_array_to_ordered_array($options);
  2306. $first_options = array();
  2307. if (!empty($options)) {
  2308. foreach ($options as $option) {
  2309. foreach ($option as $sub_option) {
  2310. if ($sub_option['option_value'] == 0) {
  2311. $first_options[] = $sub_option['field_id'].'#'.$sub_option['id'].':'.$sub_option['display_text'];
  2312. }
  2313. }
  2314. }
  2315. }
  2316. $search_options['value'] = implode(';', $first_options);
  2317. $search_options['dataInit'] = 'fill_second_select';
  2318. // First
  2319. $column_model[] = array(
  2320. 'name' => 'extra_'.$field['variable'],
  2321. 'index' => 'extra_'.$field['variable'],
  2322. 'width' => '100',
  2323. 'hidden' => 'true',
  2324. 'search' => 'true',
  2325. 'stype' => 'select',
  2326. 'searchoptions' => $search_options,
  2327. );
  2328. $columns[] = $field['display_text'].' (1)';
  2329. $rules[] = array(
  2330. 'field' => 'extra_'.$field['variable'],
  2331. 'op' => 'cn',
  2332. );
  2333. //Second
  2334. $search_options['value'] = $field['id'].':';
  2335. $search_options['dataInit'] = 'register_second_select';
  2336. $column_model[] = array(
  2337. 'name' => 'extra_'.$field['variable'].'_second',
  2338. 'index' => 'extra_'.$field['variable'].'_second',
  2339. 'width' => '100',
  2340. 'hidden' => 'true',
  2341. 'search' => 'true',
  2342. 'stype' => 'select',
  2343. 'searchoptions' => $search_options,
  2344. );
  2345. $columns[] = $field['display_text'].' (2)';
  2346. $rules[] = array('field' => 'extra_'.$field['variable'].'_second', 'op' => 'cn');
  2347. continue;
  2348. } else {
  2349. $search_options['value'] = $extraFieldOption->getFieldOptionsToString(
  2350. $field['id'],
  2351. false,
  2352. 'display_text'
  2353. );
  2354. }
  2355. $column_model[] = array(
  2356. 'name' => 'extra_'.$field['variable'],
  2357. 'index' => 'extra_'.$field['variable'],
  2358. 'width' => '100',
  2359. 'hidden' => 'true',
  2360. 'search' => 'true',
  2361. 'stype' => $type,
  2362. 'searchoptions' => $search_options,
  2363. );
  2364. $columns[] = $field['display_text'];
  2365. $rules[] = array(
  2366. 'field' => 'extra_'.$field['variable'],
  2367. 'op' => 'cn'
  2368. );
  2369. }
  2370. }
  2371. return $rules;
  2372. }
  2373. /**
  2374. * @param array $options
  2375. * @return array
  2376. */
  2377. public function parseConditions($options)
  2378. {
  2379. $inject_extra_fields = null;
  2380. $extraFieldOption = new ExtraFieldOption($this->type);
  2381. $double_fields = array();
  2382. if (isset($options['extra'])) {
  2383. $extra_fields = $options['extra'];
  2384. if (!empty($extra_fields)) {
  2385. $counter = 1;
  2386. foreach ($extra_fields as &$extra) {
  2387. $extra_field_obj = new ExtraField($this->type);
  2388. $extra_field_info = $extra_field_obj->get($extra['id']);
  2389. $extra['extra_field_info'] = $extra_field_info;
  2390. if (isset($extra_field_info['field_type']) && in_array(
  2391. $extra_field_info['field_type'],
  2392. array(
  2393. self::FIELD_TYPE_SELECT,
  2394. self::FIELD_TYPE_SELECT,
  2395. self::FIELD_TYPE_DOUBLE_SELECT
  2396. )
  2397. )
  2398. ) {
  2399. $inject_extra_fields .= " fvo$counter.display_text as {$extra['field']}, ";
  2400. } else {
  2401. $inject_extra_fields .= " fv$counter.value as {$extra['field']}, ";
  2402. }
  2403. if (isset($extra_fields_info[$extra['id']])) {
  2404. $info = $extra_fields_info[$extra['id']];
  2405. } else {
  2406. $info = $this->get($extra['id']);
  2407. $extra_fields_info[$extra['id']] = $info;
  2408. }
  2409. if (isset($info['field_type']) && $info['field_type'] == self::FIELD_TYPE_DOUBLE_SELECT) {
  2410. $double_fields[$info['id']] = $info;
  2411. }
  2412. $counter++;
  2413. }
  2414. }
  2415. }
  2416. $options_by_double = array();
  2417. foreach ($double_fields as $double) {
  2418. $my_options = $extraFieldOption->get_field_options_by_field(
  2419. $double['id'],
  2420. true
  2421. );
  2422. $options_by_double['extra_'.$double['variable']] = $my_options;
  2423. }
  2424. $field_value_to_join = array();
  2425. //filter can be all/any = and/or
  2426. $inject_joins = null;
  2427. $inject_where = null;
  2428. $where = null;
  2429. if (!empty($options['where'])) {
  2430. if (!empty($options['extra'])) {
  2431. // Removing double 1=1
  2432. $options['where'] = str_replace(' 1 = 1 AND', '', $options['where']);
  2433. // Always OR
  2434. $counter = 1;
  2435. foreach ($extra_fields as $extra_info) {
  2436. $extra_field_info = $extra_info['extra_field_info'];
  2437. $inject_joins .= " INNER JOIN $this->table_field_values fv$counter
  2438. ON (s.".$this->primaryKey." = fv$counter.".$this->handler_id.") ";
  2439. // Add options
  2440. if (isset($extra_field_info['field_type']) && in_array(
  2441. $extra_field_info['field_type'],
  2442. array(
  2443. self::FIELD_TYPE_SELECT,
  2444. self::FIELD_TYPE_SELECT,
  2445. self::FIELD_TYPE_DOUBLE_SELECT
  2446. )
  2447. )
  2448. ) {
  2449. $options['where'] = str_replace(
  2450. $extra_info['field'],
  2451. 'fv'.$counter.'.field_id = '.$extra_info['id'].' AND fvo'.$counter.'.option_value',
  2452. $options['where']
  2453. );
  2454. $inject_joins .= "
  2455. INNER JOIN $this->table_field_options fvo$counter
  2456. ON (
  2457. fv$counter.field_id = fvo$counter.field_id AND
  2458. fv$counter.value = fvo$counter.option_value
  2459. )
  2460. ";
  2461. } else if (isset($extra_field_info['field_type']) &&
  2462. $extra_field_info['field_type'] == self::FIELD_TYPE_TAG
  2463. ) {
  2464. $options['where'] = str_replace(
  2465. $extra_info['field'],
  2466. 'tag'.$counter.'.tag ',
  2467. $options['where']
  2468. );
  2469. $inject_joins .= "
  2470. INNER JOIN $this->table_field_rel_tag tag_rel$counter
  2471. ON (tag_rel$counter.field_id = ".$extra_info['id']." AND tag_rel$counter.item_id = s.".$this->primaryKey.")
  2472. INNER JOIN $this->table_field_tag tag$counter
  2473. ON (tag$counter.id = tag_rel$counter.tag_id)
  2474. ";
  2475. } else {
  2476. //text, textarea, etc
  2477. $options['where'] = str_replace(
  2478. $extra_info['field'],
  2479. 'fv'.$counter.'.field_id = '.$extra_info['id'].' AND fv'.$counter.'.value',
  2480. $options['where']
  2481. );
  2482. }
  2483. $field_value_to_join[] = " fv$counter.$this->handler_id ";
  2484. $counter++;
  2485. }
  2486. if (!empty($field_value_to_join)) {
  2487. //$inject_where .= " AND s.id = ".implode(' = ', $field_value_to_join);
  2488. }
  2489. }
  2490. $where .= ' AND '.$options['where'];
  2491. }
  2492. $order = null;
  2493. if (!empty($options['order'])) {
  2494. $order = " ORDER BY ".$options['order'];
  2495. }
  2496. $limit = null;
  2497. if (!empty($options['limit'])) {
  2498. $limit = " LIMIT ".$options['limit'];
  2499. }
  2500. return array(
  2501. 'order' => $order,
  2502. 'limit' => $limit,
  2503. 'where' => $where,
  2504. 'inject_where' => $inject_where,
  2505. 'inject_joins' => $inject_joins,
  2506. 'field_value_to_join' => $field_value_to_join,
  2507. 'inject_extra_fields' => $inject_extra_fields,
  2508. );
  2509. }
  2510. //@todo move this in the display_class or somewhere else
  2511. /**
  2512. * @param $col
  2513. * @param $oper
  2514. * @param $val
  2515. * @return string
  2516. */
  2517. public function get_where_clause($col, $oper, $val)
  2518. {
  2519. if (empty($col)) {
  2520. return '';
  2521. }
  2522. if ($oper == 'bw' || $oper == 'bn') {
  2523. $val .= '%';
  2524. }
  2525. if ($oper == 'ew' || $oper == 'en') {
  2526. $val = '%'.$val;
  2527. }
  2528. if ($oper == 'cn' || $oper == 'nc' || $oper == 'in' || $oper == 'ni') {
  2529. if (is_array($val) || is_object($val)) {
  2530. $val = (array) $val;
  2531. $result = '"%'.implode(';', $val).'%"';
  2532. foreach ($val as $item) {
  2533. $item = trim($item);
  2534. $result .= ' OR '.$col.' LIKE "%'.$item.'%"';
  2535. }
  2536. $val = $result;
  2537. return " $col {$this->ops[$oper]} $val ";
  2538. } else {
  2539. $val = '%'.$val.'%';
  2540. }
  2541. }
  2542. $val = \Database::escape_string($val);
  2543. return " $col {$this->ops[$oper]} '$val' ";
  2544. }
  2545. /**
  2546. * @param $filters
  2547. * @param string $stringToSearch
  2548. * @return array
  2549. */
  2550. public function getExtraFieldRules($filters, $stringToSearch = 'extra_')
  2551. {
  2552. $extra_fields = array();
  2553. // Getting double select if exists
  2554. $double_select = array();
  2555. foreach ($filters->rules as $rule) {
  2556. if (empty($rule)) {
  2557. continue;
  2558. }
  2559. if (strpos($rule->field, '_second') === false) {
  2560. } else {
  2561. $my_field = str_replace('_second', '', $rule->field);
  2562. $double_select[$my_field] = $rule->data;
  2563. }
  2564. }
  2565. $condition_array = array();
  2566. foreach ($filters->rules as $rule) {
  2567. if (empty($rule)) {
  2568. continue;
  2569. }
  2570. if (strpos($rule->field, $stringToSearch) === false) {
  2571. // normal fields
  2572. $field = $rule->field;
  2573. if (isset($rule->data) && is_string($rule->data) && $rule->data != -1) {
  2574. $condition_array[] = $this->get_where_clause($field, $rule->op, $rule->data);
  2575. }
  2576. } else {
  2577. // Extra fields
  2578. if (strpos($rule->field, '_second') === false) {
  2579. //No _second
  2580. $original_field = str_replace($stringToSearch, '', $rule->field);
  2581. $field_option = $this->get_handler_field_info_by_field_variable($original_field);
  2582. if ($field_option['field_type'] == self::FIELD_TYPE_DOUBLE_SELECT) {
  2583. if (isset($double_select[$rule->field])) {
  2584. $data = explode('#', $rule->data);
  2585. $rule->data = $data[1].'::'.$double_select[$rule->field];
  2586. } else {
  2587. // only was sent 1 select
  2588. if (is_string($rule->data)) {
  2589. $data = explode('#', $rule->data);
  2590. $rule->data = $data[1];
  2591. }
  2592. }
  2593. if (!isset($rule->data)) {
  2594. $condition_array[] = ' ('.$this->get_where_clause($rule->field, $rule->op, $rule->data).') ';
  2595. $extra_fields[] = array('field' => $rule->field, 'id' => $field_option['id']);
  2596. }
  2597. } else {
  2598. if (isset($rule->data)) {
  2599. if ($rule->data == -1) {
  2600. continue;
  2601. }
  2602. $condition_array[] = ' ('.$this->get_where_clause($rule->field, $rule->op, $rule->data).') ';
  2603. $extra_fields[] = array(
  2604. 'field' => $rule->field,
  2605. 'id' => $field_option['id'],
  2606. 'data' => $rule->data
  2607. );
  2608. }
  2609. }
  2610. } else {
  2611. $my_field = str_replace('_second', '', $rule->field);
  2612. $original_field = str_replace($stringToSearch, '', $my_field);
  2613. $field_option = $this->get_handler_field_info_by_field_variable($original_field);
  2614. $extra_fields[] = array(
  2615. 'field' => $rule->field,
  2616. 'id' => $field_option['id']
  2617. );
  2618. }
  2619. }
  2620. }
  2621. return array(
  2622. 'extra_fields' => $extra_fields,
  2623. 'condition_array' => $condition_array
  2624. );
  2625. }
  2626. /**
  2627. * Get the extra fields and their formatted values
  2628. * @param int|string $itemId The item ID (It could be a session_id, course_id or user_id)
  2629. * @return array The extra fields data
  2630. */
  2631. public function getDataAndFormattedValues($itemId)
  2632. {
  2633. $valuesData = array();
  2634. $fields = $this->get_all();
  2635. $em = Database::getManager();
  2636. foreach ($fields as $field) {
  2637. if ($field['visible_to_self'] != '1') {
  2638. continue;
  2639. }
  2640. $fieldValue = new ExtraFieldValue($this->type);
  2641. $valueData = $fieldValue->get_values_by_handler_and_field_id($itemId, $field['id'], true);
  2642. if ($field['field_type'] == ExtraField::FIELD_TYPE_TAG) {
  2643. $tags = $em
  2644. ->getRepository('ChamiloCoreBundle:ExtraFieldRelTag')
  2645. ->findBy(
  2646. [
  2647. 'fieldId' => $field['id'],
  2648. 'itemId' => $itemId,
  2649. ]
  2650. );
  2651. if ($tags) {
  2652. /** @var \Chamilo\CoreBundle\Entity\ExtraFieldRelTag $tag */
  2653. $data = [];
  2654. foreach ($tags as $extraFieldTag) {
  2655. /** @var \Chamilo\CoreBundle\Entity\Tag $tag */
  2656. $tag = $em->find('ChamiloCoreBundle:Tag', $extraFieldTag->getTagId());
  2657. /*$data[] = [
  2658. 'id' => $extraFieldTag->getTagId(),
  2659. 'value' => $tag->getTag()
  2660. ];*/
  2661. $data[] = $tag->getTag();
  2662. }
  2663. $valueData = implode(',', $data);
  2664. }
  2665. }
  2666. if (!$valueData) {
  2667. continue;
  2668. }
  2669. $displayedValue = get_lang('None');
  2670. switch ($field['field_type']) {
  2671. case self::FIELD_TYPE_CHECKBOX:
  2672. if ($valueData !== false && $valueData['value'] == '1') {
  2673. $displayedValue = get_lang('Yes');
  2674. } else {
  2675. $displayedValue = get_lang('No');
  2676. }
  2677. break;
  2678. case self::FIELD_TYPE_DATE:
  2679. if ($valueData !== false && !empty($valueData['value'])) {
  2680. $displayedValue = api_format_date($valueData['value'], DATE_FORMAT_LONG_NO_DAY);
  2681. }
  2682. break;
  2683. case ExtraField::FIELD_TYPE_TAG:
  2684. if (!empty($valueData)) {
  2685. $displayedValue = $valueData;
  2686. }
  2687. break;
  2688. case ExtraField::FIELD_TYPE_FILE_IMAGE:
  2689. if ($valueData === false || empty($valueData['value'])) {
  2690. break;
  2691. }
  2692. if (!file_exists(api_get_path(SYS_UPLOAD_PATH).$valueData['value'])) {
  2693. break;
  2694. }
  2695. $image = Display::img(
  2696. api_get_path(WEB_UPLOAD_PATH).$valueData['value'],
  2697. $field['display_text'],
  2698. array('width' => '300')
  2699. );
  2700. $displayedValue = Display::url(
  2701. $image,
  2702. api_get_path(WEB_UPLOAD_PATH).$valueData['value'],
  2703. array('target' => '_blank')
  2704. );
  2705. break;
  2706. case self::FIELD_TYPE_FILE:
  2707. if ($valueData === false || empty($valueData['value'])) {
  2708. break;
  2709. }
  2710. if (!file_exists(api_get_path(SYS_UPLOAD_PATH).$valueData['value'])) {
  2711. break;
  2712. }
  2713. $displayedValue = Display::url(
  2714. get_lang('Download'),
  2715. api_get_path(WEB_UPLOAD_PATH).$valueData['value'],
  2716. array(
  2717. 'title' => $field['display_text'],
  2718. 'target' => '_blank'
  2719. )
  2720. );
  2721. break;
  2722. default:
  2723. $displayedValue = $valueData['value'];
  2724. break;
  2725. }
  2726. $valuesData[] = array(
  2727. 'text' => $field['display_text'],
  2728. 'value' => $displayedValue
  2729. );
  2730. }
  2731. return $valuesData;
  2732. }
  2733. /**
  2734. * Gets an element
  2735. * @param int $id
  2736. * @param bool $translateDisplayText Optional
  2737. * @return array
  2738. */
  2739. public function get($id, $translateDisplayText = true)
  2740. {
  2741. $info = parent::get($id);
  2742. if ($translateDisplayText) {
  2743. $info['display_text'] = self::translateDisplayName($info['variable'], $info['display_text']);
  2744. }
  2745. return $info;
  2746. }
  2747. /**
  2748. * Translate the display text for a extra field
  2749. * @param string $variable
  2750. * @param string $defaultDisplayText
  2751. * @return string
  2752. */
  2753. public static function translateDisplayName($variable, $defaultDisplayText)
  2754. {
  2755. $camelCase = api_underscore_to_camel_case($variable);
  2756. return isset($GLOBALS[$camelCase]) ? $GLOBALS[$camelCase] : $defaultDisplayText;
  2757. }
  2758. /**
  2759. * @param int $fieldId
  2760. * @param string $tag
  2761. *
  2762. * @return array
  2763. */
  2764. public function getAllUserPerTag($fieldId, $tag)
  2765. {
  2766. $tagRelUserTable = Database::get_main_table(TABLE_MAIN_USER_REL_TAG);
  2767. $tag = Database::escape_string($tag);
  2768. $fieldId = (int) $fieldId;
  2769. $sql = "SELECT user_id
  2770. FROM {$this->table_field_tag} f INNER JOIN $tagRelUserTable ft
  2771. ON tag_id = f.id
  2772. WHERE tag = '$tag' AND f.field_id = $fieldId;
  2773. ";
  2774. $result = Database::query($sql);
  2775. return Database::store_result($result, 'ASSOC');
  2776. }
  2777. /**
  2778. * @param int $fieldId
  2779. * @param int $tagId
  2780. *
  2781. * @return array
  2782. */
  2783. public function getAllSkillPerTag($fieldId, $tagId)
  2784. {
  2785. $skillTable = Database::get_main_table(TABLE_MAIN_SKILL);
  2786. $tagRelExtraTable = Database::get_main_table(TABLE_MAIN_EXTRA_FIELD_REL_TAG);
  2787. $fieldId = intval($fieldId);
  2788. $tagId = intval($tagId);
  2789. $sql = "SELECT s.id
  2790. FROM $skillTable s INNER JOIN $tagRelExtraTable t
  2791. ON t.item_id = s.id
  2792. WHERE tag_id = $tagId AND t.field_id = $fieldId;
  2793. ";
  2794. $result = Database::query($sql);
  2795. $result = Database::store_result($result, 'ASSOC');
  2796. $skillList = [];
  2797. foreach ($result as $index => $value) {
  2798. $skillList[$value['id']] = $value['id'];
  2799. }
  2800. return $skillList;
  2801. }
  2802. /**
  2803. * @param string $from
  2804. * @param string $search
  2805. *
  2806. * @return array
  2807. */
  2808. public function searchOptionsFromTags($from, $search, $options)
  2809. {
  2810. $extraFieldInfo = $this->get_handler_field_info_by_field_variable(str_replace('extra_', '', $from));
  2811. $extraFieldInfoTag = $this->get_handler_field_info_by_field_variable(str_replace('extra_', '', $search));
  2812. if (empty($extraFieldInfo) || empty($extraFieldInfoTag)) {
  2813. return [];
  2814. }
  2815. $id = $extraFieldInfo['id'];
  2816. $tagId = $extraFieldInfoTag['id'];
  2817. $table = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
  2818. $tagRelExtraTable = Database::get_main_table(TABLE_MAIN_EXTRA_FIELD_REL_TAG);
  2819. $tagTable = Database::get_main_table(TABLE_MAIN_TAG);
  2820. $optionsTable = Database::get_main_table(TABLE_EXTRA_FIELD_OPTIONS);
  2821. $sql = "SELECT DISTINCT t.*, v.value, o.display_text
  2822. FROM $tagRelExtraTable te
  2823. INNER JOIN $tagTable t
  2824. ON (t.id = te.tag_id AND te.field_id = t.field_id AND te.field_id = $tagId)
  2825. INNER JOIN $table v
  2826. ON (te.item_id = v.item_id AND v.field_id = $id)
  2827. INNER JOIN $optionsTable o
  2828. ON (o.option_value = v.value)
  2829. WHERE v.value IN ('".implode("','", $options)."')
  2830. ORDER BY o.option_order, t.tag
  2831. ";
  2832. $result = Database::query($sql);
  2833. $result = Database::store_result($result);
  2834. return $result;
  2835. }
  2836. }