admin.inc 203 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581
  1. <?php
  2. /**
  3. * @file
  4. * Provides the Views' administrative interface.
  5. */
  6. /**
  7. * Create an array of Views admin CSS for adding or attaching.
  8. *
  9. * @return array
  10. * An array of arrays. Each array represents a single file. The array format
  11. * is:
  12. * - file: The fully qualified name of the file to send to drupal_add_css
  13. * - options: An array of options to pass to drupal_add_css.
  14. */
  15. function views_ui_get_admin_css() {
  16. $module_path = drupal_get_path('module', 'views_ui');
  17. $list = array();
  18. $list[$module_path . '/css/views-admin.css'] = array();
  19. $list[$module_path . '/css/ie/views-admin.ie7.css'] = array(
  20. 'browsers' => array(
  21. 'IE' => 'lte IE 7',
  22. '!IE' => FALSE,
  23. ),
  24. 'preprocess' => FALSE,
  25. );
  26. $list[$module_path . '/css/views-admin.theme.css'] = array();
  27. // Add in any theme specific CSS files we have.
  28. $themes = list_themes();
  29. $theme_key = $GLOBALS['theme'];
  30. while ($theme_key) {
  31. // Try to find the admin css file for non-core themes.
  32. if (!in_array($theme_key, array('garland', 'seven', 'bartik'))) {
  33. $theme_path = drupal_get_path('theme', $theme_key);
  34. // First search in the css directory, then in the root folder of the
  35. // theme.
  36. if (file_exists($theme_path . "/css/views-admin.$theme_key.css")) {
  37. $list[$theme_path . "/css/views-admin.$theme_key.css"] = array(
  38. 'group' => CSS_THEME,
  39. );
  40. }
  41. elseif (file_exists($theme_path . "/views-admin.$theme_key.css")) {
  42. $list[$theme_path . "/views-admin.$theme_key.css"] = array(
  43. 'group' => CSS_THEME,
  44. );
  45. }
  46. }
  47. else {
  48. $list[$module_path . "/css/views-admin.$theme_key.css"] = array(
  49. 'group' => CSS_THEME,
  50. );
  51. }
  52. $theme_key = isset($themes[$theme_key]->base_theme) ? $themes[$theme_key]->base_theme : '';
  53. }
  54. // Views contains style overrides for the following modules.
  55. $module_list = array('contextual', 'advanced_help', 'ctools');
  56. foreach ($module_list as $module) {
  57. if (module_exists($module)) {
  58. $list[$module_path . '/css/views-admin.' . $module . '.css'] = array();
  59. }
  60. }
  61. return $list;
  62. }
  63. /**
  64. * Adds standard Views administration CSS to the current page.
  65. */
  66. function views_ui_add_admin_css() {
  67. foreach (views_ui_get_admin_css() as $file => $options) {
  68. drupal_add_css($file, $options);
  69. }
  70. }
  71. /**
  72. * Check to see if the advanced help module is installed.
  73. *
  74. * If not display a message.
  75. *
  76. * Only call this function if the user is already in a position for this to be
  77. * useful.
  78. */
  79. function views_ui_check_advanced_help() {
  80. if (!variable_get('views_ui_show_advanced_help_warning', TRUE)) {
  81. return;
  82. }
  83. if (!module_exists('advanced_help')) {
  84. $filename = db_query_range("SELECT filename FROM {system} WHERE type = 'module' AND name = 'advanced_help'", 0, 1)
  85. ->fetchField();
  86. if ($filename && file_exists($filename)) {
  87. drupal_set_message(t('If you <a href="@modules">enable the advanced help module</a>, Views will provide more and better help. <a href="@hide">You can disable this message at the Views settings page.</a>',
  88. array(
  89. '@modules' => url('admin/modules'),
  90. '@hide' => url('admin/structure/views/settings'),
  91. )));
  92. }
  93. else {
  94. drupal_set_message(t('If you install the advanced help module from !href, Views will provide more and better help. <a href="@hide">You can disable this message at the Views settings page.</a>',
  95. array(
  96. '!href' => l('http://drupal.org/project/advanced_help', 'http://drupal.org/project/advanced_help'),
  97. '@hide' => url('admin/structure/views/settings'),
  98. )));
  99. }
  100. }
  101. }
  102. /**
  103. * Returns the results of the live preview.
  104. */
  105. function views_ui_preview($view, $display_id, $args = array()) {
  106. // When this function is invoked as a page callback, each Views argument is
  107. // passed separately.
  108. if (!is_array($args)) {
  109. $args = array_slice(func_get_args(), 2);
  110. }
  111. // Save $_GET['q'] so it can be restored before returning from this function.
  112. $q = $_GET['q'];
  113. // Determine where the query and performance statistics should be output.
  114. $show_query = variable_get('views_ui_show_sql_query', FALSE);
  115. $show_info = variable_get('views_ui_show_preview_information', FALSE);
  116. $show_location = variable_get('views_ui_show_sql_query_where', 'above');
  117. $show_stats = variable_get('views_ui_show_performance_statistics', FALSE);
  118. if ($show_stats) {
  119. $show_stats = variable_get('views_ui_show_sql_query_where', 'above');
  120. }
  121. $combined = $show_query && $show_stats;
  122. $rows = array('query' => array(), 'statistics' => array());
  123. $output = '';
  124. $errors = $view->validate();
  125. if ($errors === TRUE) {
  126. $view->ajax = TRUE;
  127. $view->live_preview = TRUE;
  128. $view->views_ui_context = TRUE;
  129. // AJAX happens via $_POST but everything expects exposed data to be in
  130. // GET. Copy stuff but remove ajax-framework specific keys. If we're
  131. // clicking on links in a preview, though, we could actually still have
  132. // some in $_GET, so we use $_REQUEST to ensure we get it all.
  133. $exposed_input = $_REQUEST;
  134. foreach (array('view_name', 'view_display_id', 'view_args', 'view_path', 'view_dom_id', 'pager_element', 'view_base_path', 'ajax_html_ids', 'ajax_page_state', 'form_id', 'form_build_id', 'form_token') as $key) {
  135. if (isset($exposed_input[$key])) {
  136. unset($exposed_input[$key]);
  137. }
  138. }
  139. $view->set_exposed_input($exposed_input);
  140. if (!$view->set_display($display_id)) {
  141. return t('Invalid display id @display', array('@display' => $display_id));
  142. }
  143. $view->set_arguments($args);
  144. // Store the current view URL for later use.
  145. if ($view->display_handler->get_option('path')) {
  146. $path = $view->get_url();
  147. }
  148. // Make view links come back to preview.
  149. $view->override_path = 'admin/structure/views/nojs/preview/' . $view->name . '/' . $display_id;
  150. // Also override $_GET['q'] so we get the pager.
  151. $original_path = current_path();
  152. $_GET['q'] = $view->override_path;
  153. if ($args) {
  154. $_GET['q'] .= '/' . implode('/', $args);
  155. }
  156. // Suppress contextual links of entities within the result set during a
  157. // Preview.
  158. // @todo We'll want to add contextual links specific to editing the View, so
  159. // the suppression may need to be moved deeper into the Preview pipeline.
  160. views_ui_contextual_links_suppress_push();
  161. $preview = $view->preview($display_id, $args);
  162. views_ui_contextual_links_suppress_pop();
  163. // Reset variables.
  164. unset($view->override_path);
  165. $_GET['q'] = $original_path;
  166. // Prepare the query information and statistics to show either above or
  167. // below the view preview.
  168. if ($show_info || $show_query || $show_stats) {
  169. // Get information from the preview for display.
  170. if (!empty($view->build_info['query'])) {
  171. if ($show_query) {
  172. $query = $view->build_info['query'];
  173. // Only the SQL default class has a method getArguments.
  174. $quoted = array();
  175. if (get_class($view->query) == 'views_plugin_query_default') {
  176. $quoted = $query->getArguments();
  177. $connection = Database::getConnection();
  178. foreach ($quoted as $key => $val) {
  179. if (is_array($val)) {
  180. $quoted[$key] = implode(', ', array_map(array($connection, 'quote'), $val));
  181. }
  182. else {
  183. $quoted[$key] = $connection->quote($val);
  184. }
  185. }
  186. }
  187. $rows['query'][] = array('<strong>' . t('Query') . '</strong>', '<pre>' . check_plain(strtr($query, $quoted)) . '</pre>');
  188. if (!empty($view->additional_queries)) {
  189. $queries = '<strong>' . t('These queries were run during view rendering:') . '</strong>';
  190. foreach ($view->additional_queries as $query) {
  191. if ($queries) {
  192. $queries .= "\n";
  193. }
  194. $queries .= t('[@time ms]', array('@time' => intval($query[1] * 100000) / 100)) . ' ' . $query[0];
  195. }
  196. $rows['query'][] = array('<strong>' . t('Other queries') . '</strong>', '<pre>' . $queries . '</pre>');
  197. }
  198. }
  199. if ($show_info) {
  200. $rows['query'][] = array('<strong>' . t('Title') . '</strong>', filter_xss_admin($view->get_title()));
  201. if (isset($path)) {
  202. $path = l($path, $path);
  203. }
  204. else {
  205. $path = t('This display has no path.');
  206. }
  207. $rows['query'][] = array('<strong>' . t('Path') . '</strong>', $path);
  208. }
  209. if ($show_stats) {
  210. $rows['statistics'][] = array('<strong>' . t('Query build time') . '</strong>', t('@time ms', array('@time' => intval($view->build_time * 100000) / 100)));
  211. $rows['statistics'][] = array('<strong>' . t('Query execute time') . '</strong>', t('@time ms', array('@time' => intval($view->execute_time * 100000) / 100)));
  212. $rows['statistics'][] = array('<strong>' . t('View render time') . '</strong>', t('@time ms', array('@time' => intval($view->render_time * 100000) / 100)));
  213. }
  214. drupal_alter('views_preview_info', $rows, $view);
  215. }
  216. else {
  217. // No query was run. Display that information in place of either the
  218. // query or the performance statistics, whichever comes first.
  219. if ($combined || ($show_location === 'above')) {
  220. $rows['query'] = array(array('<strong>' . t('Query') . '</strong>', t('No query was run')));
  221. }
  222. else {
  223. $rows['statistics'] = array(array('<strong>' . t('Query') . '</strong>', t('No query was run')));
  224. }
  225. }
  226. }
  227. }
  228. else {
  229. foreach ($errors as $error) {
  230. drupal_set_message($error, 'error');
  231. }
  232. $preview = t('Unable to preview due to validation errors.');
  233. }
  234. // Assemble the preview, the query info, and the query statistics in the
  235. // requested order.
  236. if ($show_location === 'above') {
  237. if ($combined) {
  238. $output .= '<div class="views-query-info">' . theme('table', array('rows' => array_merge($rows['query'], $rows['statistics']))) . '</div>';
  239. }
  240. else {
  241. $output .= '<div class="views-query-info">' . theme('table', array('rows' => $rows['query'])) . '</div>';
  242. }
  243. }
  244. elseif ($show_stats === 'above') {
  245. $output .= '<div class="views-query-info">' . theme('table', array('rows' => $rows['statistics'])) . '</div>';
  246. }
  247. $output .= $preview;
  248. if ($show_location === 'below') {
  249. if ($combined) {
  250. $output .= '<div class="views-query-info">' . theme('table', array('rows' => array_merge($rows['query'], $rows['statistics']))) . '</div>';
  251. }
  252. else {
  253. $output .= '<div class="views-query-info">' . theme('table', array('rows' => $rows['query'])) . '</div>';
  254. }
  255. }
  256. elseif ($show_stats === 'below') {
  257. $output .= '<div class="views-query-info">' . theme('table', array('rows' => $rows['statistics'])) . '</div>';
  258. }
  259. $_GET['q'] = $q;
  260. return $output;
  261. }
  262. /**
  263. * Page callback to add a new view.
  264. */
  265. function views_ui_add_page() {
  266. views_ui_add_admin_css();
  267. drupal_set_title(t('Add new view'));
  268. return drupal_get_form('views_ui_add_form');
  269. }
  270. /**
  271. * Form builder for the "add new view" page.
  272. */
  273. function views_ui_add_form($form, &$form_state) {
  274. ctools_include('dependent');
  275. $form['#attached']['js'][] = drupal_get_path('module', 'views_ui') . '/js/views-admin.js';
  276. $form['#attributes']['class'] = array('views-admin');
  277. $form['human_name'] = array(
  278. '#type' => 'textfield',
  279. '#title' => t('View name'),
  280. '#required' => TRUE,
  281. '#size' => 32,
  282. '#default_value' => !empty($form_state['view']) ? $form_state['view']->human_name : '',
  283. '#maxlength' => 255,
  284. );
  285. $form['name'] = array(
  286. '#type' => 'machine_name',
  287. '#maxlength' => 128,
  288. '#machine_name' => array(
  289. 'exists' => 'views_get_view',
  290. 'source' => array('human_name'),
  291. ),
  292. '#description' => t('A unique machine-readable name for this View. It must only contain lowercase letters, numbers, and underscores.'),
  293. );
  294. $form['description_enable'] = array(
  295. '#type' => 'checkbox',
  296. '#title' => t('Description'),
  297. );
  298. $form['description'] = array(
  299. '#type' => 'textfield',
  300. '#title' => t('Provide description'),
  301. '#title_display' => 'invisible',
  302. '#size' => 64,
  303. '#default_value' => !empty($form_state['view']) ? $form_state['view']->description : '',
  304. '#dependency' => array(
  305. 'edit-description-enable' => array(1),
  306. ),
  307. );
  308. // Create a wrapper for the entire dynamic portion of the form. Everything
  309. // that can be updated by AJAX goes somewhere inside here. For example, this
  310. // is needed by "Show" dropdown (below); it changes the base table of the
  311. // view and therefore potentially requires all options on the form to be
  312. // dynamically updated.
  313. $form['displays'] = array();
  314. // Create the part of the form that allows the user to select the basic
  315. // properties of what the view will display.
  316. $form['displays']['show'] = array(
  317. '#type' => 'fieldset',
  318. '#tree' => TRUE,
  319. '#attributes' => array('class' => array('container-inline')),
  320. );
  321. // Create the "Show" dropdown, which allows the base table of the view to be
  322. // selected.
  323. $wizard_plugins = views_ui_get_wizards();
  324. $options = array();
  325. foreach ($wizard_plugins as $key => $wizard) {
  326. $options[$key] = $wizard['title'];
  327. }
  328. $form['displays']['show']['wizard_key'] = array(
  329. '#type' => 'select',
  330. '#title' => t('Show'),
  331. '#options' => $options,
  332. );
  333. $show_form = &$form['displays']['show'];
  334. $show_form['wizard_key']['#default_value'] = views_ui_get_selected($form_state, array('show', 'wizard_key'), 'node', $show_form['wizard_key']);
  335. // Changing this dropdown updates the entire content of $form['displays'] via
  336. // AJAX.
  337. views_ui_add_ajax_trigger($show_form, 'wizard_key', array('displays'));
  338. // Build the rest of the form based on the currently selected wizard plugin.
  339. $wizard_key = $show_form['wizard_key']['#default_value'];
  340. $get_instance = $wizard_plugins[$wizard_key]['get_instance'];
  341. $wizard_instance = $get_instance($wizard_plugins[$wizard_key]);
  342. $form = $wizard_instance->build_form($form, $form_state);
  343. $form['save'] = array(
  344. '#type' => 'submit',
  345. '#value' => t('Save & exit'),
  346. '#validate' => array('views_ui_wizard_form_validate'),
  347. '#submit' => array('views_ui_add_form_save_submit'),
  348. );
  349. $form['continue'] = array(
  350. '#type' => 'submit',
  351. '#value' => t('Continue & edit'),
  352. '#validate' => array('views_ui_wizard_form_validate'),
  353. '#submit' => array('views_ui_add_form_store_edit_submit'),
  354. '#process' => array_merge(array('views_ui_default_button'), element_info_property('submit', '#process', array())),
  355. );
  356. $form['cancel'] = array(
  357. '#type' => 'submit',
  358. '#value' => t('Cancel'),
  359. '#submit' => array('views_ui_add_form_cancel_submit'),
  360. '#limit_validation_errors' => array(),
  361. );
  362. return $form;
  363. }
  364. /**
  365. * Helper form element validator: integer.
  366. *
  367. * The problem with this is that the function is private so it's not guaranteed
  368. * that it might not be renamed/changed. In the future field.module or something
  369. * else should provide a public validate function.
  370. *
  371. * @see _element_validate_integer_positive()
  372. */
  373. function views_element_validate_integer($element, &$form_state) {
  374. $value = $element['#value'];
  375. if ($value !== '' && (!is_numeric($value) || intval($value) != $value || abs($value) != $value)) {
  376. form_error($element, t('%name must be a positive integer.', array('%name' => $element['#title'])));
  377. }
  378. }
  379. /**
  380. * Gets the current value of a #select element, from within a form constructor.
  381. *
  382. * This function is intended for use in highly dynamic forms (in particular the
  383. * add view wizard) which are rebuilt in different ways depending on which
  384. * triggering element (AJAX or otherwise) was most recently fired. For example,
  385. * sometimes it is necessary to decide how to build one dynamic form element
  386. * based on the value of a different dynamic form element that may not have
  387. * even been present on the form the last time it was submitted. This function
  388. * takes care of resolving those conflicts and gives you the proper current
  389. * value of the requested #select element.
  390. *
  391. * By necessity, this function sometimes uses non-validated user input from
  392. * $form_state['input'] in making its determination. Although it performs some
  393. * minor validation of its own, it is not complete. The intention is that the
  394. * return value of this function should only be used to help decide how to
  395. * build the current form the next time it is reloaded, not to be saved as if
  396. * it had gone through the normal, final form validation process. Do NOT use
  397. * the results of this function for any other purpose besides deciding how to
  398. * build the next version of the form.
  399. *
  400. * @param array $form_state
  401. * The standard associative array containing the current state of the form.
  402. * @param array $parents
  403. * An array of parent keys that point to the part of the submitted form
  404. * values that are expected to contain the element's value (in the case where
  405. * this form element was actually submitted). In a simple case (assuming
  406. * #tree is TRUE throughout the form), if the select element is located in
  407. * $form['wrapper']['select'], so that the submitted form values would
  408. * normally be found in $form_state['values']['wrapper']['select'], you would
  409. * pass array('wrapper', 'select') for this parameter.
  410. * @param mixed $default_value
  411. * The default value to return if the #select element does not currently have
  412. * a proper value set based on the submitted input.
  413. * @param array $element
  414. * An array representing the current version of the #select element within
  415. * the form.
  416. *
  417. * @return array
  418. * The current value of the #select element. A common use for this is to feed
  419. * it back into $element['#default_value'] so that the form will be rendered
  420. * with the correct value selected.
  421. */
  422. function views_ui_get_selected($form_state, $parents, $default_value, $element) {
  423. // For now, don't trust this to work on anything but a #select element.
  424. if (!isset($element['#type']) || $element['#type'] != 'select' || !isset($element['#options'])) {
  425. return $default_value;
  426. }
  427. // If there is a user-submitted value for this element that matches one of
  428. // the currently available options attached to it, use that. We need to check
  429. // $form_state['input'] rather than $form_state['values'] here because the
  430. // triggering element often has the #limit_validation_errors property set to
  431. // prevent unwanted errors elsewhere on the form. This means that the
  432. // $form_state['values'] array won't be complete. We could make it complete
  433. // by adding each required part of the form to the #limit_validation_errors
  434. // property individually as the form is being built, but this is difficult to
  435. // do for a highly dynamic and extensible form. This method is much simpler.
  436. if (!empty($form_state['input'])) {
  437. $key_exists = NULL;
  438. $submitted = drupal_array_get_nested_value($form_state['input'], $parents, $key_exists);
  439. // Check that the user-submitted value is one of the allowed options before
  440. // returning it. This is not a substitute for actual form validation;
  441. // rather it is necessary because, for example, the same select element
  442. // might have #options A, B, and C under one set of conditions but #options
  443. // D, E, F under a different set of conditions. So the form submission
  444. // might have occurred with option A selected, but when the form is rebuilt
  445. // option A is no longer one of the choices. In that case, we don't want to
  446. // use the value that was submitted anymore but rather fall back to the
  447. // default value.
  448. if ($key_exists && in_array($submitted, array_keys($element['#options']))) {
  449. return $submitted;
  450. }
  451. }
  452. // Fall back on returning the default value if nothing was returned above.
  453. return $default_value;
  454. }
  455. /**
  456. * Converts a form element in the add view wizard to be AJAX-enabled.
  457. *
  458. * This function takes a form element and adds AJAX behaviors to it such that
  459. * changing it triggers another part of the form to update automatically. It
  460. * also adds a submit button to the form that appears next to the triggering
  461. * element and that duplicates its functionality for users who do not have
  462. * JavaScript enabled (the button is automatically hidden for users who do have
  463. * JavaScript).
  464. *
  465. * To use this function, call it directly from your form builder function
  466. * immediately after you have defined the form element that will serve as the
  467. * JavaScript trigger. Calling it elsewhere (such as in hook_form_alter()) may
  468. * mean that the non-JavaScript fallback button does not appear in the correct
  469. * place in the form.
  470. *
  471. * @param array $wrapping_element
  472. * The element whose child will server as the AJAX trigger. For example, if
  473. * $form['some_wrapper']['triggering_element'] represents the element which
  474. * will trigger the AJAX behavior, you would pass $form['some_wrapper'] for
  475. * this parameter.
  476. * @param string $trigger_key
  477. * The key within the wrapping element that identifies which of its children
  478. * serves as the AJAX trigger. In the above example, you would pass
  479. * 'triggering_element' for this parameter.
  480. * @param array $refresh_parents
  481. * An array of parent keys that point to the part of the form that will be
  482. * refreshed by AJAX. For example, if triggering the AJAX behavior should
  483. * cause $form['dynamic_content']['section'] to be refreshed, you would pass
  484. * array('dynamic_content', 'section') for this parameter.
  485. */
  486. function views_ui_add_ajax_trigger(&$wrapping_element, $trigger_key, $refresh_parents) {
  487. $seen_ids = &drupal_static(__FUNCTION__ . ':seen_ids', array());
  488. $seen_buttons = &drupal_static(__FUNCTION__ . ':seen_buttons', array());
  489. // Add the AJAX behavior to the triggering element.
  490. $triggering_element = &$wrapping_element[$trigger_key];
  491. $triggering_element['#ajax']['callback'] = 'views_ui_ajax_update_form';
  492. // We do not use drupal_html_id() to get an ID for the AJAX wrapper, because
  493. // it remembers IDs across AJAX requests (and won't reuse them), but in our
  494. // case we need to use the same ID from request to request so that the
  495. // wrapper can be recognized by the AJAX system and its content can be
  496. // dynamically updated. So instead, we will keep track of duplicate IDs
  497. // (within a single request) on our own, later in this function.
  498. $triggering_element['#ajax']['wrapper'] = 'edit-view-' . implode('-', $refresh_parents) . '-wrapper';
  499. // Add a submit button for users who do not have JavaScript enabled. It
  500. // should be displayed next to the triggering element on the form.
  501. $button_key = $trigger_key . '_trigger_update';
  502. $wrapping_element[$button_key] = array(
  503. '#type' => 'submit',
  504. // Hide this button when JavaScript is enabled.
  505. '#attributes' => array('class' => array('js-hide')),
  506. '#submit' => array('views_ui_nojs_submit'),
  507. // Add a process function to limit this button's validation errors to the
  508. // triggering element only. We have to do this in #process since until the
  509. // form API has added the #parents property to the triggering element for
  510. // us, we don't have any (easy) way to find out where its submitted values
  511. // will eventually appear in $form_state['values'].
  512. '#process' => array_merge(array('views_ui_add_limited_validation'), element_info_property('submit', '#process', array())),
  513. // Add an after-build function that inserts a wrapper around the region of
  514. // the form that needs to be refreshed by AJAX (so that the AJAX system can
  515. // detect and dynamically update it). This is done in #after_build because
  516. // it's a convenient place where we have automatic access to the complete
  517. // form array, but also to minimize the chance that the HTML we add will
  518. // get clobbered by code that runs after we have added it.
  519. '#after_build' => array_merge(element_info_property('submit', '#after_build', array()), array('views_ui_add_ajax_wrapper')),
  520. );
  521. // Copy #weight and #access from the triggering element to the button, so
  522. // that the two elements will be displayed together.
  523. foreach (array('#weight', '#access') as $property) {
  524. if (isset($triggering_element[$property])) {
  525. $wrapping_element[$button_key][$property] = $triggering_element[$property];
  526. }
  527. }
  528. // For easiest integration with the form API and the testing framework, we
  529. // always give the button a unique #value, rather than playing around with
  530. // #name.
  531. $button_title = !empty($triggering_element['#title']) ? $triggering_element['#title'] : $trigger_key;
  532. if (empty($seen_buttons[$button_title])) {
  533. $wrapping_element[$button_key]['#value'] = t('Update "@title" choice', array(
  534. '@title' => $button_title,
  535. ));
  536. $seen_buttons[$button_title] = 1;
  537. }
  538. else {
  539. $wrapping_element[$button_key]['#value'] = t('Update "@title" choice (@number)', array(
  540. '@title' => $button_title,
  541. '@number' => ++$seen_buttons[$button_title],
  542. ));
  543. }
  544. // Attach custom data to the triggering element and submit button, so we can
  545. // use it in both the process function and AJAX callback.
  546. $ajax_data = array(
  547. 'wrapper' => $triggering_element['#ajax']['wrapper'],
  548. 'trigger_key' => $trigger_key,
  549. 'refresh_parents' => $refresh_parents,
  550. // Keep track of duplicate wrappers so we don't add the same wrapper to the
  551. // page more than once.
  552. 'duplicate_wrapper' => !empty($seen_ids[$triggering_element['#ajax']['wrapper']]),
  553. );
  554. $seen_ids[$triggering_element['#ajax']['wrapper']] = TRUE;
  555. $triggering_element['#views_ui_ajax_data'] = $ajax_data;
  556. $wrapping_element[$button_key]['#views_ui_ajax_data'] = $ajax_data;
  557. }
  558. /**
  559. * Processes a non-JS fallback submit button to limit its validation errors.
  560. */
  561. function views_ui_add_limited_validation($element, &$form_state) {
  562. // Retrieve the AJAX triggering element so we can determine its parents. (We
  563. // know it's at the same level of the complete form array as the submit
  564. // button, so all we have to do to find it is swap out the submit button's
  565. // last array parent.)
  566. $array_parents = $element['#array_parents'];
  567. array_pop($array_parents);
  568. $array_parents[] = $element['#views_ui_ajax_data']['trigger_key'];
  569. $ajax_triggering_element = drupal_array_get_nested_value($form_state['complete form'], $array_parents);
  570. // Limit this button's validation to the AJAX triggering element, so it can
  571. // update the form for that change without requiring that the rest of the
  572. // form be filled out properly yet.
  573. $element['#limit_validation_errors'] = array($ajax_triggering_element['#parents']);
  574. // If we are in the process of a form submission and this is the button that
  575. // was clicked, the form API workflow in form_builder() will have already
  576. // copied it to $form_state['triggering_element'] before our #process
  577. // function is run. So we need to make the same modifications in $form_state
  578. // as we did to the element itself, to ensure that #limit_validation_errors
  579. // will actually be set in the correct place.
  580. if (!empty($form_state['triggering_element'])) {
  581. $clicked_button = &$form_state['triggering_element'];
  582. if ($clicked_button['#name'] == $element['#name'] && $clicked_button['#value'] == $element['#value']) {
  583. $clicked_button['#limit_validation_errors'] = $element['#limit_validation_errors'];
  584. }
  585. }
  586. return $element;
  587. }
  588. /**
  589. * After-build function that adds a wrapper to a form region (AJAX refreshes).
  590. *
  591. * This function inserts a wrapper around the region of the form that needs to
  592. * be refreshed by AJAX, based on information stored in the corresponding submit
  593. * button form element.
  594. */
  595. function views_ui_add_ajax_wrapper($element, &$form_state) {
  596. // Don't add the wrapper <div> if the same one was already inserted on this
  597. // form.
  598. if (empty($element['#views_ui_ajax_data']['duplicate_wrapper'])) {
  599. // Find the region of the complete form that needs to be refreshed by AJAX.
  600. // This was earlier stored in a property on the element.
  601. $complete_form = &$form_state['complete form'];
  602. $refresh_parents = $element['#views_ui_ajax_data']['refresh_parents'];
  603. $refresh_element = drupal_array_get_nested_value($complete_form, $refresh_parents);
  604. // The HTML ID that AJAX expects was also stored in a property on the
  605. // element, so use that information to insert the wrapper <div> here.
  606. $id = $element['#views_ui_ajax_data']['wrapper'];
  607. $refresh_element += array(
  608. '#prefix' => '',
  609. '#suffix' => '',
  610. );
  611. $refresh_element['#prefix'] = '<div id="' . $id . '" class="views-ui-ajax-wrapper">' . $refresh_element['#prefix'];
  612. $refresh_element['#suffix'] .= '</div>';
  613. // Copy the element that needs to be refreshed back into the form, with our
  614. // modifications to it.
  615. drupal_array_set_nested_value($complete_form, $refresh_parents, $refresh_element);
  616. }
  617. return $element;
  618. }
  619. /**
  620. * Updates a part of the add view form via AJAX.
  621. *
  622. * @return
  623. * The part of the form that has changed.
  624. */
  625. function views_ui_ajax_update_form($form, $form_state) {
  626. // The region that needs to be updated was stored in a property of the
  627. // triggering element by views_ui_add_ajax_trigger(), so all we have to do is
  628. // retrieve that here.
  629. return drupal_array_get_nested_value($form, $form_state['triggering_element']['#views_ui_ajax_data']['refresh_parents']);
  630. }
  631. /**
  632. * Non-JavaScript fallback for updating the add view form.
  633. */
  634. function views_ui_nojs_submit($form, &$form_state) {
  635. $form_state['rebuild'] = TRUE;
  636. }
  637. /**
  638. * Validate the add view form.
  639. */
  640. function views_ui_wizard_form_validate($form, &$form_state) {
  641. $wizard = views_ui_get_wizard($form_state['values']['show']['wizard_key']);
  642. $form_state['wizard'] = $wizard;
  643. $get_instance = $wizard['get_instance'];
  644. $form_state['wizard_instance'] = $get_instance($wizard);
  645. $errors = $form_state['wizard_instance']->validate($form, $form_state);
  646. foreach ($errors as $name => $message) {
  647. form_set_error($name, $message);
  648. }
  649. }
  650. /**
  651. * Process the add view form, 'save'.
  652. */
  653. function views_ui_add_form_save_submit($form, &$form_state) {
  654. try {
  655. $view = $form_state['wizard_instance']->create_view($form, $form_state);
  656. }
  657. catch (ViewsWizardException $e) {
  658. drupal_set_message($e->getMessage(), 'error');
  659. $form_state['redirect'] = 'admin/structure/views';
  660. }
  661. $view->save();
  662. $form_state['redirect'] = 'admin/structure/views';
  663. if (!empty($view->display['page'])) {
  664. $display = $view->display['page'];
  665. if ($display->handler->has_path()) {
  666. $one_path = $display->handler->get_option('path');
  667. if (strpos($one_path, '%') === FALSE) {
  668. $form_state['redirect'] = $one_path;
  669. // PATH TO THE VIEW IF IT HAS ONE.
  670. return;
  671. }
  672. }
  673. }
  674. drupal_set_message(t('Your view was saved. You may edit it from the list below.'));
  675. }
  676. /**
  677. * Process the add view form, 'continue'.
  678. */
  679. function views_ui_add_form_store_edit_submit($form, &$form_state) {
  680. try {
  681. $view = $form_state['wizard_instance']->create_view($form, $form_state);
  682. }
  683. catch (ViewsWizardException $e) {
  684. drupal_set_message($e->getMessage(), 'error');
  685. $form_state['redirect'] = 'admin/structure/views';
  686. }
  687. // Just cache it temporarily to edit it.
  688. views_ui_cache_set($view);
  689. // If there is a destination query, ensure we still redirect the user to the
  690. // edit view page, and then redirect the user to the destination.
  691. $destination = array();
  692. if (isset($_GET['destination'])) {
  693. $destination = drupal_get_destination();
  694. unset($_GET['destination']);
  695. }
  696. $form_state['redirect'] = array('admin/structure/views/view/' . $view->name, array('query' => $destination));
  697. }
  698. /**
  699. * Cancel the add view form.
  700. */
  701. function views_ui_add_form_cancel_submit($form, &$form_state) {
  702. $form_state['redirect'] = 'admin/structure/views';
  703. }
  704. /**
  705. * Form element validation handler for a taxonomy autocomplete field.
  706. *
  707. * This allows a taxonomy autocomplete field to be validated outside the
  708. * standard Field API workflow, without passing in a complete field widget.
  709. * Instead, all that is required is that $element['#field_name'] contain the
  710. * name of the taxonomy autocomplete field that is being validated.
  711. *
  712. * This function is currently not used for validation directly, although it
  713. * could be. Instead, it is only used to store the term IDs and vocabulary name
  714. * in the element value, based on the tags that the user typed in.
  715. *
  716. * @see taxonomy_autocomplete_validate()
  717. */
  718. function views_ui_taxonomy_autocomplete_validate($element, &$form_state) {
  719. $value = array();
  720. if ($tags = $element['#value']) {
  721. // Get the machine names of the vocabularies we will search, keyed by the
  722. // vocabulary IDs.
  723. $field = field_info_field($element['#field_name']);
  724. $vocabularies = array();
  725. if (!empty($field['settings']['allowed_values'])) {
  726. foreach ($field['settings']['allowed_values'] as $tree) {
  727. if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {
  728. $vocabularies[$vocabulary->vid] = $tree['vocabulary'];
  729. }
  730. }
  731. }
  732. // Store the term ID of each (valid) tag that the user typed.
  733. $typed_terms = drupal_explode_tags($tags);
  734. foreach ($typed_terms as $typed_term) {
  735. if ($terms = taxonomy_term_load_multiple(array(), array('name' => trim($typed_term), 'vid' => array_keys($vocabularies)))) {
  736. $term = array_pop($terms);
  737. $value['tids'][] = $term->tid;
  738. }
  739. }
  740. // Store the term IDs along with the name of the vocabulary. Currently
  741. // Views (as well as the Field UI) assumes that there will only be one
  742. // vocabulary, although technically the API allows there to be more than
  743. // one.
  744. if (!empty($value['tids'])) {
  745. $value['tids'] = array_unique($value['tids']);
  746. $value['vocabulary'] = array_pop($vocabularies);
  747. }
  748. }
  749. form_set_value($element, $value, $form_state);
  750. }
  751. /**
  752. * Theme function; returns basic administrative information about a view.
  753. *
  754. * TODO: template + preprocess.
  755. */
  756. function theme_views_ui_view_info($variables) {
  757. $view = $variables['view'];
  758. $title = $view->get_human_name();
  759. $displays = _views_ui_get_displays_list($view);
  760. $displays = empty($displays) ? t('None') : format_plural(count($displays), 'Display', 'Displays') . ': ' . '<em>' . implode(', ', $displays) . '</em>';
  761. switch ($view->type) {
  762. case t('Default'):
  763. default:
  764. $type = t('In code');
  765. break;
  766. case t('Normal'):
  767. $type = t('In database');
  768. break;
  769. case t('Overridden'):
  770. $type = t('Database overriding code');
  771. break;
  772. }
  773. $output = '';
  774. $output .= '<div class="views-ui-view-title">' . check_plain($title) . "</div>\n";
  775. $output .= '<div class="views-ui-view-displays">' . $displays . "</div>\n";
  776. $output .= '<div class="views-ui-view-storage">' . $type . "</div>\n";
  777. $output .= '<div class="views-ui-view-base">' . t('Type') . ': ' . check_plain($variables['base']) . "</div>\n";
  778. return $output;
  779. }
  780. /**
  781. * Page to delete a view.
  782. */
  783. function views_ui_break_lock_confirm($form, &$form_state, $view) {
  784. $form_state['view'] = &$view;
  785. $form = array();
  786. if (empty($view->locked)) {
  787. $form['message']['#markup'] = t('There is no lock on view %name to break.', array('%name' => $view->name));
  788. return $form;
  789. }
  790. $cancel = 'admin/structure/views/view/' . $view->name . '/edit';
  791. $account = user_load($view->locked->uid);
  792. return confirm_form($form,
  793. t('Are you sure you want to break the lock on view %name?',
  794. array('%name' => $view->name)),
  795. $cancel,
  796. t('By breaking this lock, any unsaved changes made by !user will be lost!', array('!user' => theme('username', array('account' => $account)))),
  797. t('Break lock'),
  798. t('Cancel'));
  799. }
  800. /**
  801. * Submit handler to break_lock a view.
  802. */
  803. function views_ui_break_lock_confirm_submit(&$form, &$form_state) {
  804. ctools_object_cache_clear_all('view', $form_state['view']->name);
  805. $form_state['redirect'] = 'admin/structure/views/view/' . $form_state['view']->name . '/edit';
  806. drupal_set_message(t('The lock has been broken and you may now edit this view.'));
  807. }
  808. /**
  809. * Helper function to return the used display_id for the edit page.
  810. *
  811. * This function handles access to the display.
  812. */
  813. function views_ui_edit_page_display($view, $display_id) {
  814. // Determine the displays available for editing.
  815. if ($tabs = views_ui_edit_page_display_tabs($view, $display_id)) {
  816. // If a display isn't specified, use the first one.
  817. if (empty($display_id)) {
  818. foreach ($tabs as $id => $tab) {
  819. if (!isset($tab['#access']) || $tab['#access']) {
  820. $display_id = $id;
  821. break;
  822. }
  823. }
  824. }
  825. // If a display is specified, but we don't have access to it, return
  826. // an access denied page.
  827. if ($display_id && (!isset($tabs[$display_id]) || (isset($tabs[$display_id]['#access']) && !$tabs[$display_id]['#access']))) {
  828. return MENU_ACCESS_DENIED;
  829. }
  830. return $display_id;
  831. }
  832. elseif ($display_id) {
  833. return MENU_ACCESS_DENIED;
  834. }
  835. else {
  836. $display_id = NULL;
  837. }
  838. return $display_id;
  839. }
  840. /**
  841. * Page callback for the Edit View page.
  842. */
  843. function views_ui_edit_page($view, $display_id = NULL) {
  844. $display_id = views_ui_edit_page_display($view, $display_id);
  845. if (!in_array($display_id, array(MENU_ACCESS_DENIED, MENU_NOT_FOUND))) {
  846. $build = array();
  847. $build['edit_form'] = drupal_get_form('views_ui_edit_form', $view, $display_id);
  848. $build['preview'] = views_ui_build_preview($view, $display_id, FALSE);
  849. }
  850. else {
  851. $build = $display_id;
  852. }
  853. return $build;
  854. }
  855. /**
  856. *
  857. */
  858. function views_ui_build_preview($view, $display_id, $render = TRUE) {
  859. if (isset($_POST['ajax_html_ids'])) {
  860. unset($_POST['ajax_html_ids']);
  861. }
  862. $build = array(
  863. '#theme_wrappers' => array('container'),
  864. '#attributes' => array('id' => 'views-preview-wrapper', 'class' => 'views-admin clearfix'),
  865. );
  866. $form_state = array('build_info' => array('args' => array($view, $display_id)));
  867. $build['controls'] = drupal_build_form('views_ui_preview_form', $form_state);
  868. $args = array();
  869. if (!empty($form_state['values']['view_args'])) {
  870. $args = explode('/', $form_state['values']['view_args']);
  871. }
  872. $build['preview'] = array(
  873. '#theme_wrappers' => array('container'),
  874. '#attributes' => array('id' => 'views-live-preview'),
  875. '#markup' => $render ? views_ui_preview($view->clone_view(), $display_id, $args) : '',
  876. );
  877. return $build;
  878. }
  879. /**
  880. * Form builder callback for editing a View.
  881. *
  882. * @todo Remove as many #prefix/#suffix lines as possible. Use #theme_wrappers
  883. * instead.
  884. *
  885. * @todo Rename to views_ui_edit_view_form(). See that function for the "old"
  886. * version.
  887. *
  888. * @see views_ui_ajax_get_form()
  889. */
  890. function views_ui_edit_form($form, &$form_state, $view, $display_id = NULL) {
  891. // Do not allow the form to be cached, because $form_state['view'] can become
  892. // stale between page requests.
  893. // See views_ui_ajax_get_form() for how this affects #ajax.
  894. // @todo To remove this and allow the form to be cacheable:
  895. // - Change $form_state['view'] to $form_state['temporary']['view'].
  896. // - Add a #process function to initialize $form_state['temporary']['view']
  897. // on cached form submissions.
  898. // - Update ctools_include() to support cached forms, or else use
  899. // form_load_include().
  900. $form_state['no_cache'] = TRUE;
  901. if ($display_id) {
  902. if (!$view->set_display($display_id)) {
  903. $form['#markup'] = t('Invalid display id @display', array('@display' => $display_id));
  904. return $form;
  905. }
  906. $view->fix_missing_relationships();
  907. }
  908. ctools_include('dependent');
  909. $form['#attached']['js'][] = ctools_attach_js('dependent');
  910. $form['#attached']['js'][] = ctools_attach_js('collapsible-div');
  911. $form['#tree'] = TRUE;
  912. // @todo When more functionality is added to this form, cloning here may be
  913. // too soon. But some of what we do with $view later in this function
  914. // results in making it unserializable due to PDO limitations.
  915. $form_state['view'] = clone $view;
  916. $form['#attached']['library'][] = array('system', 'ui.tabs');
  917. $form['#attached']['library'][] = array('system', 'ui.dialog');
  918. $form['#attached']['library'][] = array('system', 'drupal.ajax');
  919. $form['#attached']['library'][] = array('system', 'jquery.form');
  920. // @todo This should be getting added to the page when an ajax popup calls
  921. // for it, instead of having to add it manually here.
  922. $form['#attached']['js'][] = 'misc/tabledrag.js';
  923. $form['#attached']['css'] = views_ui_get_admin_css();
  924. $module_path = drupal_get_path('module', 'views_ui');
  925. $form['#attached']['js'][] = $module_path . '/js/views-admin.js';
  926. $form['#attached']['js'][] = array(
  927. 'data' => array(
  928. 'views' => array(
  929. 'ajax' => array(
  930. 'id' => '#views-ajax-body',
  931. 'title' => '#views-ajax-title',
  932. 'popup' => '#views-ajax-popup',
  933. 'defaultForm' => views_ui_get_default_ajax_message(),
  934. ),
  935. ),
  936. ),
  937. 'type' => 'setting',
  938. );
  939. $form += array(
  940. '#prefix' => '',
  941. '#suffix' => '',
  942. );
  943. $form['#prefix'] = $form['#prefix'] . '<div class="views-edit-view views-admin clearfix">';
  944. $form['#suffix'] = '</div>' . $form['#suffix'];
  945. $form['#attributes']['class'] = array('form-edit');
  946. if (isset($view->locked) && is_object($view->locked)) {
  947. $form['locked'] = array(
  948. '#theme_wrappers' => array('container'),
  949. '#attributes' => array(
  950. 'class' => array('view-locked', 'messages', 'warning'),
  951. ),
  952. '#markup' => t('This view is being edited by user !user, and is therefore locked from editing by others. This lock is !age old. Click here to <a href="!break">break this lock</a>.', array('!user' => theme('username', array('account' => user_load($view->locked->uid))), '!age' => format_interval(REQUEST_TIME - $view->locked->updated), '!break' => url('admin/structure/views/view/' . $view->name . '/break-lock'))),
  953. );
  954. }
  955. if (isset($view->vid) && $view->vid == 'new') {
  956. $message = t('* All changes are stored temporarily. Click Save to make your changes permanent. Click Cancel to discard the view.');
  957. }
  958. else {
  959. $message = t('* All changes are stored temporarily. Click Save to make your changes permanent. Click Cancel to discard your changes.');
  960. }
  961. $form['changed'] = array(
  962. '#theme_wrappers' => array('container'),
  963. '#attributes' => array(
  964. 'class' => array('view-changed', 'messages', 'warning'),
  965. ),
  966. '#markup' => $message,
  967. );
  968. if (empty($view->changed)) {
  969. $form['changed']['#attributes']['class'][] = 'js-hide';
  970. }
  971. $form['help_text'] = array(
  972. '#prefix' => '<div>',
  973. '#suffix' => '</div>',
  974. '#markup' => t('Modify the display(s) of your view below or add new displays.'),
  975. );
  976. $form['actions'] = array(
  977. '#type' => 'actions',
  978. '#weight' => 0,
  979. );
  980. if (empty($view->changed)) {
  981. $form['actions']['#attributes'] = array(
  982. 'class' => array(
  983. 'js-hide',
  984. ),
  985. );
  986. }
  987. $form['actions']['save'] = array(
  988. '#type' => 'submit',
  989. '#value' => t('Save'),
  990. // Taken from the "old" UI. @todo: Review and rename.
  991. '#validate' => array('views_ui_edit_view_form_validate'),
  992. '#submit' => array('views_ui_edit_view_form_submit'),
  993. );
  994. $form['actions']['cancel'] = array(
  995. '#type' => 'submit',
  996. '#value' => t('Cancel'),
  997. '#submit' => array('views_ui_edit_view_form_cancel'),
  998. );
  999. $form['displays'] = array(
  1000. '#prefix' => '<h1 class="unit-title clearfix">' . t('Displays') . "</h1>\n"
  1001. . '<div class="views-displays">',
  1002. '#suffix' => '</div>',
  1003. );
  1004. $form['displays']['top'] = views_ui_render_display_top($view, $display_id);
  1005. // The rest requires a display to be selected.
  1006. if ($display_id) {
  1007. $form_state['display_id'] = $display_id;
  1008. // The part of the page where editing will take place. This element is the
  1009. // CTools collapsible-div container for the display edit elements.
  1010. $form['displays']['settings'] = array(
  1011. '#theme_wrappers' => array('container'),
  1012. '#attributes' => array(
  1013. 'class' => array(
  1014. 'views-display-settings',
  1015. 'box-margin',
  1016. 'ctools-collapsible-container',
  1017. ),
  1018. ),
  1019. '#id' => 'edit-display-settings',
  1020. );
  1021. $display_title = views_ui_get_display_label($view, $display_id, FALSE);
  1022. // Add a handle for the ctools collapsible-div. The handle is the title of
  1023. // the display.
  1024. $form['displays']['settings']['tab_title']['#markup'] = '<h2 id="edit-display-settings-title" class="ctools-collapsible-handle">' . t('@display_title details', array('@display_title' => ucwords($display_title))) . '</h2>';
  1025. // Add a text that the display is disabled.
  1026. if (!empty($view->display[$display_id]->handler)) {
  1027. $enabled = $view->display[$display_id]->handler->get_option('enabled');
  1028. if (empty($enabled)) {
  1029. $form['displays']['settings']['disabled']['#markup'] = t('This display is disabled.');
  1030. }
  1031. }
  1032. // The ctools collapsible-div content.
  1033. $form['displays']['settings']['settings_content'] = array(
  1034. '#theme_wrappers' => array('container'),
  1035. '#id' => 'edit-display-settings-content',
  1036. '#attributes' => array(
  1037. 'class' => array(
  1038. 'ctools-collapsible-content',
  1039. ),
  1040. ),
  1041. );
  1042. // Add the edit display content.
  1043. $form['displays']['settings']['settings_content']['tab_content'] = views_ui_get_display_tab($view, $display_id);
  1044. $form['displays']['settings']['settings_content']['tab_content']['#theme_wrappers'] = array('container');
  1045. $form['displays']['settings']['settings_content']['tab_content']['#attributes'] = array('class' => array('views-display-tab'));
  1046. $form['displays']['settings']['settings_content']['tab_content']['#id'] = 'views-tab-' . $display_id;
  1047. // Mark deleted displays as such.
  1048. if (!empty($view->display[$display_id]->deleted)) {
  1049. $form['displays']['settings']['settings_content']['tab_content']['#attributes']['class'][] = 'views-display-deleted';
  1050. }
  1051. // Mark disabled displays as such.
  1052. if (empty($enabled)) {
  1053. $form['displays']['settings']['settings_content']['tab_content']['#attributes']['class'][] = 'views-display-disabled';
  1054. }
  1055. // The content of the popup dialog.
  1056. $form['ajax-area'] = array(
  1057. '#theme_wrappers' => array('container'),
  1058. '#id' => 'views-ajax-popup',
  1059. );
  1060. $form['ajax-area']['ajax-title'] = array(
  1061. '#markup' => '<h2 id="views-ajax-title"></h2>',
  1062. );
  1063. $form['ajax-area']['ajax-body'] = array(
  1064. '#theme_wrappers' => array('container'),
  1065. '#id' => 'views-ajax-body',
  1066. '#markup' => views_ui_get_default_ajax_message(),
  1067. );
  1068. }
  1069. // If relationships had to be fixed, we want to get that into the cache
  1070. // so that edits work properly, and to try to get the user to save it
  1071. // so that it's not using weird fixed up relationships.
  1072. if (!empty($view->relationships_changed) && empty($_POST)) {
  1073. drupal_set_message(t('This view has been automatically updated to fix missing relationships. While this View should continue to work, you should verify that the automatic updates are correct and save this view.'));
  1074. views_ui_cache_set($view);
  1075. }
  1076. return $form;
  1077. }
  1078. /**
  1079. * Provide the preview formulas and the preview output, too.
  1080. */
  1081. function views_ui_preview_form($form, &$form_state, $view, $display_id = 'default') {
  1082. $form_state['no_cache'] = TRUE;
  1083. $form_state['view'] = $view;
  1084. $form['#attributes'] = array('class' => array('clearfix'));
  1085. // Add a checkbox controlling whether or not this display auto-previews.
  1086. $form['live_preview'] = array(
  1087. '#type' => 'checkbox',
  1088. '#id' => 'edit-displays-live-preview',
  1089. '#title' => t('Auto preview'),
  1090. '#default_value' => variable_get('views_ui_always_live_preview', TRUE),
  1091. );
  1092. // Add the arguments textfield.
  1093. $form['view_args'] = array(
  1094. '#type' => 'textfield',
  1095. '#title' => t('Preview with contextual filters:'),
  1096. '#description' => t('Separate contextual filter values with a "/". For example, %example.', array('%example' => '40/12/10')),
  1097. '#id' => 'preview-args',
  1098. // '#attributes' => array('class' => array('ctools-auto-submit')),
  1099. );
  1100. // Add the preview button.
  1101. $form['button'] = array(
  1102. '#type' => 'submit',
  1103. '#value' => t('Update preview'),
  1104. '#attributes' => array('class' => array('arguments-preview', 'ctools-auto-submit-click')),
  1105. '#pre_render' => array('ctools_dependent_pre_render'),
  1106. '#prefix' => '<div id="preview-submit-wrapper">',
  1107. '#suffix' => '</div>',
  1108. '#id' => 'preview-submit',
  1109. '#submit' => array('views_ui_edit_form_submit_preview'),
  1110. '#ajax' => array(
  1111. 'path' => 'admin/structure/views/view/' . $view->name . '/preview/' . $display_id . '/ajax',
  1112. 'wrapper' => 'views-preview-wrapper',
  1113. 'event' => 'click',
  1114. 'progress' => array('type' => 'throbber'),
  1115. 'method' => 'replace',
  1116. ),
  1117. // Make ENTER in arguments textfield (and other controls) submit the form
  1118. // as this button, not the Save button.
  1119. // @todo This only works for JS users. To make this work for nojs users,
  1120. // we may need to split Preview into a separate form.
  1121. '#process' => array_merge(array('views_ui_default_button'), element_info_property('submit', '#process', array())),
  1122. );
  1123. $form['#action'] = url('admin/structure/views/view/' . $view->name . '/preview/' . $display_id);
  1124. return $form;
  1125. }
  1126. /**
  1127. * Render the top of the display so it can be updated during ajax operations.
  1128. */
  1129. function views_ui_render_display_top($view, $display_id) {
  1130. $element['#theme_wrappers'] = array('views_container');
  1131. $element['#attributes']['class'] = array('views-display-top', 'clearfix');
  1132. $element['#attributes']['id'] = array('views-display-top');
  1133. // Extra actions for the display.
  1134. $element['extra_actions'] = array(
  1135. '#theme' => 'links__ctools_dropbutton',
  1136. '#attributes' => array(
  1137. 'id' => 'views-display-extra-actions',
  1138. 'class' => array(
  1139. 'horizontal', 'right', 'links', 'actions',
  1140. ),
  1141. ),
  1142. '#links' => array(
  1143. 'edit-details' => array(
  1144. 'title' => t('edit view name/description'),
  1145. 'href' => "admin/structure/views/nojs/edit-details/$view->name",
  1146. 'attributes' => array('class' => array('views-ajax-link')),
  1147. ),
  1148. 'analyze' => array(
  1149. 'title' => t('analyze view'),
  1150. 'href' => "admin/structure/views/nojs/analyze/$view->name/$display_id",
  1151. 'attributes' => array('class' => array('views-ajax-link')),
  1152. ),
  1153. 'clone' => array(
  1154. 'title' => t('clone view'),
  1155. 'href' => "admin/structure/views/view/$view->name/clone",
  1156. ),
  1157. 'export' => array(
  1158. 'title' => t('export view'),
  1159. 'href' => "admin/structure/views/view/$view->name/export",
  1160. ),
  1161. 'reorder' => array(
  1162. 'title' => t('reorder displays'),
  1163. 'href' => "admin/structure/views/nojs/reorder-displays/$view->name/$display_id",
  1164. 'attributes' => array('class' => array('views-ajax-link')),
  1165. ),
  1166. ),
  1167. );
  1168. // Let other modules add additional links here.
  1169. drupal_alter('views_ui_display_top_links', $element['extra_actions']['#links'], $view, $display_id);
  1170. if (isset($view->type) && $view->type != t('Default')) {
  1171. if ($view->type == t('Overridden')) {
  1172. $element['extra_actions']['#links']['revert'] = array(
  1173. 'title' => t('revert view'),
  1174. 'href' => "admin/structure/views/view/$view->name/revert",
  1175. 'query' => array('destination' => "admin/structure/views/view/$view->name"),
  1176. );
  1177. }
  1178. else {
  1179. $element['extra_actions']['#links']['delete'] = array(
  1180. 'title' => t('delete view'),
  1181. 'href' => "admin/structure/views/view/$view->name/delete",
  1182. );
  1183. }
  1184. }
  1185. // Determine the displays available for editing.
  1186. if ($tabs = views_ui_edit_page_display_tabs($view, $display_id)) {
  1187. if ($display_id) {
  1188. $tabs[$display_id]['#active'] = TRUE;
  1189. }
  1190. $tabs['#prefix'] = '<h2 class="element-invisible">' . t('Secondary tabs') . '</h2><ul id = "views-display-menu-tabs" class="tabs secondary">';
  1191. $tabs['#suffix'] = '</ul>';
  1192. $element['tabs'] = $tabs;
  1193. }
  1194. // Buttons for adding a new display.
  1195. foreach (views_fetch_plugin_names('display', NULL, array($view->base_table)) as $type => $label) {
  1196. $element['add_display'][$type] = array(
  1197. '#type' => 'submit',
  1198. '#value' => t('Add !display', array('!display' => $label)),
  1199. '#limit_validation_errors' => array(),
  1200. '#submit' => array('views_ui_edit_form_submit_add_display', 'views_ui_edit_form_submit_delay_destination'),
  1201. '#attributes' => array('class' => array('add-display')),
  1202. // Allow JavaScript to remove the 'Add ' prefix from the button label when
  1203. // placing the button in a "Add" dropdown menu.
  1204. '#process' => array_merge(array('views_ui_form_button_was_clicked'), element_info_property('submit', '#process', array())),
  1205. '#values' => array(t('Add !display', array('!display' => $label)), $label),
  1206. );
  1207. }
  1208. return $element;
  1209. }
  1210. /**
  1211. *
  1212. */
  1213. function views_ui_get_default_ajax_message() {
  1214. return '<div class="message">' . t("Click on an item to edit that item's details.") . '</div>';
  1215. }
  1216. /**
  1217. * Submit handler to add a display to a view.
  1218. */
  1219. function views_ui_edit_form_submit_add_display($form, &$form_state) {
  1220. $view = $form_state['view'];
  1221. // Create the new display.
  1222. $parents = $form_state['triggering_element']['#parents'];
  1223. $display_type = array_pop($parents);
  1224. $display_id = $view->add_display($display_type);
  1225. views_ui_cache_set($view);
  1226. // Redirect to the new display's edit page.
  1227. $form_state['redirect'] = 'admin/structure/views/view/' . $view->name . '/edit/' . $display_id;
  1228. }
  1229. /**
  1230. * Submit handler to duplicate a display for a view.
  1231. */
  1232. function views_ui_edit_form_submit_duplicate_display($form, &$form_state) {
  1233. $view = $form_state['view'];
  1234. $display_id = $form_state['display_id'];
  1235. // Create the new display.
  1236. $display = $view->display[$display_id];
  1237. $new_display_id = $view->add_display($display->display_plugin);
  1238. $view->display[$new_display_id] = clone $display;
  1239. $view->display[$new_display_id]->id = $new_display_id;
  1240. // By setting the current display the changed marker will appear on the new
  1241. // display.
  1242. $view->current_display = $new_display_id;
  1243. views_ui_cache_set($view);
  1244. // Redirect to the new display's edit page.
  1245. $form_state['redirect'] = 'admin/structure/views/view/' . $view->name . '/edit/' . $new_display_id;
  1246. }
  1247. /**
  1248. * Submit handler to delete a display from a view.
  1249. */
  1250. function views_ui_edit_form_submit_delete_display($form, &$form_state) {
  1251. $view = $form_state['view'];
  1252. $display_id = $form_state['display_id'];
  1253. // Mark the display for deletion.
  1254. $view->display[$display_id]->deleted = TRUE;
  1255. views_ui_cache_set($view);
  1256. // Redirect to the top-level edit page. The first remaining display will
  1257. // become the active display.
  1258. $form_state['redirect'] = 'admin/structure/views/view/' . $view->name;
  1259. }
  1260. /**
  1261. * Submit handler to add a restore a removed display to a view.
  1262. */
  1263. function views_ui_edit_form_submit_undo_delete_display($form, &$form_state) {
  1264. // Create the new display.
  1265. $id = $form_state['display_id'];
  1266. $form_state['view']->display[$id]->deleted = FALSE;
  1267. // Store in cache.
  1268. views_ui_cache_set($form_state['view']);
  1269. // Redirect to the top-level edit page.
  1270. $form_state['redirect'] = 'admin/structure/views/view/' . $form_state['view']->name . '/edit/' . $id;
  1271. }
  1272. /**
  1273. * Submit handler to enable a disabled display.
  1274. */
  1275. function views_ui_edit_form_submit_enable_display($form, &$form_state) {
  1276. $id = $form_state['display_id'];
  1277. // set_option doesn't work because this would might affect upper displays.
  1278. $form_state['view']->display[$id]->handler->set_option('enabled', TRUE);
  1279. // Store in cache.
  1280. views_ui_cache_set($form_state['view']);
  1281. // Redirect to the top-level edit page.
  1282. $form_state['redirect'] = 'admin/structure/views/view/' . $form_state['view']->name . '/edit/' . $id;
  1283. }
  1284. /**
  1285. * Submit handler to disable display.
  1286. */
  1287. function views_ui_edit_form_submit_disable_display($form, &$form_state) {
  1288. $id = $form_state['display_id'];
  1289. $form_state['view']->display[$id]->handler->set_option('enabled', FALSE);
  1290. // Store in cache.
  1291. views_ui_cache_set($form_state['view']);
  1292. // Redirect to the top-level edit page.
  1293. $form_state['redirect'] = 'admin/structure/views/view/' . $form_state['view']->name . '/edit/' . $id;
  1294. }
  1295. /**
  1296. * Submit handler when Preview button is clicked.
  1297. */
  1298. function views_ui_edit_form_submit_preview($form, &$form_state) {
  1299. // Rebuild the form with a pristine $view object.
  1300. $form_state['build_info']['args'][0] = views_ui_cache_load($form_state['view']->name);
  1301. $form_state['show_preview'] = TRUE;
  1302. $form_state['rebuild'] = TRUE;
  1303. }
  1304. /**
  1305. * Submit handler for form buttons that do not complete a form workflow.
  1306. *
  1307. * The Edit View form is a multistep form workflow, but with state managed by
  1308. * the CTools object cache rather than $form_state['rebuild']. Without this
  1309. * submit handler, buttons that add or remove displays would redirect to the
  1310. * destination parameter (e.g., when the Edit View form is linked to from a
  1311. * contextual link). This handler can be added to buttons whose form submission
  1312. * should not yet redirect to the destination.
  1313. */
  1314. function views_ui_edit_form_submit_delay_destination($form, &$form_state) {
  1315. if (isset($_GET['destination']) && $form_state['redirect'] !== FALSE) {
  1316. if (!isset($form_state['redirect'])) {
  1317. $form_state['redirect'] = $_GET['q'];
  1318. }
  1319. if (is_string($form_state['redirect'])) {
  1320. $form_state['redirect'] = array($form_state['redirect']);
  1321. }
  1322. $options = isset($form_state['redirect'][1]) ? $form_state['redirect'][1] : array();
  1323. if (!isset($options['query']['destination'])) {
  1324. $options['query']['destination'] = $_GET['destination'];
  1325. }
  1326. $form_state['redirect'][1] = $options;
  1327. unset($_GET['destination']);
  1328. }
  1329. }
  1330. /**
  1331. * Adds tabs for navigating across Displays when editing a View.
  1332. *
  1333. * This function can be called from hook_menu_local_tasks_alter() to implement
  1334. * these tabs as secondary local tasks, or it can be called from elsewhere if
  1335. * having them as secondary local tasks isn't desired. The caller is responsible
  1336. * for setting the active tab's #active property to TRUE.
  1337. *
  1338. * @param view $view
  1339. * The view which will be edited.
  1340. * @param string $display_id
  1341. * The display_id which is edited on the current request.
  1342. */
  1343. function views_ui_edit_page_display_tabs(view $view, $display_id = NULL) {
  1344. $tabs = array();
  1345. // Create a tab for each display.
  1346. foreach ($view->display as $id => $display) {
  1347. $tabs[$id] = array(
  1348. '#theme' => 'menu_local_task',
  1349. '#link' => array(
  1350. 'title' => views_ui_get_display_label($view, $id),
  1351. 'href' => 'admin/structure/views/view/' . $view->name . '/edit/' . $id,
  1352. 'localized_options' => array(),
  1353. ),
  1354. );
  1355. if (!empty($display->deleted)) {
  1356. $tabs[$id]['#link']['localized_options']['attributes']['class'][] = 'views-display-deleted-link';
  1357. }
  1358. if (isset($display->display_options['enabled']) && !$display->display_options['enabled']) {
  1359. $tabs[$id]['#link']['localized_options']['attributes']['class'][] = 'views-display-disabled-link';
  1360. }
  1361. }
  1362. // If the default display isn't supposed to be shown, don't display its tab,
  1363. // unless it's the only display.
  1364. if ((!views_ui_show_default_display($view) && $display_id != 'default') && count($tabs) > 1) {
  1365. $tabs['default']['#access'] = FALSE;
  1366. }
  1367. // Mark the display tab as red to show validation errors.
  1368. $view->validate();
  1369. foreach ($view->display as $id => $display) {
  1370. if (!empty($view->display_errors[$id])) {
  1371. // Always show the tab.
  1372. $tabs[$id]['#access'] = TRUE;
  1373. // Add a class to mark the error and a title to make a hover tip.
  1374. $tabs[$id]['#link']['localized_options']['attributes']['class'][] = 'error';
  1375. $tabs[$id]['#link']['localized_options']['attributes']['title'] = t('This display has one or more validation errors; please review it.');
  1376. }
  1377. }
  1378. return $tabs;
  1379. }
  1380. /**
  1381. * Controls whether or not the default display should have its own tab on edit.
  1382. */
  1383. function views_ui_show_default_display($view) {
  1384. // Always show the default display for advanced users who prefer that mode.
  1385. $advanced_mode = variable_get('views_ui_show_master_display', FALSE);
  1386. // For other users, show the default display only if there are no others, and
  1387. // hide it if there's at least one "real" display.
  1388. $additional_displays = (count($view->display) == 1);
  1389. return $advanced_mode || $additional_displays;
  1390. }
  1391. /**
  1392. * Returns a renderable array representing the edit page for one display.
  1393. */
  1394. function views_ui_get_display_tab($view, $display_id) {
  1395. $build = array();
  1396. $display = $view->display[$display_id];
  1397. // If the plugin doesn't exist, display an error message instead of an edit
  1398. // page.
  1399. if (empty($display->handler)) {
  1400. $title = isset($display->display_title) ? $display->display_title : t('Invalid');
  1401. // @todo: Improved UX for the case where a plugin is missing.
  1402. $build['#markup'] = t("Error: Display @display refers to a plugin named '@plugin', but that plugin is not available.", array('@display' => $display->id, '@plugin' => $display->display_plugin));
  1403. }
  1404. // Build the content of the edit page.
  1405. else {
  1406. $build['details'] = views_ui_get_display_tab_details($view, $display);
  1407. }
  1408. // In AJAX context, views_ui_regenerate_tab() returns this outside of form
  1409. // context, so hook_form_views_ui_edit_form_alter() is insufficient.
  1410. drupal_alter('views_ui_display_tab', $build, $view, $display_id);
  1411. return $build;
  1412. }
  1413. /**
  1414. * Helper function to get the display details section of the edit UI.
  1415. *
  1416. * @param view $view
  1417. * The full view object.
  1418. * @param object $display
  1419. * The display object to work with.
  1420. *
  1421. * @return array
  1422. * A renderable page build array.
  1423. */
  1424. function views_ui_get_display_tab_details($view, $display) {
  1425. $display_title = views_ui_get_display_label($view, $display->id, FALSE);
  1426. $build = array(
  1427. '#theme_wrappers' => array('container'),
  1428. '#attributes' => array('id' => 'edit-display-settings-details'),
  1429. );
  1430. $plugin = views_fetch_plugin_data('display', $view->display[$display->id]->display_plugin);
  1431. // The following is for display purposes only. We need to determine if there
  1432. // is more than one button and wrap the buttons in a .ctools-dropbutton class
  1433. // if more than one is present. Otherwise, we'll just wrap the actions in the
  1434. // .ctools-button class.
  1435. $is_display_deleted = !empty($display->deleted);
  1436. $is_deletable = empty($plugin['no remove']);
  1437. // The master display cannot be cloned.
  1438. $is_default = $display->id == 'default';
  1439. // @todo: Figure out why get_option doesn't work here.
  1440. $is_enabled = $display->handler->get_option('enabled');
  1441. if (!$is_display_deleted && $is_deletable && !$is_default) {
  1442. $prefix = '<div class="ctools-no-js ctools-button ctools-dropbutton"><div class="ctools-link"><a href="#" class="ctools-twisty ctools-text"><span class="element-invisible">open</span></a></div><div class="ctools-content"><ul class="horizontal right actions">';
  1443. $suffix = '</ul></div></div>';
  1444. $item_element = 'li';
  1445. }
  1446. else {
  1447. $prefix = '<div class="ctools-button"><div class="ctools-content"><ul class="horizontal right actions">';
  1448. $suffix = '</ul></div></div>';
  1449. $item_element = 'li';
  1450. }
  1451. if ($display->id != 'default') {
  1452. $build['top']['#theme_wrappers'] = array('container');
  1453. $build['top']['#attributes']['id'] = 'edit-display-settings-top';
  1454. $build['top']['#attributes']['class'] = array(
  1455. 'views-ui-display-tab-actions',
  1456. 'views-ui-display-tab-bucket',
  1457. 'clearfix',
  1458. );
  1459. // The Delete, Duplicate and Undo Delete buttons.
  1460. $build['top']['actions'] = array(
  1461. '#prefix' => $prefix,
  1462. '#suffix' => $suffix,
  1463. );
  1464. if (!$is_display_deleted) {
  1465. if (!$is_enabled) {
  1466. $build['top']['actions']['enable'] = array(
  1467. '#type' => 'submit',
  1468. '#value' => t('enable @display_title', array('@display_title' => $display_title)),
  1469. '#limit_validation_errors' => array(),
  1470. '#submit' => array(
  1471. 'views_ui_edit_form_submit_enable_display',
  1472. 'views_ui_edit_form_submit_delay_destination',
  1473. ),
  1474. '#prefix' => '<' . $item_element . ' class="enable">',
  1475. "#suffix" => '</' . $item_element . '>',
  1476. );
  1477. }
  1478. // Add a link to view the page.
  1479. elseif ($display->handler->has_path()) {
  1480. $path = $display->handler->get_path();
  1481. if (strpos($path, '%') === FALSE) {
  1482. $build['top']['actions']['path'] = array(
  1483. '#type' => 'link',
  1484. '#title' => t('view @display', array('@display' => $display->display_title)),
  1485. '#options' => array(
  1486. 'alt' => array(t('Go to the real page for this display')),
  1487. ),
  1488. '#href' => $path,
  1489. '#prefix' => '<' . $item_element . ' class="view">',
  1490. "#suffix" => '</' . $item_element . '>',
  1491. );
  1492. }
  1493. }
  1494. if (!$is_default) {
  1495. $build['top']['actions']['duplicate'] = array(
  1496. '#type' => 'submit',
  1497. '#value' => t('clone @display_title', array('@display_title' => $display_title)),
  1498. '#limit_validation_errors' => array(),
  1499. '#submit' => array(
  1500. 'views_ui_edit_form_submit_duplicate_display',
  1501. 'views_ui_edit_form_submit_delay_destination',
  1502. ),
  1503. '#prefix' => '<' . $item_element . ' class="duplicate">',
  1504. "#suffix" => '</' . $item_element . '>',
  1505. );
  1506. }
  1507. if ($is_deletable) {
  1508. $build['top']['actions']['delete'] = array(
  1509. '#type' => 'submit',
  1510. '#value' => t('delete @display_title', array('@display_title' => $display_title)),
  1511. '#limit_validation_errors' => array(),
  1512. '#submit' => array(
  1513. 'views_ui_edit_form_submit_delete_display',
  1514. 'views_ui_edit_form_submit_delay_destination',
  1515. ),
  1516. '#prefix' => '<' . $item_element . ' class="delete">',
  1517. "#suffix" => '</' . $item_element . '>',
  1518. );
  1519. }
  1520. if ($is_enabled) {
  1521. $build['top']['actions']['disable'] = array(
  1522. '#type' => 'submit',
  1523. '#value' => t('disable @display_title', array('@display_title' => $display_title)),
  1524. '#limit_validation_errors' => array(),
  1525. '#submit' => array(
  1526. 'views_ui_edit_form_submit_disable_display',
  1527. 'views_ui_edit_form_submit_delay_destination',
  1528. ),
  1529. '#prefix' => '<' . $item_element . ' class="disable">',
  1530. "#suffix" => '</' . $item_element . '>',
  1531. );
  1532. }
  1533. }
  1534. else {
  1535. $build['top']['actions']['undo_delete'] = array(
  1536. '#type' => 'submit',
  1537. '#value' => t('undo delete of @display_title', array('@display_title' => $display_title)),
  1538. '#limit_validation_errors' => array(),
  1539. '#submit' => array(
  1540. 'views_ui_edit_form_submit_undo_delete_display',
  1541. 'views_ui_edit_form_submit_delay_destination',
  1542. ),
  1543. '#prefix' => '<' . $item_element . ' class="undo-delete">',
  1544. "#suffix" => '</' . $item_element . '>',
  1545. );
  1546. }
  1547. // The area above the three columns.
  1548. $build['top']['display_title'] = array(
  1549. '#theme' => 'views_ui_display_tab_setting',
  1550. '#description' => t('Display name'),
  1551. '#link' => $display->handler->option_link(check_plain($display_title), 'display_title'),
  1552. );
  1553. }
  1554. $build['columns'] = array();
  1555. $build['columns']['#theme_wrappers'] = array('container');
  1556. $build['columns']['#attributes'] = array(
  1557. 'id' => 'edit-display-settings-main',
  1558. 'class' => array('clearfix', 'views-display-columns'),
  1559. );
  1560. $build['columns']['first']['#theme_wrappers'] = array('container');
  1561. $build['columns']['first']['#attributes'] = array(
  1562. 'class' => array('views-display-column', 'first'),
  1563. );
  1564. $build['columns']['second']['#theme_wrappers'] = array('container');
  1565. $build['columns']['second']['#attributes'] = array(
  1566. 'class' => array('views-display-column', 'second'),
  1567. );
  1568. $build['columns']['second']['settings'] = array();
  1569. $build['columns']['second']['header'] = array();
  1570. $build['columns']['second']['footer'] = array();
  1571. $build['columns']['second']['pager'] = array();
  1572. // The third column buckets are wrapped in a CTools collapsible div.
  1573. $build['columns']['third']['#theme_wrappers'] = array('container');
  1574. $build['columns']['third']['#attributes'] = array(
  1575. 'class' => array(
  1576. 'views-display-column',
  1577. 'third',
  1578. 'ctools-collapsible-container',
  1579. 'ctools-collapsible-remember',
  1580. ),
  1581. );
  1582. // Specify an id that won't change after AJAX requests, so ctools can keep
  1583. // track of the user's preferred collapsible state. Use the same id across
  1584. // different displays of the same view, so changing displays doesn't
  1585. // recollapse the column.
  1586. $build['columns']['third']['#attributes']['id'] = 'views-ui-advanced-column-' . $view->name;
  1587. // Collapse the div by default.
  1588. if (!variable_get('views_ui_show_advanced_column', FALSE)) {
  1589. $build['columns']['third']['#attributes']['class'][] = 'ctools-collapsed';
  1590. }
  1591. $build['columns']['third']['advanced'] = array('#markup' => '<h3 class="ctools-collapsible-handle"><a href="">' . t('Advanced') . '</a></h3>');
  1592. $build['columns']['third']['collapse']['#theme_wrappers'] = array('container');
  1593. $build['columns']['third']['collapse']['#attributes'] = array(
  1594. 'class' => array('ctools-collapsible-content'),
  1595. );
  1596. // Each option (e.g. title, access, display as grid/table/list) fits into one
  1597. // of several "buckets," or boxes (Format, Fields, Sort, and so on).
  1598. $buckets = array();
  1599. // Fetch options from the display plugin, with a list of buckets they go into.
  1600. $options = array();
  1601. $display->handler->options_summary($buckets, $options);
  1602. // Place each option into its bucket.
  1603. foreach ($options as $id => $option) {
  1604. // Each option self-identifies as belonging in a particular bucket.
  1605. $buckets[$option['category']]['build'][$id] = views_ui_edit_form_get_build_from_option($id, $option, $view, $display);
  1606. }
  1607. // Place each bucket into the proper column.
  1608. foreach ($buckets as $id => $bucket) {
  1609. // Let buckets identify themselves as belonging in a column.
  1610. if (isset($bucket['column']) && isset($build['columns'][$bucket['column']])) {
  1611. $column = $bucket['column'];
  1612. }
  1613. // If a bucket doesn't pick one of our predefined columns to belong to, put
  1614. // it in the last one.
  1615. else {
  1616. $column = 'third';
  1617. }
  1618. if (isset($bucket['build']) && is_array($bucket['build'])) {
  1619. // The third column is a CTools collapsible div, so
  1620. // the structure of the form is a little different for this column.
  1621. if ($column === 'third') {
  1622. $build['columns'][$column]['collapse'][$id] = $bucket['build'];
  1623. $build['columns'][$column]['collapse'][$id]['#theme_wrappers'][] = 'views_ui_display_tab_bucket';
  1624. $build['columns'][$column]['collapse'][$id]['#title'] = !empty($bucket['title']) ? $bucket['title'] : '';
  1625. $build['columns'][$column]['collapse'][$id]['#name'] = !empty($bucket['title']) ? $bucket['title'] : $id;
  1626. }
  1627. else {
  1628. $build['columns'][$column][$id] = $bucket['build'];
  1629. $build['columns'][$column][$id]['#theme_wrappers'][] = 'views_ui_display_tab_bucket';
  1630. $build['columns'][$column][$id]['#title'] = !empty($bucket['title']) ? $bucket['title'] : '';
  1631. $build['columns'][$column][$id]['#name'] = !empty($bucket['title']) ? $bucket['title'] : $id;
  1632. }
  1633. }
  1634. }
  1635. $build['columns']['first']['fields'] = views_ui_edit_form_get_bucket('field', $view, $display);
  1636. $build['columns']['first']['filters'] = views_ui_edit_form_get_bucket('filter', $view, $display);
  1637. $build['columns']['first']['sorts'] = views_ui_edit_form_get_bucket('sort', $view, $display);
  1638. $build['columns']['second']['header'] = views_ui_edit_form_get_bucket('header', $view, $display);
  1639. $build['columns']['second']['footer'] = views_ui_edit_form_get_bucket('footer', $view, $display);
  1640. $build['columns']['third']['collapse']['arguments'] = views_ui_edit_form_get_bucket('argument', $view, $display);
  1641. $build['columns']['third']['collapse']['relationships'] = views_ui_edit_form_get_bucket('relationship', $view, $display);
  1642. $build['columns']['third']['collapse']['empty'] = views_ui_edit_form_get_bucket('empty', $view, $display);
  1643. return $build;
  1644. }
  1645. /**
  1646. * Build a renderable array representing one option on the edit form.
  1647. *
  1648. * This function might be more logical as a method on an object, if a suitable
  1649. * object emerges out of refactoring.
  1650. */
  1651. function views_ui_edit_form_get_build_from_option($id, $option, $view, $display) {
  1652. $option_build = array();
  1653. $option_build['#theme'] = 'views_ui_display_tab_setting';
  1654. $option_build['#description'] = $option['title'];
  1655. $option_build['#link'] = $display->handler->option_link($option['value'], $id, '', empty($option['desc']) ? '' : $option['desc']);
  1656. $option_build['#links'] = array();
  1657. if (!empty($option['links']) && is_array($option['links'])) {
  1658. foreach ($option['links'] as $link_id => $link_value) {
  1659. $option_build['#settings_links'][] = $display->handler->option_link($option['setting'], $link_id, 'views-button-configure', $link_value);
  1660. }
  1661. }
  1662. if (!empty($display->handler->options['defaults'][$id])) {
  1663. $display_id = 'default';
  1664. $option_build['#defaulted'] = TRUE;
  1665. }
  1666. else {
  1667. $display_id = $display->id;
  1668. if (!$display->handler->is_default_display()) {
  1669. if ($display->handler->defaultable_sections($id)) {
  1670. $option_build['#overridden'] = TRUE;
  1671. }
  1672. }
  1673. }
  1674. $option_build['#attributes']['class'][] = drupal_clean_css_identifier($display_id . '-' . $id);
  1675. if (!empty($view->changed_sections[$display_id . '-' . $id])) {
  1676. $option_build['#changed'] = TRUE;
  1677. }
  1678. return $option_build;
  1679. }
  1680. /**
  1681. *
  1682. */
  1683. function template_preprocess_views_ui_display_tab_setting(&$variables) {
  1684. static $zebra = 0;
  1685. $variables['zebra'] = ($zebra % 2 === 0 ? 'odd' : 'even');
  1686. $zebra++;
  1687. // Put the main link to the left side.
  1688. array_unshift($variables['settings_links'], $variables['link']);
  1689. $variables['settings_links'] = implode('<span class="label">&nbsp;|&nbsp;</span>', $variables['settings_links']);
  1690. // Add classes associated with this display tab to the overall list.
  1691. $variables['classes_array'] = array_merge($variables['classes_array'], $variables['class']);
  1692. if (!empty($variables['defaulted'])) {
  1693. $variables['classes_array'][] = 'defaulted';
  1694. }
  1695. if (!empty($variables['overridden'])) {
  1696. $variables['classes_array'][] = 'overridden';
  1697. $variables['attributes_array']['title'][] = t('Overridden');
  1698. }
  1699. // Append a colon to the description, if requested.
  1700. if ($variables['description'] && $variables['description_separator']) {
  1701. $variables['description'] .= t(':');
  1702. }
  1703. }
  1704. /**
  1705. *
  1706. */
  1707. function template_preprocess_views_ui_display_tab_bucket(&$variables) {
  1708. $element = $variables['element'];
  1709. $variables['item_help_icon'] = '';
  1710. if (!empty($element['#item_help_icon'])) {
  1711. $variables['item_help_icon'] = render($element['#item_help_icon']);
  1712. }
  1713. if (!empty($element['#name'])) {
  1714. $variables['classes_array'][] = drupal_clean_css_identifier(strtolower($element['#name']));
  1715. }
  1716. if (!empty($element['#overridden'])) {
  1717. $variables['classes_array'][] = 'overridden';
  1718. $variables['attributes_array']['title'][] = t('Overridden');
  1719. }
  1720. $variables['content'] = $element['#children'];
  1721. $variables['title'] = $element['#title'];
  1722. $variables['actions'] = !empty($element['#actions']) ? $element['#actions'] : '';
  1723. }
  1724. /**
  1725. *
  1726. */
  1727. function template_preprocess_views_ui_display_tab_column(&$variables) {
  1728. $element = $variables['element'];
  1729. $variables['content'] = $element['#children'];
  1730. $variables['column'] = $element['#column'];
  1731. }
  1732. /**
  1733. * Move form elements into fieldsets for presentation purposes.
  1734. *
  1735. * Many views forms use #tree = TRUE to keep their values in a hierarchy for
  1736. * easier storage. Moving the form elements into fieldsets during form building
  1737. * would break up that hierarchy. Therefore, we wait until the pre_render stage,
  1738. * where any changes we make affect presentation only and aren't reflected in
  1739. * $form_state['values'].
  1740. */
  1741. function views_ui_pre_render_add_fieldset_markup($form) {
  1742. foreach (element_children($form) as $key) {
  1743. $element = $form[$key];
  1744. // In our form builder functions, we added an arbitrary #fieldset property
  1745. // to any element that belongs in a fieldset. If this form element has that
  1746. // property, move it into its fieldset.
  1747. if (isset($element['#fieldset']) && isset($form[$element['#fieldset']])) {
  1748. $form[$element['#fieldset']][$key] = $element;
  1749. // Remove the original element this duplicates.
  1750. unset($form[$key]);
  1751. }
  1752. }
  1753. return $form;
  1754. }
  1755. /**
  1756. * Flattens the structure of an element containing the #flatten property.
  1757. *
  1758. * If a form element has #flatten = TRUE, then all of it's children
  1759. * get moved to the same level as the element itself.
  1760. * So $form['to_be_flattened'][$key] becomes $form[$key], and
  1761. * $form['to_be_flattened'] gets unset.
  1762. */
  1763. function views_ui_pre_render_flatten_data($form) {
  1764. foreach (element_children($form) as $key) {
  1765. $element = $form[$key];
  1766. if (!empty($element['#flatten'])) {
  1767. foreach (element_children($element) as $child_key) {
  1768. $form[$child_key] = $form[$key][$child_key];
  1769. }
  1770. // All done, remove the now-empty parent.
  1771. unset($form[$key]);
  1772. }
  1773. }
  1774. return $form;
  1775. }
  1776. /**
  1777. * Moves argument options into their place.
  1778. *
  1779. * When configuring the default argument behavior, almost each of the radio
  1780. * buttons has its own fieldset shown bellow it when the radio button is
  1781. * clicked. That fieldset is created through a custom form process callback.
  1782. * Each element that has #argument_option defined and pointing to a default
  1783. * behavior gets moved to the appropriate fieldset.
  1784. * So if #argument_option is specified as 'default', the element is moved
  1785. * to the 'default_options' fieldset.
  1786. */
  1787. function views_ui_pre_render_move_argument_options($form) {
  1788. foreach (element_children($form) as $key) {
  1789. $element = $form[$key];
  1790. if (!empty($element['#argument_option'])) {
  1791. $container_name = $element['#argument_option'] . '_options';
  1792. if (isset($form['no_argument']['default_action'][$container_name])) {
  1793. $form['no_argument']['default_action'][$container_name][$key] = $element;
  1794. }
  1795. // Remove the original element this duplicates.
  1796. unset($form[$key]);
  1797. }
  1798. }
  1799. return $form;
  1800. }
  1801. /**
  1802. * Custom form radios process function.
  1803. *
  1804. * Roll out a single radios element to a list of radios,
  1805. * using the options array as index.
  1806. * While doing that, create a container element underneath each option, which
  1807. * contains the settings related to that option.
  1808. *
  1809. * @see form_process_radios()
  1810. */
  1811. function views_ui_process_container_radios($element) {
  1812. if (count($element['#options']) > 0) {
  1813. foreach ($element['#options'] as $key => $choice) {
  1814. $element += array($key => array());
  1815. // Generate the parents as the autogenerator does, so we will have a
  1816. // unique id for each radio button.
  1817. $parents_for_id = array_merge($element['#parents'], array($key));
  1818. $element[$key] += array(
  1819. '#type' => 'radio',
  1820. '#title' => $choice,
  1821. // The key is sanitized in drupal_attributes() during output from the
  1822. // theme function.
  1823. '#return_value' => $key,
  1824. '#default_value' => isset($element['#default_value']) ? $element['#default_value'] : NULL,
  1825. '#attributes' => $element['#attributes'],
  1826. '#parents' => $element['#parents'],
  1827. '#id' => drupal_html_id('edit-' . implode('-', $parents_for_id)),
  1828. '#ajax' => isset($element['#ajax']) ? $element['#ajax'] : NULL,
  1829. );
  1830. $element[$key . '_options'] = array(
  1831. '#type' => 'container',
  1832. '#attributes' => array('class' => array('views-admin-dependent')),
  1833. );
  1834. }
  1835. }
  1836. return $element;
  1837. }
  1838. /**
  1839. * Import a view from cut & paste.
  1840. */
  1841. function views_ui_import_page($form, &$form_state) {
  1842. $form['name'] = array(
  1843. '#type' => 'textfield',
  1844. '#title' => t('View name'),
  1845. '#description' => t('Enter the name to use for this view if it is different from the source view. Leave blank to use the name of the view.'),
  1846. );
  1847. $form['name_override'] = array(
  1848. '#type' => 'checkbox',
  1849. '#title' => t('Replace an existing view if one exists with the same name'),
  1850. );
  1851. $form['bypass_validation'] = array(
  1852. '#type' => 'checkbox',
  1853. '#title' => t('Bypass view validation'),
  1854. '#description' => t('Bypass the validation of plugins and handlers when importing this view.'),
  1855. );
  1856. $form['view'] = array(
  1857. '#type' => 'textarea',
  1858. '#title' => t('Paste view code here'),
  1859. '#required' => TRUE,
  1860. );
  1861. $form['submit'] = array(
  1862. '#type' => 'submit',
  1863. '#value' => t('Import'),
  1864. '#submit' => array('views_ui_import_submit'),
  1865. '#validate' => array('views_ui_import_validate'),
  1866. );
  1867. return $form;
  1868. }
  1869. /**
  1870. * Validate handler to import a view.
  1871. */
  1872. function views_ui_import_validate($form, &$form_state) {
  1873. $view = '';
  1874. views_include('view');
  1875. // Be forgiving if someone pastes views code that starts with '<?php'.
  1876. if (substr($form_state['values']['view'], 0, 5) == '<?php') {
  1877. $form_state['values']['view'] = substr($form_state['values']['view'], 5);
  1878. }
  1879. ob_start();
  1880. eval($form_state['values']['view']);
  1881. ob_end_clean();
  1882. if (!is_object($view)) {
  1883. return form_error($form['view'], t('Unable to interpret view code.'));
  1884. }
  1885. if (empty($view->api_version) || $view->api_version < 2) {
  1886. form_error($form['view'], t('That view is not compatible with this version of Views.
  1887. If you have a view from views1 you have to go to a drupal6 installation and import it there.'));
  1888. }
  1889. elseif (version_compare($view->api_version, views_api_version(), '>')) {
  1890. form_error($form['view'], t('That view is created for the version @import_version of views, but you only have @api_version', array(
  1891. '@import_version' => $view->api_version,
  1892. '@api_version' => views_api_version(),
  1893. )));
  1894. }
  1895. // View name must be alphanumeric or underscores, no other punctuation.
  1896. if (!empty($form_state['values']['name']) && preg_match('/[^a-zA-Z0-9_]/', $form_state['values']['name'])) {
  1897. form_error($form['name'], t('View name must be alphanumeric or underscores only.'));
  1898. }
  1899. if ($form_state['values']['name']) {
  1900. $view->name = $form_state['values']['name'];
  1901. }
  1902. $test = views_get_view($view->name);
  1903. if (!$form_state['values']['name_override']) {
  1904. if ($test && $test->type != t('Default')) {
  1905. form_set_error('', t('A view by that name already exists; please choose a different name'));
  1906. }
  1907. }
  1908. else {
  1909. if ($test->vid) {
  1910. $view->vid = $test->vid;
  1911. }
  1912. }
  1913. // Make sure base table gets set properly if it got moved.
  1914. $view->update();
  1915. $view->init_display();
  1916. $broken = FALSE;
  1917. // Bypass the validation of view pluigns/handlers if option is checked.
  1918. if (!$form_state['values']['bypass_validation']) {
  1919. // Make sure that all plugins and handlers needed by this view actually
  1920. // exist.
  1921. foreach ($view->display as $id => $display) {
  1922. if (empty($display->handler) || !empty($display->handler->broken)) {
  1923. drupal_set_message(t('Display plugin @plugin is not available.', array(
  1924. '@plugin' => $display->display_plugin,
  1925. )), 'error');
  1926. $broken = TRUE;
  1927. continue;
  1928. }
  1929. $plugin = views_get_plugin('style', $display->handler->get_option('style_plugin'));
  1930. if (!$plugin) {
  1931. drupal_set_message(t('Style plugin @plugin is not available.', array(
  1932. '@plugin' => $display->handler->get_option('style_plugin'),
  1933. )), 'error');
  1934. $broken = TRUE;
  1935. }
  1936. elseif ($plugin->uses_row_plugin()) {
  1937. $plugin = views_get_plugin('row', $display->handler->get_option('row_plugin'));
  1938. if (!$plugin) {
  1939. drupal_set_message(t('Row plugin @plugin is not available.', array(
  1940. '@plugin' => $display->handler->get_option('row_plugin'),
  1941. )), 'error');
  1942. $broken = TRUE;
  1943. }
  1944. }
  1945. foreach (views_object_types() as $type => $info) {
  1946. $handlers = $display->handler->get_handlers($type);
  1947. if ($handlers) {
  1948. foreach ($handlers as $id => $handler) {
  1949. if ($handler->broken()) {
  1950. drupal_set_message(t('@type handler @table.@field is not available.', array(
  1951. '@type' => $info['stitle'],
  1952. '@table' => $handler->table,
  1953. '@field' => $handler->field,
  1954. )), 'error');
  1955. $broken = TRUE;
  1956. }
  1957. }
  1958. }
  1959. }
  1960. }
  1961. }
  1962. if ($broken) {
  1963. form_set_error('', t('Unable to import view.'));
  1964. }
  1965. $form_state['view'] = &$view;
  1966. }
  1967. /**
  1968. * Submit handler for view import.
  1969. */
  1970. function views_ui_import_submit($form, &$form_state) {
  1971. // Store in cache and then go to edit.
  1972. views_ui_cache_set($form_state['view']);
  1973. $form_state['redirect'] = 'admin/structure/views/view/' . $form_state['view']->name . '/edit';
  1974. }
  1975. /**
  1976. * Validate that a view is complete and whole.
  1977. */
  1978. function views_ui_edit_view_form_validate($form, &$form_state) {
  1979. // Do not validate cancel or delete or revert.
  1980. if (empty($form_state['clicked_button']['#value']) || $form_state['clicked_button']['#value'] != t('Save')) {
  1981. return;
  1982. }
  1983. $errors = $form_state['view']->validate();
  1984. if ($errors !== TRUE) {
  1985. foreach ($errors as $error) {
  1986. form_set_error('', $error);
  1987. }
  1988. }
  1989. }
  1990. /**
  1991. * Submit handler for the edit view form.
  1992. */
  1993. function views_ui_edit_view_form_submit($form, &$form_state) {
  1994. // Go through and remove displayed scheduled for removal.
  1995. foreach ($form_state['view']->display as $id => $display) {
  1996. if (!empty($display->deleted)) {
  1997. unset($form_state['view']->display[$id]);
  1998. }
  1999. }
  2000. // Rename display ids if needed.
  2001. foreach ($form_state['view']->display as $id => $display) {
  2002. if (!empty($display->new_id)) {
  2003. $form_state['view']->display[$id]->id = $display->new_id;
  2004. // Redirect the user to the renamed display to be sure that the page
  2005. // itself exists and doesn't throw errors.
  2006. $form_state['redirect'] = 'admin/structure/views/view/' . $form_state['view']->name . '/edit/' . $display->new_id;
  2007. }
  2008. }
  2009. // Direct the user to the right url, if the path of the display has changed.
  2010. if (!empty($_GET['destination'])) {
  2011. $destination = $_GET['destination'];
  2012. // Find out the first display which has a changed path and redirect to this
  2013. // URL.
  2014. $old_view = views_get_view($form_state['view']->name);
  2015. foreach ($old_view->display as $id => $display) {
  2016. // Only check for displays with a path.
  2017. if (!isset($display->display_options['path'])) {
  2018. continue;
  2019. }
  2020. $old_path = $display->display_options['path'];
  2021. if (($display->display_plugin == 'page') && ($old_path == $destination) && ($old_path != $form_state['view']->display[$id]->display_options['path'])) {
  2022. $destination = $form_state['view']->display[$id]->display_options['path'];
  2023. unset($_GET['destination']);
  2024. }
  2025. }
  2026. $form_state['redirect'] = $destination;
  2027. }
  2028. $form_state['view']->save();
  2029. drupal_set_message(t('The view %name has been saved.', array('%name' => $form_state['view']->get_human_name())));
  2030. // Remove this view from cache so we can edit it properly.
  2031. ctools_object_cache_clear('view', $form_state['view']->name);
  2032. }
  2033. /**
  2034. * Submit handler for the edit view form.
  2035. */
  2036. function views_ui_edit_view_form_cancel($form, &$form_state) {
  2037. // Remove this view from cache so edits will be lost.
  2038. ctools_object_cache_clear('view', $form_state['view']->name);
  2039. if (empty($form['view']->vid)) {
  2040. // I seem to have to drupal_goto here because I can't get fapi to
  2041. // honor the redirect target. Not sure what I screwed up here.
  2042. drupal_goto('admin/structure/views');
  2043. }
  2044. }
  2045. /**
  2046. *
  2047. */
  2048. function views_ui_edit_view_form_delete($form, &$form_state) {
  2049. unset($_REQUEST['destination']);
  2050. // Redirect to the delete confirm page.
  2051. $form_state['redirect'] = array('admin/structure/views/view/' . $form_state['view']->name . '/delete', array('query' => drupal_get_destination() + array('cancel' => 'admin/structure/views/view/' . $form_state['view']->name . '/edit')));
  2052. }
  2053. /**
  2054. * Add information about a section to a display.
  2055. */
  2056. function views_ui_edit_form_get_bucket($type, $view, $display) {
  2057. $build = array(
  2058. '#theme_wrappers' => array('views_ui_display_tab_bucket'),
  2059. );
  2060. $types = views_object_types();
  2061. $build['#overridden'] = FALSE;
  2062. $build['#defaulted'] = FALSE;
  2063. if (module_exists('advanced_help')) {
  2064. $build['#item_help_icon'] = array(
  2065. '#theme' => 'advanced_help_topic',
  2066. '#module' => 'views',
  2067. '#topic' => $type,
  2068. );
  2069. }
  2070. $build['#name'] = $build['#title'] = $types[$type]['title'];
  2071. // Different types now have different rearrange forms, so we use this switch
  2072. // to get the right one.
  2073. switch ($type) {
  2074. case 'filter':
  2075. $rearrange_url = "admin/structure/views/nojs/rearrange-$type/$view->name/$display->id/$type";
  2076. $rearrange_text = t('And/Or, Rearrange');
  2077. // @todo Add another class to have another symbol for filter rearrange.
  2078. $class = 'icon compact rearrange';
  2079. break;
  2080. case 'field':
  2081. // Fetch the style plugin info so we know whether to list fields or not.
  2082. $style_plugin = $display->handler->get_plugin();
  2083. $uses_fields = $style_plugin && $style_plugin->uses_fields();
  2084. if (!$uses_fields) {
  2085. $build['fields'][] = array(
  2086. '#markup' => t('The selected style or row format does not utilize fields.'),
  2087. '#theme_wrappers' => array('views_container'),
  2088. '#attributes' => array('class' => array('views-display-setting')),
  2089. );
  2090. return $build;
  2091. }
  2092. default:
  2093. $rearrange_url = "admin/structure/views/nojs/rearrange/$view->name/$display->id/$type";
  2094. $rearrange_text = t('Rearrange');
  2095. $class = 'icon compact rearrange';
  2096. }
  2097. // Create an array of actions to pass to theme_links
  2098. $actions = array();
  2099. $count_handlers = count($display->handler->get_handlers($type));
  2100. $actions['add'] = array(
  2101. 'title' => t('Add'),
  2102. 'href' => "admin/structure/views/nojs/add-item/$view->name/$display->id/$type",
  2103. 'attributes' => array('class' => array('icon compact add', 'views-ajax-link'), 'title' => t('Add'), 'id' => 'views-add-' . $type),
  2104. 'html' => TRUE,
  2105. );
  2106. if ($count_handlers > 0) {
  2107. $actions['rearrange'] = array(
  2108. 'title' => $rearrange_text,
  2109. 'href' => $rearrange_url,
  2110. 'attributes' => array('class' => array($class, 'views-ajax-link'), 'title' => t('Rearrange'), 'id' => 'views-rearrange-' . $type),
  2111. 'html' => TRUE,
  2112. );
  2113. }
  2114. // Render the array of links.
  2115. $build['#actions'] = theme('links__ctools_dropbutton',
  2116. array(
  2117. 'links' => $actions,
  2118. 'attributes' => array(
  2119. 'class' => array('inline', 'links', 'actions', 'horizontal', 'right'),
  2120. ),
  2121. 'class' => array('views-ui-settings-bucket-operations'),
  2122. )
  2123. );
  2124. if (!$display->handler->is_default_display()) {
  2125. if (!$display->handler->is_defaulted($types[$type]['plural'])) {
  2126. $build['#overridden'] = TRUE;
  2127. }
  2128. else {
  2129. $build['#defaulted'] = TRUE;
  2130. }
  2131. }
  2132. // If there's an options form for the bucket, link to it.
  2133. if (!empty($types[$type]['options'])) {
  2134. $build['#title'] = l($build['#title'], "admin/structure/views/nojs/config-type/$view->name/$display->id/$type", array('attributes' => array('class' => array('views-ajax-link'), 'id' => 'views-title-' . $type)));
  2135. }
  2136. static $relationships = NULL;
  2137. if (!isset($relationships)) {
  2138. // Get relationship labels.
  2139. $relationships = array();
  2140. // @todo: get_handlers()
  2141. $handlers = $display->handler->get_option('relationships');
  2142. if ($handlers) {
  2143. foreach ($handlers as $id => $info) {
  2144. $handler = $display->handler->get_handler('relationship', $id);
  2145. $relationships[$id] = $handler->label();
  2146. }
  2147. }
  2148. }
  2149. // Filters can now be grouped so we do a little bit extra.
  2150. $groups = array();
  2151. $grouping = FALSE;
  2152. if ($type == 'filter') {
  2153. $group_info = $view->display_handler->get_option('filter_groups');
  2154. // If there is only one group but it is using the "OR" filter, we still
  2155. // treat it as a group for display purposes, since we want to display the
  2156. // "OR" label next to items within the group.
  2157. if (!empty($group_info['groups']) && (count($group_info['groups']) > 1 || current($group_info['groups']) == 'OR')) {
  2158. $grouping = TRUE;
  2159. $groups = array(0 => array());
  2160. }
  2161. }
  2162. $build['fields'] = array();
  2163. foreach ($display->handler->get_option($types[$type]['plural']) as $id => $field) {
  2164. // Build the option link for this handler ("Node: ID = article").
  2165. $build['fields'][$id] = array();
  2166. $build['fields'][$id]['#theme'] = 'views_ui_display_tab_setting';
  2167. $handler = $display->handler->get_handler($type, $id);
  2168. if (empty($handler)) {
  2169. $build['fields'][$id]['#class'][] = 'broken';
  2170. $field_name = t('Broken/missing handler: @table > @field', array('@table' => $field['table'], '@field' => $field['field']));
  2171. $build['fields'][$id]['#link'] = l($field_name, "admin/structure/views/nojs/config-item/$view->name/$display->id/$type/$id", array('attributes' => array('class' => array('views-ajax-link')), 'html' => TRUE));
  2172. continue;
  2173. }
  2174. $field_name = check_plain($handler->ui_name(TRUE));
  2175. if (!empty($field['relationship']) && !empty($relationships[$field['relationship']])) {
  2176. $field_name = '(' . $relationships[$field['relationship']] . ') ' . $field_name;
  2177. }
  2178. $description = filter_xss_admin($handler->admin_summary());
  2179. $link_text = $field_name . (empty($description) ? '' : " ($description)");
  2180. $link_attributes = array('class' => array('views-ajax-link'));
  2181. if (!empty($field['exclude'])) {
  2182. $link_attributes['class'][] = 'views-field-excluded';
  2183. }
  2184. $build['fields'][$id]['#link'] = l($link_text, "admin/structure/views/nojs/config-item/$view->name/$display->id/$type/$id", array('attributes' => $link_attributes, 'html' => TRUE));
  2185. $build['fields'][$id]['#class'][] = drupal_clean_css_identifier($display->id . '-' . $type . '-' . $id);
  2186. if (!empty($view->changed_sections[$display->id . '-' . $type . '-' . $id])) {
  2187. // @todo: #changed is no longer being used?
  2188. $build['fields'][$id]['#changed'] = TRUE;
  2189. }
  2190. if ($display->handler->use_group_by() && $handler->use_group_by()) {
  2191. $build['fields'][$id]['#settings_links'][] = l('<span class="label">' . t('Aggregation settings') . '</span>', "admin/structure/views/nojs/config-item-group/$view->name/$display->id/$type/$id", array('attributes' => array('class' => 'views-button-configure views-ajax-link', 'title' => t('Aggregation settings')), 'html' => TRUE));
  2192. }
  2193. if ($handler->has_extra_options()) {
  2194. $build['fields'][$id]['#settings_links'][] = l('<span class="label">' . t('Settings') . '</span>', "admin/structure/views/nojs/config-item-extra/$view->name/$display->id/$type/$id", array('attributes' => array('class' => array('views-button-configure', 'views-ajax-link'), 'title' => t('Settings')), 'html' => TRUE));
  2195. }
  2196. if ($grouping) {
  2197. $gid = $handler->options['group'];
  2198. // Show in default group if the group does not exist.
  2199. if (empty($group_info['groups'][$gid])) {
  2200. $gid = 0;
  2201. }
  2202. $groups[$gid][] = $id;
  2203. }
  2204. }
  2205. // If using grouping, re-order fields so that they show up properly in the
  2206. // list.
  2207. if ($type == 'filter' && $grouping) {
  2208. $store = $build['fields'];
  2209. $build['fields'] = array();
  2210. foreach ($groups as $gid => $contents) {
  2211. // Display an operator between each group.
  2212. if (!empty($build['fields'])) {
  2213. $build['fields'][] = array(
  2214. '#theme' => 'views_ui_display_tab_setting',
  2215. '#class' => array('views-group-text'),
  2216. '#link' => ($group_info['operator'] == 'OR' ? t('OR') : t('AND')),
  2217. );
  2218. }
  2219. // Display an operator between each pair of filters within the group.
  2220. $keys = array_keys($contents);
  2221. $last = end($keys);
  2222. foreach ($contents as $key => $pid) {
  2223. if ($key != $last) {
  2224. $store[$pid]['#link'] .= '&nbsp;&nbsp;' . ($group_info['groups'][$gid] == 'OR' ? t('OR') : t('AND'));
  2225. }
  2226. $build['fields'][$pid] = $store[$pid];
  2227. }
  2228. }
  2229. }
  2230. return $build;
  2231. }
  2232. /**
  2233. * Regenerate the current tab for AJAX updates.
  2234. */
  2235. function views_ui_regenerate_tab(&$view, &$output, $display_id) {
  2236. if (!$view->set_display('default')) {
  2237. return;
  2238. }
  2239. // Regenerate the main display area.
  2240. $build = views_ui_get_display_tab($view, $display_id);
  2241. views_ui_add_microweights($build);
  2242. $output[] = ajax_command_html('#views-tab-' . $display_id, drupal_render($build));
  2243. // Regenerate the top area so changes to display names and order will appear.
  2244. $build = views_ui_render_display_top($view, $display_id);
  2245. views_ui_add_microweights($build);
  2246. $output[] = ajax_command_replace('#views-display-top', drupal_render($build));
  2247. }
  2248. /**
  2249. * Recursively adds microweights to a render array.
  2250. *
  2251. * Similar to what form_builder() does for forms.
  2252. *
  2253. * @todo Submit a core patch to fix drupal_render() to do this, so that all
  2254. * render arrays automatically preserve array insertion order, as forms do.
  2255. */
  2256. function views_ui_add_microweights(&$build) {
  2257. $count = 0;
  2258. foreach (element_children($build) as $key) {
  2259. if (!isset($build[$key]['#weight'])) {
  2260. $build[$key]['#weight'] = $count / 1000;
  2261. }
  2262. views_ui_add_microweights($build[$key]);
  2263. $count++;
  2264. }
  2265. }
  2266. /**
  2267. * Provide a standard set of Apply/Cancel/OK buttons for the forms. Also provide
  2268. * a hidden op operator because the forms plugin doesn't seem to properly
  2269. * provide which button was clicked.
  2270. *
  2271. * TODO: Is the hidden op operator still here somewhere, or is that part of the
  2272. * docblock outdated?
  2273. */
  2274. function views_ui_standard_form_buttons(&$form, &$form_state, $form_id, $name = NULL, $third = NULL, $submit = NULL) {
  2275. $form['buttons'] = array(
  2276. '#prefix' => '<div class="clearfix"><div class="form-buttons">',
  2277. '#suffix' => '</div></div>',
  2278. );
  2279. if (empty($name)) {
  2280. $name = t('Apply');
  2281. $view = $form_state['view'];
  2282. if (!empty($view->stack) && count($view->stack) > 1) {
  2283. $name = t('Apply and continue');
  2284. }
  2285. $names = array(t('Apply'), t('Apply and continue'));
  2286. }
  2287. // Forms that are purely informational set an ok_button flag, so we know not
  2288. // to create an "Apply" button for them.
  2289. if (empty($form_state['ok_button'])) {
  2290. $form['buttons']['submit'] = array(
  2291. '#type' => 'submit',
  2292. '#value' => $name,
  2293. // The regular submit handler ($form_id . '_submit') does not apply if
  2294. // we're updating the default display. It does apply if we're updating
  2295. // the current display. Since we have no way of knowing at this point
  2296. // which display the user wants to update, views_ui_standard_submit will
  2297. // take care of running the regular submit handler as appropriate.
  2298. '#submit' => array('views_ui_standard_submit'),
  2299. );
  2300. // Form API button click detection requires the button's #value to be the
  2301. // same between the form build of the initial page request, and the initial
  2302. // form build of the request processing the form submission. Ideally, the
  2303. // button's #value shouldn't change until the form rebuild step. However,
  2304. // views_ui_ajax_form() implements a different multistep form workflow than
  2305. // the Form API does, and adjusts $view->stack prior to form processing, so
  2306. // we compensate by extending button click detection code to support any of
  2307. // the possible button labels.
  2308. if (isset($names)) {
  2309. $form['buttons']['submit']['#values'] = $names;
  2310. $form['buttons']['submit']['#process'] = array_merge(array('views_ui_form_button_was_clicked'), element_info_property($form['buttons']['submit']['#type'], '#process', array()));
  2311. }
  2312. // If a validation handler exists for the form, assign it to this button.
  2313. if (function_exists($form_id . '_validate')) {
  2314. $form['buttons']['submit']['#validate'][] = $form_id . '_validate';
  2315. }
  2316. }
  2317. // Create a "Cancel" button. For purely informational forms, label it "OK".
  2318. $cancel_submit = function_exists($form_id . '_cancel') ? $form_id . '_cancel' : 'views_ui_standard_cancel';
  2319. $form['buttons']['cancel'] = array(
  2320. '#type' => 'submit',
  2321. '#value' => empty($form_state['ok_button']) ? t('Cancel') : t('Ok'),
  2322. '#submit' => array($cancel_submit),
  2323. '#validate' => array(),
  2324. );
  2325. // Some forms specify a third button, with a name and submit handler.
  2326. if ($third) {
  2327. if (empty($submit)) {
  2328. $submit = 'third';
  2329. }
  2330. $third_submit = function_exists($form_id . '_' . $submit) ? $form_id . '_' . $submit : 'views_ui_standard_cancel';
  2331. $form['buttons'][$submit] = array(
  2332. '#type' => 'submit',
  2333. '#value' => $third,
  2334. '#validate' => array(),
  2335. '#submit' => array($third_submit),
  2336. );
  2337. }
  2338. // Compatibility, to be removed later: // @todo When is "later"? We used to
  2339. // set these items on the form, but now we want them on the $form_state.
  2340. if (isset($form['#title'])) {
  2341. $form_state['title'] = $form['#title'];
  2342. }
  2343. if (isset($form['#help_topic'])) {
  2344. $form_state['help_topic'] = $form['#help_topic'];
  2345. }
  2346. if (isset($form['#help_module'])) {
  2347. $form_state['help_module'] = $form['#help_module'];
  2348. }
  2349. if (isset($form['#url'])) {
  2350. $form_state['url'] = $form['#url'];
  2351. }
  2352. if (isset($form['#section'])) {
  2353. $form_state['#section'] = $form['#section'];
  2354. }
  2355. // Finally, we never want these cached -- our object cache does that for us.
  2356. $form['#no_cache'] = TRUE;
  2357. // If this isn't an ajaxy form, then we want to set the title.
  2358. if (!empty($form['#title'])) {
  2359. drupal_set_title($form['#title']);
  2360. }
  2361. }
  2362. /**
  2363. * Basic submit handler applicable to all 'standard' forms.
  2364. *
  2365. * This submit handler determines whether the user wants the submitted changes
  2366. * to apply to the default display or to the current display, and dispatches
  2367. * control appropriately.
  2368. */
  2369. function views_ui_standard_submit($form, &$form_state) {
  2370. // Determine whether the values the user entered are intended to apply to
  2371. // the current display or the default display.
  2372. list($was_defaulted, $is_defaulted, $revert) = views_ui_standard_override_values($form, $form_state);
  2373. // Mark the changed section of the view as changed.
  2374. // @todo Document why we are doing this, and see if we still need it.
  2375. if (!empty($form['#section'])) {
  2376. $form_state['view']->changed_sections[$form['#section']] = TRUE;
  2377. }
  2378. // Based on the user's choice in the display dropdown, determine which display
  2379. // these changes apply to.
  2380. if ($revert) {
  2381. // If it's revert just change the override and return.
  2382. $display = &$form_state['view']->display[$form_state['display_id']];
  2383. $display->handler->options_override($form, $form_state);
  2384. // Don't execute the normal submit handling but still store the changed
  2385. // view into cache.
  2386. views_ui_cache_set($form_state['view']);
  2387. return;
  2388. }
  2389. elseif ($was_defaulted === $is_defaulted) {
  2390. // We're not changing which display these form values apply to.
  2391. // Run the regular submit handler for this form.
  2392. }
  2393. elseif ($was_defaulted && !$is_defaulted) {
  2394. // We were using the default display's values, but we're now overriding
  2395. // the default display and saving values specific to this display.
  2396. $display = &$form_state['view']->display[$form_state['display_id']];
  2397. // options_override toggles the override of this section.
  2398. $display->handler->options_override($form, $form_state);
  2399. $display->handler->options_submit($form, $form_state);
  2400. }
  2401. elseif (!$was_defaulted && $is_defaulted) {
  2402. // We used to have an override for this display, but the user now wants
  2403. // to go back to the default display.
  2404. // Overwrite the default display with the current form values, and make
  2405. // the current display use the new default values.
  2406. $display = &$form_state['view']->display[$form_state['display_id']];
  2407. // options_override toggles the override of this section.
  2408. $display->handler->options_override($form, $form_state);
  2409. $display->handler->options_submit($form, $form_state);
  2410. }
  2411. $submit_handler = $form['#form_id'] . '_submit';
  2412. if (function_exists($submit_handler)) {
  2413. $submit_handler($form, $form_state);
  2414. }
  2415. }
  2416. /**
  2417. * Return the was_defaulted, is_defaulted and revert state of a form.
  2418. */
  2419. function views_ui_standard_override_values($form, $form_state) {
  2420. // Make sure the dropdown exists in the first place.
  2421. if (isset($form_state['values']['override']['dropdown'])) {
  2422. // #default_value is used to determine whether it was the default value or
  2423. // not. So the available options are: $display, 'default' and
  2424. // 'default_revert', not 'defaults'.
  2425. $was_defaulted = ($form['override']['dropdown']['#default_value'] === 'defaults');
  2426. $is_defaulted = ($form_state['values']['override']['dropdown'] === 'default');
  2427. $revert = ($form_state['values']['override']['dropdown'] === 'default_revert');
  2428. if ($was_defaulted !== $is_defaulted && isset($form['#section'])) {
  2429. // We're changing which display these values apply to.
  2430. // Update the #section so it knows what to mark changed.
  2431. $form['#section'] = str_replace('default-', $form_state['display_id'] . '-', $form['#section']);
  2432. }
  2433. }
  2434. else {
  2435. // The user didn't get the dropdown for overriding the default display.
  2436. $was_defaulted = FALSE;
  2437. $is_defaulted = FALSE;
  2438. $revert = FALSE;
  2439. }
  2440. return array($was_defaulted, $is_defaulted, $revert);
  2441. }
  2442. /**
  2443. * Submit handler for cancel button.
  2444. */
  2445. function views_ui_standard_cancel($form, &$form_state) {
  2446. if (!empty($form_state['view']->changed) && isset($form_state['view']->form_cache)) {
  2447. unset($form_state['view']->form_cache);
  2448. views_ui_cache_set($form_state['view']);
  2449. }
  2450. $form_state['redirect'] = 'admin/structure/views/view/' . $form_state['view']->name . '/edit';
  2451. }
  2452. /**
  2453. * Add a <select> dropdown for a given section.
  2454. *
  2455. * Allows the user to change whether this info is stored on the default display
  2456. * or on the current display.
  2457. */
  2458. function views_ui_standard_display_dropdown(&$form, &$form_state, $section) {
  2459. $view = &$form_state['view'];
  2460. $display_id = $form_state['display_id'];
  2461. $displays = $view->display;
  2462. $current_display = $view->display[$display_id];
  2463. // Add the "2 of 3" progress indicator.
  2464. // @todo: Move this to a separate function if it's needed on any forms that
  2465. // don't have the display dropdown.
  2466. if ($form_progress = views_ui_get_form_progress($view)) {
  2467. $form['progress']['#markup'] = '<div id="views-progress-indicator">' . t('@current of @total', array('@current' => $form_progress['current'], '@total' => $form_progress['total'])) . '</div>';
  2468. $form['progress']['#weight'] = -1001;
  2469. }
  2470. if ($current_display->handler->is_default_display()) {
  2471. return;
  2472. }
  2473. // Determine whether any other displays have overrides for this section.
  2474. $section_overrides = FALSE;
  2475. $section_defaulted = $current_display->handler->is_defaulted($section);
  2476. foreach ($displays as $id => $display) {
  2477. if ($id === 'default' || $id === $display_id) {
  2478. continue;
  2479. }
  2480. if ($display->handler && !$display->handler->is_defaulted($section)) {
  2481. $section_overrides = TRUE;
  2482. }
  2483. }
  2484. $display_dropdown['default'] = ($section_overrides ? t('All displays (except overridden)') : t('All displays'));
  2485. $display_dropdown[$display_id] = t('This @display_type (override)', array('@display_type' => $current_display->display_plugin));
  2486. // Only display the revert option if we are in a overridden section.
  2487. if (!$section_defaulted) {
  2488. $display_dropdown['default_revert'] = t('Revert to default');
  2489. }
  2490. $form['override'] = array(
  2491. '#prefix' => '<div class="views-override clearfix container-inline">',
  2492. '#suffix' => '</div>',
  2493. '#weight' => -1000,
  2494. '#tree' => TRUE,
  2495. );
  2496. $form['override']['dropdown'] = array(
  2497. '#type' => 'select',
  2498. '#title' => t('For'),
  2499. // @todo: Translators may need more context than this.
  2500. '#options' => $display_dropdown,
  2501. );
  2502. if ($current_display->handler->is_defaulted($section)) {
  2503. $form['override']['dropdown']['#default_value'] = 'defaults';
  2504. }
  2505. else {
  2506. $form['override']['dropdown']['#default_value'] = $display_id;
  2507. }
  2508. }
  2509. /**
  2510. * Get the user's current progress through the form stack.
  2511. *
  2512. * @param view $view
  2513. * The current view.
  2514. *
  2515. * @return
  2516. * FALSE if the user is not currently in a multiple-form stack. Otherwise,
  2517. * an associative array with the following keys:
  2518. * - current: The number of the current form on the stack.
  2519. * - total: The total number of forms originally on the stack.
  2520. */
  2521. function views_ui_get_form_progress($view) {
  2522. $progress = FALSE;
  2523. if (!empty($view->stack)) {
  2524. $stack = $view->stack;
  2525. // The forms on the stack have integer keys that don't change as the forms
  2526. // are completed, so we can see which ones are still left.
  2527. $keys = array_keys($view->stack);
  2528. // Add 1 to the array keys for the benefit of humans, who start counting
  2529. // from 1 and not 0.
  2530. $current = reset($keys) + 1;
  2531. $total = end($keys) + 1;
  2532. if ($total > 1) {
  2533. $progress = array();
  2534. $progress['current'] = $current;
  2535. $progress['total'] = $total;
  2536. }
  2537. }
  2538. return $progress;
  2539. }
  2540. // --------------------------------------------------------------------------
  2541. // Various subforms for editing the pieces of a view.
  2542. function views_ui_ajax_forms($key = NULL) {
  2543. $forms = array(
  2544. 'display' => array(
  2545. 'form_id' => 'views_ui_edit_display_form',
  2546. 'args' => array('section'),
  2547. ),
  2548. 'remove-display' => array(
  2549. 'form_id' => 'views_ui_remove_display_form',
  2550. 'args' => array(),
  2551. ),
  2552. 'config-type' => array(
  2553. 'form_id' => 'views_ui_config_type_form',
  2554. 'args' => array('type'),
  2555. ),
  2556. 'rearrange' => array(
  2557. 'form_id' => 'views_ui_rearrange_form',
  2558. 'args' => array('type'),
  2559. ),
  2560. 'rearrange-filter' => array(
  2561. 'form_id' => 'views_ui_rearrange_filter_form',
  2562. 'args' => array('type'),
  2563. ),
  2564. 'reorder-displays' => array(
  2565. 'form_id' => 'views_ui_reorder_displays_form',
  2566. 'args' => array(),
  2567. ),
  2568. 'add-item' => array(
  2569. 'form_id' => 'views_ui_add_item_form',
  2570. 'args' => array('type'),
  2571. ),
  2572. 'config-item' => array(
  2573. 'form_id' => 'views_ui_config_item_form',
  2574. 'args' => array('type', 'id'),
  2575. ),
  2576. 'config-item-extra' => array(
  2577. 'form_id' => 'views_ui_config_item_extra_form',
  2578. 'args' => array('type', 'id'),
  2579. ),
  2580. 'config-item-group' => array(
  2581. 'form_id' => 'views_ui_config_item_group_form',
  2582. 'args' => array('type', 'id'),
  2583. ),
  2584. 'config-style' => array(
  2585. 'form_id' => 'views_ui_config_style_form',
  2586. 'args' => array('type', 'id'),
  2587. ),
  2588. 'edit-details' => array(
  2589. 'form_id' => 'views_ui_edit_details_form',
  2590. 'args' => array(),
  2591. ),
  2592. 'analyze' => array(
  2593. 'form_id' => 'views_ui_analyze_view_form',
  2594. 'args' => array(),
  2595. ),
  2596. );
  2597. if ($key) {
  2598. return !empty($forms[$key]) ? $forms[$key] : NULL;
  2599. }
  2600. return $forms;
  2601. }
  2602. /**
  2603. * Build a form identifier that we can use to see if one form
  2604. * is the same as another. Since the arguments differ slightly
  2605. * we do a lot of spiffy concatenation here.
  2606. */
  2607. function views_ui_build_identifier($key, $view, $display_id, $args) {
  2608. $form = views_ui_ajax_forms($key);
  2609. // Automatically remove the single-form cache if it exists and
  2610. // does not match the key.
  2611. $identifier = implode('-', array($key, $view->name, $display_id));
  2612. foreach ($form['args'] as $id) {
  2613. $arg = (!empty($args)) ? array_shift($args) : NULL;
  2614. $identifier .= '-' . $arg;
  2615. }
  2616. return $identifier;
  2617. }
  2618. /**
  2619. * Build up a $form_state object suitable for use with drupal_build_form
  2620. * based on known information about a form.
  2621. */
  2622. function views_ui_build_form_state($js, $key, &$view, $display_id, $args) {
  2623. $form = views_ui_ajax_forms($key);
  2624. // Build up form state.
  2625. $form_state = array(
  2626. 'form_key' => $key,
  2627. 'form_id' => $form['form_id'],
  2628. 'view' => &$view,
  2629. 'ajax' => $js,
  2630. 'display_id' => $display_id,
  2631. 'no_redirect' => TRUE,
  2632. );
  2633. foreach ($form['args'] as $id) {
  2634. $form_state[$id] = (!empty($args)) ? array_shift($args) : NULL;
  2635. }
  2636. return $form_state;
  2637. }
  2638. /**
  2639. * Create the URL for one of our standard AJAX forms based upon known
  2640. * information about the form.
  2641. */
  2642. function views_ui_build_form_url($form_state) {
  2643. $form = views_ui_ajax_forms($form_state['form_key']);
  2644. $ajax = empty($form_state['ajax']) ? 'nojs' : 'ajax';
  2645. $name = $form_state['view']->name;
  2646. $url = "admin/structure/views/$ajax/$form_state[form_key]/$name/$form_state[display_id]";
  2647. foreach ($form['args'] as $arg) {
  2648. $url .= '/' . $form_state[$arg];
  2649. }
  2650. return $url;
  2651. }
  2652. /**
  2653. * Add another form to the stack; clicking 'apply' will go to this form
  2654. * rather than closing the ajax popup.
  2655. */
  2656. function views_ui_add_form_to_stack($key, &$view, $display_id, $args, $top = FALSE, $rebuild_keys = FALSE) {
  2657. if (empty($view->stack)) {
  2658. $view->stack = array();
  2659. }
  2660. $stack = array(views_ui_build_identifier($key, $view, $display_id, $args), $key, &$view, $display_id, $args);
  2661. // If we're being asked to add this form to the bottom of the stack, no
  2662. // special logic is required. Our work is equally easy if we were asked to add
  2663. // to the top of the stack, but there's nothing in it yet.
  2664. if (!$top || empty($view->stack)) {
  2665. $view->stack[] = $stack;
  2666. }
  2667. // If we're adding to the top of an existing stack, we have to maintain the
  2668. // existing integer keys, so they can be used for the "2 of 3" progress
  2669. // indicator (which will now read "2 of 4").
  2670. else {
  2671. $keys = array_keys($view->stack);
  2672. $first = current($keys);
  2673. $last = end($keys);
  2674. for ($i = $last; $i >= $first; $i--) {
  2675. if (!isset($view->stack[$i])) {
  2676. continue;
  2677. }
  2678. // Move form number $i to the next position in the stack.
  2679. $view->stack[$i + 1] = $view->stack[$i];
  2680. unset($view->stack[$i]);
  2681. }
  2682. // Now that the previously $first slot is free, move the new form into it.
  2683. $view->stack[$first] = $stack;
  2684. ksort($view->stack);
  2685. // Start the keys from 0 again, if requested.
  2686. if ($rebuild_keys) {
  2687. $view->stack = array_values($view->stack);
  2688. }
  2689. }
  2690. }
  2691. /**
  2692. * Generic entry point to handle forms.
  2693. *
  2694. * We do this for consistency and to make it easy to chain forms
  2695. * together.
  2696. */
  2697. function views_ui_ajax_form($js, $key, $view, $display_id = '') {
  2698. $args = func_get_args();
  2699. // Remove the known args.
  2700. array_splice($args, 0, 4);
  2701. // Reset the cache of IDs. Drupal rather aggressively prevents id duplication
  2702. // but this causes it to remember IDs that are no longer even being used.
  2703. if (isset($_POST['ajax_html_ids'])) {
  2704. unset($_POST['ajax_html_ids']);
  2705. }
  2706. $form = views_ui_ajax_forms($key);
  2707. if (empty($form)) {
  2708. return MENU_NOT_FOUND;
  2709. }
  2710. views_include('ajax');
  2711. $form_state = views_ui_build_form_state($js, $key, $view, $display_id, $args);
  2712. // check to see if this is the top form of the stack. If it is, pop
  2713. // it off; if it isn't, the user clicked somewhere else and the stack is
  2714. // now irrelevant.
  2715. if (!empty($view->stack)) {
  2716. $identifier = views_ui_build_identifier($key, $view, $display_id, $args);
  2717. // Retrieve the first form from the stack without changing the integer keys,
  2718. // as they're being used for the "2 of 3" progress indicator.
  2719. reset($view->stack);
  2720. $key = key($view->stack);
  2721. $top = current($view->stack);
  2722. unset($view->stack[$key]);
  2723. if (array_shift($top) != $identifier) {
  2724. $view->stack = array();
  2725. }
  2726. }
  2727. // Automatically remove the form cache if it is set and the key does
  2728. // not match. This way navigating away from the form without hitting
  2729. // update will work.
  2730. if (isset($view->form_cache) && $view->form_cache['key'] != $key) {
  2731. unset($view->form_cache);
  2732. }
  2733. // With the below logic, we may end up rendering a form twice (or two forms
  2734. // each sharing the same element ids), potentially resulting in
  2735. // drupal_add_js() being called twice to add the same setting. drupal_get_js()
  2736. // is ok with that, but until ajax_render() is, reset the drupal_add_js()
  2737. // static before rendering the second time.
  2738. // @see http://drupal.org/node/208611
  2739. $drupal_add_js_original = drupal_add_js();
  2740. $drupal_add_js = &drupal_static('drupal_add_js');
  2741. $output = views_ajax_form_wrapper($form_state['form_id'], $form_state);
  2742. if ($form_state['submitted'] && empty($form_state['rerender'])) {
  2743. // Sometimes we need to re-generate the form for multi-step type operations.
  2744. $object = NULL;
  2745. if (!empty($view->stack)) {
  2746. $drupal_add_js = $drupal_add_js_original;
  2747. $stack = $view->stack;
  2748. $top = array_shift($stack);
  2749. $top[0] = $js;
  2750. // Change view into a reference.
  2751. $stepview = $top[2];
  2752. $top[2] = &$stepview;
  2753. $form_state = call_user_func_array('views_ui_build_form_state', $top);
  2754. $form_state['input'] = array();
  2755. $form_state['url'] = url(views_ui_build_form_url($form_state));
  2756. if (!$js) {
  2757. return drupal_goto(views_ui_build_form_url($form_state));
  2758. }
  2759. $output = views_ajax_form_wrapper($form_state['form_id'], $form_state);
  2760. }
  2761. elseif (!$js) {
  2762. // If nothing on the stack, non-js forms just go back to the main view
  2763. // editor.
  2764. return drupal_goto("admin/structure/views/view/$view->name/edit");
  2765. }
  2766. else {
  2767. $output = array();
  2768. $output[] = views_ajax_command_dismiss_form();
  2769. $output[] = views_ajax_command_show_buttons(!empty($view->changed));
  2770. $output[] = views_ajax_command_trigger_preview();
  2771. if (!empty($form_state['#page_title'])) {
  2772. $output[] = views_ajax_command_replace_title($form_state['#page_title']);
  2773. }
  2774. }
  2775. // If this form was for view-wide changes, there's no need to regenerate
  2776. // the display section of the form.
  2777. if ($display_id !== '') {
  2778. views_ui_regenerate_tab($view, $output, $display_id);
  2779. }
  2780. }
  2781. return $js ? array('#type' => 'ajax', '#commands' => $output) : $output;
  2782. }
  2783. /**
  2784. * Submit handler to add a restore a removed display to a view.
  2785. */
  2786. function views_ui_remove_display_form_restore($form, &$form_state) {
  2787. // Create the new display.
  2788. $id = $form_state['display_id'];
  2789. $form_state['view']->display[$id]->deleted = FALSE;
  2790. // Store in cache.
  2791. views_ui_cache_set($form_state['view']);
  2792. }
  2793. /**
  2794. * Form constructor callback to display analysis information on a view.
  2795. */
  2796. function views_ui_analyze_view_form($form, &$form_state) {
  2797. $view = &$form_state['view'];
  2798. $form['#title'] = t('View analysis');
  2799. $form['#section'] = 'analyze';
  2800. views_include('analyze');
  2801. $messages = views_analyze_view($view);
  2802. $form['analysis'] = array(
  2803. '#prefix' => '<div class="form-item">',
  2804. '#suffix' => '</div>',
  2805. '#markup' => views_analyze_format_result($view, $messages),
  2806. );
  2807. // Inform the standard button function that we want an OK button.
  2808. $form_state['ok_button'] = TRUE;
  2809. views_ui_standard_form_buttons($form, $form_state, 'views_ui_analyze_view_form');
  2810. return $form;
  2811. }
  2812. /**
  2813. * Submit handler for views_ui_analyze_view_form.
  2814. */
  2815. function views_ui_analyze_view_form_submit($form, &$form_state) {
  2816. $form_state['redirect'] = 'admin/structure/views/view/' . $form_state['view']->name . '/edit';
  2817. }
  2818. /**
  2819. * Form constructor callback to reorder displays on a view.
  2820. */
  2821. function views_ui_reorder_displays_form($form, &$form_state) {
  2822. $view = &$form_state['view'];
  2823. $display_id = $form_state['display_id'];
  2824. $form['view'] = array('#type' => 'value', '#value' => $view);
  2825. $form['#tree'] = TRUE;
  2826. $last_display = end($view->display);
  2827. foreach ($view->display as $display) {
  2828. $form[$display->id] = array(
  2829. 'title' => array('#markup' => check_plain($display->display_title)),
  2830. 'weight' => array(
  2831. '#type' => 'weight',
  2832. '#value' => $display->position,
  2833. '#delta' => $last_display->position,
  2834. '#title' => t('Weight for @display', array('@display' => $display->display_title)),
  2835. '#title_display' => 'invisible',
  2836. ),
  2837. '#tree' => TRUE,
  2838. '#display' => $display,
  2839. 'removed' => array(
  2840. '#type' => 'checkbox',
  2841. '#id' => 'display-removed-' . $display->id,
  2842. '#attributes' => array('class' => array('views-remove-checkbox')),
  2843. '#default_value' => isset($display->deleted),
  2844. ),
  2845. );
  2846. if (isset($display->deleted) && $display->deleted) {
  2847. $form[$display->id]['deleted'] = array('#type' => 'value', '#value' => TRUE);
  2848. }
  2849. if ($display->id === 'default') {
  2850. unset($form[$display->id]['weight']);
  2851. unset($form[$display->id]['removed']);
  2852. }
  2853. }
  2854. $form['#title'] = t('Displays Reorder');
  2855. $form['#section'] = 'reorder';
  2856. // Add JavaScript settings that will be added via $.extend for tabledragging.
  2857. $form['#js']['tableDrag']['reorder-displays']['weight'][0] = array(
  2858. 'target' => 'weight',
  2859. 'source' => NULL,
  2860. 'relationship' => 'sibling',
  2861. 'action' => 'order',
  2862. 'hidden' => TRUE,
  2863. 'limit' => 0,
  2864. );
  2865. $form['#action'] = url('admin/structure/views/nojs/reorder-displays/' . $view->name . '/' . $display_id);
  2866. views_ui_standard_form_buttons($form, $form_state, 'views_ui_reorder_displays_form');
  2867. return $form;
  2868. }
  2869. /**
  2870. * Display position sorting function.
  2871. */
  2872. function _views_position_sort($display1, $display2) {
  2873. if ($display1->position != $display2->position) {
  2874. return $display1->position < $display2->position ? -1 : 1;
  2875. }
  2876. return 0;
  2877. }
  2878. /**
  2879. * Submit handler for rearranging display form.
  2880. */
  2881. function views_ui_reorder_displays_form_submit($form, &$form_state) {
  2882. foreach ($form_state['input'] as $display => $info) {
  2883. // add each value that is a field with a weight to our list, but only if
  2884. // it has had its 'removed' checkbox checked.
  2885. if (is_array($info) && isset($info['weight']) && empty($info['removed'])) {
  2886. $order[$display] = $info['weight'];
  2887. }
  2888. }
  2889. // Sort the order array.
  2890. asort($order);
  2891. // Fixing up positions.
  2892. $position = 2;
  2893. foreach (array_keys($order) as $display) {
  2894. $order[$display] = $position++;
  2895. }
  2896. // Setting up position and removing deleted displays.
  2897. $displays = $form_state['view']->display;
  2898. foreach ($displays as $display_id => $display) {
  2899. // Don't touch the default !!!
  2900. if ($display_id === 'default') {
  2901. continue;
  2902. }
  2903. if (isset($order[$display_id])) {
  2904. $form_state['view']->display[$display_id]->position = $order[$display_id];
  2905. }
  2906. else {
  2907. $form_state['view']->display[$display_id]->deleted = TRUE;
  2908. }
  2909. }
  2910. // Sorting back the display array as the position is not enough.
  2911. uasort($form_state['view']->display, '_views_position_sort');
  2912. // Store in cache.
  2913. views_ui_cache_set($form_state['view']);
  2914. $form_state['redirect'] = array('admin/structure/views/view/' . $form_state['view']->name . '/edit', array('fragment' => 'views-tab-default'));
  2915. }
  2916. /**
  2917. * Turn the reorder form into a proper table.
  2918. */
  2919. function theme_views_ui_reorder_displays_form($vars) {
  2920. $form = $vars['form'];
  2921. $rows = array();
  2922. foreach (element_children($form) as $key) {
  2923. if (isset($form[$key]['#display'])) {
  2924. $display = &$form[$key];
  2925. $row = array();
  2926. $row[] = drupal_render($display['title']);
  2927. $form[$key]['weight']['#attributes']['class'] = array('weight');
  2928. $row[] = drupal_render($form[$key]['weight']);
  2929. if (isset($display['removed'])) {
  2930. $row[] = drupal_render($form[$key]['removed']) .
  2931. l('<span>' . t('Remove') . '</span>',
  2932. 'javascript:void()',
  2933. array(
  2934. 'attributes' => array(
  2935. 'id' => 'display-remove-link-' . $key,
  2936. 'class' => array('views-button-remove display-remove-link'),
  2937. 'alt' => t('Remove this display'),
  2938. 'title' => t('Remove this display')),
  2939. 'html' => TRUE));
  2940. }
  2941. else {
  2942. $row[] = '';
  2943. }
  2944. $class = array();
  2945. $styles = array();
  2946. if (isset($form[$key]['weight']['#type'])) {
  2947. $class[] = 'draggable';
  2948. }
  2949. if (isset($form[$key]['deleted']['#value']) && $form[$key]['deleted']['#value']) {
  2950. $styles[] = 'display: none;';
  2951. }
  2952. $rows[] = array('data' => $row, 'class' => $class, 'id' => 'display-row-' . $key, 'style' => $styles);
  2953. }
  2954. }
  2955. $header = array(t('Display'), t('Weight'), t('Remove'));
  2956. $output = '';
  2957. drupal_add_tabledrag('reorder-displays', 'order', 'sibling', 'weight');
  2958. $output = drupal_render($form['override']);
  2959. $output .= '<div class="scroll">';
  2960. $output .= theme('table',
  2961. array(
  2962. 'header' => $header,
  2963. 'rows' => $rows,
  2964. 'attributes' => array('id' => 'reorder-displays'),
  2965. ));
  2966. $output .= '</div>';
  2967. $output .= drupal_render_children($form);
  2968. return $output;
  2969. }
  2970. /**
  2971. * Form builder to edit details of a view.
  2972. */
  2973. function views_ui_edit_details_form($form, &$form_state) {
  2974. $view = &$form_state['view'];
  2975. $form['#title'] = t('View name and description');
  2976. $form['#section'] = 'details';
  2977. $form['details'] = array(
  2978. '#theme_wrappers' => array('container'),
  2979. '#attributes' => array('class' => array('scroll')),
  2980. );
  2981. $form['details']['human_name'] = array(
  2982. '#type' => 'textfield',
  2983. '#title' => t('Human-readable name'),
  2984. '#description' => t('A descriptive human-readable name for this view. Spaces are allowed'),
  2985. '#default_value' => $view->get_human_name(),
  2986. );
  2987. $form['details']['tag'] = array(
  2988. '#type' => 'textfield',
  2989. '#title' => t('View tag'),
  2990. '#description' => t('Optionally, enter a comma delimited list of tags for this view to use in filtering and sorting views on the administrative page.'),
  2991. '#default_value' => $view->tag,
  2992. '#autocomplete_path' => 'admin/views/ajax/autocomplete/tag',
  2993. );
  2994. $form['details']['description'] = array(
  2995. '#type' => 'textfield',
  2996. '#title' => t('View description'),
  2997. '#description' => t('This description will appear on the Views administrative UI to tell you what the view is about.'),
  2998. '#default_value' => $view->description,
  2999. );
  3000. views_ui_standard_form_buttons($form, $form_state, 'views_ui_edit_details_form');
  3001. return $form;
  3002. }
  3003. /**
  3004. * Submit handler for views_ui_edit_details_form.
  3005. */
  3006. function views_ui_edit_details_form_submit($form, &$form_state) {
  3007. $view = $form_state['view'];
  3008. foreach ($form_state['values'] as $key => $value) {
  3009. // Only save values onto the view if they're actual view properties
  3010. // (as opposed to 'op' or 'form_build_id').
  3011. if (isset($form['details'][$key])) {
  3012. $view->$key = $value;
  3013. }
  3014. }
  3015. $form_state['#page_title'] = views_ui_edit_page_title($view);
  3016. views_ui_cache_set($view);
  3017. }
  3018. /**
  3019. * Form constructor callback to edit display of a view.
  3020. */
  3021. function views_ui_edit_display_form($form, &$form_state) {
  3022. $view = &$form_state['view'];
  3023. $display_id = $form_state['display_id'];
  3024. $section = $form_state['section'];
  3025. if (!$view->set_display($display_id)) {
  3026. views_ajax_error(t('Invalid display id @display', array('@display' => $display_id)));
  3027. }
  3028. $display = &$view->display[$display_id];
  3029. // Get form from the handler.
  3030. $form['options'] = array(
  3031. '#theme_wrappers' => array('container'),
  3032. '#attributes' => array('class' => array('scroll')),
  3033. );
  3034. $display->handler->options_form($form['options'], $form_state);
  3035. // The handler options form sets $form['#title'], which we need on the entire
  3036. // $form instead of just the ['options'] section.
  3037. $form['#title'] = $form['options']['#title'];
  3038. unset($form['options']['#title']);
  3039. // Move the override dropdown out of the scrollable section of the form.
  3040. if (isset($form['options']['override'])) {
  3041. $form['override'] = $form['options']['override'];
  3042. unset($form['options']['override']);
  3043. }
  3044. $name = NULL;
  3045. if (isset($form_state['update_name'])) {
  3046. $name = $form_state['update_name'];
  3047. }
  3048. views_ui_standard_form_buttons($form, $form_state, 'views_ui_edit_display_form', $name);
  3049. return $form;
  3050. }
  3051. /**
  3052. * Validate handler for views_ui_edit_display_form.
  3053. */
  3054. function views_ui_edit_display_form_validate($form, &$form_state) {
  3055. $display = &$form_state['view']->display[$form_state['display_id']];
  3056. $display->handler->options_validate($form['options'], $form_state);
  3057. if (form_get_errors()) {
  3058. $form_state['rerender'] = TRUE;
  3059. }
  3060. }
  3061. /**
  3062. * Submit handler for views_ui_edit_display_form.
  3063. */
  3064. function views_ui_edit_display_form_submit($form, &$form_state) {
  3065. $display = &$form_state['view']->display[$form_state['display_id']];
  3066. $display->handler->options_submit($form, $form_state);
  3067. views_ui_cache_set($form_state['view']);
  3068. }
  3069. /**
  3070. * Override handler for views_ui_edit_display_form.
  3071. *
  3072. * @todo: Not currently used. Remove unless we implement an override toggle.
  3073. */
  3074. function views_ui_edit_display_form_override($form, &$form_state) {
  3075. $display = &$form_state['view']->display[$form_state['display_id']];
  3076. $display->handler->options_override($form, $form_state);
  3077. views_ui_cache_set($form_state['view']);
  3078. $form_state['rerender'] = TRUE;
  3079. $form_state['rebuild'] = TRUE;
  3080. }
  3081. /**
  3082. * Form to config items in the views UI.
  3083. */
  3084. function views_ui_config_type_form($form, &$form_state) {
  3085. $view = &$form_state['view'];
  3086. $display_id = $form_state['display_id'];
  3087. $type = $form_state['type'];
  3088. $types = views_object_types();
  3089. if (!$view->set_display($display_id)) {
  3090. views_ajax_error(t('Invalid display id @display', array('@display' => $display_id)));
  3091. }
  3092. $display = &$view->display[$display_id];
  3093. $form['#title'] = t('Configure @type', array('@type' => $types[$type]['ltitle']));
  3094. $form['#section'] = $display_id . 'config-item';
  3095. if ($display->handler->defaultable_sections($types[$type]['plural'])) {
  3096. $form_state['section'] = $types[$type]['plural'];
  3097. views_ui_standard_display_dropdown($form, $form_state, $form_state['section']);
  3098. }
  3099. if (!empty($types[$type]['options']) && function_exists($types[$type]['options'])) {
  3100. $options = $type . '_options';
  3101. $form[$options] = array('#tree' => TRUE);
  3102. $types[$type]['options']($form, $form_state);
  3103. }
  3104. $name = NULL;
  3105. if (isset($form_state['update_name'])) {
  3106. $name = $form_state['update_name'];
  3107. }
  3108. views_ui_standard_form_buttons($form, $form_state, 'views_ui_config_type_form', $name);
  3109. return $form;
  3110. }
  3111. /**
  3112. * Submit handler for type configuration form.
  3113. */
  3114. function views_ui_config_type_form_submit($form, &$form_state) {
  3115. $types = views_object_types();
  3116. $display = &$form_state['view']->display[$form_state['display_id']];
  3117. // Store in cache.
  3118. views_ui_cache_set($form_state['view']);
  3119. }
  3120. /**
  3121. * Form to rearrange items in the views UI.
  3122. */
  3123. function views_ui_rearrange_form($form, &$form_state) {
  3124. $view = &$form_state['view'];
  3125. $display_id = $form_state['display_id'];
  3126. $type = $form_state['type'];
  3127. $types = views_object_types();
  3128. if (!$view->set_display($display_id)) {
  3129. views_ajax_error(t('Invalid display id @display', array('@display' => $display_id)));
  3130. }
  3131. $display = &$view->display[$display_id];
  3132. $form['#title'] = t('Rearrange @type', array('@type' => $types[$type]['ltitle']));
  3133. $form['#section'] = $display_id . 'rearrange-item';
  3134. if ($display->handler->defaultable_sections($types[$type]['plural'])) {
  3135. $form_state['section'] = $types[$type]['plural'];
  3136. views_ui_standard_display_dropdown($form, $form_state, $form_state['section']);
  3137. }
  3138. $count = 0;
  3139. // Get relationship labels.
  3140. $relationships = array();
  3141. foreach ($display->handler->get_handlers('relationship') as $id => $handler) {
  3142. $relationships[$id] = $handler->label();
  3143. $handlers = $display->handler->get_option('relationships');
  3144. if ($handlers) {
  3145. foreach ($handlers as $id => $info) {
  3146. $handler = $display->handler->get_handler('relationship', $id);
  3147. $relationships[$id] = $handler->label();
  3148. }
  3149. }
  3150. }
  3151. // Filters can now be grouped so we do a little bit extra.
  3152. $groups = array();
  3153. $grouping = FALSE;
  3154. if ($type == 'filter') {
  3155. $group_info = $view->display_handler->get_option('filter_groups');
  3156. if (!empty($group_info['groups']) && count($group_info['groups']) > 1) {
  3157. $grouping = TRUE;
  3158. $groups = array(0 => array());
  3159. }
  3160. }
  3161. foreach ($display->handler->get_option($types[$type]['plural']) as $id => $field) {
  3162. $form['fields'][$id] = array('#tree' => TRUE);
  3163. $form['fields'][$id]['weight'] = array(
  3164. '#type' => 'textfield',
  3165. '#default_value' => ++$count,
  3166. );
  3167. $handler = $display->handler->get_handler($type, $id);
  3168. if ($handler) {
  3169. $name = $handler->ui_name() . ' ' . $handler->admin_summary();
  3170. if (!empty($field['relationship']) && !empty($relationships[$field['relationship']])) {
  3171. $name = '(' . $relationships[$field['relationship']] . ') ' . $name;
  3172. }
  3173. $form['fields'][$id]['name'] = array(
  3174. '#markup' => $name,
  3175. );
  3176. }
  3177. else {
  3178. $form['fields'][$id]['name'] = array('#markup' => t('Broken field @id', array('@id' => $id)));
  3179. }
  3180. $form['fields'][$id]['removed'] = array(
  3181. '#type' => 'checkbox',
  3182. '#id' => 'views-removed-' . $id,
  3183. '#attributes' => array('class' => array('views-remove-checkbox')),
  3184. '#default_value' => 0,
  3185. );
  3186. }
  3187. // Add JavaScript settings that will be added via $.extend for tabledragging.
  3188. $form['#js']['tableDrag']['arrange']['weight'][0] = array(
  3189. 'target' => 'weight',
  3190. 'source' => NULL,
  3191. 'relationship' => 'sibling',
  3192. 'action' => 'order',
  3193. 'hidden' => TRUE,
  3194. 'limit' => 0,
  3195. );
  3196. $name = NULL;
  3197. if (isset($form_state['update_name'])) {
  3198. $name = $form_state['update_name'];
  3199. }
  3200. views_ui_standard_form_buttons($form, $form_state, 'views_ui_rearrange_form');
  3201. return $form;
  3202. }
  3203. /**
  3204. * Turn the rearrange form into a proper table.
  3205. */
  3206. function theme_views_ui_rearrange_form($variables) {
  3207. $form = $variables['form'];
  3208. $rows = array();
  3209. foreach (element_children($form['fields']) as $id) {
  3210. if (isset($form['fields'][$id]['name'])) {
  3211. $row = array();
  3212. $row[] = drupal_render($form['fields'][$id]['name']);
  3213. $form['fields'][$id]['weight']['#attributes']['class'] = array('weight');
  3214. $row[] = drupal_render($form['fields'][$id]['weight']);
  3215. $row[] = drupal_render($form['fields'][$id]['removed']) . l('<span>' . t('Remove') . '</span>', 'javascript:void()', array('attributes' => array('id' => 'views-remove-link-' . $id, 'class' => array('views-hidden', 'views-button-remove', 'views-remove-link'), 'alt' => t('Remove this item'), 'title' => t('Remove this item')), 'html' => TRUE));
  3216. $rows[] = array('data' => $row, 'class' => array('draggable'), 'id' => 'views-row-' . $id);
  3217. }
  3218. }
  3219. if (empty($rows)) {
  3220. $rows[] = array(array('data' => t('No fields available.'), 'colspan' => '2'));
  3221. }
  3222. $header = array('', t('Weight'), t('Remove'));
  3223. $output = drupal_render($form['override']);
  3224. $output .= '<div class="scroll">';
  3225. $output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'arrange')));
  3226. $output .= '</div>';
  3227. $output .= drupal_render_children($form);
  3228. drupal_add_tabledrag('arrange', 'order', 'sibling', 'weight');
  3229. return $output;
  3230. }
  3231. /**
  3232. * Theme the expose filter form.
  3233. */
  3234. function theme_views_ui_expose_filter_form($variables) {
  3235. $form = $variables['form'];
  3236. $more = drupal_render($form['more']);
  3237. $output = drupal_render($form['form_description']);
  3238. $output .= drupal_render($form['expose_button']);
  3239. $output .= drupal_render($form['group_button']);
  3240. if (isset($form['required'])) {
  3241. $output .= drupal_render($form['required']);
  3242. }
  3243. $output .= drupal_render($form['label']);
  3244. $output .= drupal_render($form['description']);
  3245. $output .= drupal_render($form['operator']);
  3246. $output .= drupal_render($form['value']);
  3247. if (isset($form['use_operator'])) {
  3248. $output .= '<div class="views-left-40">';
  3249. $output .= drupal_render($form['use_operator']);
  3250. $output .= '</div>';
  3251. }
  3252. // Only output the right column markup if there's a left column to begin with.
  3253. if (!empty($form['operator']['#type'])) {
  3254. $output .= '<div class="views-right-60">';
  3255. $output .= drupal_render_children($form);
  3256. $output .= '</div>';
  3257. }
  3258. else {
  3259. $output .= drupal_render_children($form);
  3260. }
  3261. $output .= $more;
  3262. return $output;
  3263. }
  3264. /**
  3265. * Theme the build group filter form.
  3266. */
  3267. function theme_views_ui_build_group_filter_form($variables) {
  3268. $form = $variables['form'];
  3269. $more = drupal_render($form['more']);
  3270. $output = drupal_render($form['form_description']);
  3271. $output .= drupal_render($form['expose_button']);
  3272. $output .= drupal_render($form['group_button']);
  3273. if (isset($form['required'])) {
  3274. $output .= drupal_render($form['required']);
  3275. }
  3276. $output .= drupal_render($form['operator']);
  3277. $output .= drupal_render($form['value']);
  3278. $output .= '<div class="views-left-40">';
  3279. $output .= drupal_render($form['optional']);
  3280. $output .= drupal_render($form['remember']);
  3281. $output .= '</div>';
  3282. $output .= '<div class="views-right-60">';
  3283. $output .= drupal_render($form['widget']);
  3284. $output .= drupal_render($form['label']);
  3285. $output .= drupal_render($form['description']);
  3286. $output .= '</div>';
  3287. $header = array(
  3288. t('Default'),
  3289. t('Weight'),
  3290. t('Label'),
  3291. t('Operator'),
  3292. t('Value'),
  3293. t('Operations'),
  3294. );
  3295. $form['default_group'] = form_process_radios($form['default_group']);
  3296. $form['default_group_multiple'] = form_process_checkboxes($form['default_group_multiple']);
  3297. $form['default_group']['All']['#title'] = '';
  3298. drupal_render($form['default_group_multiple']['All']);
  3299. // Don't render.
  3300. $rows[] = array(
  3301. drupal_render($form['default_group']['All']),
  3302. '',
  3303. array(
  3304. 'data' => variable_get('views_exposed_filter_any_label', 'new_any') == 'old_any' ? t('&lt;Any&gt;') : t('- Any -'),
  3305. 'colspan' => 4,
  3306. 'class' => array('class' => 'any-default-radios-row'),
  3307. ),
  3308. );
  3309. foreach (element_children($form['group_items']) as $group_id) {
  3310. $form['group_items'][$group_id]['value']['#title'] = '';
  3311. $data = array(
  3312. 'default' => drupal_render($form['default_group'][$group_id]) . drupal_render($form['default_group_multiple'][$group_id]),
  3313. 'weight' => drupal_render($form['group_items'][$group_id]['weight']),
  3314. 'title' => drupal_render($form['group_items'][$group_id]['title']),
  3315. 'operator' => drupal_render($form['group_items'][$group_id]['operator']),
  3316. 'value' => drupal_render($form['group_items'][$group_id]['value']),
  3317. 'remove' => drupal_render($form['group_items'][$group_id]['remove']) . l('<span>' . t('Remove') . '</span>', 'javascript:void()', array('attributes' => array('id' => 'views-remove-link-' . $group_id, 'class' => array('views-hidden', 'views-button-remove', 'views-groups-remove-link', 'views-remove-link'), 'alt' => t('Remove this item'), 'title' => t('Remove this item')), 'html' => TRUE)),
  3318. );
  3319. $rows[] = array('data' => $data, 'id' => 'views-row-' . $group_id, 'class' => array('draggable'));
  3320. }
  3321. $table = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('class' => array('views-filter-groups'), 'id' => 'views-filter-groups'))) . drupal_render($form['add_group']);
  3322. drupal_add_tabledrag('views-filter-groups', 'order', 'sibling', 'weight');
  3323. $render_form = drupal_render_children($form);
  3324. return $output . $render_form . $table . $more;
  3325. }
  3326. /**
  3327. * Submit handler for rearranging form.
  3328. */
  3329. function views_ui_rearrange_form_submit($form, &$form_state) {
  3330. $types = views_object_types();
  3331. $display = &$form_state['view']->display[$form_state['display_id']];
  3332. $old_fields = $display->handler->get_option($types[$form_state['type']]['plural']);
  3333. $new_fields = $order = array();
  3334. // Make an array with the weights.
  3335. foreach ($form_state['values'] as $field => $info) {
  3336. // add each value that is a field with a weight to our list, but only if
  3337. // it has had its 'removed' checkbox checked.
  3338. if (is_array($info) && isset($info['weight']) && empty($info['removed'])) {
  3339. $order[$field] = $info['weight'];
  3340. }
  3341. }
  3342. // Sort the array.
  3343. asort($order);
  3344. // Create a new list of fields in the new order.
  3345. foreach (array_keys($order) as $field) {
  3346. $new_fields[$field] = $old_fields[$field];
  3347. }
  3348. $display->handler->set_option($types[$form_state['type']]['plural'], $new_fields);
  3349. // Store in cache.
  3350. views_ui_cache_set($form_state['view']);
  3351. }
  3352. /**
  3353. * Form to rearrange items in the views UI.
  3354. */
  3355. function views_ui_rearrange_filter_form($form, &$form_state) {
  3356. $view = &$form_state['view'];
  3357. $display_id = $form_state['display_id'];
  3358. $type = $form_state['type'];
  3359. $types = views_object_types();
  3360. if (!$view->set_display($display_id)) {
  3361. views_ajax_render(t('Invalid display id @display', array('@display' => $display_id)));
  3362. }
  3363. $display = &$view->display[$display_id];
  3364. $form['#title'] = check_plain($display->display_title) . ': ';
  3365. $form['#title'] .= t('Rearrange @type', array('@type' => $types[$type]['ltitle']));
  3366. $form['#section'] = $display_id . 'rearrange-item';
  3367. if ($display->handler->defaultable_sections($types[$type]['plural'])) {
  3368. $form_state['section'] = $types[$type]['plural'];
  3369. views_ui_standard_display_dropdown($form, $form_state, $form_state['section']);
  3370. }
  3371. if (!empty($view->form_cache)) {
  3372. $groups = $view->form_cache['groups'];
  3373. $handlers = $view->form_cache['handlers'];
  3374. }
  3375. else {
  3376. $groups = $display->handler->get_option('filter_groups');
  3377. $handlers = $display->handler->get_option($types[$type]['plural']);
  3378. }
  3379. $count = 0;
  3380. // Get relationship labels.
  3381. $relationships = array();
  3382. foreach ($display->handler->get_handlers('relationship') as $id => $handler) {
  3383. $relationships[$id] = $handler->label();
  3384. }
  3385. $group_options = array();
  3386. /**
  3387. * Filter groups is an array that contains:
  3388. * array(
  3389. * 'operator' => 'and' || 'or',
  3390. * 'groups' => array(
  3391. * $group_id => 'and' || 'or',
  3392. * ),
  3393. * );
  3394. */
  3395. $grouping = count(array_keys($groups['groups'])) > 1;
  3396. $form['filter_groups']['#tree'] = TRUE;
  3397. $form['filter_groups']['operator'] = array(
  3398. '#type' => 'select',
  3399. '#options' => array(
  3400. 'AND' => t('And'),
  3401. 'OR' => t('Or'),
  3402. ),
  3403. '#default_value' => $groups['operator'],
  3404. '#attributes' => array(
  3405. 'class' => array('warning-on-change'),
  3406. ),
  3407. '#title' => t('Operator to use on all groups'),
  3408. '#description' => t('Either "group 0 AND group 1 AND group 2" or "group 0 OR group 1 OR group 2", etc'),
  3409. '#access' => $grouping,
  3410. );
  3411. $form['remove_groups']['#tree'] = TRUE;
  3412. foreach ($groups['groups'] as $id => $group) {
  3413. $form['filter_groups']['groups'][$id] = array(
  3414. '#title' => t('Operator'),
  3415. '#type' => 'select',
  3416. '#options' => array(
  3417. 'AND' => t('And'),
  3418. 'OR' => t('Or'),
  3419. ),
  3420. '#default_value' => $group,
  3421. '#attributes' => array(
  3422. 'class' => array('warning-on-change'),
  3423. ),
  3424. );
  3425. $form['remove_groups'][$id] = array();
  3426. // to prevent a notice.
  3427. if ($id != 1) {
  3428. $form['remove_groups'][$id] = array(
  3429. '#type' => 'submit',
  3430. '#value' => t('Remove group @group', array('@group' => $id)),
  3431. '#id' => "views-remove-group-$id",
  3432. '#attributes' => array(
  3433. 'class' => array('views-remove-group'),
  3434. ),
  3435. '#group' => $id,
  3436. );
  3437. }
  3438. $group_options[$id] = $id == 1 ? t('Default group') : t('Group @group', array('@group' => $id));
  3439. $form['#group_renders'][$id] = array();
  3440. }
  3441. $form['#group_options'] = $group_options;
  3442. $form['#groups'] = $groups;
  3443. // We don't use get_handlers() because we want items without handlers to
  3444. // appear and show up as 'broken' so that the user can see them.
  3445. $form['filters'] = array('#tree' => TRUE);
  3446. foreach ($handlers as $id => $field) {
  3447. // If the group does not exist, move the filters to the default group.
  3448. if (empty($field['group']) || empty($groups['groups'][$field['group']])) {
  3449. $field['group'] = 1;
  3450. }
  3451. $handler = $display->handler->get_handler($type, $id);
  3452. if ($grouping && $handler && !$handler->can_group()) {
  3453. $field['group'] = 'ungroupable';
  3454. }
  3455. // If not grouping and the handler is set ungroupable, move it back to the
  3456. // default group to prevent weird errors from having it be in its own group.
  3457. if (!$grouping && $field['group'] == 'ungroupable') {
  3458. $field['group'] = 1;
  3459. }
  3460. // Place this item into the proper group for rendering.
  3461. $form['#group_renders'][$field['group']][] = $id;
  3462. $form['filters'][$id]['weight'] = array(
  3463. '#type' => 'textfield',
  3464. '#default_value' => ++$count,
  3465. '#size' => 8,
  3466. );
  3467. $form['filters'][$id]['group'] = array(
  3468. '#type' => 'select',
  3469. '#options' => $group_options,
  3470. '#default_value' => $field['group'],
  3471. '#attributes' => array(
  3472. 'class' => array('views-region-select', 'views-region-' . $id),
  3473. ),
  3474. '#access' => $field['group'] !== 'ungroupable',
  3475. );
  3476. if ($handler) {
  3477. $name = $handler->ui_name() . ' ' . $handler->admin_summary();
  3478. if (!empty($field['relationship']) && !empty($relationships[$field['relationship']])) {
  3479. $name = '(' . $relationships[$field['relationship']] . ') ' . $name;
  3480. }
  3481. $form['filters'][$id]['name'] = array(
  3482. '#markup' => $name,
  3483. );
  3484. }
  3485. else {
  3486. $form['filters'][$id]['name'] = array('#markup' => t('Broken field @id', array('@id' => $id)));
  3487. }
  3488. $form['filters'][$id]['removed'] = array(
  3489. '#type' => 'checkbox',
  3490. '#id' => 'views-removed-' . $id,
  3491. '#attributes' => array('class' => array('views-remove-checkbox')),
  3492. '#default_value' => 0,
  3493. );
  3494. }
  3495. if (isset($form_state['update_name'])) {
  3496. $name = $form_state['update_name'];
  3497. }
  3498. views_ui_standard_form_buttons($form, $form_state, 'views_ui_rearrange_filter_form');
  3499. $form['buttons']['add_group'] = array(
  3500. '#type' => 'submit',
  3501. '#value' => t('Create new filter group'),
  3502. '#id' => 'views-add-group',
  3503. '#group' => 'add',
  3504. );
  3505. return $form;
  3506. }
  3507. /**
  3508. * Turn the rearrange form into a proper table.
  3509. */
  3510. function theme_views_ui_rearrange_filter_form(&$vars) {
  3511. $form = $vars['form'];
  3512. $rows = $ungroupable_rows = array();
  3513. // Enable grouping only if > 1 group.
  3514. $grouping = count(array_keys($form['#group_options'])) > 1;
  3515. foreach ($form['#group_renders'] as $group_id => $contents) {
  3516. // Header row for the group.
  3517. if ($group_id !== 'ungroupable') {
  3518. // Set up tabledrag so that it changes the group dropdown when rows are
  3519. // dragged between groups.
  3520. drupal_add_tabledrag('views-rearrange-filters', 'match', 'sibling', 'views-group-select', 'views-group-select-' . $group_id);
  3521. // Title row, spanning all columns.
  3522. $row = array();
  3523. // Add a cell to the first row, containing the group operator.
  3524. $row[] = array('class' => array('group', 'group-operator', 'container-inline'), 'data' => drupal_render($form['filter_groups']['groups'][$group_id]), 'rowspan' => max(array(2, count($contents) + 1)));
  3525. // Title.
  3526. $row[] = array('class' => array('group', 'group-title'), 'data' => '<span>' . $form['#group_options'][$group_id] . '</span>', 'colspan' => 4);
  3527. $rows[] = array('class' => array('views-group-title'), 'data' => $row, 'id' => 'views-group-title-' . $group_id);
  3528. // Row which will only appear if the group has nothing in it.
  3529. $row = array();
  3530. $class = 'group-' . (count($contents) ? 'populated' : 'empty');
  3531. $instructions = '<span>' . t('No filters have been added.') . '</span> <span class="js-only">' . t('Drag to add filters.') . '</span>';
  3532. // When JavaScript is enabled, the button for removing the group (if it's
  3533. // present) should be hidden, since it will be replaced by a link on the
  3534. // client side.
  3535. if (!empty($form['remove_groups'][$group_id]['#type']) && $form['remove_groups'][$group_id]['#type'] == 'submit') {
  3536. $form['remove_groups'][$group_id]['#attributes']['class'][] = 'js-hide';
  3537. }
  3538. $row[] = array('colspan' => 5, 'data' => $instructions . drupal_render($form['remove_groups'][$group_id]));
  3539. $rows[] = array('class' => array("group-message", "group-$group_id-message", $class), 'data' => $row, 'id' => 'views-group-' . $group_id);
  3540. }
  3541. foreach ($contents as $id) {
  3542. if (isset($form['filters'][$id]['name'])) {
  3543. $row = array();
  3544. $row[] = drupal_render($form['filters'][$id]['name']);
  3545. $form['filters'][$id]['weight']['#attributes']['class'] = array('weight');
  3546. $row[] = drupal_render($form['filters'][$id]['weight']);
  3547. $form['filters'][$id]['group']['#attributes']['class'] = array('views-group-select views-group-select-' . $group_id);
  3548. $row[] = drupal_render($form['filters'][$id]['group']);
  3549. $form['filters'][$id]['removed']['#attributes']['class'][] = 'js-hide';
  3550. $row[] = drupal_render($form['filters'][$id]['removed']) . l('<span>' . t('Remove') . '</span>', 'javascript:void()', array('attributes' => array('id' => 'views-remove-link-' . $id, 'class' => array('views-hidden', 'views-button-remove', 'views-groups-remove-link', 'views-remove-link'), 'alt' => t('Remove this item'), 'title' => t('Remove this item')), 'html' => TRUE));
  3551. $row = array('data' => $row, 'class' => array('draggable'), 'id' => 'views-row-' . $id);
  3552. if ($group_id !== 'ungroupable') {
  3553. $rows[] = $row;
  3554. }
  3555. else {
  3556. $ungroupable_rows[] = $row;
  3557. }
  3558. }
  3559. }
  3560. }
  3561. if (empty($rows)) {
  3562. $rows[] = array(array('data' => t('No fields available.'), 'colspan' => '2'));
  3563. }
  3564. $output = drupal_render($form['override']);
  3565. $output .= '<div class="scroll">';
  3566. if ($grouping) {
  3567. $output .= drupal_render($form['filter_groups']['operator']);
  3568. }
  3569. else {
  3570. $form['filter_groups']['groups'][0]['#title'] = t('Operator');
  3571. $output .= drupal_render($form['filter_groups']['groups'][0]);
  3572. }
  3573. if (!empty($ungroupable_rows)) {
  3574. drupal_add_tabledrag('views-rearrange-filters-ungroupable', 'order', 'sibling', 'weight');
  3575. $header = array(t('Ungroupable filters'), t('Weight'), array('class' => array('views-hide-label'), 'data' => t('Group')), array('class' => array('views-hide-label'), 'data' => t('Remove')));
  3576. $output .= theme('table', array('header' => $header, 'rows' => $ungroupable_rows, 'attributes' => array('id' => 'views-rearrange-filters-ungroupable', 'class' => array('arrange'))));
  3577. }
  3578. // Set up tabledrag so that the weights are changed when rows are dragged.
  3579. drupal_add_tabledrag('views-rearrange-filters', 'order', 'sibling', 'weight');
  3580. $output .= theme('table', array('rows' => $rows, 'attributes' => array('id' => 'views-rearrange-filters', 'class' => array('arrange'))));
  3581. $output .= '</div>';
  3582. // When JavaScript is enabled, the button for adding a new group should be
  3583. // hidden, since it will be replaced by a link on the client side.
  3584. $form['buttons']['add_group']['#attributes']['class'][] = 'js-hide';
  3585. // Render the rest of the form and return.
  3586. $output .= drupal_render_children($form);
  3587. return $output;
  3588. }
  3589. /**
  3590. * Submit handler for rearranging form.
  3591. */
  3592. function views_ui_rearrange_filter_form_submit($form, &$form_state) {
  3593. $types = views_object_types();
  3594. $display = &$form_state['view']->display[$form_state['display_id']];
  3595. $remember_groups = array();
  3596. if (!empty($form_state['view']->form_cache)) {
  3597. $old_fields = $form_state['view']->form_cache['handlers'];
  3598. }
  3599. else {
  3600. $old_fields = $display->handler->get_option($types[$form_state['type']]['plural']);
  3601. }
  3602. $count = 0;
  3603. $groups = $form_state['values']['filter_groups'];
  3604. // Whatever button was clicked, re-calculate field information.
  3605. $new_fields = $order = array();
  3606. // Make an array with the weights.
  3607. foreach ($form_state['values']['filters'] as $field => $info) {
  3608. // add each value that is a field with a weight to our list, but only if
  3609. // it has had its 'removed' checkbox checked.
  3610. if (is_array($info) && empty($info['removed'])) {
  3611. if (isset($info['weight'])) {
  3612. $order[$field] = $info['weight'];
  3613. }
  3614. if (isset($info['group'])) {
  3615. $old_fields[$field]['group'] = $info['group'];
  3616. $remember_groups[$info['group']][] = $field;
  3617. }
  3618. }
  3619. }
  3620. // Sort the array.
  3621. asort($order);
  3622. // Create a new list of fields in the new order.
  3623. foreach (array_keys($order) as $field) {
  3624. $new_fields[$field] = $old_fields[$field];
  3625. }
  3626. // If the #group property is set on the clicked button, that means we are
  3627. // either adding or removing a group, not actually updating the filters.
  3628. if (!empty($form_state['clicked_button']['#group'])) {
  3629. if ($form_state['clicked_button']['#group'] == 'add') {
  3630. // Add a new group.
  3631. $groups['groups'][] = 'AND';
  3632. }
  3633. else {
  3634. // Renumber groups above the removed one down.
  3635. foreach (array_keys($groups['groups']) as $group_id) {
  3636. if ($group_id >= $form_state['clicked_button']['#group']) {
  3637. $old_group = $group_id + 1;
  3638. if (isset($groups['groups'][$old_group])) {
  3639. $groups['groups'][$group_id] = $groups['groups'][$old_group];
  3640. if (isset($remember_groups[$old_group])) {
  3641. foreach ($remember_groups[$old_group] as $id) {
  3642. $new_fields[$id]['group'] = $group_id;
  3643. }
  3644. }
  3645. }
  3646. else {
  3647. // If this is the last one, just unset it.
  3648. unset($groups['groups'][$group_id]);
  3649. }
  3650. }
  3651. }
  3652. }
  3653. // Update our cache with values so that cancel still works the way
  3654. // people expect.
  3655. $form_state['view']->form_cache = array(
  3656. 'key' => 'rearrange-filter',
  3657. 'groups' => $groups,
  3658. 'handlers' => $new_fields,
  3659. );
  3660. // Return to this form except on actual Update.
  3661. views_ui_add_form_to_stack('rearrange-filter', $form_state['view'], $form_state['display_id'], array($form_state['type']));
  3662. }
  3663. else {
  3664. // The actual update button was clicked. Remove the empty groups, and
  3665. // renumber them sequentially.
  3666. ksort($remember_groups);
  3667. $groups['groups'] = views_array_key_plus(array_values(array_intersect_key($groups['groups'], $remember_groups)));
  3668. // Change the 'group' key on each field to match. Here, $mapping is an
  3669. // array whose keys are the old group numbers and whose values are the new
  3670. // (sequentially numbered) ones.
  3671. $mapping = array_flip(views_array_key_plus(array_keys($remember_groups)));
  3672. foreach ($new_fields as &$new_field) {
  3673. $new_field['group'] = $mapping[$new_field['group']];
  3674. }
  3675. // Write the changed handler values.
  3676. $display->handler->set_option($types[$form_state['type']]['plural'], $new_fields);
  3677. $display->handler->set_option('filter_groups', $groups);
  3678. if (isset($form_state['view']->form_cache)) {
  3679. unset($form_state['view']->form_cache);
  3680. }
  3681. }
  3682. // Store in cache.
  3683. views_ui_cache_set($form_state['view']);
  3684. }
  3685. /**
  3686. * Form to add_item items in the views UI.
  3687. */
  3688. function views_ui_add_item_form($form, &$form_state) {
  3689. $view = &$form_state['view'];
  3690. $display_id = $form_state['display_id'];
  3691. $type = $form_state['type'];
  3692. $form = array(
  3693. 'options' => array(
  3694. '#theme_wrappers' => array('container'),
  3695. '#attributes' => array('class' => array('scroll')),
  3696. ),
  3697. );
  3698. ctools_add_js('dependent');
  3699. if (!$view->set_display($display_id)) {
  3700. views_ajax_error(t('Invalid display id @display', array('@display' => $display_id)));
  3701. }
  3702. $display = &$view->display[$display_id];
  3703. $types = views_object_types();
  3704. $ltitle = $types[$type]['ltitle'];
  3705. $section = $types[$type]['plural'];
  3706. if (!empty($types[$type]['type'])) {
  3707. $type = $types[$type]['type'];
  3708. }
  3709. $form['#title'] = t('Add @type', array('@type' => $ltitle));
  3710. $form['#section'] = $display_id . 'add-item';
  3711. // Add the display override dropdown.
  3712. views_ui_standard_display_dropdown($form, $form_state, $section);
  3713. // Figure out all the base tables allowed based upon what the relationships
  3714. // provide.
  3715. $base_tables = $view->get_base_tables();
  3716. $options = views_fetch_fields(array_keys($base_tables), $type, $display->handler->use_group_by());
  3717. if (!empty($options)) {
  3718. $form['options']['controls'] = array(
  3719. '#theme_wrappers' => array('container'),
  3720. '#id' => 'views-filterable-options-controls',
  3721. '#attributes' => array('class' => array('container-inline')),
  3722. );
  3723. $form['options']['controls']['options_search'] = array(
  3724. '#type' => 'textfield',
  3725. '#title' => t('Search'),
  3726. );
  3727. $groups = array('all' => t('- All -'));
  3728. $form['options']['controls']['group'] = array(
  3729. '#type' => 'select',
  3730. '#title' => t('Filter'),
  3731. '#options' => array(),
  3732. '#attributes' => array('class' => array('ctools-master-dependent')),
  3733. );
  3734. $form['options']['name'] = array(
  3735. '#prefix' => '<div class="views-radio-box form-checkboxes views-filterable-options">',
  3736. '#suffix' => '</div>',
  3737. '#tree' => TRUE,
  3738. '#default_value' => 'all',
  3739. );
  3740. // Group options first to simplify the DOM objects that Views
  3741. // dependent JS will act upon.
  3742. $grouped_options = array();
  3743. foreach ($options as $key => $option) {
  3744. $group = preg_replace('/[^a-z0-9]/', '-', strtolower($option['group']));
  3745. $groups[$group] = $option['group'];
  3746. $grouped_options[$group][$key] = $option;
  3747. if (!empty($option['aliases']) && is_array($option['aliases'])) {
  3748. foreach ($option['aliases'] as $id => $alias) {
  3749. if (empty($alias['base']) || !empty($base_tables[$alias['base']])) {
  3750. $copy = $option;
  3751. $copy['group'] = $alias['group'];
  3752. $copy['title'] = $alias['title'];
  3753. if (isset($alias['help'])) {
  3754. $copy['help'] = $alias['help'];
  3755. }
  3756. $group = preg_replace('/[^a-z0-9]/', '-', strtolower($copy['group']));
  3757. $groups[$group] = $copy['group'];
  3758. $grouped_options[$group][$key . '$' . $id] = $copy;
  3759. }
  3760. }
  3761. }
  3762. }
  3763. foreach ($grouped_options as $group => $group_options) {
  3764. $form['options']['name'][$group . '_start']['#markup'] = '<div class="ctools-dependent-all ctools-dependent-' . $group . '">';
  3765. $zebra = 0;
  3766. foreach ($group_options as $key => $option) {
  3767. $zebra_class = ($zebra % 2) ? 'odd' : 'even';
  3768. $form['options']['name'][$key] = array(
  3769. '#type' => 'checkbox',
  3770. '#title' => t('!group: !field', array('!group' => check_plain($option['group']), '!field' => check_plain($option['title']))),
  3771. '#description' => filter_xss_admin($option['help']),
  3772. '#return_value' => $key,
  3773. '#prefix' => "<div class='$zebra_class filterable-option'>",
  3774. '#suffix' => '</div>',
  3775. );
  3776. $zebra++;
  3777. }
  3778. $form['options']['name'][$group . '_end']['#markup'] = '</div>';
  3779. }
  3780. $form['options']['controls']['group']['#options'] = $groups;
  3781. }
  3782. else {
  3783. $form['options']['markup'] = array(
  3784. '#markup' => '<div class="form-item">' . t('There are no @types available to add.', array('@types' => $ltitle)) . '</div>',
  3785. );
  3786. }
  3787. // Add a div to show the selected items.
  3788. $form['selected'] = array(
  3789. '#type' => 'item',
  3790. '#markup' => '<div class="views-selected-options"></div>',
  3791. '#title' => t('Selected') . ':',
  3792. '#theme_wrappers' => array('form_element', 'views_container'),
  3793. '#attributes' => array('class' => array('container-inline', 'views-add-form-selected')),
  3794. );
  3795. ctools_include('dependent');
  3796. views_ui_standard_form_buttons($form, $form_state, 'views_ui_add_item_form', t('Add and configure @types', array('@types' => $ltitle)));
  3797. // Remove the default submit function.
  3798. $form['buttons']['submit']['#submit'] = array_diff($form['buttons']['submit']['#submit'], array('views_ui_standard_submit'));
  3799. $form['buttons']['submit']['#submit'][] = 'views_ui_add_item_form_submit';
  3800. return $form;
  3801. }
  3802. /**
  3803. * Submit handler for adding new item(s) to a view.
  3804. */
  3805. function views_ui_add_item_form_submit($form, &$form_state) {
  3806. $type = $form_state['type'];
  3807. $types = views_object_types();
  3808. $section = $types[$type]['plural'];
  3809. // Handle the override select.
  3810. list($was_defaulted, $is_defaulted) = views_ui_standard_override_values($form, $form_state);
  3811. if ($was_defaulted && !$is_defaulted) {
  3812. // We were using the default display's values, but we're now overriding
  3813. // the default display and saving values specific to this display.
  3814. $display = &$form_state['view']->display[$form_state['display_id']];
  3815. // set_override toggles the override of this section.
  3816. $display->handler->set_override($section);
  3817. }
  3818. elseif (!$was_defaulted && $is_defaulted) {
  3819. // We used to have an override for this display, but the user now wants
  3820. // to go back to the default display.
  3821. // Overwrite the default display with the current form values, and make
  3822. // the current display use the new default values.
  3823. $display = &$form_state['view']->display[$form_state['display_id']];
  3824. // options_override toggles the override of this section.
  3825. $display->handler->set_override($section);
  3826. }
  3827. if (!empty($form_state['values']['name']) && is_array($form_state['values']['name'])) {
  3828. // Loop through each of the items that were checked and add them to the
  3829. // view.
  3830. foreach (array_keys(array_filter($form_state['values']['name'])) as $field) {
  3831. list($table, $field) = explode('.', $field, 2);
  3832. if ($cut = strpos($field, '$')) {
  3833. $field = substr($field, 0, $cut);
  3834. }
  3835. $id = $form_state['view']->add_item($form_state['display_id'], $type, $table, $field);
  3836. // Check to see if we have group by settings.
  3837. $key = $type;
  3838. // Footer,header and empty text have a different internal handler type
  3839. // (area).
  3840. if (isset($types[$type]['type'])) {
  3841. $key = $types[$type]['type'];
  3842. }
  3843. $handler = views_get_handler($table, $field, $key);
  3844. if ($form_state['view']->display_handler->use_group_by() && $handler->use_group_by()) {
  3845. views_ui_add_form_to_stack('config-item-group', $form_state['view'], $form_state['display_id'], array($type, $id));
  3846. }
  3847. // Check to see if this type has settings, if so add the settings form
  3848. // first.
  3849. if ($handler && $handler->has_extra_options()) {
  3850. views_ui_add_form_to_stack('config-item-extra', $form_state['view'], $form_state['display_id'], array($type, $id));
  3851. }
  3852. // Then add the form to the stack.
  3853. views_ui_add_form_to_stack('config-item', $form_state['view'], $form_state['display_id'], array($type, $id));
  3854. }
  3855. }
  3856. if (isset($form_state['view']->form_cache)) {
  3857. unset($form_state['view']->form_cache);
  3858. }
  3859. // Store in cache.
  3860. views_ui_cache_set($form_state['view']);
  3861. }
  3862. /**
  3863. * Override handler for views_ui_edit_display_form.
  3864. */
  3865. function views_ui_config_item_form_build_group($form, &$form_state) {
  3866. $item = &$form_state['handler']->options;
  3867. // flip. If the filter was a group, set back to a standard filter.
  3868. $item['is_grouped'] = empty($item['is_grouped']);
  3869. // If necessary, set new defaults.
  3870. if ($item['is_grouped']) {
  3871. $form_state['handler']->build_group_options();
  3872. }
  3873. $form_state['view']->set_item($form_state['display_id'], $form_state['type'], $form_state['id'], $item);
  3874. views_ui_add_form_to_stack($form_state['form_key'], $form_state['view'], $form_state['display_id'], array($form_state['type'], $form_state['id']), TRUE, TRUE);
  3875. views_ui_cache_set($form_state['view']);
  3876. $form_state['rerender'] = TRUE;
  3877. $form_state['rebuild'] = TRUE;
  3878. $form_state['force_build_group_options'] = TRUE;
  3879. }
  3880. /**
  3881. * Add a new group to the exposed filter groups.
  3882. */
  3883. function views_ui_config_item_form_add_group($form, &$form_state) {
  3884. $item =& $form_state['handler']->options;
  3885. // Add a new row.
  3886. $item['group_info']['group_items'][] = array();
  3887. $form_state['view']->set_item($form_state['display_id'], $form_state['type'], $form_state['id'], $item);
  3888. views_ui_cache_set($form_state['view']);
  3889. $form_state['rerender'] = TRUE;
  3890. $form_state['rebuild'] = TRUE;
  3891. $form_state['force_build_group_options'] = TRUE;
  3892. }
  3893. /**
  3894. * Form to config_item items in the views UI.
  3895. */
  3896. function views_ui_config_item_form($form, &$form_state) {
  3897. $view = &$form_state['view'];
  3898. $display_id = $form_state['display_id'];
  3899. $type = $form_state['type'];
  3900. $id = $form_state['id'];
  3901. $form = array(
  3902. 'options' => array(
  3903. '#tree' => TRUE,
  3904. '#theme_wrappers' => array('container'),
  3905. '#attributes' => array('class' => array('scroll')),
  3906. ),
  3907. );
  3908. if (!$view->set_display($display_id)) {
  3909. views_ajax_error(t('Invalid display id @display', array('@display' => $display_id)));
  3910. }
  3911. $item = $view->get_item($display_id, $type, $id);
  3912. if ($item) {
  3913. $handler = $view->display_handler->get_handler($type, $id);
  3914. if (empty($handler)) {
  3915. $form['markup'] = array('#markup' => t("Error: handler for @table > @field doesn't exist!", array('@table' => $item['table'], '@field' => $item['field'])));
  3916. }
  3917. else {
  3918. $types = views_object_types();
  3919. // If this item can come from the default display, show a dropdown
  3920. // that lets the user choose which display the changes should apply to.
  3921. if ($view->display_handler->defaultable_sections($types[$type]['plural'])) {
  3922. $form_state['section'] = $types[$type]['plural'];
  3923. views_ui_standard_display_dropdown($form, $form_state, $form_state['section']);
  3924. }
  3925. // A whole bunch of code to figure out what relationships are valid for
  3926. // this item.
  3927. $relationships = $view->display_handler->get_option('relationships');
  3928. $relationship_options = array();
  3929. foreach ($relationships as $relationship) {
  3930. // Relationships can't link back to self. But also, due to ordering,
  3931. // relationships can only link to prior relationships.
  3932. if ($type == 'relationship' && $id == $relationship['id']) {
  3933. break;
  3934. }
  3935. $relationship_handler = views_get_handler($relationship['table'], $relationship['field'], 'relationship');
  3936. // Ignore invalid/broken relationships.
  3937. if (empty($relationship_handler)) {
  3938. continue;
  3939. }
  3940. // If this relationship is valid for this type, add it to the list.
  3941. $data = views_fetch_data($relationship['table']);
  3942. $base = $data[$relationship['field']]['relationship']['base'];
  3943. $base_fields = views_fetch_fields($base, $form_state['type'], $view->display_handler->use_group_by());
  3944. if (isset($base_fields[$item['table'] . '.' . $item['field']])) {
  3945. $relationship_handler->init($view, $relationship);
  3946. $relationship_options[$relationship['id']] = $relationship_handler->label();
  3947. }
  3948. }
  3949. if (!empty($relationship_options)) {
  3950. // Make sure the existing relationship is even valid. If not, force
  3951. // it to none.
  3952. $base_fields = views_fetch_fields($view->base_table, $form_state['type'], $view->display_handler->use_group_by());
  3953. if (isset($base_fields[$item['table'] . '.' . $item['field']])) {
  3954. $relationship_options = array_merge(array('none' => t('Do not use a relationship')), $relationship_options);
  3955. }
  3956. $rel = empty($item['relationship']) ? 'none' : $item['relationship'];
  3957. if (empty($relationship_options[$rel])) {
  3958. // Pick the first relationship.
  3959. $rel = key($relationship_options);
  3960. // We want this relationship option to get saved even if the user
  3961. // skips submitting the form.
  3962. $view->set_item_option($display_id, $type, $id, 'relationship', $rel);
  3963. $temp_view = $view->clone_view();
  3964. views_ui_cache_set($temp_view);
  3965. }
  3966. $form['options']['relationship'] = array(
  3967. '#type' => 'select',
  3968. '#title' => t('Relationship'),
  3969. '#options' => $relationship_options,
  3970. '#default_value' => $rel,
  3971. '#weight' => -500,
  3972. );
  3973. }
  3974. else {
  3975. $form['options']['relationship'] = array(
  3976. '#type' => 'value',
  3977. '#value' => 'none',
  3978. );
  3979. }
  3980. $form['#title'] = t('Configure @type: @item', array('@type' => $types[$type]['lstitle'], '@item' => $handler->ui_name()));
  3981. if (!empty($handler->definition['help'])) {
  3982. $form['options']['form_description'] = array(
  3983. '#markup' => $handler->definition['help'],
  3984. '#theme_wrappers' => array('container'),
  3985. '#attributes' => array('class' => array('form-item description')),
  3986. '#weight' => -1000,
  3987. );
  3988. }
  3989. $form['#section'] = $display_id . '-' . $type . '-' . $id;
  3990. // Get form from the handler.
  3991. $handler->options_form($form['options'], $form_state);
  3992. $form_state['handler'] = &$handler;
  3993. }
  3994. $name = NULL;
  3995. if (isset($form_state['update_name'])) {
  3996. $name = $form_state['update_name'];
  3997. }
  3998. views_ui_standard_form_buttons($form, $form_state, 'views_ui_config_item_form', $name, t('Remove'), 'remove');
  3999. // Only validate the override values, because this values are required for
  4000. // the override selection.
  4001. $form['buttons']['remove']['#limit_validation_errors'] = array(array('override'));
  4002. }
  4003. return $form;
  4004. }
  4005. /**
  4006. * Submit handler for configing new item(s) to a view.
  4007. */
  4008. function views_ui_config_item_form_validate($form, &$form_state) {
  4009. $form_state['handler']->options_validate($form['options'], $form_state);
  4010. if (form_get_errors()) {
  4011. $form_state['rerender'] = TRUE;
  4012. }
  4013. }
  4014. /**
  4015. * A submit handler that is used for storing temporary items when using
  4016. * multi-step changes, such as ajax requests.
  4017. */
  4018. function views_ui_config_item_form_submit_temporary($form, &$form_state) {
  4019. // Run it through the handler's submit function.
  4020. $form_state['handler']->options_submit($form['options'], $form_state);
  4021. $item = $form_state['handler']->options;
  4022. $types = views_object_types();
  4023. // For footer/header $handler_type is area but $type is footer/header.
  4024. // For all other handle types it's the same.
  4025. $handler_type = $type = $form_state['type'];
  4026. if (!empty($types[$type]['type'])) {
  4027. $handler_type = $types[$type]['type'];
  4028. }
  4029. $override = NULL;
  4030. if ($form_state['view']->display_handler->use_group_by() && !empty($item['group_type'])) {
  4031. if (empty($form_state['view']->query)) {
  4032. $form_state['view']->init_query();
  4033. }
  4034. $aggregate = $form_state['view']->query->get_aggregation_info();
  4035. if (!empty($aggregate[$item['group_type']]['handler'][$type])) {
  4036. $override = $aggregate[$item['group_type']]['handler'][$type];
  4037. }
  4038. }
  4039. // Create a new handler and unpack the options from the form onto it. We
  4040. // can use that for storage.
  4041. $handler = views_get_handler($item['table'], $item['field'], $handler_type, $override);
  4042. $handler->init($form_state['view'], $item);
  4043. // Add the incoming options to existing options because items using
  4044. // the extra form may not have everything in the form here.
  4045. $options = $form_state['values']['options'] + $form_state['handler']->options;
  4046. // This unpacks only options that are in the definition, ensuring random
  4047. // extra stuff on the form is not sent through.
  4048. $handler->unpack_options($handler->options, $options, NULL, FALSE);
  4049. // Store the item back on the view.
  4050. $form_state['view']->temporary_options[$type][$form_state['id']] = $handler->options;
  4051. // @todo: Figure out whether views_ui_ajax_form is perhaps the better place
  4052. // to fix the issue. views_ui_ajax_form() drops the current form from the
  4053. // stack, even if it's an #ajax. So add the item back to the top of the stack.
  4054. views_ui_add_form_to_stack($form_state['form_key'], $form_state['view'], $form_state['display_id'], array($type, $item['id']), TRUE);
  4055. $form_state['rerender'] = TRUE;
  4056. $form_state['rebuild'] = TRUE;
  4057. // Write to cache.
  4058. views_ui_cache_set($form_state['view']);
  4059. }
  4060. /**
  4061. * Submit handler for configing new item(s) to a view.
  4062. */
  4063. function views_ui_config_item_form_submit($form, &$form_state) {
  4064. // Run it through the handler's submit function.
  4065. $form_state['handler']->options_submit($form['options'], $form_state);
  4066. $item = $form_state['handler']->options;
  4067. $types = views_object_types();
  4068. // For footer/header $handler_type is area but $type is footer/header.
  4069. // For all other handle types it's the same.
  4070. $handler_type = $type = $form_state['type'];
  4071. if (!empty($types[$type]['type'])) {
  4072. $handler_type = $types[$type]['type'];
  4073. }
  4074. $override = NULL;
  4075. if ($form_state['view']->display_handler->use_group_by() && !empty($item['group_type'])) {
  4076. if (empty($form_state['view']->query)) {
  4077. $form_state['view']->init_query();
  4078. }
  4079. $aggregate = $form_state['view']->query->get_aggregation_info();
  4080. if (!empty($aggregate[$item['group_type']]['handler'][$type])) {
  4081. $override = $aggregate[$item['group_type']]['handler'][$type];
  4082. }
  4083. }
  4084. // Create a new handler and unpack the options from the form onto it. We
  4085. // can use that for storage.
  4086. $handler = views_get_handler($item['table'], $item['field'], $handler_type, $override);
  4087. $handler->init($form_state['view'], $item);
  4088. // Add the incoming options to existing options because items using
  4089. // the extra form may not have everything in the form here.
  4090. $options = $form_state['values']['options'] + $form_state['handler']->options;
  4091. // This unpacks only options that are in the definition, ensuring random
  4092. // extra stuff on the form is not sent through.
  4093. $handler->unpack_options($handler->options, $options, NULL, FALSE);
  4094. // Store the item back on the view.
  4095. $form_state['view']->set_item($form_state['display_id'], $form_state['type'], $form_state['id'], $handler->options);
  4096. // Ensure any temporary options are removed.
  4097. if (isset($form_state['view']->temporary_options[$type][$form_state['id']])) {
  4098. unset($form_state['view']->temporary_options[$type][$form_state['id']]);
  4099. }
  4100. // Write to cache.
  4101. views_ui_cache_set($form_state['view']);
  4102. }
  4103. /**
  4104. * Form to config_item items in the views UI.
  4105. */
  4106. function views_ui_config_item_group_form($type, &$form_state) {
  4107. $view = &$form_state['view'];
  4108. $display_id = $form_state['display_id'];
  4109. $type = $form_state['type'];
  4110. $id = $form_state['id'];
  4111. $form = array(
  4112. 'options' => array(
  4113. '#tree' => TRUE,
  4114. '#theme_wrappers' => array('container'),
  4115. '#attributes' => array('class' => array('scroll')),
  4116. ),
  4117. );
  4118. if (!$view->set_display($display_id)) {
  4119. views_ajax_render(t('Invalid display id @display', array('@display' => $display_id)));
  4120. }
  4121. $view->init_query();
  4122. $item = $view->get_item($display_id, $type, $id);
  4123. if ($item) {
  4124. $handler = $view->display_handler->get_handler($type, $id);
  4125. if (empty($handler)) {
  4126. $form['markup'] = array('#markup' => t("Error: handler for @table > @field doesn't exist!", array('@table' => $item['table'], '@field' => $item['field'])));
  4127. }
  4128. else {
  4129. $handler->init($view, $item);
  4130. $types = views_object_types();
  4131. $form['#title'] = t('Configure group settings for @type %item', array('@type' => $types[$type]['lstitle'], '%item' => $handler->ui_name()));
  4132. $handler->groupby_form($form['options'], $form_state);
  4133. $form_state['handler'] = &$handler;
  4134. }
  4135. views_ui_standard_form_buttons($form, $form_state, 'views_ui_config_item_group_form');
  4136. }
  4137. return $form;
  4138. }
  4139. /**
  4140. * Submit handler for configing group settings on a view.
  4141. */
  4142. function views_ui_config_item_group_form_submit($form, &$form_state) {
  4143. $item =& $form_state['handler']->options;
  4144. $type = $form_state['type'];
  4145. $id = $form_state['id'];
  4146. $handler = views_get_handler($item['table'], $item['field'], $type);
  4147. $handler->init($form_state['view'], $item);
  4148. $handler->groupby_form_submit($form, $form_state);
  4149. // Store the item back on the view.
  4150. $form_state['view']->set_item($form_state['display_id'], $form_state['type'], $form_state['id'], $item);
  4151. // Write to cache.
  4152. views_ui_cache_set($form_state['view']);
  4153. }
  4154. /**
  4155. * Submit handler for removing an item from a view.
  4156. */
  4157. function views_ui_config_item_form_remove($form, &$form_state) {
  4158. // Store the item back on the view.
  4159. list($was_defaulted, $is_defaulted) = views_ui_standard_override_values($form, $form_state);
  4160. // If the display selection was changed toggle the override value.
  4161. if ($was_defaulted != $is_defaulted) {
  4162. $display =& $form_state['view']->display[$form_state['display_id']];
  4163. $display->handler->options_override($form, $form_state);
  4164. }
  4165. $form_state['view']->set_item($form_state['display_id'], $form_state['type'], $form_state['id'], NULL);
  4166. // Write to cache.
  4167. views_ui_cache_set($form_state['view']);
  4168. }
  4169. /**
  4170. * Override handler for views_ui_edit_display_form.
  4171. */
  4172. function views_ui_config_item_form_expose($form, &$form_state) {
  4173. $item = &$form_state['handler']->options;
  4174. // flip.
  4175. $item['exposed'] = empty($item['exposed']);
  4176. // If necessary, set new defaults.
  4177. if ($item['exposed']) {
  4178. $form_state['handler']->expose_options();
  4179. }
  4180. $form_state['view']->set_item($form_state['display_id'], $form_state['type'], $form_state['id'], $item);
  4181. views_ui_add_form_to_stack($form_state['form_key'], $form_state['view'], $form_state['display_id'], array($form_state['type'], $form_state['id']), TRUE, TRUE);
  4182. views_ui_cache_set($form_state['view']);
  4183. $form_state['rerender'] = TRUE;
  4184. $form_state['rebuild'] = TRUE;
  4185. $form_state['force_expose_options'] = TRUE;
  4186. }
  4187. /**
  4188. * Form to config_item items in the views UI.
  4189. */
  4190. function views_ui_config_item_extra_form($form, &$form_state) {
  4191. $view = &$form_state['view'];
  4192. $display_id = $form_state['display_id'];
  4193. $type = $form_state['type'];
  4194. $id = $form_state['id'];
  4195. $form = array(
  4196. 'options' => array(
  4197. '#tree' => TRUE,
  4198. '#theme_wrappers' => array('container'),
  4199. '#attributes' => array('class' => array('scroll')),
  4200. ),
  4201. );
  4202. if (!$view->set_display($display_id)) {
  4203. views_ajax_error(t('Invalid display id @display', array('@display' => $display_id)));
  4204. }
  4205. $item = $view->get_item($display_id, $type, $id);
  4206. if ($item) {
  4207. $handler = $view->display_handler->get_handler($type, $id);
  4208. if (empty($handler)) {
  4209. $form['markup'] = array('#markup' => t("Error: handler for @table > @field doesn't exist!", array('@table' => $item['table'], '@field' => $item['field'])));
  4210. }
  4211. else {
  4212. $handler->init($view, $item);
  4213. $types = views_object_types();
  4214. $form['#title'] = t('Configure extra settings for @type %item', array('@type' => $types[$type]['lstitle'], '%item' => $handler->ui_name()));
  4215. $form['#section'] = $display_id . '-' . $type . '-' . $id;
  4216. // Get form from the handler.
  4217. $handler->extra_options_form($form['options'], $form_state);
  4218. $form_state['handler'] = &$handler;
  4219. }
  4220. views_ui_standard_form_buttons($form, $form_state, 'views_ui_config_item_extra_form');
  4221. }
  4222. return $form;
  4223. }
  4224. /**
  4225. * Validation handler for configing new item(s) to a view.
  4226. */
  4227. function views_ui_config_item_extra_form_validate($form, &$form_state) {
  4228. $form_state['handler']->extra_options_validate($form['options'], $form_state);
  4229. }
  4230. /**
  4231. * Submit handler for configing new item(s) to a view.
  4232. */
  4233. function views_ui_config_item_extra_form_submit($form, &$form_state) {
  4234. // Run it through the handler's submit function.
  4235. $form_state['handler']->extra_options_submit($form['options'], $form_state);
  4236. $item = $form_state['handler']->options;
  4237. // Store the data we're given.
  4238. foreach ($form_state['values']['options'] as $key => $value) {
  4239. $item[$key] = $value;
  4240. }
  4241. // Store the item back on the view.
  4242. $form_state['view']->set_item($form_state['display_id'], $form_state['type'], $form_state['id'], $item);
  4243. // Write to cache.
  4244. views_ui_cache_set($form_state['view']);
  4245. }
  4246. /**
  4247. * Form to config_style items in the views UI.
  4248. */
  4249. function views_ui_config_style_form($form, &$form_state) {
  4250. $view = &$form_state['view'];
  4251. $display_id = $form_state['display_id'];
  4252. $type = $form_state['type'];
  4253. $id = $form_state['id'];
  4254. $form = array(
  4255. 'options' => array(
  4256. '#tree' => TRUE,
  4257. '#theme_wrappers' => array('container'),
  4258. '#attributes' => array('class' => array('scroll')),
  4259. ),
  4260. );
  4261. if (!$view->set_display($display_id)) {
  4262. views_ajax_error(t('Invalid display id @display', array('@display' => $display_id)));
  4263. }
  4264. $item = $view->get_item($display_id, $type, $id);
  4265. if ($item) {
  4266. $handler = views_get_handler($item['table'], $item['field'], $type);
  4267. if (empty($handler)) {
  4268. $form['markup'] = array('#markup' => t("Error: handler for @table > @field doesn't exist!", array('@table' => $item['table'], '@field' => $item['field'])));
  4269. }
  4270. else {
  4271. $handler->init($view, $item);
  4272. $types = views_object_types();
  4273. $form['#title'] = t('Configure summary style for @type %item', array('@type' => $types[$type]['lstitle'], '%item' => $handler->ui_name()));
  4274. $form['#section'] = $display_id . '-' . $type . '-style-options';
  4275. $plugin = views_get_plugin('style', $handler->options['style_plugin']);
  4276. if ($plugin) {
  4277. $form['style_options'] = array(
  4278. '#tree' => TRUE,
  4279. );
  4280. $plugin->init($view, $view->display[$display_id], $handler->options['style_options']);
  4281. $plugin->options_form($form['style_options'], $form_state);
  4282. }
  4283. $form_state['handler'] = &$handler;
  4284. }
  4285. views_ui_standard_form_buttons($form, $form_state, 'views_ui_config_style_form');
  4286. }
  4287. return $form;
  4288. }
  4289. /**
  4290. * Submit handler for configing new item(s) to a view.
  4291. */
  4292. function views_ui_config_style_form_submit($form, &$form_state) {
  4293. // Run it through the handler's submit function.
  4294. $form_state['handler']->options_submit($form['style_options'], $form_state);
  4295. $item = $form_state['handler']->options;
  4296. // Store the data we're given.
  4297. $item['style_options'] = $form_state['values']['style_options'];
  4298. // Store the item back on the view.
  4299. $form_state['view']->set_item($form_state['display_id'], $form_state['type'], $form_state['id'], $item);
  4300. // Write to cache.
  4301. views_ui_cache_set($form_state['view']);
  4302. }
  4303. /**
  4304. * Get a list of roles in the system.
  4305. */
  4306. function views_ui_get_roles() {
  4307. static $roles = NULL;
  4308. if (!isset($roles)) {
  4309. $roles = array();
  4310. $result = db_query("SELECT r.rid, r.name FROM {role} r ORDER BY r.name");
  4311. foreach ($result as $obj) {
  4312. $roles[$obj->rid] = $obj->name;
  4313. }
  4314. }
  4315. return $roles;
  4316. }
  4317. /**
  4318. * Form builder for the admin display defaults page.
  4319. */
  4320. function views_ui_admin_settings_basic() {
  4321. $form = array();
  4322. $form['#attached']['css'] = views_ui_get_admin_css();
  4323. $options = array();
  4324. foreach (list_themes() as $name => $theme) {
  4325. if ($theme->status) {
  4326. $options[$name] = $theme->info['name'];
  4327. }
  4328. }
  4329. // This is not currently a fieldset but we may want it to be later,
  4330. // so this will make it easier to change if we do.
  4331. $form['basic'] = array();
  4332. $form['basic']['views_ui_show_listing_filters'] = array(
  4333. '#type' => 'checkbox',
  4334. '#title' => t('Show filters on the list of views'),
  4335. '#default_value' => variable_get('views_ui_show_listing_filters', FALSE),
  4336. );
  4337. $form['basic']['views_ui_show_advanced_help_warning'] = array(
  4338. '#type' => 'checkbox',
  4339. '#title' => t('Show advanced help warning'),
  4340. '#default_value' => variable_get('views_ui_show_advanced_help_warning', TRUE),
  4341. );
  4342. $form['basic']['views_ui_show_master_display'] = array(
  4343. '#type' => 'checkbox',
  4344. '#title' => t('Always show the master display'),
  4345. '#description' => t('Advanced users of views may choose to see the master (i.e. default) display.'),
  4346. '#default_value' => variable_get('views_ui_show_master_display', FALSE),
  4347. );
  4348. $form['basic']['views_ui_show_advanced_column'] = array(
  4349. '#type' => 'checkbox',
  4350. '#title' => t('Always show advanced display settings'),
  4351. '#description' => t('Default to showing advanced display settings, such as relationships and contextual filters.'),
  4352. '#default_value' => variable_get('views_ui_show_advanced_column', FALSE),
  4353. );
  4354. $form['basic']['views_ui_display_embed'] = array(
  4355. '#type' => 'checkbox',
  4356. '#title' => t('Show the embed display in the ui.'),
  4357. '#description' => t('Allow advanced user to use the embed view display. The plugin itself works if it\'s not visible in the ui'),
  4358. '#default_value' => variable_get('views_ui_display_embed', FALSE),
  4359. );
  4360. $form['basic']['views_ui_custom_theme'] = array(
  4361. '#type' => 'select',
  4362. '#title' => t('Custom admin theme for the Views UI'),
  4363. '#options' => array('_default' => t('- Use default -')) + $options,
  4364. '#default_value' => variable_get('views_ui_custom_theme', '_default'),
  4365. '#description' => t('In some cases you might want to select a different admin theme for the Views UI.'),
  4366. );
  4367. $form['basic']['views_exposed_filter_any_label'] = array(
  4368. '#type' => 'select',
  4369. '#title' => t('Label for "Any" value on non-required single-select exposed filters'),
  4370. '#options' => array('old_any' => '<Any>', 'new_any' => t('- Any -')),
  4371. '#default_value' => variable_get('views_exposed_filter_any_label', 'new_any'),
  4372. );
  4373. $form['live_preview'] = array(
  4374. '#type' => 'fieldset',
  4375. '#title' => t('Live preview settings'),
  4376. );
  4377. $form['live_preview']['views_ui_always_live_preview'] = array(
  4378. '#type' => 'checkbox',
  4379. '#title' => t('Automatically update preview on changes'),
  4380. '#default_value' => variable_get('views_ui_always_live_preview', TRUE),
  4381. );
  4382. // $form['live_preview']['views_ui_always_live_preview_button'] = array(
  4383. // '#type' => 'checkbox',
  4384. // '#title' => t('Always show the preview button, even when the automatically update option is checked'),
  4385. // '#default_value' => variable_get('views_ui_always_live_preview_button', FALSE),
  4386. // );
  4387. $form['live_preview']['views_ui_show_preview_information'] = array(
  4388. '#type' => 'checkbox',
  4389. '#title' => t('Show information and statistics about the view during live preview'),
  4390. '#default_value' => variable_get('views_ui_show_preview_information', TRUE),
  4391. );
  4392. $form['live_preview']['views_ui_show_sql_query_where'] = array(
  4393. '#type' => 'radios',
  4394. '#options' => array(
  4395. 'above' => t('Above the preview'),
  4396. 'below' => t('Below the preview'),
  4397. ),
  4398. '#id' => 'edit-show-sql',
  4399. '#default_value' => variable_get('views_ui_show_sql_query_where', 'above'),
  4400. '#dependency' => array('edit-views-ui-show-preview-information' => array(TRUE)),
  4401. '#prefix' => '<div id="edit-show-sql-wrapper" class="views-dependent">',
  4402. '#suffix' => '</div>',
  4403. );
  4404. $form['live_preview']['views_ui_show_sql_query'] = array(
  4405. '#type' => 'checkbox',
  4406. '#title' => t('Show the SQL query'),
  4407. '#default_value' => variable_get('views_ui_show_sql_query', FALSE),
  4408. '#dependency' => array('edit-views-ui-show-preview-information' => array(TRUE)),
  4409. );
  4410. $form['live_preview']['views_ui_show_performance_statistics'] = array(
  4411. '#type' => 'checkbox',
  4412. '#title' => t('Show performance statistics'),
  4413. '#default_value' => variable_get('views_ui_show_performance_statistics', FALSE),
  4414. '#dependency' => array('edit-views-ui-show-preview-information' => array(TRUE)),
  4415. );
  4416. $form['live_preview']['views_show_additional_queries'] = array(
  4417. '#type' => 'checkbox',
  4418. '#title' => t('Show other queries run during render during live preview'),
  4419. '#description' => t("Drupal has the potential to run many queries while a view is being rendered. Checking this box will display every query run during view render as part of the live preview."),
  4420. '#default_value' => variable_get('views_show_additional_queries', FALSE),
  4421. '#dependency' => array('edit-views-ui-show-preview-information' => array(TRUE)),
  4422. );
  4423. // $form['live_preview']['views_ui_show_performance_statistics_where'] = array(
  4424. return system_settings_form($form);
  4425. }
  4426. /**
  4427. * Form builder for the advanced admin settings page.
  4428. */
  4429. function views_ui_admin_settings_advanced() {
  4430. $form = array();
  4431. $form['#attached']['css'] = views_ui_get_admin_css();
  4432. $form['cache'] = array(
  4433. '#type' => 'fieldset',
  4434. '#title' => t('Caching'),
  4435. );
  4436. $form['cache']['views_skip_cache'] = array(
  4437. '#type' => 'checkbox',
  4438. '#title' => t('Disable views data caching'),
  4439. '#description' => t("Views caches data about tables, modules and views available, to increase performance. By checking this box, Views will skip this cache and always rebuild this data when needed. This can have a serious performance impact on your site."),
  4440. '#default_value' => variable_get('views_skip_cache', FALSE),
  4441. );
  4442. $form['cache']['clear_cache'] = array(
  4443. '#type' => 'submit',
  4444. '#value' => t("Clear Views' cache"),
  4445. '#submit' => array('views_ui_tools_clear_cache'),
  4446. );
  4447. $form['debug'] = array(
  4448. '#type' => 'fieldset',
  4449. '#title' => t('Debugging'),
  4450. );
  4451. $form['debug']['views_sql_signature'] = array(
  4452. '#type' => 'checkbox',
  4453. '#title' => t('Add Views signature to all SQL queries'),
  4454. '#description' => t("All Views-generated queries will include the name of the views and display 'view-name:display-name' as a string at the end of the SELECT clause. This makes identifying Views queries in database server logs simpler, but should only be used when troubleshooting."),
  4455. '#default_value' => variable_get('views_sql_signature', FALSE),
  4456. );
  4457. $form['debug']['views_no_javascript'] = array(
  4458. '#type' => 'checkbox',
  4459. '#title' => t('Disable JavaScript with Views'),
  4460. '#description' => t("If you are having problems with the JavaScript, you can disable it here. The Views UI should degrade and still be usable without JavaScript; it's just not as good."),
  4461. '#default_value' => variable_get('views_no_javascript', FALSE),
  4462. );
  4463. $form['debug']['views_devel_output'] = array(
  4464. '#type' => 'checkbox',
  4465. '#title' => t('Enable views performance statistics/debug messages via the Devel module'),
  4466. '#description' => t("Check this to enable some Views query and performance statistics/debug messages <em>if Devel is installed</em>."),
  4467. '#default_value' => variable_get('views_devel_output', FALSE),
  4468. );
  4469. $form['locale'] = array(
  4470. '#type' => 'fieldset',
  4471. '#title' => t('Localization'),
  4472. );
  4473. $form['locale']['views_localization_plugin'] = array(
  4474. '#type' => 'radios',
  4475. '#title' => t('Translation method'),
  4476. '#options' => views_fetch_plugin_names('localization', NULL, array()),
  4477. '#default_value' => views_get_localization_plugin(),
  4478. '#description' => t('Select a translation method to use for Views data like header, footer, and empty text.'),
  4479. );
  4480. $form['locale']['views_localize_all'] = array(
  4481. '#type' => 'checkbox',
  4482. '#title' => t('Use same translation method for exported views'),
  4483. '#description' => t('Exported views will use Core translation by default. Enable this to always use the configured translation method.'),
  4484. '#default_value' => variable_get('views_localize_all', FALSE),
  4485. );
  4486. $regions = array();
  4487. $regions['watchdog'] = t('Watchdog');
  4488. if (module_exists('devel')) {
  4489. $regions['message'] = t('Devel message(dpm)');
  4490. $regions['drupal_debug'] = t('Devel logging (tmp://drupal_debug.txt)');
  4491. }
  4492. $form['debug']['views_devel_region'] = array(
  4493. '#type' => 'select',
  4494. '#title' => t('Page region to output performance statistics/debug messages'),
  4495. '#default_value' => variable_get('views_devel_region', 'footer'),
  4496. '#options' => $regions,
  4497. '#dependency' => array('edit-views-devel-output' => array(1)),
  4498. );
  4499. $options = views_fetch_plugin_names('display_extender');
  4500. if (!empty($options)) {
  4501. $form['extenders'] = array(
  4502. '#type' => 'fieldset',
  4503. );
  4504. ;
  4505. $form['extenders']['views_display_extenders'] = array(
  4506. '#title' => t('Display extenders'),
  4507. '#default_value' => views_get_enabled_display_extenders(),
  4508. '#options' => $options,
  4509. '#type' => 'checkboxes',
  4510. '#description' => t('Select extensions of the views interface.'),
  4511. );
  4512. }
  4513. return system_settings_form($form);
  4514. }
  4515. /**
  4516. * Submit hook to clear the views cache.
  4517. */
  4518. function views_ui_tools_clear_cache() {
  4519. views_invalidate_cache();
  4520. drupal_set_message(t('The cache has been cleared.'));
  4521. }
  4522. /**
  4523. * Submit hook to clear Drupal's theme registry (thereby triggering
  4524. * a templates rescan).
  4525. */
  4526. function views_ui_config_item_form_rescan($form, &$form_state) {
  4527. drupal_theme_rebuild();
  4528. // The 'Theme: Information' page is about to be shown again. That page
  4529. // analyzes the output of theme_get_registry(). However, this latter
  4530. // function uses an internal cache (which was initialized before we
  4531. // called drupal_theme_rebuild()) so it won't reflect the
  4532. // current state of our theme registry. The only way to clear that cache
  4533. // is to re-initialize the theme system.
  4534. unset($GLOBALS['theme']);
  4535. drupal_theme_initialize();
  4536. $form_state['rerender'] = TRUE;
  4537. $form_state['rebuild'] = TRUE;
  4538. }
  4539. /**
  4540. * Override handler for views_ui_edit_display_form.
  4541. */
  4542. function views_ui_edit_display_form_change_theme($form, &$form_state) {
  4543. // This is just a temporary variable.
  4544. $form_state['view']->theme = $form_state['values']['theme'];
  4545. views_ui_cache_set($form_state['view']);
  4546. $form_state['rerender'] = TRUE;
  4547. $form_state['rebuild'] = TRUE;
  4548. }
  4549. /**
  4550. * Page callback for views tag autocomplete.
  4551. */
  4552. function views_ui_autocomplete_tag($string = '') {
  4553. $matches = array();
  4554. // Get matches from default views.
  4555. views_include('view');
  4556. $views = views_get_all_views();
  4557. foreach ($views as $view) {
  4558. if (!empty($view->tag) && strpos($view->tag, $string) === 0) {
  4559. $matches[$view->tag] = check_plain($view->tag);
  4560. if (count($matches) >= 10) {
  4561. break;
  4562. }
  4563. }
  4564. }
  4565. drupal_json_output($matches);
  4566. }
  4567. // ------------------------------------------------------------------
  4568. // Get information from the Views data.
  4569. function _views_weight_sort($a, $b) {
  4570. if ($a['weight'] != $b['weight']) {
  4571. return $a['weight'] < $b['weight'] ? -1 : 1;
  4572. }
  4573. if ($a['title'] != $b['title']) {
  4574. return $a['title'] < $b['title'] ? -1 : 1;
  4575. }
  4576. return 0;
  4577. }
  4578. /**
  4579. * Fetch a list of all base tables available.
  4580. *
  4581. * @return
  4582. * A keyed array of in the form of 'base_table' => 'Description'.
  4583. */
  4584. function views_fetch_base_tables() {
  4585. static $base_tables = array();
  4586. if (empty($base_tables)) {
  4587. $weights = array();
  4588. $tables = array();
  4589. $data = views_fetch_data();
  4590. foreach ($data as $table => $info) {
  4591. if (!empty($info['table']['base'])) {
  4592. $tables[$table] = array(
  4593. 'title' => $info['table']['base']['title'],
  4594. 'description' => !empty($info['table']['base']['help']) ? $info['table']['base']['help'] : '',
  4595. 'weight' => !empty($info['table']['base']['weight']) ? $info['table']['base']['weight'] : 0,
  4596. );
  4597. }
  4598. }
  4599. uasort($tables, '_views_weight_sort');
  4600. $base_tables = $tables;
  4601. }
  4602. return $base_tables;
  4603. }
  4604. /**
  4605. *
  4606. */
  4607. function _views_sort_types($a, $b) {
  4608. $a_group = drupal_strtolower($a['group']);
  4609. $b_group = drupal_strtolower($b['group']);
  4610. if ($a_group != $b_group) {
  4611. return $a_group < $b_group ? -1 : 1;
  4612. }
  4613. $a_title = drupal_strtolower($a['title']);
  4614. $b_title = drupal_strtolower($b['title']);
  4615. if ($a_title != $b_title) {
  4616. return $a_title < $b_title ? -1 : 1;
  4617. }
  4618. return 0;
  4619. }
  4620. /**
  4621. * Fetch a list of all fields available for a given base type.
  4622. *
  4623. * @param (array|string) $base
  4624. * A list or a single base_table, for example node.
  4625. * @param string $type
  4626. * The handler type, for example field or filter.
  4627. * @param bool $grouping
  4628. * Should the result grouping by its 'group' label.
  4629. *
  4630. * @return array
  4631. * A keyed array of in the form of 'base_table' => 'Description'.
  4632. */
  4633. function views_fetch_fields($base, $type, $grouping = FALSE) {
  4634. static $fields = array();
  4635. if (empty($fields)) {
  4636. $data = views_fetch_data();
  4637. // This constructs this ginormous multi dimensional array to
  4638. // collect the important data about fields. In the end,
  4639. // the structure looks a bit like this (using nid as an example)
  4640. // $strings['nid']['filter']['title'] = 'string'.
  4641. //
  4642. // This is constructed this way because the above referenced strings
  4643. // can appear in different places in the actual data structure so that
  4644. // the data doesn't have to be repeated a lot. This essentially lets
  4645. // each field have a cheap kind of inheritance.
  4646. $components = array('title', 'group', 'help', 'base', 'aliases');
  4647. $components_errors = array();
  4648. // Fixed number of calls to t() to produce error components.
  4649. foreach ($components as $string) {
  4650. $components_errors[$string] = t("Error: missing @component", array('@component' => $string));
  4651. }
  4652. foreach ($data as $table => $table_data) {
  4653. $bases = array();
  4654. $strings = array();
  4655. $skip_bases = array();
  4656. foreach ($table_data as $field => $info) {
  4657. // Collect table data from this table.
  4658. if ($field == 'table') {
  4659. // calculate what tables this table can join to.
  4660. if (!empty($info['join'])) {
  4661. $bases = array_keys($info['join']);
  4662. }
  4663. // And it obviously joins to itself.
  4664. $bases[] = $table;
  4665. continue;
  4666. }
  4667. foreach (array('field', 'sort', 'filter', 'argument', 'relationship', 'area') as $key) {
  4668. if (!empty($info[$key])) {
  4669. if ($grouping && !empty($info[$key]['no group by'])) {
  4670. continue;
  4671. }
  4672. if (!empty($info[$key]['skip base'])) {
  4673. foreach ((array) $info[$key]['skip base'] as $base_name) {
  4674. $skip_bases[$field][$key][$base_name] = TRUE;
  4675. }
  4676. }
  4677. elseif (!empty($info['skip base'])) {
  4678. foreach ((array) $info['skip base'] as $base_name) {
  4679. $skip_bases[$field][$key][$base_name] = TRUE;
  4680. }
  4681. }
  4682. // Don't show old fields. The real field will be added right.
  4683. if (isset($info[$key]['moved to'])) {
  4684. continue;
  4685. }
  4686. foreach ($components as $string) {
  4687. // First, try the lowest possible level.
  4688. if (!empty($info[$key][$string])) {
  4689. $strings[$field][$key][$string] = $info[$key][$string];
  4690. }
  4691. // Then try the field level.
  4692. elseif (!empty($info[$string])) {
  4693. $strings[$field][$key][$string] = $info[$string];
  4694. }
  4695. // Finally, try the table level.
  4696. elseif (!empty($table_data['table'][$string])) {
  4697. $strings[$field][$key][$string] = $table_data['table'][$string];
  4698. }
  4699. else {
  4700. if ($string != 'base') {
  4701. $strings[$field][$key][$string] = $components_errors[$string];
  4702. }
  4703. }
  4704. }
  4705. }
  4706. }
  4707. }
  4708. foreach ($bases as $base_name) {
  4709. foreach ($strings as $field => $field_strings) {
  4710. foreach ($field_strings as $type_name => $type_strings) {
  4711. if (empty($skip_bases[$field][$type_name][$base_name])) {
  4712. $fields[$base_name][$type_name]["$table.$field"] = $type_strings;
  4713. }
  4714. }
  4715. }
  4716. }
  4717. }
  4718. }
  4719. // If we have an array of base tables available, go through them all and add
  4720. // them together. Duplicate keys will be lost and that's Just Fine.
  4721. if (is_array($base)) {
  4722. $strings = array();
  4723. foreach ($base as $base_table) {
  4724. if (isset($fields[$base_table][$type])) {
  4725. $strings += $fields[$base_table][$type];
  4726. }
  4727. }
  4728. uasort($strings, '_views_sort_types');
  4729. return $strings;
  4730. }
  4731. if (isset($fields[$base][$type])) {
  4732. uasort($fields[$base][$type], '_views_sort_types');
  4733. return $fields[$base][$type];
  4734. }
  4735. return array();
  4736. }
  4737. /**
  4738. * Theme the form for the table style plugin.
  4739. */
  4740. function theme_views_ui_style_plugin_table($variables) {
  4741. $form = $variables['form'];
  4742. $output = drupal_render($form['description_markup']);
  4743. $header = array(
  4744. t('Field'),
  4745. t('Column'),
  4746. t('Align'),
  4747. t('Separator'),
  4748. array(
  4749. 'data' => t('Sortable'),
  4750. 'align' => 'center',
  4751. ),
  4752. array(
  4753. 'data' => t('Default order'),
  4754. 'align' => 'center',
  4755. ),
  4756. array(
  4757. 'data' => t('Default sort'),
  4758. 'align' => 'center',
  4759. ),
  4760. array(
  4761. 'data' => t('Hide empty column'),
  4762. 'align' => 'center',
  4763. ),
  4764. );
  4765. $rows = array();
  4766. foreach (element_children($form['columns']) as $id) {
  4767. $row = array();
  4768. $row[] = check_plain(drupal_render($form['info'][$id]['name']));
  4769. $row[] = drupal_render($form['columns'][$id]);
  4770. $row[] = drupal_render($form['info'][$id]['align']);
  4771. $row[] = drupal_render($form['info'][$id]['separator']);
  4772. if (!empty($form['info'][$id]['sortable'])) {
  4773. $row[] = array(
  4774. 'data' => drupal_render($form['info'][$id]['sortable']),
  4775. 'align' => 'center',
  4776. );
  4777. $row[] = array(
  4778. 'data' => drupal_render($form['info'][$id]['default_sort_order']),
  4779. 'align' => 'center',
  4780. );
  4781. $row[] = array(
  4782. 'data' => drupal_render($form['default'][$id]),
  4783. 'align' => 'center',
  4784. );
  4785. }
  4786. else {
  4787. $row[] = '';
  4788. $row[] = '';
  4789. $row[] = '';
  4790. }
  4791. $row[] = array(
  4792. 'data' => drupal_render($form['info'][$id]['empty_column']),
  4793. 'align' => 'center',
  4794. );
  4795. $rows[] = $row;
  4796. }
  4797. // Add the special 'None' row.
  4798. $rows[] = array(t('None'), '', '', '', '', '', array('align' => 'center', 'data' => drupal_render($form['default'][-1])), '');
  4799. $output .= theme('table', array('header' => $header, 'rows' => $rows));
  4800. $output .= drupal_render_children($form);
  4801. return $output;
  4802. }
  4803. /**
  4804. * Placeholder function for overriding $display->display_title.
  4805. *
  4806. * @todo Remove this function once editing the display title is possible.
  4807. */
  4808. function views_ui_get_display_label($view, $display_id, $check_changed = TRUE) {
  4809. $title = $display_id == 'default' ? t('Master') : $view->display[$display_id]->display_title;
  4810. $title = views_ui_truncate($title, 25);
  4811. if ($check_changed && !empty($view->changed_display[$display_id])) {
  4812. $changed = '*';
  4813. $title = $title . $changed;
  4814. }
  4815. return $title;
  4816. }
  4817. /**
  4818. *
  4819. */
  4820. function views_ui_add_template_page() {
  4821. $templates = views_get_all_templates();
  4822. if (empty($templates)) {
  4823. return t('There are no templates available.');
  4824. }
  4825. $header = array(
  4826. t('Name'),
  4827. t('Description'),
  4828. t('Operation'),
  4829. );
  4830. $rows = array();
  4831. foreach ($templates as $name => $template) {
  4832. $rows[] = array(
  4833. array('data' => check_plain($template->get_human_name())),
  4834. array('data' => check_plain($template->description)),
  4835. array('data' => l('add', 'admin/structure/views/template/' . $template->name . '/add')),
  4836. );
  4837. }
  4838. $output = theme('table', array('header' => $header, 'rows' => $rows));
  4839. return $output;
  4840. }
  4841. /**
  4842. * #process callback for a button.
  4843. *
  4844. * Determines if a button is the form's triggering element.
  4845. *
  4846. * The Form API has logic to determine the form's triggering element based on
  4847. * the data in $_POST. However, it only checks buttons based on a single #value
  4848. * per button. This function may be added to a button's #process callbacks to
  4849. * extend button click detection to support multiple #values per button. If the
  4850. * data in $_POST matches any value in the button's #values array, then the
  4851. * button is detected as having been clicked. This can be used when the value
  4852. * (label) of the same logical button may be different based on context (e.g.,
  4853. * "Apply" vs. "Apply and continue").
  4854. *
  4855. * @see _form_builder_handle_input_element()
  4856. * @see _form_button_was_clicked()
  4857. */
  4858. function views_ui_form_button_was_clicked($element, &$form_state) {
  4859. $process_input = empty($element['#disabled']) && ($form_state['programmed'] || ($form_state['process_input'] && (!isset($element['#access']) || $element['#access'])));
  4860. if ($process_input && !isset($form_state['triggering_element']) && isset($element['#button_type']) && isset($form_state['input'][$element['#name']]) && isset($element['#values']) && in_array($form_state['input'][$element['#name']], $element['#values'], TRUE)) {
  4861. $form_state['triggering_element'] = $element;
  4862. }
  4863. return $element;
  4864. }
  4865. /**
  4866. * #process callback for a button.
  4867. *
  4868. * Makes implicit form submissions trigger as this button.
  4869. *
  4870. * @see Drupal.behaviors.viewsImplicitFormSubmission
  4871. */
  4872. function views_ui_default_button($element, &$form_state, $form) {
  4873. $setting['viewsImplicitFormSubmission'][$form['#id']]['defaultButton'] = $element['#id'];
  4874. $element['#attached']['js'][] = array('type' => 'setting', 'data' => $setting);
  4875. return $element;
  4876. }
  4877. /**
  4878. * List all instances of fields on any views.
  4879. *
  4880. * Therefore it builds up a table of each field which is used in any view.
  4881. *
  4882. * @see field_ui_fields_list()
  4883. */
  4884. function views_ui_field_list() {
  4885. $views = views_get_all_views();
  4886. // Fetch all fieldapi fields which are used in views
  4887. // Therefore search in all views, displays and handler-types.
  4888. $fields = array();
  4889. foreach ($views as $view) {
  4890. foreach ($view->display as $display_id => $display) {
  4891. if ($view->set_display($display_id)) {
  4892. foreach (views_object_types() as $type => $info) {
  4893. foreach ($view->get_items($type, $display_id) as $item) {
  4894. $data = views_fetch_data($item['table']);
  4895. if (isset($data[$item['field']]) && isset($data[$item['field']][$type])
  4896. && $data = $data[$item['field']][$type]) {
  4897. // The final check that we have a fieldapi field now.
  4898. if (isset($data['field_name'])) {
  4899. $fields[$data['field_name']][$view->name] = $view->name;
  4900. }
  4901. }
  4902. }
  4903. }
  4904. }
  4905. }
  4906. }
  4907. $header = array(t('Field name'), t('Used in'));
  4908. $rows = array();
  4909. foreach ($fields as $field_name => $views) {
  4910. $rows[$field_name]['data'][0] = check_plain($field_name);
  4911. foreach ($views as $view) {
  4912. $rows[$field_name]['data'][1][] = l($view, "admin/structure/views/view/$view");
  4913. }
  4914. $rows[$field_name]['data'][1] = implode(', ', $rows[$field_name]['data'][1]);
  4915. }
  4916. // Sort rows by field name.
  4917. ksort($rows);
  4918. $output = array(
  4919. '#theme' => 'table',
  4920. '#header' => $header,
  4921. '#rows' => $rows,
  4922. '#empty' => t('No fields have been used in views yet.'),
  4923. );
  4924. return $output;
  4925. }
  4926. /**
  4927. * Lists all plugins and what enabled Views use them.
  4928. */
  4929. function views_ui_plugin_list() {
  4930. $rows = views_plugin_list();
  4931. foreach ($rows as &$row) {
  4932. // Link each view name to the view itself.
  4933. foreach ($row['views'] as $row_name => $view) {
  4934. $row['views'][$row_name] = l($view, "admin/structure/views/view/$view");
  4935. }
  4936. $row['views'] = implode(', ', $row['views']);
  4937. }
  4938. // Sort rows by field name.
  4939. ksort($rows);
  4940. return array(
  4941. '#theme' => 'table',
  4942. '#header' => array(t('Type'), t('Name'), t('Provided by'), t('Used in')),
  4943. '#rows' => $rows,
  4944. '#empty' => t('There are no enabled views.'),
  4945. );
  4946. }