extra_field.lib.php 146 KB

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