webform.module 145 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969
  1. <?php
  2. /**
  3. * This module provides a simple way to create forms and questionnaires.
  4. *
  5. * The initial development of this module was sponsered by ÅF Industri AB, Open
  6. * Source City and Karlstad University Library. Continued development sponsored
  7. * by Lullabot.
  8. *
  9. * @author Nathan Haug <nate@lullabot.com>
  10. */
  11. /**
  12. * Implements hook_help().
  13. */
  14. function webform_help($section = 'admin/help#webform', $arg = NULL) {
  15. $output = '';
  16. switch ($section) {
  17. case 'admin/config/content/webform':
  18. module_load_include('inc', 'webform', 'includes/webform.admin');
  19. $type_list = webform_admin_type_list();
  20. $output = t('Webform enables nodes to have attached forms and questionnaires.');
  21. if ($type_list) {
  22. $output .= ' ' . t('To add one, create a !types piece of content.', array('!types' => $type_list));
  23. }
  24. else {
  25. $output .= ' <strong>' . t('Webform is currently not enabled on any content types.') . '</strong> ' . t('To use Webform, please enable it on at least one content type on this page.');
  26. }
  27. $output = '<p>' . $output . '</p>';
  28. break;
  29. case 'admin/content/webform':
  30. $output = '<p>' . t('This page lists all of the content on the site that may have a webform attached to it.') . '</p>';
  31. break;
  32. case 'admin/help#webform':
  33. module_load_include('inc', 'webform', 'includes/webform.admin');
  34. $types = webform_admin_type_list();
  35. if (empty($types)) {
  36. $types = t('Webform-enabled piece of content');
  37. $types_message = t('Webform is currently not enabled on any content types.') . ' ' . t('Visit the <a href="!url">Webform settings</a> page and enable Webform on at least one content type.', array('!url' => url('admin/config/content/webform')));
  38. }
  39. else {
  40. $types_message = t('Optional: Enable Webform on multiple types by visiting the <a href="!url">Webform settings</a> page.', array('!url' => url('admin/config/content/webform')));
  41. }
  42. $output = t("<p>This module lets you create forms or questionnaires and define their content. Submissions from these forms are stored in the database and optionally also sent by e-mail to a predefined address.</p>
  43. <p>Here is how to create one:</p>
  44. <ul>
  45. <li>!webform-types-message</li>
  46. <li>Go to <a href=\"!create-content\">Create content</a> and add a !types piece of content.</li>
  47. <li>After saving the new content, you will be redirected to the main field list of the form that will be created. Add the fields you would like on your form.</li>
  48. <li>Once finished adding fields, you may want to send e-mails to administrators or back to the user who filled out the form. Click on the <em>Emails</em> sub-tab underneath the <em>Webform</em> tab on the piece of content.</li>
  49. <li>Finally, visit the <em>Form settings</em> sub-tab under the <em>Webform</em> tab to configure remaining configurations options for your form.
  50. <ul>
  51. <li>Add a confirmation message and/or redirect URL that is to be displayed after successful submission.</li>
  52. <li>Set a submission limit.</li>
  53. <li>Determine which roles may submit the form.</li>
  54. <li>Advanced configuration options such as allowing drafts or show users a message indicating how they can edit their submissions.</li>
  55. </ul>
  56. </li>
  57. <li>Your form is now ready for viewing. After receiving submissions, you can check the results users have submitted by visiting the <em>Results</em> tab on the piece of content.</li>
  58. </ul>
  59. <p>Help on adding and configuring the components will be shown after you add your first component.</p>
  60. ", array('!webform-types-message' => $types_message, '!create-content' => url('node/add'), '!types' => $types));
  61. break;
  62. case 'node/%/submission/%/resend':
  63. $output .= '<p>' . t('This form may be used to resend e-mails configured for this webform. Check the e-mails that need to be sent and click <em>Resend e-mails</em> to send these e-mails again.') . '</p>';
  64. break;
  65. }
  66. return $output;
  67. }
  68. /**
  69. * Implements hook_menu().
  70. */
  71. function webform_menu() {
  72. $items = array();
  73. // Submissions listing.
  74. $items['admin/content/webform'] = array(
  75. 'title' => 'Webforms',
  76. 'page callback' => 'webform_admin_content',
  77. 'access callback' => 'user_access',
  78. 'access arguments' => array('access all webform results'),
  79. 'description' => 'View and edit all the available webforms on your site.',
  80. 'file' => 'includes/webform.admin.inc',
  81. 'type' => MENU_LOCAL_TASK,
  82. );
  83. // Admin Settings.
  84. $items['admin/config/content/webform'] = array(
  85. 'title' => 'Webform settings',
  86. 'page callback' => 'drupal_get_form',
  87. 'page arguments' => array('webform_admin_settings'),
  88. 'access callback' => 'user_access',
  89. 'access arguments' => array('administer site configuration'),
  90. 'description' => 'Global configuration of webform functionality.',
  91. 'file' => 'includes/webform.admin.inc',
  92. 'type' => MENU_NORMAL_ITEM,
  93. );
  94. // Node page tabs.
  95. $items['node/%webform_menu/done'] = array(
  96. 'title' => 'Webform confirmation',
  97. 'page callback' => '_webform_confirmation',
  98. 'page arguments' => array(1),
  99. 'access callback' => 'node_access',
  100. 'access arguments' => array('view', 1),
  101. 'type' => MENU_CALLBACK,
  102. );
  103. $items['node/%webform_menu/webform'] = array(
  104. 'title' => 'Webform',
  105. 'page callback' => 'webform_components_page',
  106. 'page arguments' => array(1),
  107. 'access callback' => 'node_access',
  108. 'access arguments' => array('update', 1),
  109. 'file' => 'includes/webform.components.inc',
  110. 'weight' => 1,
  111. 'type' => MENU_LOCAL_TASK,
  112. 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
  113. );
  114. $items['node/%webform_menu/webform/components'] = array(
  115. 'title' => 'Form components',
  116. 'page callback' => 'webform_components_page',
  117. 'page arguments' => array(1),
  118. 'access callback' => 'node_access',
  119. 'access arguments' => array('update', 1),
  120. 'file' => 'includes/webform.components.inc',
  121. 'weight' => 0,
  122. 'type' => MENU_DEFAULT_LOCAL_TASK,
  123. );
  124. $items['node/%webform_menu/webform/configure'] = array(
  125. 'title' => 'Form settings',
  126. 'page callback' => 'drupal_get_form',
  127. 'page arguments' => array('webform_configure_form', 1),
  128. 'access callback' => 'node_access',
  129. 'access arguments' => array('update', 1),
  130. 'file' => 'includes/webform.pages.inc',
  131. 'weight' => 2,
  132. 'type' => MENU_LOCAL_TASK,
  133. );
  134. // Node e-mail forms.
  135. $items['node/%webform_menu/webform/emails'] = array(
  136. 'title' => 'E-mails',
  137. 'page callback' => 'drupal_get_form',
  138. 'page arguments' => array('webform_emails_form', 1),
  139. 'access callback' => 'node_access',
  140. 'access arguments' => array('update', 1),
  141. 'file' => 'includes/webform.emails.inc',
  142. 'weight' => 1,
  143. 'type' => MENU_LOCAL_TASK,
  144. );
  145. $items['node/%webform_menu/webform/emails/%webform_menu_email'] = array(
  146. 'load arguments' => array(1),
  147. 'page arguments' => array('webform_email_edit_form', 1, 4),
  148. 'access callback' => 'node_access',
  149. 'access arguments' => array('update', 1),
  150. 'file' => 'includes/webform.emails.inc',
  151. 'type' => MENU_CALLBACK,
  152. );
  153. $items['node/%webform_menu/webform/emails/%webform_menu_email/delete'] = array(
  154. 'load arguments' => array(1),
  155. 'page arguments' => array('webform_email_delete_form', 1, 4),
  156. 'access callback' => 'node_access',
  157. 'access arguments' => array('update', 1),
  158. 'type' => MENU_CALLBACK,
  159. );
  160. // Node component forms.
  161. $items['node/%webform_menu/webform/components/%webform_menu_component'] = array(
  162. 'load arguments' => array(1, 5),
  163. 'page callback' => 'drupal_get_form',
  164. 'page arguments' => array('webform_component_edit_form', 1, 4, FALSE),
  165. 'access callback' => 'node_access',
  166. 'access arguments' => array('update', 1),
  167. 'file' => 'includes/webform.components.inc',
  168. 'type' => MENU_LOCAL_TASK,
  169. );
  170. $items['node/%webform_menu/webform/components/%webform_menu_component/clone'] = array(
  171. 'load arguments' => array(1, 5),
  172. 'page callback' => 'drupal_get_form',
  173. 'page arguments' => array('webform_component_edit_form', 1, 4, TRUE),
  174. 'access callback' => 'node_access',
  175. 'access arguments' => array('update', 1),
  176. 'file' => 'includes/webform.components.inc',
  177. 'type' => MENU_LOCAL_TASK,
  178. );
  179. $items['node/%webform_menu/webform/components/%webform_menu_component/delete'] = array(
  180. 'load arguments' => array(1, 5),
  181. 'page callback' => 'drupal_get_form',
  182. 'page arguments' => array('webform_component_delete_form', 1, 4),
  183. 'access callback' => 'node_access',
  184. 'access arguments' => array('update', 1),
  185. 'file' => 'includes/webform.components.inc',
  186. 'type' => MENU_LOCAL_TASK,
  187. );
  188. // AJAX callback for loading select list options.
  189. $items['webform/ajax/options/%webform_menu'] = array(
  190. 'load arguments' => array(3),
  191. 'page callback' => 'webform_select_options_ajax',
  192. 'access callback' => 'node_access',
  193. 'access arguments' => array('update', 3),
  194. 'file' => 'components/select.inc',
  195. 'type' => MENU_CALLBACK,
  196. );
  197. // Node webform results.
  198. $items['node/%webform_menu/webform-results'] = array(
  199. 'title' => 'Results',
  200. 'page callback' => 'webform_results_submissions',
  201. 'page arguments' => array(1, FALSE, '50'),
  202. 'access callback' => 'webform_results_access',
  203. 'access arguments' => array(1),
  204. 'file' => 'includes/webform.report.inc',
  205. 'weight' => 2,
  206. 'type' => MENU_LOCAL_TASK,
  207. 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
  208. );
  209. $items['node/%webform_menu/webform-results/submissions'] = array(
  210. 'title' => 'Submissions',
  211. 'page callback' => 'webform_results_submissions',
  212. 'page arguments' => array(1, FALSE, '50'),
  213. 'access callback' => 'webform_results_access',
  214. 'access arguments' => array(1),
  215. 'file' => 'includes/webform.report.inc',
  216. 'weight' => 4,
  217. 'type' => MENU_DEFAULT_LOCAL_TASK,
  218. );
  219. $items['node/%webform_menu/webform-results/analysis'] = array(
  220. 'title' => 'Analysis',
  221. 'page callback' => 'webform_results_analysis',
  222. 'page arguments' => array(1),
  223. 'access callback' => 'webform_results_access',
  224. 'access arguments' => array(1),
  225. 'file' => 'includes/webform.report.inc',
  226. 'weight' => 5,
  227. 'type' => MENU_LOCAL_TASK,
  228. );
  229. $items['node/%webform_menu/webform-results/analysis/%webform_menu_component'] = array(
  230. 'title' => 'Analysis',
  231. 'load arguments' => array(1, 4),
  232. 'page callback' => 'webform_results_analysis',
  233. 'page arguments' => array(1, array(), 4),
  234. 'access callback' => 'webform_results_access',
  235. 'access arguments' => array(1),
  236. 'file' => 'includes/webform.report.inc',
  237. 'type' => MENU_CALLBACK,
  238. );
  239. $items['node/%webform_menu/webform-results/table'] = array(
  240. 'title' => 'Table',
  241. 'page callback' => 'webform_results_table',
  242. 'page arguments' => array(1, '50'),
  243. 'access callback' => 'webform_results_access',
  244. 'access arguments' => array(1),
  245. 'file' => 'includes/webform.report.inc',
  246. 'weight' => 6,
  247. 'type' => MENU_LOCAL_TASK,
  248. );
  249. $items['node/%webform_menu/webform-results/download'] = array(
  250. 'title' => 'Download',
  251. 'page callback' => 'drupal_get_form',
  252. 'page arguments' => array('webform_results_download_form', 1),
  253. 'access callback' => 'webform_results_access',
  254. 'access arguments' => array(1),
  255. 'file' => 'includes/webform.report.inc',
  256. 'weight' => 7,
  257. 'type' => MENU_LOCAL_TASK,
  258. );
  259. $items['node/%webform_menu/webform-results/clear'] = array(
  260. 'title' => 'Clear',
  261. 'page callback' => 'drupal_get_form',
  262. 'page arguments' => array('webform_results_clear_form', 1),
  263. 'access callback' => 'webform_results_clear_access',
  264. 'access arguments' => array(1),
  265. 'file' => 'includes/webform.report.inc',
  266. 'weight' => 8,
  267. 'type' => MENU_LOCAL_TASK,
  268. );
  269. // Node submissions.
  270. $items['node/%webform_menu/submissions'] = array(
  271. 'title' => 'Submissions',
  272. 'page callback' => 'webform_results_submissions',
  273. 'page arguments' => array(1, TRUE, '50'),
  274. 'access callback' => 'webform_submission_access',
  275. 'access arguments' => array(1, NULL, 'list'),
  276. 'file' => 'includes/webform.report.inc',
  277. 'type' => MENU_CALLBACK,
  278. );
  279. $items['node/%webform_menu/submission/%webform_menu_submission'] = array(
  280. 'title' => 'Webform submission',
  281. 'load arguments' => array(1),
  282. 'page callback' => 'webform_submission_page',
  283. 'page arguments' => array(1, 3, 'html'),
  284. 'title callback' => 'webform_submission_title',
  285. 'title arguments' => array(1, 3),
  286. 'access callback' => 'webform_submission_access',
  287. 'access arguments' => array(1, 3, 'view'),
  288. 'file' => 'includes/webform.submissions.inc',
  289. 'type' => MENU_CALLBACK,
  290. );
  291. $items['node/%webform_menu/submission/%webform_menu_submission/view'] = array(
  292. 'title' => 'View',
  293. 'load arguments' => array(1),
  294. 'page callback' => 'webform_submission_page',
  295. 'page arguments' => array(1, 3, 'html'),
  296. 'access callback' => 'webform_submission_access',
  297. 'access arguments' => array(1, 3, 'view'),
  298. 'weight' => 0,
  299. 'file' => 'includes/webform.submissions.inc',
  300. 'type' => MENU_DEFAULT_LOCAL_TASK,
  301. );
  302. $items['node/%webform_menu/submission/%webform_menu_submission/edit'] = array(
  303. 'title' => 'Edit',
  304. 'load arguments' => array(1),
  305. 'page callback' => 'webform_submission_page',
  306. 'page arguments' => array(1, 3, 'form'),
  307. 'access callback' => 'webform_submission_access',
  308. 'access arguments' => array(1, 3, 'edit'),
  309. 'weight' => 1,
  310. 'file' => 'includes/webform.submissions.inc',
  311. 'type' => MENU_LOCAL_TASK,
  312. );
  313. $items['node/%webform_menu/submission/%webform_menu_submission/delete'] = array(
  314. 'title' => 'Delete',
  315. 'load arguments' => array(1),
  316. 'page callback' => 'drupal_get_form',
  317. 'page arguments' => array('webform_submission_delete_form', 1, 3),
  318. 'access callback' => 'webform_submission_access',
  319. 'access arguments' => array(1, 3, 'delete'),
  320. 'weight' => 2,
  321. 'file' => 'includes/webform.submissions.inc',
  322. 'type' => MENU_LOCAL_TASK,
  323. );
  324. $items['node/%webform_menu/submission/%webform_menu_submission/resend'] = array(
  325. 'title' => 'Resend e-mails',
  326. 'load arguments' => array(1),
  327. 'page callback' => 'drupal_get_form',
  328. 'page arguments' => array('webform_submission_resend', 1, 3),
  329. 'access callback' => 'webform_results_access',
  330. 'access arguments' => array(1),
  331. 'file' => 'includes/webform.submissions.inc',
  332. 'type' => MENU_CALLBACK,
  333. );
  334. return $items;
  335. }
  336. /**
  337. * Menu loader callback. Load a webform node if the given nid is a webform.
  338. */
  339. function webform_menu_load($nid) {
  340. if (!is_numeric($nid)) {
  341. return FALSE;
  342. }
  343. $node = node_load($nid);
  344. if (!isset($node->type) || !in_array($node->type, webform_variable_get('webform_node_types'))) {
  345. return FALSE;
  346. }
  347. return $node;
  348. }
  349. /**
  350. * Menu loader callback. Load a webform submission if the given sid is a valid.
  351. */
  352. function webform_menu_submission_load($sid, $nid) {
  353. module_load_include('inc', 'webform', 'includes/webform.submissions');
  354. $submission = webform_get_submission($nid, $sid);
  355. return empty($submission) ? FALSE : $submission;
  356. }
  357. /**
  358. * Menu loader callback. Load a webform component if the given cid is a valid.
  359. */
  360. function webform_menu_component_load($cid, $nid, $type) {
  361. module_load_include('inc', 'webform', 'includes/webform.components');
  362. if ($cid == 'new') {
  363. $components = webform_components();
  364. $component = in_array($type, array_keys($components)) ? array('type' => $type, 'nid' => $nid, 'name' => $_GET['name'], 'mandatory' => $_GET['mandatory'], 'pid' => $_GET['pid'], 'weight' => $_GET['weight']) : FALSE;
  365. }
  366. else {
  367. $node = node_load($nid);
  368. $component = isset($node->webform['components'][$cid]) ? $node->webform['components'][$cid] : FALSE;
  369. }
  370. if ($component) {
  371. webform_component_defaults($component);
  372. }
  373. return $component;
  374. }
  375. /**
  376. * Menu loader callback. Load a webform e-mail if the given eid is a valid.
  377. */
  378. function webform_menu_email_load($eid, $nid) {
  379. module_load_include('inc', 'webform', 'includes/webform.emails');
  380. $node = node_load($nid);
  381. $email = webform_email_load($eid, $nid);
  382. if ($eid == 'new') {
  383. if (isset($_GET['option']) && isset($_GET['email'])) {
  384. $type = $_GET['option'];
  385. if ($type == 'custom') {
  386. $email['email'] = $_GET['email'];
  387. }
  388. elseif ($type == 'component' && isset($node->webform['components'][$_GET['email']])) {
  389. $email['email'] = $_GET['email'];
  390. }
  391. }
  392. }
  393. return $email;
  394. }
  395. function webform_submission_access($node, $submission, $op = 'view', $account = NULL) {
  396. global $user;
  397. $account = isset($account) ? $account : $user;
  398. $access_all = user_access('access all webform results', $account);
  399. $access_own_submission = isset($submission) && user_access('access own webform submissions', $account) && (($account->uid && $account->uid == $submission->uid) || isset($_SESSION['webform_submission'][$submission->sid]));
  400. $access_node_submissions = user_access('access own webform results', $account) && $account->uid == $node->uid;
  401. $general_access = $access_all || $access_own_submission || $access_node_submissions;
  402. // Disable the page cache for anonymous users in this access callback,
  403. // otherwise the "Access denied" page gets cached.
  404. if (!$account->uid && user_access('access own webform submissions', $account)) {
  405. webform_disable_page_cache();
  406. }
  407. $module_access = count(array_filter(module_invoke_all('webform_submission_access', $node, $submission, $op, $account))) > 0;
  408. switch ($op) {
  409. case 'view':
  410. return $module_access || $general_access;
  411. case 'edit':
  412. return $module_access || ($general_access && (user_access('edit all webform submissions', $account) || (user_access('edit own webform submissions', $account) && $account->uid == $submission->uid)));
  413. case 'delete':
  414. return $module_access || ($general_access && (user_access('delete all webform submissions', $account) || (user_access('delete own webform submissions', $account) && $account->uid == $submission->uid)));
  415. case 'list':
  416. return $module_access || user_access('access all webform results', $account) || (user_access('access own webform submissions', $account) && ($account->uid || isset($_SESSION['webform_submission']))) || (user_access('access own webform results', $account) && $account->uid == $node->uid);
  417. }
  418. }
  419. /**
  420. * Menu access callback. Ensure a user both access and node 'view' permission.
  421. */
  422. function webform_results_access($node, $account = NULL) {
  423. global $user;
  424. $account = isset($account) ? $account : $user;
  425. $module_access = count(array_filter(module_invoke_all('webform_results_access', $node, $account))) > 0;
  426. return node_access('view', $node, $account) && ($module_access || user_access('access all webform results', $account) || (user_access('access own webform results', $account) && $account->uid == $node->uid));
  427. }
  428. function webform_results_clear_access($node, $account = NULL) {
  429. global $user;
  430. $account = isset($account) ? $account : $user;
  431. $module_access = count(array_filter(module_invoke_all('webform_results_clear_access', $node, $account))) > 0;
  432. return webform_results_access($node, $account) && ($module_access || user_access('delete all webform submissions', $account));
  433. }
  434. /**
  435. * Implements hook_admin_paths().
  436. */
  437. function webform_admin_paths() {
  438. if (variable_get('node_admin_theme')) {
  439. return array(
  440. 'node/*/webform' => TRUE,
  441. 'node/*/webform/*' => TRUE,
  442. 'node/*/webform-results' => TRUE,
  443. 'node/*/webform-results/*' => TRUE,
  444. 'node/*/submission/*' => TRUE,
  445. );
  446. }
  447. }
  448. /**
  449. * Implements hook_perm().
  450. */
  451. function webform_permission() {
  452. return array(
  453. 'access all webform results' => array(
  454. 'title' => t('Access all webform results'),
  455. 'description' => t('Grants access to the "Results" tab on all webform content. Generally an administrative permission.'),
  456. ),
  457. 'access own webform results' => array(
  458. 'title' => t('Access own webform results'),
  459. 'description' => t('Grants access to the "Results" tab to the author of webform content they have created.'),
  460. ),
  461. 'edit all webform submissions' => array(
  462. 'title' => t('Edit all webform submissions'),
  463. 'description' => t('Allows editing of any webform submission by any user. Generally an administrative permission.'),
  464. ),
  465. 'delete all webform submissions' => array(
  466. 'title' => t('Delete all webform submissions'),
  467. 'description' => t('Allows deleting of any webform submission by any user. Generally an administrative permission.'),
  468. ),
  469. 'access own webform submissions' => array(
  470. 'title' => t('Access own webform submissions'),
  471. ),
  472. 'edit own webform submissions' => array(
  473. 'title' => t('Edit own webform submissions'),
  474. ),
  475. 'delete own webform submissions' => array(
  476. 'title' => t('Delete own webform submissions'),
  477. ),
  478. );
  479. }
  480. /**
  481. * Implements hook_theme().
  482. */
  483. function webform_theme() {
  484. $theme = array(
  485. // webform.module.
  486. 'webform_view' => array(
  487. 'render element' => 'webform',
  488. ),
  489. 'webform_view_messages' => array(
  490. 'variables' => array('node' => NULL, 'teaser' => NULL, 'page' => NULL, 'submission_count' => NULL, 'user_limit_exceeded' => NULL, 'total_limit_exceeded' => NULL, 'allowed_roles' => NULL, 'closed' => NULL, 'cached' => NULL),
  491. ),
  492. 'webform_form' => array(
  493. 'render element' => 'form',
  494. 'template' => 'templates/webform-form',
  495. 'pattern' => 'webform_form_[0-9]+',
  496. ),
  497. 'webform_confirmation' => array(
  498. 'variables' => array('node' => NULL, 'sid' => NULL),
  499. 'template' => 'templates/webform-confirmation',
  500. 'pattern' => 'webform_confirmation_[0-9]+',
  501. ),
  502. 'webform_element' => array(
  503. 'render element' => 'element',
  504. ),
  505. 'webform_element_text' => array(
  506. 'render element' => 'element',
  507. ),
  508. 'webform_inline_radio' => array(
  509. 'render element' => 'element',
  510. ),
  511. 'webform_mail_message' => array(
  512. 'variables' => array('node' => NULL, 'submission' => NULL, 'email' => NULL),
  513. 'template' => 'templates/webform-mail',
  514. 'pattern' => 'webform_mail(_[0-9]+)?',
  515. ),
  516. 'webform_mail_headers' => array(
  517. 'variables' => array('node' => NULL, 'submission' => NULL, 'email' => NULL),
  518. 'pattern' => 'webform_mail_headers_[0-9]+',
  519. ),
  520. 'webform_token_help' => array(
  521. 'variables' => array('groups' => array()),
  522. ),
  523. // webform.admin.inc.
  524. 'webform_admin_settings' => array(
  525. 'render element' => 'form',
  526. 'file' => 'includes/webform.admin.inc',
  527. ),
  528. 'webform_admin_content' => array(
  529. 'variables' => array('nodes' => NULL),
  530. 'file' => 'includes/webform.admin.inc',
  531. ),
  532. // webform.emails.inc.
  533. 'webform_emails_form' => array(
  534. 'render element' => 'form',
  535. 'file' => 'includes/webform.emails.inc',
  536. ),
  537. 'webform_email_add_form' => array(
  538. 'render element' => 'form',
  539. 'file' => 'includes/webform.emails.inc',
  540. ),
  541. 'webform_email_edit_form' => array(
  542. 'render element' => 'form',
  543. 'file' => 'includes/webform.emails.inc',
  544. ),
  545. // webform.components.inc.
  546. 'webform_components_page' => array(
  547. 'variables' => array('node' => NULL, 'form' => NULL),
  548. 'file' => 'includes/webform.components.inc',
  549. ),
  550. 'webform_components_form' => array(
  551. 'render element' => 'form',
  552. 'file' => 'includes/webform.components.inc',
  553. ),
  554. 'webform_component_select' => array(
  555. 'render element' => 'element',
  556. 'file' => 'includes/webform.components.inc',
  557. ),
  558. // webform.pages.inc.
  559. 'webform_advanced_redirection_form' => array(
  560. 'render element' => 'form',
  561. 'file' => 'includes/webform.pages.inc',
  562. ),
  563. 'webform_advanced_submit_limit_form' => array(
  564. 'render element' => 'form',
  565. 'file' => 'includes/webform.pages.inc',
  566. ),
  567. 'webform_advanced_total_submit_limit_form' => array(
  568. 'render element' => 'form',
  569. 'file' => 'includes/webform.pages.inc',
  570. ),
  571. // webform.report.inc.
  572. 'webform_results_per_page' => array(
  573. 'variables' => array('total_count' => NULL, 'pager_count' => NULL),
  574. 'file' => 'includes/webform.report.inc',
  575. ),
  576. 'webform_results_submissions_header' => array(
  577. 'variables' => array('node' => NULL),
  578. 'file' => 'includes/webform.report.inc',
  579. ),
  580. 'webform_results_submissions' => array(
  581. 'render element' => 'element',
  582. 'template' => 'templates/webform-results-submissions',
  583. 'file' => 'includes/webform.report.inc',
  584. ),
  585. 'webform_results_table_header' => array(
  586. 'variables' => array('node' => NULL),
  587. 'file' => 'includes/webform.report.inc',
  588. ),
  589. 'webform_results_table' => array(
  590. 'variables' => array('node' => NULL, 'components' => NULL, 'submissions' => NULL, 'node' => NULL, 'total_count' => NULL, 'pager_count' => NULL),
  591. 'file' => 'includes/webform.report.inc',
  592. ),
  593. 'webform_results_download_range' => array(
  594. 'render element' => 'element',
  595. 'file' => 'includes/webform.report.inc',
  596. ),
  597. 'webform_results_download_select_format' => array(
  598. 'render element' => 'element',
  599. 'file' => 'includes/webform.report.inc',
  600. ),
  601. 'webform_results_analysis' => array(
  602. 'variables' => array('node' => NULL, 'data' => NULL, 'sids' => array(), 'component' => NULL),
  603. 'file' => 'includes/webform.report.inc',
  604. ),
  605. // webform.submissions.inc
  606. 'webform_submission' => array(
  607. 'render element' => 'renderable',
  608. 'template' => 'templates/webform-submission',
  609. 'pattern' => 'webform_submission_[0-9]+',
  610. 'file' => 'includes/webform.submissions.inc',
  611. ),
  612. 'webform_submission_page' => array(
  613. 'variables' => array('node' => NULL, 'submission' => NULL, 'submission_content' => NULL, 'submission_navigation' => NULL, 'submission_information' => NULL, 'submission_actions' => NULL, 'mode' => NULL),
  614. 'template' => 'templates/webform-submission-page',
  615. 'file' => 'includes/webform.submissions.inc',
  616. ),
  617. 'webform_submission_information' => array(
  618. 'variables' => array('node' => NULL, 'submission' => NULL, 'mode' => 'display'),
  619. 'template' => 'templates/webform-submission-information',
  620. 'file' => 'includes/webform.submissions.inc',
  621. ),
  622. 'webform_submission_navigation' => array(
  623. 'variables' => array('node' => NULL, 'submission' => NULL, 'mode' => NULL),
  624. 'template' => 'templates/webform-submission-navigation',
  625. 'file' => 'includes/webform.submissions.inc',
  626. ),
  627. 'webform_submission_resend' => array(
  628. 'render element' => 'form',
  629. 'file' => 'includes/webform.submissions.inc',
  630. ),
  631. );
  632. // Theme functions in all components.
  633. $components = webform_components(TRUE);
  634. foreach ($components as $type => $component) {
  635. if ($theme_additions = webform_component_invoke($type, 'theme')) {
  636. $theme = array_merge($theme, $theme_additions);
  637. }
  638. }
  639. return $theme;
  640. }
  641. /**
  642. * Implements hook_library().
  643. */
  644. function webform_library() {
  645. $module_path = drupal_get_path('module', 'webform');
  646. // Webform administration.
  647. $libraries['admin'] = array(
  648. 'title' => 'Webform: Administration',
  649. 'website' => 'http://drupal.org/project/webform',
  650. 'version' => '1.0',
  651. 'js' => array(
  652. $module_path . '/js/webform-admin.js' => array('group' => JS_DEFAULT),
  653. ),
  654. 'css' => array(
  655. $module_path . '/css/webform-admin.css' => array('group' => CSS_DEFAULT, 'weight' => 1),
  656. ),
  657. );
  658. return $libraries;
  659. }
  660. /**
  661. * Implements hook_element_info().
  662. */
  663. function webform_element_info() {
  664. // A few of our components need to be defined here because Drupal does not
  665. // provide these components natively. Because this hook fires on every page
  666. // load (even on non-webform pages), we don't put this in the component .inc
  667. // files because of the unnecessary loading that it would require.
  668. $elements['webform_time'] = array('#input' => 'TRUE');
  669. $elements['webform_grid'] = array('#input' => 'TRUE');
  670. $elements['webform_email'] = array(
  671. '#input' => TRUE,
  672. '#theme' => 'webform_email',
  673. '#size' => 60,
  674. );
  675. $elements['webform_number'] = array(
  676. '#input' => TRUE,
  677. '#theme' => 'webform_number',
  678. '#min' => NULL,
  679. '#max' => NULL,
  680. '#step' => NULL,
  681. );
  682. return $elements;
  683. }
  684. /**
  685. * Implements hook_webform_component_info().
  686. */
  687. function webform_webform_component_info() {
  688. $component_info = array(
  689. 'date' => array(
  690. 'label' => t('Date'),
  691. 'description' => t('Presents month, day, and year fields.'),
  692. 'features' => array(
  693. 'conditional' => FALSE,
  694. ),
  695. 'file' => 'components/date.inc',
  696. ),
  697. 'email' => array(
  698. 'label' => t('E-mail'),
  699. 'description' => t('A special textfield that accepts e-mail addresses.'),
  700. 'file' => 'components/email.inc',
  701. 'features' => array(
  702. 'email_address' => TRUE,
  703. 'spam_analysis' => TRUE,
  704. ),
  705. ),
  706. 'fieldset' => array(
  707. 'label' => t('Fieldset'),
  708. 'description' => t('Fieldsets allow you to organize multiple fields into groups.'),
  709. 'features' => array(
  710. 'csv' => FALSE,
  711. 'default_value' => FALSE,
  712. 'required' => FALSE,
  713. 'conditional' => FALSE,
  714. 'group' => TRUE,
  715. 'title_inline' => FALSE,
  716. ),
  717. 'file' => 'components/fieldset.inc',
  718. ),
  719. 'grid' => array(
  720. 'label' => t('Grid'),
  721. 'description' => t('Allows creation of grid questions, denoted by radio buttons.'),
  722. 'features' => array(
  723. 'conditional' => FALSE,
  724. 'default_value' => FALSE,
  725. 'title_inline' => FALSE,
  726. ),
  727. 'file' => 'components/grid.inc',
  728. ),
  729. 'hidden' => array(
  730. 'label' => t('Hidden'),
  731. 'description' => t('A field which is not visible to the user, but is recorded with the submission.'),
  732. 'file' => 'components/hidden.inc',
  733. 'features' => array(
  734. 'required' => FALSE,
  735. 'description' => FALSE,
  736. 'email_address' => TRUE,
  737. 'email_name' => TRUE,
  738. 'title_display' => FALSE,
  739. 'private' => FALSE,
  740. ),
  741. ),
  742. 'markup' => array(
  743. 'label' => t('Markup'),
  744. 'description' => t('Displays text as HTML in the form; does not render a field.'),
  745. 'features' => array(
  746. 'csv' => FALSE,
  747. 'default_value' => FALSE,
  748. 'description' => FALSE,
  749. 'email' => FALSE,
  750. 'required' => FALSE,
  751. 'conditional' => FALSE,
  752. 'title_display' => FALSE,
  753. 'private' => FALSE,
  754. ),
  755. 'file' => 'components/markup.inc',
  756. ),
  757. 'number' => array(
  758. 'label' => t('Number'),
  759. 'description' => t('A numeric input field (either as textfield or select list).'),
  760. 'features' => array(
  761. ),
  762. 'file' => 'components/number.inc',
  763. ),
  764. 'pagebreak' => array(
  765. 'label' => t('Page break'),
  766. 'description' => t('Organize forms into multiple pages.'),
  767. 'features' => array(
  768. 'csv' => FALSE,
  769. 'default_value' => FALSE,
  770. 'description' => FALSE,
  771. 'private' => FALSE,
  772. 'required' => FALSE,
  773. 'title_display' => FALSE,
  774. ),
  775. 'file' => 'components/pagebreak.inc',
  776. ),
  777. 'select' => array(
  778. 'label' => t('Select options'),
  779. 'description' => t('Allows creation of checkboxes, radio buttons, or select menus.'),
  780. 'file' => 'components/select.inc',
  781. 'features' => array(
  782. 'default_value' => FALSE,
  783. 'email_address' => TRUE,
  784. 'email_name' => TRUE,
  785. ),
  786. ),
  787. 'textarea' => array(
  788. 'label' => t('Textarea'),
  789. 'description' => t('A large text area that allows for multiple lines of input.'),
  790. 'file' => 'components/textarea.inc',
  791. 'features' => array(
  792. 'spam_analysis' => TRUE,
  793. 'title_inline' => FALSE,
  794. ),
  795. ),
  796. 'textfield' => array(
  797. 'label' => t('Textfield'),
  798. 'description' => t('Basic textfield type.'),
  799. 'file' => 'components/textfield.inc',
  800. 'features' => array(
  801. 'email_name' => TRUE,
  802. 'spam_analysis' => TRUE,
  803. ),
  804. ),
  805. 'time' => array(
  806. 'label' => t('Time'),
  807. 'description' => t('Presents the user with hour and minute fields. Optional am/pm fields.'),
  808. 'features' => array(
  809. 'conditional' => FALSE,
  810. ),
  811. 'file' => 'components/time.inc',
  812. ),
  813. );
  814. if (module_exists('file')) {
  815. $component_info['file'] = array(
  816. 'label' => t('File'),
  817. 'description' => t('Allow users to upload files of configurable types.'),
  818. 'features' => array(
  819. 'conditional' => FALSE,
  820. 'default_value' => FALSE,
  821. 'attachment' => TRUE,
  822. ),
  823. 'file' => 'components/file.inc',
  824. );
  825. }
  826. return $component_info;
  827. }
  828. /**
  829. * Implements hook_forms().
  830. *
  831. * All webform_client_form forms share the same form handler
  832. */
  833. function webform_forms($form_id) {
  834. $forms = array();
  835. if (strpos($form_id, 'webform_client_form_') === 0) {
  836. $forms[$form_id]['callback'] = 'webform_client_form';
  837. }
  838. return $forms;
  839. }
  840. /**
  841. * Implements hook_webform_select_options_info().
  842. */
  843. function webform_webform_select_options_info() {
  844. module_load_include('inc', 'webform', 'includes/webform.options');
  845. return _webform_options_info();
  846. }
  847. /**
  848. * Implements hook_webform_webform_submission_actions().
  849. */
  850. function webform_webform_submission_actions($node, $submission) {
  851. $actions = array();
  852. $destination = drupal_get_destination();
  853. if (module_exists('print_pdf') && user_access('access PDF version')) {
  854. $actions['printpdf'] = array(
  855. 'title' => t('Download PDF'),
  856. 'href' => 'printpdf/' . $node->nid . '/submission/' . $submission->sid,
  857. 'query' => $destination,
  858. );
  859. }
  860. if (module_exists('print') && user_access('access print')) {
  861. $actions['print'] = array(
  862. 'title' => t('Print'),
  863. 'href' => 'print/' . $node->nid . '/submission/' . $submission->sid,
  864. );
  865. }
  866. if (webform_results_access($node) && count($node->webform['emails'])) {
  867. $actions['resend'] = array(
  868. 'title' => t('Resend e-mails'),
  869. 'href' => 'node/' . $node->nid . '/submission/' . $submission->sid . '/resend',
  870. 'query' => drupal_get_destination(),
  871. );
  872. }
  873. return $actions;
  874. }
  875. /**
  876. * Implements hook_webform_submission_update().
  877. *
  878. * We implement our own hook here to facilitate the File component, which needs
  879. * to clean up manage file usage records and delete files from submissions that
  880. * have been edited if necessary.
  881. */
  882. function webform_webform_submission_presave($node, &$submission) {
  883. // Check if there are any file components in this submission and if any of
  884. // them currently contain files.
  885. $has_file_components = FALSE;
  886. $new_fids = array();
  887. $old_fids = array();
  888. foreach ($node->webform['components'] as $cid => $component) {
  889. if ($component['type'] == 'file') {
  890. $has_file_components = TRUE;
  891. if (!empty($submission->data[$cid]['value'])) {
  892. foreach ($submission->data[$cid]['value'] as $key => $value) {
  893. if (empty($value)) {
  894. unset($submission->data[$cid]['value'][$key]);
  895. }
  896. }
  897. $new_fids = array_merge($new_fids, $submission->data[$cid]['value']);
  898. }
  899. }
  900. }
  901. if ($has_file_components) {
  902. // If we're updating a submission, build a list of previous files.
  903. if (isset($submission->sid)) {
  904. $old_submission = webform_get_submission($node->nid, $submission->sid, TRUE);
  905. foreach ($node->webform['components'] as $cid => $component) {
  906. if ($component['type'] == 'file') {
  907. if (!empty($old_submission->data[$cid]['value'])) {
  908. $old_fids = array_merge($old_fids, $old_submission->data[$cid]['value']);
  909. }
  910. }
  911. }
  912. }
  913. // Save the list of added or removed files so we can add usage in
  914. // hook_webform_submission_insert() or _update().
  915. $submission->file_usage = array(
  916. // Diff the old against new to determine what files were deleted.
  917. 'deleted_fids' => array_diff($old_fids, $new_fids),
  918. // Diff the new files against old to determine new uploads.
  919. 'added_fids' => array_diff($new_fids, $old_fids)
  920. );
  921. }
  922. }
  923. /**
  924. * Implements hook_webform_submission_insert().
  925. */
  926. function webform_webform_submission_insert($node, $submission) {
  927. if (isset($submission->file_usage)) {
  928. webform_component_include('file');
  929. webform_file_usage_adjust($submission);
  930. }
  931. }
  932. /**
  933. * Implements hook_webform_submission_update().
  934. */
  935. function webform_webform_submission_update($node, $submission) {
  936. if (isset($submission->file_usage)) {
  937. webform_component_include('file');
  938. webform_file_usage_adjust($submission);
  939. }
  940. }
  941. /**
  942. * Implements hook_webform_submission_render_alter().
  943. */
  944. function webform_webform_submission_render_alter(&$renderable) {
  945. // If displaying a submission to end-users who are viewing their own
  946. // submissions (and not through an e-mail), do not show hidden values.
  947. // This needs to be implemented at the level of the entire submission, since
  948. // individual components do not get contextual information about where they
  949. // are being displayed.
  950. $node = $renderable['#node'];
  951. $is_admin = webform_results_access($node);
  952. module_load_include('inc', 'webform', 'includes/webform.components');
  953. if (empty($renderable['#email']) && !$is_admin) {
  954. // Find and hide the display of all hidden components.
  955. foreach ($node->webform['components'] as $cid => $component) {
  956. if ($component['type'] == 'hidden') {
  957. $parents = webform_component_parent_keys($node, $component);
  958. $element = &$renderable;
  959. foreach ($parents as $pid) {
  960. $element = &$element[$pid];
  961. }
  962. $element['#access'] = FALSE;
  963. }
  964. }
  965. }
  966. }
  967. /**
  968. * Implements hook_file_download().
  969. *
  970. * Only allow users with view webform submissions to download files.
  971. */
  972. function webform_file_download($uri) {
  973. module_load_include('inc', 'webform', 'includes/webform.submissions');
  974. // Determine whether this file was a webform upload.
  975. $row = db_query("SELECT fu.id as sid, f.fid FROM {file_managed} f LEFT JOIN {file_usage} fu ON f.fid = fu.fid AND fu.module = :webform AND fu.type = :submission WHERE f.uri = :uri", array('uri' => $uri, ':webform' => 'webform', ':submission' => 'submission'))->fetchObject();
  976. if ($row) {
  977. $file = file_load($row->fid);
  978. }
  979. if (!empty($row->sid)) {
  980. $submissions = webform_get_submissions(array('sid' => $row->sid));
  981. $submission = reset($submissions);
  982. }
  983. // Grant access based on access to the submission.
  984. if (!empty($submission)) {
  985. $node = node_load($submission->nid);
  986. if (webform_submission_access($node, $submission)) {
  987. return file_get_content_headers($file);
  988. }
  989. }
  990. // Grant access to files uploaded by a user before the submission is saved.
  991. elseif (!empty($file) && !empty($_SESSION['webform_files'][$file->fid])) {
  992. return file_get_content_headers($file);
  993. }
  994. }
  995. /**
  996. * Implements hook_node_type().
  997. *
  998. * Not a real hook in Drupal 7. Re-used for consistency with the D6 version.
  999. */
  1000. function webform_node_type($op, $info) {
  1001. $webform_types = webform_variable_get('webform_node_types');
  1002. $affected_type = isset($info->old_type) ? $info->old_type : $info->type;
  1003. $key = array_search($affected_type, $webform_types);
  1004. if ($key !== FALSE) {
  1005. if ($op == 'update') {
  1006. $webform_types[$key] = $info->type;
  1007. }
  1008. if ($op == 'delete') {
  1009. unset($webform_types[$key]);
  1010. }
  1011. variable_set('webform_node_types', $webform_types);
  1012. }
  1013. }
  1014. /**
  1015. * Implements hook_node_type_update().
  1016. */
  1017. function webform_node_type_update($info) {
  1018. webform_node_type('update', $info);
  1019. }
  1020. /**
  1021. * Implements hook_node_type_delete().
  1022. */
  1023. function webform_node_type_delete($info) {
  1024. webform_node_type('delete', $info);
  1025. }
  1026. /**
  1027. * Implements hook_node_insert().
  1028. */
  1029. function webform_node_insert($node) {
  1030. if (!in_array($node->type, webform_variable_get('webform_node_types'))) {
  1031. return;
  1032. }
  1033. // If added directly through node_save(), set defaults for the node.
  1034. if (!isset($node->webform)) {
  1035. $node->webform = webform_node_defaults();
  1036. }
  1037. // Do not make an entry if this node does not have any Webform settings.
  1038. if ($node->webform == webform_node_defaults() && !in_array($node->type, webform_variable_get('webform_node_types_primary'))) {
  1039. return;
  1040. }
  1041. module_load_include('inc', 'webform', 'includes/webform.components');
  1042. module_load_include('inc', 'webform', 'includes/webform.emails');
  1043. // Insert the webform.
  1044. $node->webform['nid'] = $node->nid;
  1045. $node->webform['record_exists'] = (bool) drupal_write_record('webform', $node->webform);
  1046. // Insert the components into the database. Used with clone.module.
  1047. if (isset($node->webform['components']) && !empty($node->webform['components'])) {
  1048. foreach ($node->webform['components'] as $cid => $component) {
  1049. $component['nid'] = $node->nid; // Required for clone.module.
  1050. webform_component_insert($component);
  1051. }
  1052. }
  1053. // Insert emails. Also used with clone.module.
  1054. if (isset($node->webform['emails']) && !empty($node->webform['emails'])) {
  1055. foreach ($node->webform['emails'] as $eid => $email) {
  1056. $email['nid'] = $node->nid;
  1057. webform_email_insert($email);
  1058. }
  1059. }
  1060. // Set the per-role submission access control.
  1061. foreach (array_filter($node->webform['roles']) as $rid) {
  1062. db_insert('webform_roles')->fields(array('nid' => $node->nid, 'rid' => $rid))->execute();
  1063. }
  1064. // Flush the block cache if creating a block.
  1065. if ($node->webform['block']) {
  1066. block_flush_caches();
  1067. }
  1068. }
  1069. /**
  1070. * Implements hook_node_update().
  1071. */
  1072. function webform_node_update($node) {
  1073. if (!in_array($node->type, webform_variable_get('webform_node_types'))) {
  1074. return;
  1075. }
  1076. // Check if this node needs a webform record at all. If it matches the
  1077. // defaults, any existing record will be deleted.
  1078. webform_check_record($node);
  1079. // If a webform row doesn't even exist, we can assume it needs to be inserted.
  1080. // If the the webform matches the defaults, no row will be inserted.
  1081. if (!$node->webform['record_exists']) {
  1082. webform_node_insert($node);
  1083. return;
  1084. }
  1085. // Update the webform entry.
  1086. $node->webform['nid'] = $node->nid;
  1087. drupal_write_record('webform', $node->webform, array('nid'));
  1088. // Compare the webform components and don't do anything if it's not needed.
  1089. // The internal cache needs to be reset here so that the cached node entity
  1090. // does not get loaded and invalidate the comparisons.
  1091. $original = node_load($node->nid, NULL, TRUE);
  1092. if ($original->webform['components'] != $node->webform['components']) {
  1093. module_load_include('inc', 'webform', 'includes/webform.components');
  1094. $original_cids = array_keys($original->webform['components']);
  1095. $current_cids = array_keys($node->webform['components']);
  1096. $all_cids = array_unique(array_merge($original_cids, $current_cids));
  1097. $deleted_cids = array_diff($original_cids, $current_cids);
  1098. $inserted_cids = array_diff($current_cids, $original_cids);
  1099. foreach ($all_cids as $cid) {
  1100. if (in_array($cid, $inserted_cids)) {
  1101. webform_component_insert($node->webform['components'][$cid]);
  1102. }
  1103. elseif (in_array($cid, $deleted_cids)) {
  1104. webform_component_delete($node, $original->webform['components'][$cid]);
  1105. }
  1106. elseif ($node->webform['components'][$cid] != $original->webform['components'][$cid]) {
  1107. $node->webform['components'][$cid]['nid'] = $node->nid;
  1108. webform_component_update($node->webform['components'][$cid]);
  1109. }
  1110. }
  1111. }
  1112. // Compare the webform e-mails and don't do anything if it's not needed.
  1113. if ($original->webform['emails'] != $node->webform['emails']) {
  1114. module_load_include('inc', 'webform', 'includes/webform.emails');
  1115. $original_eids = array_keys($original->webform['emails']);
  1116. $current_eids = array_keys($node->webform['emails']);
  1117. $all_eids = array_unique(array_merge($original_eids, $current_eids));
  1118. $deleted_eids = array_diff($original_eids, $current_eids);
  1119. $inserted_eids = array_diff($current_eids, $original_eids);
  1120. foreach ($all_eids as $eid) {
  1121. if (in_array($eid, $inserted_eids)) {
  1122. webform_email_insert($node->webform['emails'][$eid]);
  1123. }
  1124. elseif (in_array($eid, $deleted_eids)) {
  1125. webform_email_delete($node, $original->webform['emails'][$eid]);
  1126. }
  1127. elseif ($node->webform['emails'][$eid] != $original->webform['emails'][$eid]) {
  1128. $node->webform['emails'][$eid]['nid'] = $node->nid;
  1129. webform_email_update($node->webform['emails'][$eid]);
  1130. }
  1131. }
  1132. }
  1133. // Just delete and re-insert roles if they've changed.
  1134. if ($original->webform['roles'] != $node->webform['roles']) {
  1135. db_delete('webform_roles')->condition('nid', $node->nid)->execute();
  1136. foreach (array_filter($node->webform['roles']) as $rid) {
  1137. db_insert('webform_roles')->fields(array('nid' => $node->nid, 'rid' => $rid))->execute();
  1138. }
  1139. }
  1140. // Flush the block cache if block settings have been changed.
  1141. if ($node->webform['block'] != $original->webform['block']) {
  1142. block_flush_caches();
  1143. }
  1144. }
  1145. /**
  1146. * Implements hook_node_delete().
  1147. */
  1148. function webform_node_delete($node) {
  1149. if (!in_array($node->type, webform_variable_get('webform_node_types'))) {
  1150. return;
  1151. }
  1152. // Allow components clean up extra data, such as uploaded files.
  1153. module_load_include('inc', 'webform', 'includes/webform.components');
  1154. foreach ($node->webform['components'] as $cid => $component) {
  1155. webform_component_delete($node, $component);
  1156. }
  1157. // Remove any trace of webform data from the database.
  1158. db_delete('webform')->condition('nid', $node->nid)->execute();
  1159. db_delete('webform_component')->condition('nid', $node->nid)->execute();
  1160. db_delete('webform_emails')->condition('nid', $node->nid)->execute();
  1161. db_delete('webform_roles')->condition('nid', $node->nid)->execute();
  1162. db_delete('webform_submissions')->condition('nid', $node->nid)->execute();
  1163. db_delete('webform_submitted_data')->condition('nid', $node->nid)->execute();
  1164. db_delete('webform_last_download')->condition('nid', $node->nid)->execute();
  1165. }
  1166. /**
  1167. * Default settings for a newly created webform node.
  1168. */
  1169. function webform_node_defaults() {
  1170. $defaults = array(
  1171. 'confirmation' => '',
  1172. 'confirmation_format' => NULL,
  1173. 'redirect_url' => '<confirmation>',
  1174. 'teaser' => '0',
  1175. 'block' => '0',
  1176. 'allow_draft' => '0',
  1177. 'auto_save' => '0',
  1178. 'submit_notice' => '1',
  1179. 'submit_text' => '',
  1180. 'submit_limit' => '-1',
  1181. 'submit_interval' => '-1',
  1182. 'total_submit_limit' => '-1',
  1183. 'total_submit_interval' => '-1',
  1184. 'status' => '1',
  1185. 'record_exists' => FALSE,
  1186. 'roles' => array('1', '2'),
  1187. 'emails' => array(),
  1188. 'components' => array(),
  1189. );
  1190. drupal_alter('webform_node_defaults', $defaults);
  1191. return $defaults;
  1192. }
  1193. /**
  1194. * Implements hook_node_prepare().
  1195. */
  1196. function webform_node_prepare($node) {
  1197. $webform_types = webform_variable_get('webform_node_types');
  1198. if (in_array($node->type, $webform_types) && !isset($node->webform)) {
  1199. $node->webform = webform_node_defaults();
  1200. }
  1201. }
  1202. /**
  1203. * Implements hook_node_load().
  1204. */
  1205. function webform_node_load($nodes, $types) {
  1206. // Quick check to see if we need to do anything at all for these nodes.
  1207. $webform_types = webform_variable_get('webform_node_types');
  1208. if (count(array_intersect($types, $webform_types)) == 0) {
  1209. return;
  1210. }
  1211. module_load_include('inc', 'webform', 'includes/webform.components');
  1212. // Select all webforms that match these node IDs.
  1213. $result = db_select('webform')
  1214. ->fields('webform')
  1215. ->condition('nid', array_keys($nodes), 'IN')
  1216. ->execute()
  1217. ->fetchAllAssoc('nid', PDO::FETCH_ASSOC);
  1218. foreach ($result as $nid => $webform) {
  1219. // Load the basic information for each node.
  1220. $nodes[$nid]->webform = $webform;
  1221. $nodes[$nid]->webform['record_exists'] = TRUE;
  1222. }
  1223. // Load the components, emails, and defaults for all webform-enabled nodes.
  1224. // TODO: Increase efficiency here by pulling in all information all at once
  1225. // instead of individual queries.
  1226. foreach ($nodes as $nid => $node) {
  1227. if (!in_array($node->type, $webform_types)) {
  1228. continue;
  1229. }
  1230. // If a webform record doesn't exist, just return the defaults.
  1231. if (!isset($nodes[$nid]->webform)) {
  1232. $nodes[$nid]->webform = webform_node_defaults();
  1233. continue;
  1234. }
  1235. $nodes[$nid]->webform['roles'] = db_select('webform_roles')
  1236. ->fields('webform_roles', array('rid'))
  1237. ->condition('nid', $nid)
  1238. ->execute()
  1239. ->fetchCol();
  1240. $nodes[$nid]->webform['emails'] = db_select('webform_emails')
  1241. ->fields('webform_emails')
  1242. ->condition('nid', $nid)
  1243. ->execute()
  1244. ->fetchAllAssoc('eid', PDO::FETCH_ASSOC);
  1245. // Unserialize the exclude component list for e-mails.
  1246. foreach ($nodes[$nid]->webform['emails'] as $eid => $email) {
  1247. $nodes[$nid]->webform['emails'][$eid]['excluded_components'] = array_filter(explode(',', $email['excluded_components']));
  1248. if (variable_get('webform_format_override', 0)) {
  1249. $nodes[$nid]->webform['emails'][$eid]['html'] = variable_get('webform_default_format', 0);
  1250. }
  1251. }
  1252. // Load components for each node.
  1253. $nodes[$nid]->webform['components'] = db_select('webform_component')
  1254. ->fields('webform_component')
  1255. ->condition('nid', $nid)
  1256. ->orderBy('weight')
  1257. ->orderBy('name')
  1258. ->execute()
  1259. ->fetchAllAssoc('cid', PDO::FETCH_ASSOC);
  1260. // Do a little cleanup on each component.
  1261. foreach ($nodes[$nid]->webform['components'] as $cid => $component) {
  1262. $nodes[$nid]->webform['components'][$cid]['nid'] = $nid;
  1263. $nodes[$nid]->webform['components'][$cid]['extra'] = unserialize($component['extra']);
  1264. webform_component_defaults($nodes[$nid]->webform['components'][$cid]);
  1265. }
  1266. // Organize the components into a fieldset-based order.
  1267. if (!empty($nodes[$nid]->webform['components'])) {
  1268. $component_tree = array();
  1269. $page_count = 1;
  1270. _webform_components_tree_build($nodes[$nid]->webform['components'], $component_tree, 0, $page_count);
  1271. $nodes[$nid]->webform['components'] = _webform_components_tree_flatten($component_tree['children']);
  1272. }
  1273. }
  1274. }
  1275. /**
  1276. * Implements hook_form_alter().
  1277. */
  1278. function webform_form_alter(&$form, $form_state, $form_id) {
  1279. $matches = array();
  1280. if (isset($form['#node']->type) && $form_id == $form['#node']->type . '_node_form' && in_array($form['#node']->type, webform_variable_get('webform_node_types'))) {
  1281. $node = $form['#node'];
  1282. // Preserve all Webform options currently set on the node.
  1283. $form['webform'] = array(
  1284. '#type' => 'value',
  1285. '#value' => $node->webform,
  1286. );
  1287. // If a new node, redirect the user to the components form after save.
  1288. if (empty($node->nid) && in_array($node->type, webform_variable_get('webform_node_types_primary'))) {
  1289. $form['actions']['submit']['#submit'][] = 'webform_form_submit';
  1290. }
  1291. }
  1292. }
  1293. /**
  1294. * Submit handler for the webform node form.
  1295. *
  1296. * Redirect the user to the components form on new node inserts. Note that this
  1297. * fires after the hook_submit() function above.
  1298. */
  1299. function webform_form_submit($form, &$form_state) {
  1300. drupal_set_message(t('The new webform %title has been created. Add new fields to your webform with the form below.', array('%title' => $form_state['values']['title'])));
  1301. $form_state['redirect'] = 'node/' . $form_state['nid'] . '/webform/components';
  1302. }
  1303. /**
  1304. * Implements hook_node_view().
  1305. */
  1306. function webform_node_view($node, $view_mode) {
  1307. global $user;
  1308. if (!in_array($node->type, webform_variable_get('webform_node_types'))) {
  1309. return;
  1310. }
  1311. // Set teaser and page variables a la Drupal 6.
  1312. $teaser = $view_mode == 'teaser';
  1313. $page = arg(0) == 'node' && arg(1) == $node->nid;
  1314. // If empty, a teaser, or a new node (during preview) do not display.
  1315. if (empty($node->webform['components']) || ($teaser && !$node->webform['teaser']) || empty($node->nid)) {
  1316. return;
  1317. }
  1318. // Do not include the form in the search index if indexing is disabled.
  1319. if (module_exists('search') && $view_mode == 'search_index' && !variable_get('webform_search_index', 1)) {
  1320. return;
  1321. }
  1322. // If the webform is not set to display in this view mode, return early.
  1323. // View mode of 'form' is exempted to allow blocks and views to force display.
  1324. $extra_fields = field_extra_fields_get_display('node', $node->type, $view_mode);
  1325. if ($view_mode != 'form' && empty($extra_fields['webform']['visible'])) {
  1326. return;
  1327. }
  1328. $info = array();
  1329. $submission = array();
  1330. $submission_count = 0;
  1331. $enabled = TRUE;
  1332. $logging_in = FALSE;
  1333. $total_limit_exceeded = FALSE;
  1334. $user_limit_exceeded = FALSE;
  1335. $closed = FALSE;
  1336. $allowed_roles = array();
  1337. // If a teaser, tell the form to load subsequent pages on the node page.
  1338. if ($teaser && !isset($node->webform['action'])) {
  1339. $query = array_diff_key($_GET, array('q' => ''));
  1340. $node->webform['action'] = url('node/' . $node->nid, array('query' => $query));
  1341. }
  1342. // When logging in using a form on the same page as a webform node, suppress
  1343. // output messages so that they don't show up after the user has logged in.
  1344. // See http://drupal.org/node/239343.
  1345. if (isset($_POST['op']) && isset($_POST['name']) && isset($_POST['pass'])) {
  1346. $logging_in = TRUE;
  1347. }
  1348. if ($node->webform['status'] == 0) {
  1349. $closed = TRUE;
  1350. $enabled = FALSE;
  1351. }
  1352. else {
  1353. // Check if the user's role can submit this webform.
  1354. if (variable_get('webform_submission_access_control', 1)) {
  1355. foreach ($node->webform['roles'] as $rid) {
  1356. $allowed_roles[$rid] = isset($user->roles[$rid]) ? TRUE : FALSE;
  1357. }
  1358. if (array_search(TRUE, $allowed_roles) === FALSE && $user->uid != 1) {
  1359. $enabled = FALSE;
  1360. }
  1361. }
  1362. else {
  1363. // If not using Webform submission access control, allow for all roles.
  1364. $allowed_roles = array_keys(user_roles());
  1365. }
  1366. }
  1367. // Get a count of previous submissions by this user. Note that the
  1368. // webform_submission_access() function may disable the page cache for
  1369. // anonymous users if they are allowed to edit their own submissions!
  1370. if ($page && webform_submission_access($node, NULL, 'list')) {
  1371. module_load_include('inc', 'webform', 'includes/webform.submissions');
  1372. $submission_count = webform_get_submission_count($node->nid, $user->uid);
  1373. }
  1374. // Check if this page is cached or not.
  1375. $cached = $user->uid == 0 && (variable_get('cache', 0) || drupal_page_is_cacheable() === FALSE);
  1376. // Check if the user can add another submission.
  1377. if ($node->webform['submit_limit'] != -1) { // -1: Submissions are never throttled.
  1378. module_load_include('inc', 'webform', 'includes/webform.submissions');
  1379. // Disable the form if the limit is exceeded and page cache is not active.
  1380. if (($user_limit_exceeded = _webform_submission_user_limit_check($node)) && !$cached) {
  1381. $enabled = FALSE;
  1382. }
  1383. }
  1384. // Check if the user can add another submission if there is a limit on total
  1385. // submissions.
  1386. if ($node->webform['total_submit_limit'] != -1) { // -1: Submissions are never throttled.
  1387. module_load_include('inc', 'webform', 'includes/webform.submissions');
  1388. // Disable the form if the limit is exceeded and page cache is not active.
  1389. if (($total_limit_exceeded = _webform_submission_total_limit_check($node)) && !$cached) {
  1390. $enabled = FALSE;
  1391. }
  1392. }
  1393. // Check if this user has a draft for this webform.
  1394. $is_draft = FALSE;
  1395. if (($node->webform['allow_draft'] || $node->webform['auto_save']) && $user->uid != 0) {
  1396. // Draft found - display form with draft data for further editing.
  1397. if ($draft_sid = _webform_fetch_draft_sid($node->nid, $user->uid)) {
  1398. module_load_include('inc', 'webform', 'includes/webform.submissions');
  1399. $submission = webform_get_submission($node->nid, $draft_sid);
  1400. $enabled = TRUE;
  1401. $is_draft = TRUE;
  1402. }
  1403. }
  1404. // Avoid building the same form twice on the same page request (which can
  1405. // happen if the webform is displayed in a panel or block) because this
  1406. // causes multistep forms to build incorrectly the second time.
  1407. $cached_forms = &drupal_static(__FUNCTION__, array());
  1408. if (isset($cached_forms[$node->nid])) {
  1409. $form = $cached_forms[$node->nid];
  1410. }
  1411. // If this is the first time, generate the form array.
  1412. else {
  1413. $form = drupal_get_form('webform_client_form_' . $node->nid, $node, $submission, $is_draft);
  1414. $cached_forms[$node->nid] = $form;
  1415. }
  1416. // Remove the surrounding <form> tag if this is a preview.
  1417. if (!empty($node->in_preview)) {
  1418. $form['#type'] = 'markup';
  1419. }
  1420. // Print out messages for the webform.
  1421. if (empty($node->in_preview) && !isset($node->webform_block) && !$logging_in) {
  1422. theme('webform_view_messages', array('node' => $node, 'teaser' => $teaser, 'page' => $page, 'submission_count' => $submission_count, 'user_limit_exceeded' => $user_limit_exceeded, 'total_limit_exceeded' => $total_limit_exceeded, 'allowed_roles' => $allowed_roles, 'closed' => $closed, 'cached' => $cached));
  1423. }
  1424. // Add the output to the node.
  1425. $node->content['webform'] = array(
  1426. '#theme' => 'webform_view',
  1427. '#node' => $node,
  1428. '#teaser' => $teaser,
  1429. '#page' => $page,
  1430. '#form' => $form,
  1431. '#enabled' => $enabled,
  1432. '#weight' => 10,
  1433. );
  1434. }
  1435. /**
  1436. * Output the Webform into the node content.
  1437. *
  1438. * @param $node
  1439. * The webform node object.
  1440. * @param $teaser
  1441. * If this webform is being displayed as the teaser view of the node.
  1442. * @param $page
  1443. * If this webform node is being viewed as the main content of the page.
  1444. * @param $form
  1445. * The rendered form.
  1446. * @param $enabled
  1447. * If the form allowed to be completed by the current user.
  1448. */
  1449. function theme_webform_view($variables) {
  1450. // Only show the form if this user is allowed access.
  1451. if ($variables['webform']['#enabled']) {
  1452. return drupal_render($variables['webform']['#form']);
  1453. }
  1454. }
  1455. /**
  1456. * Display a message to a user if they are not allowed to fill out a form.
  1457. *
  1458. * @param $node
  1459. * The webform node object.
  1460. * @param $teaser
  1461. * If this webform is being displayed as the teaser view of the node.
  1462. * @param $page
  1463. * If this webform node is being viewed as the main content of the page.
  1464. * @param $submission_count
  1465. * The number of submissions this user has already submitted. Not calculated
  1466. * for anonymous users.
  1467. * @param $user_limit_exceeded
  1468. * Boolean value if the submission limit for this user has been exceeded.
  1469. * @param $total_limit_exceeded
  1470. * Boolean value if the total submission limit has been exceeded.
  1471. * @param $allowed_roles
  1472. * A list of user roles that are allowed to submit this webform.
  1473. * @param $closed
  1474. * Boolean value if submissions are closed.
  1475. */
  1476. function theme_webform_view_messages($variables) {
  1477. global $user;
  1478. $node = $variables['node'];
  1479. $teaser = $variables['teaser'];
  1480. $page = $variables['page'];
  1481. $submission_count = $variables['submission_count'];
  1482. $user_limit_exceeded = $variables['user_limit_exceeded'];
  1483. $total_limit_exceeded = $variables['total_limit_exceeded'];
  1484. $allowed_roles = $variables['allowed_roles'];
  1485. $closed = $variables['closed'];
  1486. $cached = $variables['cached'];
  1487. $type = 'status';
  1488. if ($closed) {
  1489. $message = t('Submissions for this form are closed.');
  1490. }
  1491. // If open and not allowed to submit the form, give an explanation.
  1492. elseif (array_search(TRUE, $allowed_roles) === FALSE && $user->uid != 1) {
  1493. if (empty($allowed_roles)) {
  1494. // No roles are allowed to submit the form.
  1495. $message = t('Submissions for this form are closed.');
  1496. }
  1497. elseif (isset($allowed_roles[2])) {
  1498. // The "authenticated user" role is allowed to submit and the user is currently logged-out.
  1499. $login = url('user/login', array('query' => drupal_get_destination()));
  1500. $register = url('user/register', array('query' => drupal_get_destination()));
  1501. if (variable_get('user_register', 1) == 0) {
  1502. $message = t('You must <a href="!login">login</a> to view this form.', array('!login' => $login));
  1503. }
  1504. else {
  1505. $message = t('You must <a href="!login">login</a> or <a href="!register">register</a> to view this form.', array('!login' => $login, '!register' => $register));
  1506. }
  1507. }
  1508. else {
  1509. // The user must be some other role to submit.
  1510. $message = t('You do not have permission to view this form.');
  1511. $type = 'error';
  1512. }
  1513. }
  1514. // If the user has exceeded the limit of submissions, explain the limit.
  1515. elseif ($user_limit_exceeded && !$cached) {
  1516. if ($node->webform['submit_interval'] == -1 && $node->webform['submit_limit'] > 1) {
  1517. $message = t('You have submitted this form the maximum number of times (@count).', array('@count' => $node->webform['submit_limit']));
  1518. }
  1519. elseif ($node->webform['submit_interval'] == -1 && $node->webform['submit_limit'] == 1) {
  1520. $message = t('You have already submitted this form.');
  1521. }
  1522. else {
  1523. $message = t('You may not submit another entry at this time.');
  1524. }
  1525. $type = 'error';
  1526. }
  1527. elseif ($total_limit_exceeded && !$cached) {
  1528. if ($node->webform['total_submit_interval'] == -1 && $node->webform['total_submit_limit'] > 1) {
  1529. $message = t('This form has received the maximum number of entries.');
  1530. }
  1531. else {
  1532. $message = t('You may not submit another entry at this time.');
  1533. }
  1534. }
  1535. // If the user has submitted before, give them a link to their submissions.
  1536. if ($submission_count > 0 && $node->webform['submit_notice'] == 1 && !$cached) {
  1537. if (empty($message)) {
  1538. $message = t('You have already submitted this form.') . ' ' . t('<a href="!url">View your previous submissions</a>.', array('!url' => url('node/' . $node->nid . '/submissions')));
  1539. }
  1540. else {
  1541. $message .= ' ' . t('<a href="!url">View your previous submissions</a>.', array('!url' => url('node/' . $node->nid . '/submissions')));
  1542. }
  1543. }
  1544. if ($page && isset($message)) {
  1545. drupal_set_message($message, $type, FALSE);
  1546. }
  1547. }
  1548. /**
  1549. * Implements hook_mail().
  1550. */
  1551. function webform_mail($key, &$message, $params) {
  1552. $message['headers'] = array_merge($message['headers'], $params['headers']);
  1553. $message['subject'] = $params['subject'];
  1554. $message['body'][] = $params['message'];
  1555. }
  1556. /**
  1557. * Implements hook_block_info().
  1558. */
  1559. function webform_block_info() {
  1560. $blocks = array();
  1561. $webform_node_types = webform_variable_get('webform_node_types');
  1562. if (!empty($webform_node_types)) {
  1563. $query = db_select('webform', 'w')->fields('w')->fields('n', array('title'));
  1564. $query->leftJoin('node', 'n', 'w.nid = n.nid');
  1565. $query->condition('w.block', 1);
  1566. $query->condition('n.type', $webform_node_types, 'IN');
  1567. $result = $query->execute();
  1568. foreach ($result as $data) {
  1569. $blocks['client-block-' . $data->nid] = array(
  1570. 'info' => t('Webform: !title', array('!title' => $data->title)),
  1571. 'cache' => DRUPAL_NO_CACHE,
  1572. );
  1573. }
  1574. }
  1575. return $blocks;
  1576. }
  1577. /**
  1578. * Implements hook_block_view().
  1579. */
  1580. function webform_block_view($delta = '') {
  1581. global $user;
  1582. // Load the block-specific configuration settings.
  1583. $webform_blocks = variable_get('webform_blocks', array());
  1584. $settings = isset($webform_blocks[$delta]) ? $webform_blocks[$delta] : array();
  1585. $settings += array(
  1586. 'display' => 'form',
  1587. 'pages_block' => 0,
  1588. );
  1589. // Get the node ID from delta.
  1590. $nid = drupal_substr($delta, strrpos($delta, '-') + 1);
  1591. // Load node in current language.
  1592. if (module_exists('translation')) {
  1593. global $language;
  1594. if (($translations = translation_node_get_translations($nid)) && (isset($translations[$language->language]))) {
  1595. $nid = $translations[$language->language]->nid;
  1596. }
  1597. }
  1598. // The webform node to display in the block.
  1599. $node = node_load($nid);
  1600. // Return if user has no access to the webform node.
  1601. if (!node_access('view', $node)) {
  1602. return;
  1603. }
  1604. // This is a webform node block.
  1605. $node->webform_block = TRUE;
  1606. // If not displaying pages in the block, set the #action property on the form.
  1607. if ($settings['pages_block']) {
  1608. $node->webform['action'] = FALSE;
  1609. }
  1610. else {
  1611. $query = array_diff_key($_GET, array('q' => ''));
  1612. $node->webform['action'] = url('node/' . $node->nid, array('query' => $query));
  1613. }
  1614. // Generate the content of the block based on display settings.
  1615. if ($settings['display'] == 'form') {
  1616. webform_node_view($node, 'form');
  1617. $content = isset($node->content['webform']) ? $node->content['webform'] : array();
  1618. }
  1619. else {
  1620. $teaser = ($settings['display'] == 'teaser') ? 'teaser' : 'full';
  1621. $content = node_view($node, $teaser);
  1622. }
  1623. // Add contextual links for the webform node if they aren't already there.
  1624. if (!isset($content['#contextual_links']['node'])) {
  1625. $content['#contextual_links']['node'] = array('node', array($node->nid));
  1626. }
  1627. // Create the block, using the node title for the block title.
  1628. // Note that we render the content immediately here rather than passing back
  1629. // a renderable so that if the block is empty it is hidden.
  1630. $block = array(
  1631. 'subject' => check_plain($node->title),
  1632. 'content' => drupal_render($content),
  1633. );
  1634. return $block;
  1635. }
  1636. /**
  1637. * Implements hook_block_configure().
  1638. */
  1639. function webform_block_configure($delta = '') {
  1640. // Load the block-specific configuration settings.
  1641. $webform_blocks = variable_get('webform_blocks', array());
  1642. $settings = isset($webform_blocks[$delta]) ? $webform_blocks[$delta] : array();
  1643. $settings += array(
  1644. 'display' => 'form',
  1645. 'pages_block' => 0,
  1646. );
  1647. $form = array();
  1648. $form['display'] = array(
  1649. '#type' => 'radios',
  1650. '#title' => t('Display mode'),
  1651. '#default_value' => $settings['display'],
  1652. '#options' => array(
  1653. 'form' => t('Form only'),
  1654. 'full' => t('Full node'),
  1655. 'teaser' => t('Teaser'),
  1656. ),
  1657. '#description' => t('The display mode determines how much of the webform to show within the block.'),
  1658. );
  1659. $form['pages_block'] = array(
  1660. '#type' => 'checkbox',
  1661. '#title' => t('Show all webform pages in block'),
  1662. '#default_value' => $settings['pages_block'],
  1663. '#description' => t('By default multi-page webforms redirect to the node page for all pages after the first one. If checked, all pages will be shown in the block instead.'),
  1664. );
  1665. return $form;
  1666. }
  1667. /**
  1668. * Implements hook_block_save().
  1669. */
  1670. function webform_block_save($delta = '', $edit = array()) {
  1671. // Load the previously defined block-specific configuration settings.
  1672. $settings = variable_get('webform_blocks', array());
  1673. // Build the settings array.
  1674. $new_settings[$delta] = array(
  1675. 'display' => $edit['display'],
  1676. 'pages_block' => $edit['pages_block'],
  1677. );
  1678. // We store settings for multiple blocks in just one variable
  1679. // so we merge the existing settings with the new ones before save.
  1680. variable_set('webform_blocks', array_merge($settings, $new_settings));
  1681. }
  1682. /**
  1683. * Client form generation function. If this is displaying an existing
  1684. * submission, pass in the $submission variable with the contents of the
  1685. * submission to be displayed.
  1686. *
  1687. * @param $form
  1688. * The current form array (always empty).
  1689. * @param $form_state
  1690. * The current form values of a submission, used in multipage webforms.
  1691. * @param $node
  1692. * The current webform node.
  1693. * @param $submission
  1694. * An object containing information about the form submission if we're
  1695. * displaying a result.
  1696. * @param $is_draft
  1697. * Optional. Set to TRUE if displaying a draft.
  1698. * @param $filter
  1699. * Whether or not to filter the contents of descriptions and values when
  1700. * building the form. Values need to be unfiltered to be editable by
  1701. * Form Builder.
  1702. */
  1703. function webform_client_form($form, &$form_state, $node, $submission, $is_draft = FALSE, $filter = TRUE) {
  1704. global $user;
  1705. // Attach necessary JavaScript and CSS.
  1706. $form['#attached'] = array(
  1707. 'css' => array(drupal_get_path('module', 'webform') . '/css/webform.css'),
  1708. 'js' => array(drupal_get_path('module', 'webform') . '/js/webform.js'),
  1709. );
  1710. form_load_include($form_state, 'inc', 'webform', 'includes/webform.components');
  1711. form_load_include($form_state, 'inc', 'webform', 'includes/webform.submissions');
  1712. $form['#process'] = array(
  1713. 'webform_client_form_includes',
  1714. );
  1715. // If in a multi-step form, a submission ID may be specified in form state.
  1716. // Load this submission. This allows anonymous users to use auto-save.
  1717. if (empty($submission) && !empty($form_state['values']['details']['sid'])) {
  1718. $submission = webform_get_submission($node->nid, $form_state['values']['details']['sid']);
  1719. $is_draft = $submission->is_draft;
  1720. }
  1721. // Bind arguments to $form to make them available in theming and form_alter.
  1722. $form['#node'] = $node;
  1723. $form['#submission'] = $submission;
  1724. $form['#is_draft'] = $is_draft;
  1725. $form['#filter'] = $filter;
  1726. // Add a theme function for this form.
  1727. $form['#theme'] = array('webform_form_' . $node->nid, 'webform_form');
  1728. // Add a css class for all client forms.
  1729. $form['#attributes'] = array('class' => array('webform-client-form'));
  1730. // Set the encoding type (necessary for file uploads).
  1731. $form['#attributes']['enctype'] = 'multipart/form-data';
  1732. // Sometimes when displaying a webform as a teaser or block, a custom action
  1733. // property is set to direct the user to the node page.
  1734. if (!empty($node->webform['action'])) {
  1735. $form['#action'] = $node->webform['action'];
  1736. }
  1737. $form['#submit'] = array('webform_client_form_pages', 'webform_client_form_submit');
  1738. $form['#validate'] = array('webform_client_form_validate');
  1739. if (is_array($node->webform['components']) && !empty($node->webform['components'])) {
  1740. // Prepare a new form array.
  1741. $form['submitted'] = array(
  1742. '#tree' => TRUE
  1743. );
  1744. $form['details'] = array(
  1745. '#tree' => TRUE,
  1746. );
  1747. // Put the components into a tree structure.
  1748. if (!isset($form_state['storage']['component_tree'])) {
  1749. $form_state['webform']['component_tree'] = array();
  1750. $form_state['webform']['page_count'] = 1;
  1751. $form_state['webform']['page_num'] = 1;
  1752. _webform_components_tree_build($node->webform['components'], $form_state['webform']['component_tree'], 0, $form_state['webform']['page_count']);
  1753. }
  1754. else {
  1755. $form_state['webform']['component_tree'] = $form_state['storage']['component_tree'];
  1756. $form_state['webform']['page_count'] = $form_state['storage']['page_count'];
  1757. $form_state['webform']['page_num'] = $form_state['storage']['page_num'];
  1758. }
  1759. // Shorten up our variable names.
  1760. $component_tree = $form_state['webform']['component_tree'];
  1761. $page_count = $form_state['webform']['page_count'];
  1762. $page_num = $form_state['webform']['page_num'];
  1763. if ($page_count > 1) {
  1764. $next_page_labels = array();
  1765. $prev_page_labels = array();
  1766. }
  1767. // Recursively add components to the form. The unfiltered version of the
  1768. // form (typically used in Form Builder), includes all components.
  1769. foreach ($component_tree['children'] as $cid => $component) {
  1770. $component_value = isset($form_state['values']['submitted'][$cid]) ? $form_state['values']['submitted'][$cid] : NULL;
  1771. if ($filter == FALSE || _webform_client_form_rule_check($node, $component, $page_num, $form_state)) {
  1772. if ($component['type'] == 'pagebreak') {
  1773. $next_page_labels[$component['page_num'] - 1] = !empty($component['extra']['next_page_label']) ? t($component['extra']['next_page_label']) : t('Next Page >');
  1774. $prev_page_labels[$component['page_num']] = !empty($component['extra']['prev_page_label']) ? t($component['extra']['prev_page_label']) : t('< Previous Page');
  1775. }
  1776. _webform_client_form_add_component($node, $component, $component_value, $form['submitted'], $form, $form_state, $submission, 'form', $page_num, $filter);
  1777. }
  1778. }
  1779. // These form details help managing data upon submission.
  1780. $form['details']['nid'] = array(
  1781. '#type' => 'value',
  1782. '#value' => $node->nid,
  1783. );
  1784. $form['details']['sid'] = array(
  1785. '#type' => 'hidden',
  1786. '#value' => isset($submission->sid) ? $submission->sid : NULL,
  1787. );
  1788. $form['details']['uid'] = array(
  1789. '#type' => 'value',
  1790. '#value' => isset($submission->uid) ? $submission->uid : $user->uid,
  1791. );
  1792. $form['details']['page_num'] = array(
  1793. '#type' => 'hidden',
  1794. '#value' => $page_num,
  1795. );
  1796. $form['details']['page_count'] = array(
  1797. '#type' => 'hidden',
  1798. '#value' => $page_count,
  1799. );
  1800. $form['details']['finished'] = array(
  1801. '#type' => 'hidden',
  1802. '#value' => isset($submission->is_draft) ? (!$submission->is_draft) : 0,
  1803. );
  1804. // Add buttons for pages, drafts, and submissions.
  1805. $form['actions'] = array(
  1806. '#type' => 'actions',
  1807. '#weight' => 1000,
  1808. );
  1809. // Add the draft button.
  1810. if ($node->webform['allow_draft'] && (empty($submission) || $submission->is_draft) && $user->uid != 0) {
  1811. $form['actions']['draft'] = array(
  1812. '#type' => 'submit',
  1813. '#value' => t('Save Draft'),
  1814. '#weight' => -2,
  1815. '#validate' => array(),
  1816. '#attributes' => array('formnovalidate' => 'formnovalidate'),
  1817. );
  1818. }
  1819. if ($page_count > 1) {
  1820. // Add the submit button(s).
  1821. if ($page_num > 1) {
  1822. $form['actions']['previous'] = array(
  1823. '#type' => 'submit',
  1824. '#value' => $prev_page_labels[$page_num],
  1825. '#weight' => 5,
  1826. '#validate' => array(),
  1827. '#attributes' => array('formnovalidate' => 'formnovalidate'),
  1828. );
  1829. }
  1830. if ($page_num == $page_count) {
  1831. $form['actions']['submit'] = array(
  1832. '#type' => 'submit',
  1833. '#value' => empty($node->webform['submit_text']) ? t('Submit') : t($node->webform['submit_text']),
  1834. '#weight' => 10,
  1835. );
  1836. }
  1837. elseif ($page_num < $page_count) {
  1838. $form['actions']['next'] = array(
  1839. '#type' => 'submit',
  1840. '#value' => $next_page_labels[$page_num],
  1841. '#weight' => 10,
  1842. );
  1843. }
  1844. }
  1845. else {
  1846. // Add the submit button.
  1847. $form['actions']['submit'] = array(
  1848. '#type' => 'submit',
  1849. '#value' => empty($node->webform['submit_text']) ? t('Submit') : t($node->webform['submit_text']),
  1850. '#weight' => 10,
  1851. );
  1852. }
  1853. }
  1854. return $form;
  1855. }
  1856. /**
  1857. * Process function for webform_client_form().
  1858. *
  1859. * Include all the enabled components for this form to ensure availability.
  1860. */
  1861. function webform_client_form_includes($form, $form_state) {
  1862. $components = webform_components();
  1863. foreach ($components as $component_type => $component) {
  1864. webform_component_include($component_type);
  1865. }
  1866. return $form;
  1867. }
  1868. /**
  1869. * Check if a component should be displayed on the current page.
  1870. */
  1871. function _webform_client_form_rule_check($node, $component, $page_num, $form_state = NULL, $submission = NULL) {
  1872. $conditional_values = isset($component['extra']['conditional_values']) ? $component['extra']['conditional_values'] : NULL;
  1873. $conditional_component = isset($component['extra']['conditional_component']) && isset($node->webform['components'][$component['extra']['conditional_component']]) ? $node->webform['components'][$component['extra']['conditional_component']] : NULL;
  1874. $conditional_cid = $conditional_component['cid'];
  1875. // Check the rules for this entire page. Note individual page breaks are
  1876. // checked down below in the individual component rule checks.
  1877. $show_page = TRUE;
  1878. if ($component['page_num'] > 1 && $component['type'] != 'pagebreak') {
  1879. foreach ($node->webform['components'] as $cid => $page_component) {
  1880. if ($page_component['type'] == 'pagebreak' && $page_component['page_num'] == $page_num) {
  1881. $show_page = _webform_client_form_rule_check($node, $page_component, $page_num, $form_state, $submission);
  1882. break;
  1883. }
  1884. }
  1885. }
  1886. // Check any parents' visibility rules.
  1887. $show_parent = $show_page;
  1888. if ($show_parent && $component['pid'] && isset($node->webform['components'][$component['pid']])) {
  1889. $parent_component = $node->webform['components'][$component['pid']];
  1890. $show_parent = _webform_client_form_rule_check($node, $parent_component, $page_num, $form_state, $submission);
  1891. }
  1892. // Check the individual component rules.
  1893. $show_component = $show_parent;
  1894. if ($show_component && ($page_num == 0 || $component['page_num'] == $page_num) && $conditional_component && strlen(trim($conditional_values))) {
  1895. $input_values = array();
  1896. if (isset($form_state)) {
  1897. $input_value = isset($form_state['values']['submitted'][$conditional_cid]) ? $form_state['values']['submitted'][$conditional_cid] : NULL;
  1898. $input_values = is_array($input_value) ? $input_value : array($input_value);
  1899. }
  1900. elseif (isset($submission)) {
  1901. $input_values = isset($submission->data[$conditional_cid]['value']) ? $submission->data[$conditional_cid]['value'] : array();
  1902. }
  1903. $test_values = array_map('trim', explode("\n", $conditional_values));
  1904. if (empty($input_values) && !empty($test_values)) {
  1905. $show_component = FALSE;
  1906. }
  1907. else {
  1908. foreach ($input_values as $input_value) {
  1909. if ($show_component = in_array($input_value, $test_values)) {
  1910. break;
  1911. }
  1912. }
  1913. }
  1914. if ($component['extra']['conditional_operator'] == '!=') {
  1915. $show_component = !$show_component;
  1916. }
  1917. }
  1918. return $show_component;
  1919. }
  1920. /**
  1921. * Add a component to a renderable array. Called recursively for fieldsets.
  1922. *
  1923. * This function assists in the building of the client form, as well as the
  1924. * display of results, and the text of e-mails.
  1925. *
  1926. * @param $component
  1927. * The component to be added to the form.
  1928. * @param $component_value
  1929. * The components current value if known.
  1930. * @param $parent_fieldset
  1931. * The fieldset to which this element will be added.
  1932. * @param $form
  1933. * The entire form array.
  1934. * @param $form_state
  1935. * The form state.
  1936. * @param $submission
  1937. * The Webform submission as retrieved from the database.
  1938. * @param $format
  1939. * The format the form should be displayed as. May be one of the following:
  1940. * - form: Show as an editable form.
  1941. * - html: Show as HTML results.
  1942. * - text: Show as plain text.
  1943. * @param $filter
  1944. * Whether the form element properties should be filtered. Only set to FALSE
  1945. * if needing the raw properties for editing.
  1946. *
  1947. * @see webform_client_form()
  1948. * @see webform_submission_render()
  1949. */
  1950. function _webform_client_form_add_component($node, $component, $component_value, &$parent_fieldset, &$form, $form_state, $submission, $format = 'form', $page_num = 0, $filter = TRUE) {
  1951. $cid = $component['cid'];
  1952. $component_access = empty($component['extra']['private']) || webform_results_access($node);
  1953. // Load with submission information if necessary.
  1954. if ($format != 'form') {
  1955. // This component is display only.
  1956. $data = empty($submission->data[$cid]['value']) ? NULL : $submission->data[$cid]['value'];
  1957. if ($display_element = webform_component_invoke($component['type'], 'display', $component, $data, $format)) {
  1958. // Set access based on the private property.
  1959. $element['#access'] = $component_access;
  1960. // Ensure the component is added as a property.
  1961. $display_element['#webform_component'] = $component;
  1962. // Allow modules to modify a "display only" webform component.
  1963. drupal_alter('webform_component_display', $display_element, $component);
  1964. // The form_builder() function usually adds #parents and #id for us, but
  1965. // because these are not marked for #input, we need to add them manually.
  1966. if (!isset($display_element['#parents'])) {
  1967. $parents = isset($parent_fieldset['#parents']) ? $parent_fieldset['#parents'] : array('submitted');
  1968. $parents[] = $component['form_key'];
  1969. $display_element['#parents'] = $parents;
  1970. }
  1971. if (!isset($display_element['#id'])) {
  1972. $display_element['#id'] = drupal_clean_css_identifier('edit-' . implode('-', $display_element['#parents']));
  1973. }
  1974. // Add the element into the proper parent in the display.
  1975. $parent_fieldset[$component['form_key']] = $display_element;
  1976. }
  1977. }
  1978. // Show the component only on its form page, or if building an unfiltered
  1979. // version of the form (such as for Form Builder).
  1980. elseif ($component['page_num'] == $page_num || $filter == FALSE) {
  1981. // Add this user-defined field to the form (with all the values that are always available).
  1982. $data = isset($submission->data[$cid]['value']) ? $submission->data[$cid]['value'] : NULL;
  1983. if ($element = webform_component_invoke($component['type'], 'render', $component, $data, $filter)) {
  1984. // Set access based on the private property.
  1985. $element['#access'] = $component_access;
  1986. // Ensure the component is added as a property.
  1987. $element['#webform_component'] = $component;
  1988. // The 'private' option is in most components, but it's not a real
  1989. // property. Add it for Form Builder compatibility.
  1990. if (webform_component_feature($component['type'], 'private')) {
  1991. $element['#webform_private'] = $component['extra']['private'];
  1992. }
  1993. // Allow modules to modify a webform component that is going to be render in a form.
  1994. drupal_alter('webform_component_render', $element, $component);
  1995. // Add the element into the proper parent in the form.
  1996. $parent_fieldset[$component['form_key']] = $element;
  1997. // Override the value if one already exists in the form state.
  1998. if (isset($component_value)) {
  1999. $parent_fieldset[$component['form_key']]['#default_value'] = $component_value;
  2000. if (is_array($component_value)) {
  2001. foreach ($component_value as $key => $value) {
  2002. if (isset($parent_fieldset[$component['form_key']][$key])) {
  2003. $parent_fieldset[$component['form_key']][$key]['#default_value'] = $value;
  2004. }
  2005. }
  2006. }
  2007. }
  2008. }
  2009. else {
  2010. drupal_set_message(t('The webform component @type is not able to be displayed', array('@type' => $component['type'])));
  2011. }
  2012. }
  2013. // Disable validation initially on all elements. We manually validate
  2014. // all webform elements in webform_client_form_validate().
  2015. if (isset($parent_fieldset[$component['form_key']])) {
  2016. $parent_fieldset[$component['form_key']]['#validated'] = TRUE;
  2017. $parent_fieldset[$component['form_key']]['#webform_validated'] = FALSE;
  2018. }
  2019. if (isset($component['children']) && is_array($component['children'])) {
  2020. foreach ($component['children'] as $scid => $subcomponent) {
  2021. $subcomponent_value = isset($form_state['values']['submitted'][$scid]) ? $form_state['values']['submitted'][$scid] : NULL;
  2022. if (_webform_client_form_rule_check($node, $subcomponent, $page_num, $form_state, $submission)) {
  2023. _webform_client_form_add_component($node, $subcomponent, $subcomponent_value, $parent_fieldset[$component['form_key']], $form, $form_state, $submission, $format, $page_num, $filter);
  2024. }
  2025. }
  2026. }
  2027. }
  2028. function webform_client_form_validate($form, &$form_state) {
  2029. $node = node_load($form_state['values']['details']['nid']);
  2030. $finished = $form_state['values']['details']['finished'];
  2031. // Check that the submissions have not exceeded the total submission limit.
  2032. if ($node->webform['total_submit_limit'] != -1) {
  2033. module_load_include('inc', 'webform', 'includes/webform.submissions');
  2034. // Check if the total number of entries was reached before the user submitted
  2035. // the form.
  2036. if (!$finished && $total_limit_exceeded = _webform_submission_total_limit_check($node)) {
  2037. // Show the user the limit has exceeded.
  2038. theme('webform_view_messages', array('node' => $node, 'teaser' => 0, 'page' => 1, 'submission_count' => 0, 'total_limit_exceeded' => $total_limit_exceeded, 'allowed_roles' => array_keys(user_roles()), 'closed' => FALSE, 'cached' => FALSE));
  2039. form_set_error('', NULL);
  2040. return;
  2041. }
  2042. }
  2043. // Check that the user has not exceeded the submission limit.
  2044. // This usually will only apply to anonymous users when the page cache is
  2045. // enabled, because they may submit the form even if they do not have access.
  2046. if ($node->webform['submit_limit'] != -1) { // -1: Submissions are never throttled.
  2047. module_load_include('inc', 'webform', 'includes/webform.submissions');
  2048. if (!$finished && $user_limit_exceeded = _webform_submission_user_limit_check($node)) {
  2049. // Assume that webform_view_messages will print out the necessary message,
  2050. // then stop the processing of the form with an empty form error.
  2051. theme('webform_view_messages', array('node' => $node, 'teaser' => 0, 'page' => 1, 'submission_count' => 0, 'user_limit_exceeded' => $user_limit_exceeded, 'allowed_roles' => array_keys(user_roles()), 'closed' => FALSE, 'cached' => FALSE));
  2052. form_set_error('', NULL);
  2053. return;
  2054. }
  2055. }
  2056. // Run all #element_validate and #required checks. These are skipped initially
  2057. // by setting #validated = TRUE on all components when they are added.
  2058. _webform_client_form_validate($form, $form_state);
  2059. }
  2060. /**
  2061. * Recursive validation function to trigger normal Drupal validation.
  2062. *
  2063. * This function imitates _form_validate in Drupal's form.inc, only it sets
  2064. * a different property to ensure that validation has occurred.
  2065. */
  2066. function _webform_client_form_validate($elements, &$form_state, $first_run = TRUE) {
  2067. static $form;
  2068. if ($first_run) {
  2069. $form = $elements;
  2070. }
  2071. // Recurse through all children.
  2072. foreach (element_children($elements) as $key) {
  2073. if (isset($elements[$key]) && $elements[$key]) {
  2074. _webform_client_form_validate($elements[$key], $form_state, FALSE);
  2075. }
  2076. }
  2077. // Validate the current input.
  2078. if (isset($elements['#webform_validated']) && $elements['#webform_validated'] == FALSE) {
  2079. if (isset($elements['#needs_validation'])) {
  2080. // Make sure a value is passed when the field is required.
  2081. // A simple call to empty() will not cut it here as some fields, like
  2082. // checkboxes, can return a valid value of '0'. Instead, check the
  2083. // length if it's a string, and the item count if it's an array. For
  2084. // radios, FALSE means that no value was submitted, so check that too.
  2085. if ($elements['#required'] && (!count($elements['#value']) || (is_string($elements['#value']) && strlen(trim($elements['#value'])) == 0) || $elements['#value'] === FALSE)) {
  2086. form_error($elements, t('!name field is required.', array('!name' => $elements['#title'])));
  2087. }
  2088. // Verify that the value is not longer than #maxlength.
  2089. if (isset($elements['#maxlength']) && drupal_strlen($elements['#value']) > $elements['#maxlength']) {
  2090. form_error($elements, t('!name cannot be longer than %max characters but is currently %length characters long.', array('!name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title'], '%max' => $elements['#maxlength'], '%length' => drupal_strlen($elements['#value']))));
  2091. }
  2092. if (isset($elements['#options']) && isset($elements['#value'])) {
  2093. if ($elements['#type'] == 'select') {
  2094. $options = form_options_flatten($elements['#options']);
  2095. }
  2096. else {
  2097. $options = $elements['#options'];
  2098. }
  2099. if (is_array($elements['#value'])) {
  2100. $value = $elements['#type'] == 'checkboxes' ? array_keys(array_filter($elements['#value'])) : $elements['#value'];
  2101. foreach ($value as $v) {
  2102. if (!isset($options[$v])) {
  2103. form_error($elements, t('An illegal choice has been detected. Please contact the site administrator.'));
  2104. watchdog('form', 'Illegal choice %choice in !name element.', array('%choice' => $v, '!name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title']), WATCHDOG_ERROR);
  2105. }
  2106. }
  2107. }
  2108. elseif ($elements['#value'] !== '' && !isset($options[$elements['#value']])) {
  2109. form_error($elements, t('An illegal choice has been detected. Please contact the site administrator.'));
  2110. watchdog('form', 'Illegal choice %choice in %name element.', array('%choice' => $elements['#value'], '%name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title']), WATCHDOG_ERROR);
  2111. }
  2112. }
  2113. }
  2114. // Call any element-specific validators. These must act on the element
  2115. // #value data.
  2116. if (isset($elements['#element_validate'])) {
  2117. foreach ($elements['#element_validate'] as $function) {
  2118. if (function_exists($function)) {
  2119. $function($elements, $form_state, $form);
  2120. }
  2121. }
  2122. }
  2123. $elements['#webform_validated'] = TRUE;
  2124. }
  2125. }
  2126. /**
  2127. * Handle the processing of pages and conditional logic.
  2128. */
  2129. function webform_client_form_pages($form, &$form_state) {
  2130. $node = node_load($form_state['values']['details']['nid']);
  2131. // Multistep forms may not have any components on the first page.
  2132. if (!isset($form_state['values']['submitted'])) {
  2133. $form_state['values']['submitted'] = array();
  2134. }
  2135. // Move special settings to storage.
  2136. if (isset($form_state['webform']['component_tree'])) {
  2137. $form_state['storage']['component_tree'] = $form_state['webform']['component_tree'];
  2138. $form_state['storage']['page_count'] = $form_state['webform']['page_count'];
  2139. $form_state['storage']['page_num'] = $form_state['webform']['page_num'];
  2140. }
  2141. // Perform post processing by components.
  2142. _webform_client_form_submit_process($node, $form_state['values']['submitted']);
  2143. // Flatten trees within the submission.
  2144. $form_state['values']['submitted_tree'] = $form_state['values']['submitted'];
  2145. $form_state['values']['submitted'] = _webform_client_form_submit_flatten($node, $form_state['values']['submitted']);
  2146. // Assume the form is completed unless the page logic says otherwise.
  2147. $form_state['webform_completed'] = TRUE;
  2148. // Check for a multi-page form that is not yet complete.
  2149. $submit_op = !empty($form['actions']['submit']['#value']) ? $form['actions']['submit']['#value'] : t('Submit');
  2150. $draft_op = !empty($form['actions']['draft']['#value']) ? $form['actions']['draft']['#value'] : t('Save Draft');
  2151. if (!in_array($form_state['values']['op'], array($submit_op, $draft_op))) {
  2152. // Store values from the current page in the form state storage.
  2153. if (is_array($form_state['values']['submitted'])) {
  2154. foreach ($form_state['values']['submitted'] as $key => $val) {
  2155. $form_state['storage']['submitted'][$key] = $val;
  2156. }
  2157. }
  2158. // Update form state values with those from storage.
  2159. if (isset($form_state['storage']['submitted'])) {
  2160. foreach ($form_state['storage']['submitted'] as $key => $val) {
  2161. $form_state['values']['submitted'][$key] = $val;
  2162. }
  2163. }
  2164. // Set the page number.
  2165. if (!isset($form_state['storage']['page_num'])) {
  2166. $form_state['storage']['page_num'] = 1;
  2167. }
  2168. if (end($form_state['clicked_button']['#parents']) == 'next') {
  2169. $direction = 1;
  2170. }
  2171. else {
  2172. $direction = 0;
  2173. }
  2174. // If the next page has no components that need to be displayed, skip it.
  2175. if (isset($direction)) {
  2176. $components = $direction ? $node->webform['components'] : array_reverse($node->webform['components'], TRUE);
  2177. $last_component = end($node->webform['components']);
  2178. foreach ($components as $component) {
  2179. if ($component['type'] == 'pagebreak' && (
  2180. $direction == 1 && $component['page_num'] > $form_state['storage']['page_num'] ||
  2181. $direction == 0 && $component['page_num'] <= $form_state['storage']['page_num'])) {
  2182. $previous_pagebreak = $component;
  2183. continue;
  2184. }
  2185. if (isset($previous_pagebreak)) {
  2186. $page_num = $previous_pagebreak['page_num'] + $direction - 1;
  2187. // If we've found an component on this page, advance to that page.
  2188. if ($component['page_num'] == $page_num && _webform_client_form_rule_check($node, $component, $page_num, $form_state)) {
  2189. $form_state['storage']['page_num'] = $page_num;
  2190. break;
  2191. }
  2192. // If we've gotten to the end of the form without finding any more
  2193. // components, set the page number more than the max, ending the form.
  2194. elseif ($direction && $component['cid'] == $last_component['cid']) {
  2195. $form_state['storage']['page_num'] = $page_num + 1;
  2196. }
  2197. }
  2198. }
  2199. }
  2200. // The form is done if the page number is greater than the page count.
  2201. $form_state['webform_completed'] = $form_state['storage']['page_num'] > $form_state['storage']['page_count'];
  2202. }
  2203. // Merge any stored submission data for multistep forms.
  2204. if (isset($form_state['storage']['submitted'])) {
  2205. $original_values = is_array($form_state['values']['submitted']) ? $form_state['values']['submitted'] : array();
  2206. unset($form_state['values']['submitted']);
  2207. foreach ($form_state['storage']['submitted'] as $key => $val) {
  2208. $form_state['values']['submitted'][$key] = $val;
  2209. }
  2210. foreach ($original_values as $key => $val) {
  2211. $form_state['values']['submitted'][$key] = $val;
  2212. }
  2213. // Remove the variable so it doesn't show up in the additional processing.
  2214. unset($original_values);
  2215. }
  2216. // Inform the submit handlers that a draft will be saved.
  2217. $form_state['save_draft'] = $form_state['values']['op'] == $draft_op || ($node->webform['auto_save'] && !$form_state['webform_completed']);
  2218. // Determine what we need to do on the next page.
  2219. if (!empty($form_state['save_draft']) || !$form_state['webform_completed']) {
  2220. // Rebuild the form and display the current (on drafts) or next page.
  2221. $form_state['rebuild'] = TRUE;
  2222. }
  2223. else {
  2224. // Remove the form state storage now that we're done with the pages.
  2225. $form_state['rebuild'] = FALSE;
  2226. unset($form_state['storage']);
  2227. }
  2228. }
  2229. /**
  2230. * Submit handler for saving the form values and sending e-mails.
  2231. */
  2232. function webform_client_form_submit($form, &$form_state) {
  2233. module_load_include('inc', 'webform', 'includes/webform.submissions');
  2234. module_load_include('inc', 'webform', 'includes/webform.components');
  2235. global $user;
  2236. if (empty($form_state['save_draft']) && empty($form_state['webform_completed'])) {
  2237. return;
  2238. }
  2239. $node = $form['#node'];
  2240. $sid = $form_state['values']['details']['sid'] ? (int) $form_state['values']['details']['sid'] : NULL;
  2241. // Check if user is submitting as a draft.
  2242. $is_draft = (int) !empty($form_state['save_draft']);
  2243. if (!$sid) {
  2244. // Create a new submission object.
  2245. $submission = (object) array(
  2246. 'nid' => $node->nid,
  2247. 'uid' => $form_state['values']['details']['uid'],
  2248. 'submitted' => REQUEST_TIME,
  2249. 'remote_addr' => ip_address(),
  2250. 'is_draft' => $is_draft,
  2251. 'data' => webform_submission_data($node, $form_state['values']['submitted']),
  2252. );
  2253. }
  2254. else {
  2255. // To maintain time and user information, load the existing submission.
  2256. $submission = webform_get_submission($node->webform['nid'], $sid);
  2257. $submission->is_draft = $is_draft;
  2258. // Merge with new submission data. The + operator maintains numeric keys.
  2259. // This maintains existing data with just-submitted data when a user resumes
  2260. // a submission previously saved as a draft.
  2261. $new_data = webform_submission_data($node, $form_state['values']['submitted']);
  2262. $submission->data = $new_data + $submission->data;
  2263. }
  2264. // If there is no data to be saved (such as on a multipage form with no fields
  2265. // on the first page), process no further. Submissions with no data cannot
  2266. // be loaded from the database as efficiently, so we don't save them at all.
  2267. if (empty($submission->data)) {
  2268. return;
  2269. }
  2270. // Save the submission to the database.
  2271. if (!$sid) {
  2272. // No sid was found thus insert it in the dataabase.
  2273. $form_state['values']['details']['sid'] = $sid = webform_submission_insert($node, $submission);
  2274. $form_state['values']['details']['is_new'] = TRUE;
  2275. // Set a cookie including the server's submission time.
  2276. // The cookie expires in the length of the interval plus a day to compensate for different timezones.
  2277. if (variable_get('webform_use_cookies', 0)) {
  2278. $cookie_name = 'webform-' . $node->nid;
  2279. $time = REQUEST_TIME;
  2280. $params = session_get_cookie_params();
  2281. setcookie($cookie_name . '[' . $time . ']', $time, $time + $node->webform['submit_interval'] + 86400, $params['path'], $params['domain'], $params['secure'], $params['httponly']);
  2282. }
  2283. // Save session information about this submission for anonymous users,
  2284. // allowing them to access or edit their submissions.
  2285. if (!$user->uid && user_access('access own webform submissions')) {
  2286. $_SESSION['webform_submission'][$form_state['values']['details']['sid']] = $node->nid;
  2287. }
  2288. }
  2289. else {
  2290. // Sid was found thus update the existing sid in the database.
  2291. webform_submission_update($node, $submission);
  2292. $form_state['values']['details']['is_new'] = FALSE;
  2293. }
  2294. // Check if this form is sending an email.
  2295. if (!$is_draft && !$form_state['values']['details']['finished']) {
  2296. $submission = webform_get_submission($node->webform['nid'], $sid, TRUE);
  2297. webform_submission_send_mail($node, $submission);
  2298. }
  2299. // Strip out empty tags added by WYSIWYG editors if needed.
  2300. $confirmation = strlen(trim(strip_tags($node->webform['confirmation']))) ? $node->webform['confirmation'] : '';
  2301. // Clean up the redirect URL and filter it for webform tokens.
  2302. $redirect_url = trim($node->webform['redirect_url']);
  2303. $redirect_url = _webform_filter_values($redirect_url, $node, $submission, NULL, FALSE, TRUE);
  2304. // Remove the domain name from the redirect.
  2305. $redirect_url = preg_replace('/^' . preg_quote($GLOBALS['base_url'], '/') . '\//', '', $redirect_url);
  2306. // Check confirmation and redirect_url fields.
  2307. $message = NULL;
  2308. $redirect = NULL;
  2309. $external_url = FALSE;
  2310. if (isset($form['actions']['draft']['#value']) && $form_state['values']['op'] == $form['actions']['draft']['#value']) {
  2311. $message = t('Submission saved. You may return to this form later and it will restore the current values.');
  2312. }
  2313. elseif ($is_draft) {
  2314. $redirect = NULL;
  2315. }
  2316. elseif (!empty($form_state['values']['details']['finished'])) {
  2317. $message = t('Submission updated.');
  2318. }
  2319. elseif ($redirect_url == '<none>') {
  2320. $redirect = NULL;
  2321. }
  2322. elseif ($redirect_url == '<confirmation>') {
  2323. $redirect = array('node/' . $node->nid . '/done', array('query' => array('sid' => $sid)));
  2324. }
  2325. elseif (valid_url($redirect_url, TRUE)) {
  2326. $redirect = $redirect_url;
  2327. $external_url = TRUE;
  2328. }
  2329. elseif ($redirect_url && strpos($redirect_url, 'http') !== 0) {
  2330. $parts = drupal_parse_url($redirect_url);
  2331. $parts['query'] ? ($parts['query']['sid'] = $sid) : ($parts['query'] = array('sid' => $sid));
  2332. $query = $parts['query'];
  2333. $redirect = array($parts['path'], array('query' => $query, 'fragment' => $parts['fragment']));
  2334. }
  2335. // Show a message if manually set.
  2336. if (isset($message)) {
  2337. drupal_set_message($message);
  2338. }
  2339. // If redirecting and we have a confirmation message, show it as a message.
  2340. elseif (!$is_draft && !$external_url && (!empty($redirect_url) && $redirect_url != '<confirmation>') && !empty($confirmation)) {
  2341. drupal_set_message(check_markup($confirmation, $node->webform['confirmation_format'], '', TRUE));
  2342. }
  2343. $form_state['redirect'] = $redirect;
  2344. }
  2345. /**
  2346. * Post processes the submission tree with any updates from components.
  2347. *
  2348. * @param $node
  2349. * The full webform node.
  2350. * @param $form_values
  2351. * The form values for the form.
  2352. * @param $types
  2353. * Optional. Specific types to perform processing.
  2354. * @param $parent
  2355. * Internal use. The current parent CID whose children are being processed.
  2356. */
  2357. function _webform_client_form_submit_process($node, &$form_values, $types = NULL, $parent = 0) {
  2358. if (is_array($form_values)) {
  2359. foreach ($form_values as $form_key => $value) {
  2360. $cid = webform_get_cid($node, $form_key, $parent);
  2361. if (is_array($value) && isset($node->webform['components'][$cid]['type']) && webform_component_feature($node->webform['components'][$cid]['type'], 'group')) {
  2362. _webform_client_form_submit_process($node, $form_values[$form_key], $types, $cid);
  2363. }
  2364. if (isset($node->webform['components'][$cid])) {
  2365. // Call the component process submission function.
  2366. $component = $node->webform['components'][$cid];
  2367. if ((!isset($types) || in_array($component['type'], $types)) && webform_component_implements($component['type'], 'submit')) {
  2368. $form_values[$component['form_key']] = webform_component_invoke($component['type'], 'submit', $component, $form_values[$component['form_key']]);
  2369. }
  2370. }
  2371. }
  2372. }
  2373. }
  2374. /**
  2375. * Flattens a submitted form back into a single array representation (rather than nested fields)
  2376. */
  2377. function _webform_client_form_submit_flatten($node, $fieldset, $parent = 0) {
  2378. $values = array();
  2379. if (is_array($fieldset)) {
  2380. foreach ($fieldset as $form_key => $value) {
  2381. $cid = webform_get_cid($node, $form_key, $parent);
  2382. if (is_array($value) && webform_component_feature($node->webform['components'][$cid]['type'], 'group')) {
  2383. $values += _webform_client_form_submit_flatten($node, $value, $cid);
  2384. }
  2385. else {
  2386. $values[$cid] = $value;
  2387. }
  2388. }
  2389. }
  2390. return $values;
  2391. }
  2392. /**
  2393. * Prints the confirmation message after a successful submission.
  2394. */
  2395. function _webform_confirmation($node) {
  2396. drupal_set_title($node->title);
  2397. webform_set_breadcrumb($node, TRUE);
  2398. $sid = isset($_GET['sid']) ? $_GET['sid'] : NULL;
  2399. return theme(array('webform_confirmation_' . $node->nid, 'webform_confirmation'), array('node' => $node, 'sid' => $sid));
  2400. }
  2401. /**
  2402. * Prepare for theming of the webform form.
  2403. */
  2404. function template_preprocess_webform_form(&$vars) {
  2405. if (isset($vars['form']['details']['nid']['#value'])) {
  2406. $vars['nid'] = $vars['form']['details']['nid']['#value'];
  2407. }
  2408. elseif (isset($vars['form']['submission']['#value'])) {
  2409. $vars['nid'] = $vars['form']['submission']['#value']->nid;
  2410. }
  2411. }
  2412. /**
  2413. * Prepare for theming of the webform submission confirmation.
  2414. */
  2415. function template_preprocess_webform_confirmation(&$vars) {
  2416. $confirmation = check_markup($vars['node']->webform['confirmation'], $vars['node']->webform['confirmation_format'], '', TRUE);
  2417. // Strip out empty tags added by WYSIWYG editors if needed.
  2418. $vars['confirmation_message'] = strlen(trim(strip_tags($confirmation))) ? $confirmation : '';
  2419. }
  2420. /**
  2421. * Prepare to theme the contents of e-mails sent by webform.
  2422. */
  2423. function template_preprocess_webform_mail_message(&$vars) {
  2424. global $user;
  2425. $vars['user'] = $user;
  2426. $vars['ip_address'] = ip_address();
  2427. }
  2428. /**
  2429. * A Form API #pre_render function. Sets display based on #title_display.
  2430. *
  2431. * This function is used regularly in D6 for all elements, but specifically for
  2432. * fieldsets in D7, which don't support #title_display natively.
  2433. */
  2434. function webform_element_title_display($element) {
  2435. if (isset($element['#title_display']) && strcmp($element['#title_display'], 'none') === 0) {
  2436. $element['#title'] = NULL;
  2437. }
  2438. return $element;
  2439. }
  2440. /**
  2441. * Replacement for theme_form_element().
  2442. */
  2443. function theme_webform_element($variables) {
  2444. // Ensure defaults.
  2445. $variables['element'] += array(
  2446. '#title_display' => 'before',
  2447. );
  2448. $element = $variables['element'];
  2449. // All elements using this for display only are given the "display" type.
  2450. if (isset($element['#format']) && $element['#format'] == 'html') {
  2451. $type = 'display';
  2452. }
  2453. else {
  2454. $type = (isset($element['#type']) && !in_array($element['#type'], array('markup', 'textfield', 'webform_email', 'webform_number'))) ? $element['#type'] : $element['#webform_component']['type'];
  2455. }
  2456. // Convert the parents array into a string, excluding the "submitted" wrapper.
  2457. $nested_level = $element['#parents'][0] == 'submitted' ? 1 : 0;
  2458. $parents = str_replace('_', '-', implode('--', array_slice($element['#parents'], $nested_level)));
  2459. $wrapper_classes = array(
  2460. 'form-item',
  2461. 'webform-component',
  2462. 'webform-component-' . $type,
  2463. );
  2464. if (isset($element['#title_display']) && strcmp($element['#title_display'], 'inline') === 0) {
  2465. $wrapper_classes[] = 'webform-container-inline';
  2466. }
  2467. $output = '<div class="' . implode(' ', $wrapper_classes) . '" id="webform-component-' . $parents . '">' . "\n";
  2468. // If #title_display is none, set it to invisible instead - none only used if
  2469. // we have no title at all to use.
  2470. if ($element['#title_display'] == 'none') {
  2471. $variables['element']['#title_display'] = 'invisible';
  2472. $element['#title_display'] = 'invisible';
  2473. }
  2474. // If #title is not set, we don't display any label or required marker.
  2475. if (!isset($element['#title'])) {
  2476. $element['#title_display'] = 'none';
  2477. }
  2478. $prefix = isset($element['#field_prefix']) ? '<span class="field-prefix">' . _webform_filter_xss($element['#field_prefix']) . '</span> ' : '';
  2479. $suffix = isset($element['#field_suffix']) ? ' <span class="field-suffix">' . _webform_filter_xss($element['#field_suffix']) . '</span>' : '';
  2480. switch ($element['#title_display']) {
  2481. case 'inline':
  2482. case 'before':
  2483. case 'invisible':
  2484. $output .= ' ' . theme('form_element_label', $variables);
  2485. $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
  2486. break;
  2487. case 'after':
  2488. $output .= ' ' . $prefix . $element['#children'] . $suffix;
  2489. $output .= ' ' . theme('form_element_label', $variables) . "\n";
  2490. break;
  2491. case 'none':
  2492. case 'attribute':
  2493. // Output no label and no required marker, only the children.
  2494. $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
  2495. break;
  2496. }
  2497. if (!empty($element['#description'])) {
  2498. $output .= ' <div class="description">' . $element['#description'] . "</div>\n";
  2499. }
  2500. $output .= "</div>\n";
  2501. return $output;
  2502. }
  2503. /**
  2504. * Output a form element in plain text format.
  2505. */
  2506. function theme_webform_element_text($variables) {
  2507. $element = $variables['element'];
  2508. $value = $variables['element']['#children'];
  2509. $output = '';
  2510. $is_group = webform_component_feature($element['#webform_component']['type'], 'group');
  2511. // Output the element title.
  2512. if (isset($element['#title'])) {
  2513. if ($is_group) {
  2514. $output .= '--' . $element['#title'] . '--';
  2515. }
  2516. elseif (!in_array(drupal_substr($element['#title'], -1), array('?', ':', '!', '%', ';', '@'))) {
  2517. $output .= $element['#title'] . ':';
  2518. }
  2519. else {
  2520. $output .= $element['#title'];
  2521. }
  2522. }
  2523. // Wrap long values at 65 characters, allowing for a few fieldset indents.
  2524. // It's common courtesy to wrap at 75 characters in e-mails.
  2525. if ($is_group && drupal_strlen($value) > 65) {
  2526. $value = wordwrap($value, 65, "\n");
  2527. $lines = explode("\n", $value);
  2528. foreach ($lines as $key => $line) {
  2529. $lines[$key] = ' ' . $line;
  2530. }
  2531. $value = implode("\n", $lines);
  2532. }
  2533. // Add the value to the output. Add a newline before the response if needed.
  2534. $output .= (strpos($value, "\n") === FALSE ? ' ' : "\n") . $value;
  2535. // Indent fieldsets.
  2536. if ($is_group) {
  2537. $lines = explode("\n", $output);
  2538. foreach ($lines as $number => $line) {
  2539. if (strlen($line)) {
  2540. $lines[$number] = ' ' . $line;
  2541. }
  2542. }
  2543. $output = implode("\n", $lines);
  2544. $output .= "\n";
  2545. }
  2546. if ($output) {
  2547. $output .= "\n";
  2548. }
  2549. return $output;
  2550. }
  2551. /**
  2552. * Theme a radio button and another element together.
  2553. *
  2554. * This is used in the e-mail configuration to show a radio button and a text
  2555. * field or select list on the same line.
  2556. */
  2557. function theme_webform_inline_radio($variables) {
  2558. $element = $variables['element'];
  2559. // Add element's #type and #name as class to aid with JS/CSS selectors.
  2560. $class = array('form-item');
  2561. if (!empty($element['#type'])) {
  2562. $class[] = 'form-type-' . strtr($element['#type'], '_', '-');
  2563. }
  2564. if (!empty($element['#name'])) {
  2565. $class[] = 'form-item-' . strtr($element['#name'], array(' ' => '-', '_' => '-', '[' => '-', ']' => ''));
  2566. }
  2567. // Add container-inline to all elements.
  2568. $class[] = 'webform-container-inline';
  2569. if (isset($element['#inline_element']) && isset($variables['element']['#title'])) {
  2570. $variables['element']['#title'] .= ': ';
  2571. }
  2572. $output = '<div class="' . implode(' ', $class) . '">' . "\n";
  2573. $output .= ' ' . $element['#children'];
  2574. if (!empty($element['#title'])) {
  2575. $output .= ' ' . theme('form_element_label', $variables) . "\n";
  2576. }
  2577. if (isset($element['#inline_element'])) {
  2578. $output .= ' ' . $element['#inline_element'] . "\n";
  2579. }
  2580. if (!empty($element['#description'])) {
  2581. $output .= ' <div class="description">' . $element['#description'] . "</div>\n";
  2582. }
  2583. $output .= "</div>\n";
  2584. return $output;
  2585. }
  2586. /**
  2587. * Theme the headers when sending an email from webform.
  2588. *
  2589. * @param $node
  2590. * The complete node object for the webform.
  2591. * @param $submission
  2592. * The webform submission of the user.
  2593. * @param $email
  2594. * If you desire to make different e-mail headers depending on the recipient,
  2595. * you can check the $email['email'] property to output different content.
  2596. * This will be the ID of the component that is a conditional e-mail
  2597. * recipient. For the normal e-mails, it will have the value of 'default'.
  2598. * @return
  2599. * An array of headers to be used when sending a webform email. If headers
  2600. * for "From", "To", or "Subject" are set, they will take precedence over
  2601. * the values set in the webform configuration.
  2602. */
  2603. function theme_webform_mail_headers($variables) {
  2604. $headers = array(
  2605. 'X-Mailer' => 'Drupal Webform (PHP/' . phpversion() . ')',
  2606. );
  2607. return $headers;
  2608. }
  2609. /**
  2610. * Check if current user has a draft of this webform, and return the sid.
  2611. */
  2612. function _webform_fetch_draft_sid($nid, $uid) {
  2613. return db_select('webform_submissions')
  2614. ->fields('webform_submissions', array('sid'))
  2615. ->condition('nid', $nid)
  2616. ->condition('uid', $uid)
  2617. ->condition('is_draft', 1)
  2618. ->orderBy('submitted', 'DESC')
  2619. ->execute()
  2620. ->fetchField();
  2621. }
  2622. /**
  2623. * Filters all special tokens provided by webform, such as %post and %profile.
  2624. *
  2625. * @param $string
  2626. * The string to have its tokens replaced.
  2627. * @param $node
  2628. * If replacing node-level tokens, the node for which tokens will be created.
  2629. * @param $submission
  2630. * If replacing submission-level tokens, the submission for which tokens will
  2631. * be created.
  2632. * @param $email
  2633. * If replacing tokens within the context of an e-mail, the Webform e-mail
  2634. * settings array.
  2635. * @param $strict
  2636. * Boolean value indicating if the results should be run through check_plain.
  2637. * This is used any time the values will be output as HTML, but not in
  2638. * default values or e-mails.
  2639. * @param $allow_anonymous
  2640. * Boolean value indicating if all tokens should be replaced for anonymous
  2641. * users, even if they contain sensitive user information such as %session or
  2642. * %ip_address. This is disabled by default to prevent user data from being
  2643. * preserved in the anonymous page cache and should only be used in
  2644. * non-cached situations, such as e-mails.
  2645. */
  2646. function _webform_filter_values($string, $node = NULL, $submission = NULL, $email = NULL, $strict = TRUE, $allow_anonymous = FALSE) {
  2647. global $user;
  2648. $replacements = &drupal_static(__FUNCTION__);
  2649. // Don't do any filtering if the string is empty.
  2650. if (strlen(trim($string)) == 0) {
  2651. return $string;
  2652. }
  2653. // Setup default token replacements.
  2654. if (!isset($replacements)) {
  2655. $replacements['unsafe'] = array();
  2656. $replacements['safe']['%site'] = variable_get('site_name', 'drupal');
  2657. $replacements['safe']['%date'] = format_date(REQUEST_TIME, 'long');
  2658. }
  2659. // Node replacements.
  2660. if (isset($node) && !array_key_exists('%nid', $replacements['safe'])) {
  2661. $replacements['safe']['%nid'] = $node->nid;
  2662. $replacements['safe']['%title'] = $node->title;
  2663. }
  2664. // Determine the display format.
  2665. $format = isset($email['html']) && $email['html'] ? 'html' : 'text';
  2666. // Submission replacements.
  2667. if (isset($submission) && (!isset($replacements['email'][$format]) || (isset($replacements['unsafe']['%sid']) && $replacements['unsafe']['%sid'] != $submission->sid))) {
  2668. module_load_include('inc', 'webform', 'includes/webform.components');
  2669. // Set the submission ID.
  2670. $replacements['unsafe']['%sid'] = $submission->sid;
  2671. // E-mails may be sent in two formats, keep tokens separate for each one.
  2672. $replacements['email'][$format] = array();
  2673. // Populate token values for each component.
  2674. foreach ($node->webform['components'] as $cid => $component) {
  2675. // Find by form key.
  2676. $parents = webform_component_parent_keys($node, $component);
  2677. $form_key = implode('][', $parents);
  2678. if (isset($submission->data[$cid])) {
  2679. $value = $submission->data[$cid];
  2680. $display_element = webform_component_invoke($component['type'], 'display', $component, $value['value'], $format);
  2681. // Ensure the component is added as a property.
  2682. $display_element['#webform_component'] = $component;
  2683. if (empty($display_element['#parents'])) {
  2684. $display_element['#parents'] = array_merge(array('submitted'), $parents);
  2685. }
  2686. if (empty($display_element['#id'])) {
  2687. $display_element['#id'] = drupal_html_id('edit-' . implode('-', $display_element['#parents']));
  2688. }
  2689. $replacements['email'][$format]['%email[' . $form_key . ']'] = render($display_element);
  2690. $display_element['#theme_wrappers'] = array(); // Remove label and wrappers.
  2691. $replacements['email'][$format]['%value[' . $form_key . ']'] = render($display_element);
  2692. }
  2693. else {
  2694. // Provide an empty value for components without submitted data.
  2695. $replacements['email'][$format]['%email[' . $form_key . ']'] = '';
  2696. $replacements['email'][$format]['%value[' . $form_key . ']'] = '';
  2697. }
  2698. }
  2699. // Reverse the order of tokens so that nested tokens (ie. inside fieldsets)
  2700. // come before their parents.
  2701. $replacements['email'][$format] = array_reverse($replacements['email'][$format]);
  2702. // Submission edit URL.
  2703. $replacements['unsafe']['%submission_url'] = url('node/' . $node->nid . '/submission/' . $submission->sid, array('absolute' => TRUE));
  2704. }
  2705. // Token for the entire form tree for e-mails.
  2706. if (isset($submission) && isset($email)) {
  2707. $replacements['email'][$format]['%email_values'] = webform_submission_render($node, $submission, $email, $format);
  2708. }
  2709. // Provide a list of candidates for token replacement.
  2710. $special_tokens = array(
  2711. 'safe' => array(
  2712. '%get' => $_GET,
  2713. '%post' => $_POST,
  2714. ),
  2715. 'unsafe' => array(
  2716. '%cookie' => isset($_COOKIE) ? $_COOKIE : array(),
  2717. '%session' => isset($_SESSION) ? $_SESSION : array(),
  2718. '%request' => $_REQUEST,
  2719. '%server' => $_SERVER,
  2720. '%profile' => (array) $user,
  2721. ),
  2722. );
  2723. // Replacements of global variable tokens.
  2724. if (!isset($replacements['specials_set'])) {
  2725. $replacements['specials_set'] = TRUE;
  2726. // Load profile information if available.
  2727. if ($user->uid) {
  2728. $account = user_load($user->uid);
  2729. $special_tokens['unsafe']['%profile'] = (array) $account;
  2730. }
  2731. // User replacements.
  2732. if (!array_key_exists('%uid', $replacements['unsafe'])) {
  2733. $replacements['unsafe']['%uid'] = !empty($user->uid) ? $user->uid : '';
  2734. $replacements['unsafe']['%username'] = isset($user->name) ? $user->name : '';
  2735. $replacements['unsafe']['%useremail'] = isset($user->mail) ? $user->mail : '';
  2736. $replacements['unsafe']['%ip_address'] = ip_address();
  2737. }
  2738. // Populate the replacements array with special variables.
  2739. foreach ($special_tokens as $safe_state => $tokens) {
  2740. foreach ($tokens as $token => $variable) {
  2741. // Safety check in case $_POST or some other global has been removed
  2742. // by a naughty module, in which case $variable may be NULL.
  2743. if (!is_array($variable)) {
  2744. continue;
  2745. }
  2746. foreach ($variable as $key => $value) {
  2747. // This special case for profile module dates.
  2748. if ($token == '%profile' && is_array($value) && isset($value['year'])) {
  2749. $replacement = webform_strtodate(webform_date_format(), $value['month'] . '/' . $value['day'] . '/' . $value['year'], 'UTC');
  2750. }
  2751. else {
  2752. // Checking for complex types (arrays and objects) fails here with
  2753. // incomplete objects (see http://php.net/is_object), so we check
  2754. // for simple types instead.
  2755. $replacement = (is_string($value) || is_bool($value) || is_numeric($value)) ? $value : '';
  2756. }
  2757. $replacements[$safe_state][$token . '[' . $key . ']'] = $replacement;
  2758. }
  2759. }
  2760. }
  2761. }
  2762. // Make a copy of the replacements so we don't affect the static version.
  2763. $safe_replacements = $replacements['safe'];
  2764. // Restrict replacements for anonymous users. Not all tokens can be used
  2765. // because they may expose session or other private data to other users when
  2766. // anonymous page caching is enabled.
  2767. if ($user->uid || $allow_anonymous) {
  2768. $safe_replacements += $replacements['unsafe'];
  2769. if (isset($replacements['email'][$format])) {
  2770. $safe_replacements += $replacements['email'][$format];
  2771. }
  2772. }
  2773. else {
  2774. foreach ($replacements['unsafe'] as $key => $value) {
  2775. $safe_replacements[$key] = '';
  2776. }
  2777. }
  2778. $find = array_keys($safe_replacements);
  2779. $replace = array_values($safe_replacements);
  2780. $string = str_replace($find, $replace, $string);
  2781. // Clean up any unused tokens.
  2782. foreach ($special_tokens as $safe_state => $tokens) {
  2783. foreach (array_keys($tokens) as $token) {
  2784. $string = preg_replace('/\\' . $token . '\[\w+\]/', '', $string);
  2785. }
  2786. }
  2787. return $strict ? _webform_filter_xss($string) : $string;
  2788. }
  2789. /**
  2790. * Filters all special tokens provided by webform, and allows basic layout in descriptions.
  2791. */
  2792. function _webform_filter_descriptions($string, $node = NULL, $submission = NULL) {
  2793. return strlen($string) == 0 ? '' : _webform_filter_xss(_webform_filter_values($string, $node, $submission, NULL, FALSE));
  2794. }
  2795. /**
  2796. * Filter labels for display by running through XSS checks.
  2797. */
  2798. function _webform_filter_xss($string) {
  2799. static $allowed_tags;
  2800. $allowed_tags = isset($allowed_tags) ? $allowed_tags : webform_variable_get('webform_allowed_tags');
  2801. return filter_xss($string, $allowed_tags);
  2802. }
  2803. /**
  2804. * Utility function to ensure that a webform record exists in the database.
  2805. *
  2806. * @param $node
  2807. * The node object to check if a database entry exists.
  2808. * @return
  2809. * This function should always return TRUE if no errors were encountered,
  2810. * ensuring that a webform table row has been created. Will return FALSE if
  2811. * a record does not exist and a new one could not be created.
  2812. */
  2813. function webform_ensure_record(&$node) {
  2814. if (!$node->webform['record_exists']) {
  2815. // Even though webform_node_insert() would set this property to TRUE,
  2816. // we set record_exists to trigger a difference from the defaults.
  2817. $node->webform['record_exists'] = TRUE;
  2818. webform_node_insert($node);
  2819. }
  2820. return $node->webform['record_exists'];
  2821. }
  2822. /**
  2823. * Utility function to check if a webform record is necessary in the database.
  2824. *
  2825. * If the node is no longer using any webform settings, this function will
  2826. * delete the settings from the webform table. Note that this function will NOT
  2827. * delete rows from the webform table if the node-type is exclusively used for
  2828. * webforms (per the "webform_node_types_primary" variable).
  2829. *
  2830. * @param $node
  2831. * The node object to check if a database entry is still required.
  2832. * @return
  2833. * Returns TRUE if the webform still has a record in the database. Returns
  2834. * FALSE if the webform does not have a record or if the previously existing
  2835. * record was just deleted.
  2836. */
  2837. function webform_check_record(&$node) {
  2838. $webform = $node->webform;
  2839. $webform['record_exists'] = FALSE;
  2840. unset($webform['nid']);
  2841. // Don't include empty values in the comparison, this makes it so modules that
  2842. // extend Webform with empty defaults won't affect cleanup of rows.
  2843. $webform = array_filter($webform);
  2844. $defaults = array_filter(webform_node_defaults());
  2845. if ($webform == $defaults && !in_array($node->type, webform_variable_get('webform_node_types_primary'))) {
  2846. webform_node_delete($node);
  2847. $node->webform = webform_node_defaults();
  2848. }
  2849. return $node->webform['record_exists'];
  2850. }
  2851. /**
  2852. * Given a form_key and a list of form_key parents, determine the cid.
  2853. *
  2854. * @param $node
  2855. * A fully loaded node object.
  2856. * @param $form_key
  2857. * The form key for which we're finding a cid.
  2858. * @param $parent
  2859. * The cid of the parent component.
  2860. */
  2861. function webform_get_cid(&$node, $form_key, $pid) {
  2862. foreach ($node->webform['components'] as $cid => $component) {
  2863. if ($component['form_key'] == $form_key && $component['pid'] == $pid) {
  2864. return $cid;
  2865. }
  2866. }
  2867. }
  2868. /**
  2869. * Retreive a Drupal variable with the appropriate default value.
  2870. */
  2871. function webform_variable_get($variable) {
  2872. switch ($variable) {
  2873. case 'webform_allowed_tags':
  2874. $result = variable_get('webform_allowed_tags', array('a', 'em', 'strong', 'code', 'img'));
  2875. break;
  2876. case 'webform_default_from_name':
  2877. $result = variable_get('webform_default_from_name', variable_get('site_name', ''));
  2878. break;
  2879. case 'webform_default_from_address':
  2880. $result = variable_get('webform_default_from_address', variable_get('site_mail', ini_get('sendmail_from')));
  2881. break;
  2882. case 'webform_default_subject':
  2883. $result = variable_get('webform_default_subject', t('Form submission from: %title'));
  2884. break;
  2885. case 'webform_node_types':
  2886. $result = variable_get('webform_node_types', array('webform'));
  2887. break;
  2888. case 'webform_node_types_primary':
  2889. $result = variable_get('webform_node_types_primary', array('webform'));
  2890. break;
  2891. }
  2892. return $result;
  2893. }
  2894. function theme_webform_token_help($variables) {
  2895. $groups = $variables['groups'];
  2896. $groups = empty($groups) ? array('basic', 'node', 'special') : $groups;
  2897. static $tokens = array();
  2898. if (empty($tokens)) {
  2899. $tokens['basic'] = array(
  2900. 'title' => t('Basic tokens'),
  2901. 'tokens' => array(
  2902. '%username' => t('The name of the user if logged in. Blank for anonymous users.'),
  2903. '%useremail' => t('The e-mail address of the user if logged in. Blank for anonymous users.'),
  2904. '%ip_address' => t('The IP address of the user.'),
  2905. '%site' => t('The name of the site (i.e. %site_name)', array('%site_name' => variable_get('site_name', ''))),
  2906. '%date' => t('The current date, formatted according to the site settings.'),
  2907. ),
  2908. );
  2909. $tokens['node'] = array(
  2910. 'title' => t('Node tokens'),
  2911. 'tokens' => array(
  2912. '%nid' => t('The node ID.'),
  2913. '%title' => t('The node title.'),
  2914. ),
  2915. );
  2916. $tokens['special'] = array(
  2917. 'title' => t('Special tokens'),
  2918. 'tokens' => array(
  2919. '%profile[' . t('key') . ']' => t('Any user profile field or value, such as %profile[name] or %profile[profile_first_name]'),
  2920. '%get[' . t('key') . ']' => t('Tokens may be populated from the URL by creating URLs of the form http://example.com/my-form?foo=bar. Using the token %get[foo] would print "bar".'),
  2921. '%post[' . t('key') . ']' => t('Tokens may also be populated from POST values that are submitted by forms.'),
  2922. ),
  2923. 'description' => t('In addition to %get and %post, the following super tokens may be used, though only with logged-in users: %server, %cookie, and %request. For example %server[HTTP_USER_AGENT] or %session[id].'),
  2924. );
  2925. $tokens['email'] = array(
  2926. 'title' => t('E-mail tokens'),
  2927. 'tokens' => array(
  2928. '%email_values' => t('All included components in a hierarchical structure.'),
  2929. '%email[' . t('key') . '] ' => t('A formatted value and field label. Elements may be accessed such as <em>%email[fieldset_a][key_b]</em>. Do not include quotes.'),
  2930. '%submission_url' => t('The URL for viewing the completed submission.'),
  2931. ),
  2932. );
  2933. $tokens['submission'] = array(
  2934. 'title' => t('Submission tokens'),
  2935. 'tokens' => array(
  2936. '%sid' => t('The unique submission ID.'),
  2937. '%value[key]' => t('A value without additional formatting. Elements may be accessed such as <em>%value[fieldset_a][key_b]</em>. Do not include quotes.'),
  2938. ),
  2939. );
  2940. }
  2941. $output = '';
  2942. $output .= '<p>' . t('You may use special tokens in this field that will be replaced with dynamic values.') . '</p>';
  2943. foreach ($tokens as $group_name => $group) {
  2944. if (!is_array($groups) || in_array($group_name, $groups)) {
  2945. $items = array();
  2946. foreach ($group['tokens'] as $token => $token_description) {
  2947. $items[] = $token . ' - ' . $token_description;
  2948. }
  2949. $output .= theme('item_list', array('items' => $items, 'title' => $group['title']));
  2950. $output .= isset($group['description']) ? '<p>' . $group['description'] . '</p>' : '';
  2951. }
  2952. }
  2953. $fieldset = array(
  2954. '#title' => t('Token values'),
  2955. '#type' => 'fieldset',
  2956. '#collapsible' => TRUE,
  2957. '#collapsed' => TRUE,
  2958. '#children' => '<div>' . $output . '</div>',
  2959. '#attributes' => array('class' => array('collapsible', 'collapsed')),
  2960. );
  2961. return theme('fieldset', array('element' => $fieldset));
  2962. }
  2963. function _webform_safe_name($name) {
  2964. $new = trim($name);
  2965. // If transliteration is available, use it to convert names to ASCII.
  2966. if (function_exists('transliteration_get')) {
  2967. $new = transliteration_get($new, '');
  2968. $new = str_replace(array(' ', '-', '/'), array('_', '_', '_'), $new);
  2969. }
  2970. else {
  2971. $new = str_replace(
  2972. array(' ', '-', '/', '€', 'ƒ', 'Š', 'Ž', 'š', 'ž', 'Ÿ', '¢', '¥', 'µ', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'à', 'á', 'â', 'ã', 'ä', 'å', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'Œ', 'œ', 'Æ', 'Ð', 'Þ', 'ß', 'æ', 'ð', 'þ'),
  2973. array('_', '_', '_', 'E', 'f', 'S', 'Z', 's', 'z', 'Y', 'c', 'Y', 'u', 'A', 'A', 'A', 'A', 'A', 'A', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I', 'I', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y', 'a', 'a', 'a', 'a', 'a', 'a', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'OE', 'oe', 'AE', 'DH', 'TH', 'ss', 'ae', 'dh', 'th'),
  2974. $new);
  2975. }
  2976. $new = drupal_strtolower($new);
  2977. $new = preg_replace('/[^a-z0-9_]/', '', $new);
  2978. return $new;
  2979. }
  2980. /**
  2981. * Given an email address and a name, format an e-mail address.
  2982. *
  2983. * @param $address
  2984. * The e-mail address.
  2985. * @param $name
  2986. * The name to be used in the formatted address.
  2987. * @param $node
  2988. * The webform node if replacements will be done.
  2989. * @param $submission
  2990. * The webform submission values if replacements will be done.
  2991. * @param $encode
  2992. * Encode the text for use in an e-mail.
  2993. * @param $single
  2994. * Force a single value to be returned, even if a component expands to
  2995. * multiple addresses. This is useful to ensure a single e-mail will be
  2996. * returned for the "From" address.
  2997. * @param $format
  2998. * The e-mail format, defaults to the site-wide setting. May be either "short"
  2999. * or "long".
  3000. */
  3001. function webform_format_email_address($address, $name, $node = NULL, $submission = NULL, $encode = TRUE, $single = TRUE, $format = NULL) {
  3002. if (!isset($format)) {
  3003. $format = variable_get('webform_email_address_format', 'long');
  3004. }
  3005. if ($name == 'default') {
  3006. $name = webform_variable_get('webform_default_from_name');
  3007. }
  3008. elseif (is_numeric($name) && isset($node->webform['components'][$name])) {
  3009. if (isset($submission->data[$name]['value'])) {
  3010. $name = $submission->data[$name]['value'];
  3011. }
  3012. else {
  3013. $name = t('Value of !component', array('!component' => $node->webform['components'][$name]['name']));
  3014. }
  3015. }
  3016. if ($address == 'default') {
  3017. $address = webform_variable_get('webform_default_from_address');
  3018. }
  3019. elseif (is_numeric($address) && isset($node->webform['components'][$address])) {
  3020. if (isset($submission->data[$address]['value'])) {
  3021. $values = $submission->data[$address]['value'];;
  3022. $address = array();
  3023. foreach ($values as $value) {
  3024. $address = array_merge($address, explode(',', $value));
  3025. }
  3026. }
  3027. else {
  3028. $address = t('Value of "!component"', array('!component' => $node->webform['components'][$address]['name']));
  3029. }
  3030. }
  3031. // Convert arrays into a single value for From values.
  3032. if ($single) {
  3033. $address = is_array($address) ? reset($address) : $address;
  3034. $name = is_array($name) ? reset($name) : $name;
  3035. }
  3036. // Address may be an array if a component value was used on checkboxes.
  3037. if (is_array($address)) {
  3038. foreach ($address as $key => $individual_address) {
  3039. $address[$key] = _webform_filter_values($individual_address, $node, $submission, NULL, FALSE, TRUE);
  3040. }
  3041. }
  3042. else {
  3043. $address = _webform_filter_values($address, $node, $submission, NULL, FALSE, TRUE);
  3044. }
  3045. if ($format == 'long' && !empty($name)) {
  3046. $name = _webform_filter_values($name, $node, $submission, NULL, FALSE, TRUE);
  3047. if ($encode) {
  3048. $name = mime_header_encode($name);
  3049. }
  3050. $name = trim($name);
  3051. return '"' . $name . '" <' . $address . '>';
  3052. }
  3053. else {
  3054. return $address;
  3055. }
  3056. }
  3057. /**
  3058. * Given an email subject, format it with any needed replacements.
  3059. */
  3060. function webform_format_email_subject($subject, $node = NULL, $submission = NULL) {
  3061. if ($subject == 'default') {
  3062. $subject = webform_variable_get('webform_default_subject');
  3063. }
  3064. elseif (is_numeric($subject) && isset($node->webform['components'][$subject])) {
  3065. $component = $node->webform['components'][$subject];
  3066. if (isset($submission->data[$subject]['value'])) {
  3067. $display_function = '_webform_display_' . $component['type'];
  3068. $value = $submission->data[$subject]['value'];
  3069. // Convert the value to a clean text representation if possible.
  3070. if (function_exists($display_function)) {
  3071. $display = $display_function($component, $value, 'text');
  3072. $display['#theme_wrappers'] = array();
  3073. $display['#webform_component'] = $component;
  3074. $subject = str_replace("\n", ' ', drupal_render($display));
  3075. }
  3076. else {
  3077. $subject = $value;
  3078. }
  3079. }
  3080. else {
  3081. $subject = t('Value of "!component"', array('!component' => $component['name']));
  3082. }
  3083. }
  3084. // Convert arrays to strings (may happen if checkboxes are used as the value).
  3085. if (is_array($subject)) {
  3086. $subject = reset($subject);
  3087. }
  3088. return _webform_filter_values($subject, $node, $submission, NULL, FALSE, TRUE);
  3089. }
  3090. /**
  3091. * Convert an array of components into a tree
  3092. */
  3093. function _webform_components_tree_build($src, &$tree, $parent, &$page_count) {
  3094. foreach ($src as $cid => $component) {
  3095. if ($component['pid'] == $parent) {
  3096. _webform_components_tree_build($src, $component, $cid, $page_count);
  3097. if ($component['type'] == 'pagebreak') {
  3098. $page_count++;
  3099. }
  3100. $tree['children'][$cid] = $component;
  3101. $tree['children'][$cid]['page_num'] = $page_count;
  3102. }
  3103. }
  3104. return $tree;
  3105. }
  3106. /**
  3107. * Flatten a component tree into a flat list.
  3108. */
  3109. function _webform_components_tree_flatten($tree) {
  3110. $components = array();
  3111. foreach ($tree as $cid => $component) {
  3112. if (isset($component['children'])) {
  3113. unset($component['children']);
  3114. $components[$cid] = $component;
  3115. // array_merge() can't be used here because the keys are numeric.
  3116. $children = _webform_components_tree_flatten($tree[$cid]['children']);
  3117. foreach ($children as $ccid => $ccomponent) {
  3118. $components[$ccid] = $ccomponent;
  3119. }
  3120. }
  3121. else {
  3122. $components[$cid] = $component;
  3123. }
  3124. }
  3125. return $components;
  3126. }
  3127. /**
  3128. * Helper for the uasort in webform_tree_sort()
  3129. */
  3130. function _webform_components_sort($a, $b) {
  3131. if ($a['weight'] == $b['weight']) {
  3132. return strcasecmp($a['name'], $b['name']);
  3133. }
  3134. return ($a['weight'] < $b['weight']) ? -1 : 1;
  3135. }
  3136. /**
  3137. * Sort each level of a component tree by weight and name
  3138. */
  3139. function _webform_components_tree_sort($tree) {
  3140. if (isset($tree['children']) && is_array($tree['children'])) {
  3141. $children = array();
  3142. uasort($tree['children'], '_webform_components_sort');
  3143. foreach ($tree['children'] as $cid => $component) {
  3144. $children[$cid] = _webform_components_tree_sort($component);
  3145. }
  3146. $tree['children'] = $children;
  3147. }
  3148. return $tree;
  3149. }
  3150. /**
  3151. * Get a list of all available component definitions.
  3152. */
  3153. function webform_components($include_disabled = FALSE, $reset = FALSE) {
  3154. static $components, $disabled;
  3155. if (!isset($components) || $reset) {
  3156. $components = array();
  3157. $disabled = array_flip(variable_get('webform_disabled_components', array()));
  3158. foreach (module_implements('webform_component_info') as $module) {
  3159. $module_components = module_invoke($module, 'webform_component_info');
  3160. foreach ($module_components as $type => $info) {
  3161. $module_components[$type]['module'] = $module;
  3162. $module_components[$type]['enabled'] = !array_key_exists($type, $disabled);
  3163. }
  3164. $components += $module_components;
  3165. }
  3166. drupal_alter('webform_component_info', $components);
  3167. ksort($components);
  3168. }
  3169. return $include_disabled ? $components : array_diff_key($components, $disabled);
  3170. }
  3171. /**
  3172. * Build a list of components suitable for use as select list options.
  3173. */
  3174. function webform_component_options($include_disabled = FALSE) {
  3175. $component_info = webform_components($include_disabled);
  3176. $options = array();
  3177. foreach ($component_info as $type => $info) {
  3178. $options[$type] = $info['label'];
  3179. }
  3180. return $options;
  3181. }
  3182. /**
  3183. * Load a component file into memory.
  3184. *
  3185. * @param $component_type
  3186. * The string machine name of a component.
  3187. */
  3188. function webform_component_include($component_type) {
  3189. static $included = array();
  3190. // No need to load components that have already been added once.
  3191. if (!isset($included[$component_type])) {
  3192. $components = webform_components(TRUE);
  3193. $included[$component_type] = TRUE;
  3194. if (($info = $components[$component_type]) && isset($info['file'])) {
  3195. $pathinfo = pathinfo($info['file']);
  3196. $basename = basename($pathinfo['basename'], '.' . $pathinfo['extension']);
  3197. $path = (!empty($pathinfo['dirname']) ? $pathinfo['dirname'] . '/' : '') . $basename;
  3198. module_load_include($pathinfo['extension'], $info['module'], $path);
  3199. }
  3200. }
  3201. }
  3202. /**
  3203. * Invoke a component callback.
  3204. *
  3205. * @param $type
  3206. * The component type as a string.
  3207. * @param $callback
  3208. * The callback to execute.
  3209. * @param ...
  3210. * Any additional parameters required by the $callback.
  3211. */
  3212. function webform_component_invoke($type, $callback) {
  3213. $args = func_get_args();
  3214. $type = array_shift($args);
  3215. $callback = array_shift($args);
  3216. $function = '_webform_' . $callback . '_' . $type;
  3217. webform_component_include($type);
  3218. if (function_exists($function)) {
  3219. return call_user_func_array($function, $args);
  3220. }
  3221. }
  3222. /**
  3223. * Check if a component implements a particular hook.
  3224. *
  3225. * @param $type
  3226. * The component type as a string.
  3227. * @param $callback
  3228. * The callback to check.
  3229. */
  3230. function webform_component_implements($type, $callback) {
  3231. $function = '_webform_' . $callback . '_' . $type;
  3232. webform_component_include($type);
  3233. return function_exists($function);
  3234. }
  3235. /**
  3236. * Disable the Drupal page cache.
  3237. */
  3238. function webform_disable_page_cache() {
  3239. drupal_page_is_cacheable(FALSE);
  3240. }
  3241. /**
  3242. * Set the necessary breadcrumb for the page we are on.
  3243. *
  3244. * @param object $node
  3245. * The loaded webform node.
  3246. * @param boolean|object $submission
  3247. * The submission if the current page is viewing or dealing with a submission,
  3248. * or TRUE to just include the webform node in the breadcrumbs (used for
  3249. * the submission completion confirmation page), or NULL for no extra
  3250. * processing
  3251. */
  3252. function webform_set_breadcrumb($node, $submission = NULL) {
  3253. $node_path = "node/{$node->nid}";
  3254. // Set the href of the current menu item to be the node's path. This has two
  3255. // effects. The active trail will be to the node's prefered menu tree location,
  3256. // expanding the menu as appropriate. And the breadcrumbs will be set as if
  3257. // the current page were under the node's preferred location.
  3258. // Note that menu_tree_set_path() could be used to set the path for the menu,
  3259. // but it will not affect the breadcrumbs when the webform is not in the
  3260. // default menu.
  3261. menu_set_item(NULL, array('href' => $node_path) + menu_get_item());
  3262. if ($submission) {
  3263. $breadcrumb = menu_get_active_breadcrumb();
  3264. // Append the node title (or its menu name), in case it isn't in the path already.
  3265. $active_trail = menu_get_active_trail();
  3266. $last_active = end($active_trail);
  3267. $breadcrumb[] = $last_active['href'] === $node_path && !empty($last_active['in_active_trail'])
  3268. ? l($last_active['title'], $node_path, $last_active['localized_options'])
  3269. : l($node->title, $node_path);
  3270. // Setting the current menu href will cause the submission title and current
  3271. // tab (if not the default tab) to be added to the active path when the
  3272. // webform is in the default location in the menu (node/NID). The title
  3273. // is desirable, but the tab name (e.g. Edit or Delete) isn't.
  3274. if (preg_match('/href=".*"/', end($breadcrumb), $matches)) {
  3275. foreach ($breadcrumb as $index => $link) {
  3276. if (stripos($link, $matches[0]) !== FALSE) {
  3277. $breadcrumb = array_slice($breadcrumb, 0, $index + 1);
  3278. break;
  3279. }
  3280. }
  3281. }
  3282. // If the user is dealing with a submission, then the breadcrumb should
  3283. // be fudged to allow them to return to a likely list of webforms.
  3284. // Note that this isn't necessarily where they came from, but it's the
  3285. // best guess available.
  3286. if (is_object($submission)) {
  3287. if (webform_results_access($node)) {
  3288. $breadcrumb[] = l(t('Webform results'), $node_path . '/webform-results');
  3289. }
  3290. elseif (user_access('access own webform results')) {
  3291. $breadcrumb[] = l(t('Submissions'), $node_path . '/submissions');
  3292. }
  3293. }
  3294. drupal_set_breadcrumb($breadcrumb);
  3295. }
  3296. }
  3297. /**
  3298. * Convert an ISO 8601 date or time into an array.
  3299. *
  3300. * This converts full format dates or times. Either a date or time may be
  3301. * provided, in which case only those portions will be returned. Dashes and
  3302. * colons must be used, never implied.
  3303. *
  3304. * Formats:
  3305. * Dates: YYYY-MM-DD
  3306. * Times: HH:MM:SS
  3307. * Datetimes: YYYY-MM-DDTHH:MM:SS
  3308. *
  3309. * @param $string
  3310. * An ISO 8601 date, time, or datetime.
  3311. * @param $type
  3312. * If wanting only specific fields back, specify either "date" or "time".
  3313. * Leaving empty will return an array with both date and time keys, even if
  3314. * some are empty. Returns an array with the following keys:
  3315. * - year
  3316. * - month
  3317. * - day
  3318. * - hour (in 24hr notation)
  3319. * - minute
  3320. * - second
  3321. */
  3322. function webform_date_array($string, $type = NULL) {
  3323. $pattern = '/((\d{4}?)-(\d{2}?)-(\d{2}?))?(T?(\d{2}?):(\d{2}?):(\d{2}?))?/';
  3324. $matches = array();
  3325. preg_match($pattern, $string, $matches);
  3326. $matches += array_fill(0, 9, '');
  3327. $return = array();
  3328. // Check for a date string.
  3329. if ($type == 'date' || !isset($type)) {
  3330. $return['year'] = $matches[2] !== '' ? (int) $matches[2] : '';
  3331. $return['month'] = $matches[3] !== '' ? (int) $matches[3] : '';
  3332. $return['day'] = $matches[4] !== '' ? (int) $matches[4] : '';
  3333. }
  3334. // Check for a time string.
  3335. if ($type == 'time' || !isset($type)) {
  3336. $return['hour'] = $matches[6] !== '' ? (int) $matches[6] : '';
  3337. $return['minute'] = $matches[7] !== '' ? (int) $matches[7] : '';
  3338. $return['second'] = $matches[8] !== '' ? (int) $matches[8] : '';
  3339. }
  3340. return $return;
  3341. }
  3342. /**
  3343. * Convert an array of a date or time into an ISO 8601 compatible string.
  3344. *
  3345. * @param $array
  3346. * The array to convert to a date or time string.
  3347. * @param $type
  3348. * If wanting a specific string format back specify either "date" or "time".
  3349. * Otherwise a full ISO 8601 date and time string will be returned.
  3350. */
  3351. function webform_date_string($array, $type = NULL) {
  3352. $string = '';
  3353. if ($type == 'date' || !isset($type)) {
  3354. $string .= empty($array['year']) ? '0000' : sprintf('%04d', $array['year']);
  3355. $string .= '-';
  3356. $string .= empty($array['month']) ? '00' : sprintf('%02d', $array['month']);
  3357. $string .= '-';
  3358. $string .= empty($array['day']) ? '00' : sprintf('%02d', $array['day']);
  3359. }
  3360. if (!isset($type)) {
  3361. $string .= 'T';
  3362. }
  3363. if ($type == 'time' || !isset($type)) {
  3364. $string .= empty($array['hour']) ? '00' : sprintf('%02d', $array['hour']);
  3365. $string .= ':';
  3366. $string .= empty($array['minute']) ? '00' : sprintf('%02d', $array['minute']);
  3367. $string .= ':';
  3368. $string .= empty($array['second']) ? '00' : sprintf('%02d', $array['second']);
  3369. }
  3370. return $string;
  3371. }
  3372. /**
  3373. * Get a date format according to the site settings.
  3374. *
  3375. * @param $size
  3376. * A choice of 'short', 'medium', or 'long' date formats.
  3377. */
  3378. function webform_date_format($size = 'medium') {
  3379. // Format date according to site's given format.
  3380. $format = variable_get('date_format_' . $size, 'D, m/d/Y - H:i');
  3381. $time = 'aABgGhHisueIOPTZ';
  3382. $day_of_week = 'Dlw';
  3383. $special = ',-: ';
  3384. $date_format = trim($format, $time . $day_of_week . $special);
  3385. // Ensure that a day, month, and year value are present. Use a default
  3386. // format if all the values are not found.
  3387. if (!preg_match('/[dj]/', $date_format) || !preg_match('/[FmMn]/', $date_format) || !preg_match('/[oYy]/', $date_format)) {
  3388. $date_format = 'm/d/Y';
  3389. }
  3390. return $date_format;
  3391. }
  3392. /**
  3393. * Return a date in the desired format taking into consideration user timezones.
  3394. */
  3395. function webform_strtodate($format, $string, $timezone_name = NULL) {
  3396. global $user;
  3397. // Adjust the time based on the user or site timezone.
  3398. if (variable_get('configurable_timezones', 1) && $timezone_name == 'user' && $user->uid) {
  3399. $timezone_name = isset($GLOBALS['user']->timezone) ? $GLOBALS['user']->timezone : 'UTC';
  3400. }
  3401. // If the timezone is still empty or not set, use the site timezone.
  3402. if (empty($timezone_name) || $timezone_name == 'user') {
  3403. $timezone_name = variable_get('date_default_timezone', 'UTC');
  3404. }
  3405. if (!empty($timezone_name) && class_exists('DateTimeZone')) {
  3406. // Suppress errors if encountered during string conversion. Exceptions are
  3407. // only supported for DateTime in PHP 5.3 and higher.
  3408. try {
  3409. @$timezone = new DateTimeZone($timezone_name);
  3410. @$datetime = new DateTime($string, $timezone);
  3411. return @$datetime->format($format);
  3412. }
  3413. catch (Exception $e) {
  3414. return '';
  3415. }
  3416. }
  3417. else {
  3418. return date($format, strtotime($string));
  3419. }
  3420. }
  3421. /**
  3422. * Get a timestamp in GMT time, ensuring timezone accuracy.
  3423. */
  3424. function webform_strtotime($date) {
  3425. $current_tz = date_default_timezone_get();
  3426. date_default_timezone_set('UTC');
  3427. $timestamp = strtotime($date);
  3428. date_default_timezone_set($current_tz);
  3429. return $timestamp;
  3430. }
  3431. /**
  3432. * Wrapper function for i18n_string() if i18nstrings enabled.
  3433. */
  3434. function webform_tt($name, $string, $langcode = NULL, $update = FALSE) {
  3435. if (function_exists('i18n_string')) {
  3436. $options = array(
  3437. 'langcode' => $langcode,
  3438. 'update' => $update,
  3439. );
  3440. return i18n_string($name, $string, $options);
  3441. }
  3442. else {
  3443. return $string;
  3444. }
  3445. }
  3446. /**
  3447. * Check if any available HTML mail handlers are available for Webform to use.
  3448. */
  3449. function webform_email_html_capable() {
  3450. // TODO: Right now we only support MIME Mail and HTML Mail. Support others
  3451. // if available through a hook?
  3452. $supported_html_modules = array(
  3453. 'mimemail' => 'MimeMailSystem',
  3454. 'htmlmail' => 'HTMLMailSystem',
  3455. );
  3456. foreach ($supported_html_modules as $mail_module => $mail_system_name) {
  3457. if (module_exists($mail_module)) {
  3458. $mail_systems = variable_get('mail_system', array('default-system' => 'DefaultMailSystem'));
  3459. if (isset($mail_systems['webform'])) {
  3460. $enable = strpos($mail_systems['webform'], $mail_system_name) !== FALSE ? $mail_systems['webform'] : FALSE;
  3461. }
  3462. else {
  3463. $enable = $mail_system_name;
  3464. }
  3465. }
  3466. }
  3467. if (!empty($enable)) {
  3468. // We assume that if a solution exists even if it's not specified we should
  3469. // use it. Webform will specify if e-mails sent with the system are plain-
  3470. // text or not when sending each e-mail.
  3471. $GLOBALS['conf']['mail_system']['webform'] = $enable;
  3472. return TRUE;
  3473. }
  3474. return FALSE;
  3475. }
  3476. /**
  3477. * Implements hook_views_api().
  3478. */
  3479. function webform_views_api() {
  3480. return array(
  3481. 'api' => 2.0,
  3482. 'path' => drupal_get_path('module', 'webform') . '/views',
  3483. );
  3484. }
  3485. /**
  3486. * Implements hook_field_extra_fields().
  3487. */
  3488. function webform_field_extra_fields() {
  3489. $extra = array();
  3490. foreach (webform_variable_get('webform_node_types') as $type) {
  3491. $extra['node'][$type]['display']['webform'] = array(
  3492. 'label' => t('Webform'),
  3493. 'description' => t('Webform client form.'),
  3494. 'weight' => 10,
  3495. );
  3496. }
  3497. return $extra;
  3498. }
  3499. /**
  3500. * Implements hook_mollom_form_list().
  3501. */
  3502. function webform_mollom_form_list() {
  3503. $forms = array();
  3504. $webform_types = webform_variable_get('webform_node_types');
  3505. if (empty($webform_types)) {
  3506. return $forms;
  3507. }
  3508. $query = db_select('webform', 'w');
  3509. $query->innerJoin('node', 'n', 'n.nid = w.nid');
  3510. $query->fields('n', array('nid', 'title'));
  3511. $query->condition('n.type', $webform_types, 'IN');
  3512. $result = $query->execute();
  3513. foreach ($result as $node) {
  3514. $form_id = 'webform_client_form_' . $node->nid;
  3515. $forms[$form_id] = array(
  3516. 'title' => t('@name form', array('@name' => $node->title)),
  3517. 'entity' => 'webform',
  3518. 'delete form' => 'webform_submission_delete_form',
  3519. );
  3520. }
  3521. return $forms;
  3522. }
  3523. /**
  3524. * Implements hook_mollom_form_info().
  3525. */
  3526. function webform_mollom_form_info($form_id) {
  3527. module_load_include('inc', 'webform', 'includes/webform.components');
  3528. $nid = drupal_substr($form_id, 20);
  3529. $node = node_load($nid);
  3530. $form_info = array(
  3531. 'title' => t('@name form', array('@name' => $node->title)),
  3532. 'mode' => MOLLOM_MODE_ANALYSIS,
  3533. 'bypass access' => array('edit all webform submissions', 'edit any webform content'),
  3534. 'entity' => 'webform',
  3535. 'elements' => array(),
  3536. 'mapping' => array(
  3537. 'post_id' => 'details][sid',
  3538. 'author_id' => 'details][uid',
  3539. ),
  3540. );
  3541. // Add components as elements.
  3542. // These components can be enabled for textual analysis (when not using a
  3543. // CAPTCHA-only protection) in Mollom's form configuration.
  3544. foreach ($node->webform['components'] as $cid => $component) {
  3545. if (webform_component_feature($component['type'], 'spam_analysis')) {
  3546. $parents = implode('][', webform_component_parent_keys($node, $component));
  3547. $form_info['elements']['submitted][' . $parents] = check_plain(t($component['name']));
  3548. }
  3549. }
  3550. // Assign field mappings based on webform configuration.
  3551. // Since multiple emails can be configured, we iterate over all and take
  3552. // over the assigned component for the field mapping in any email, unless
  3553. // we already assigned one. We are not interested in administratively
  3554. // configured static strings, only user-submitted values.
  3555. foreach ($node->webform['emails'] as $email) {
  3556. // Subject (post_title).
  3557. if (!isset($form_info['mapping']['post_title'])) {
  3558. $cid = $email['subject'];
  3559. if (is_numeric($cid)) {
  3560. $parents = implode('][', webform_component_parent_keys($node, $node->webform['components'][$cid]));
  3561. $form_info['mapping']['post_title'] = 'submitted][' . $parents;
  3562. }
  3563. }
  3564. // From name (author_name).
  3565. if (!isset($form_info['mapping']['author_name'])) {
  3566. $cid = $email['from_name'];
  3567. if (is_numeric($cid)) {
  3568. $parents = implode('][', webform_component_parent_keys($node, $node->webform['components'][$cid]));
  3569. $form_info['mapping']['author_name'] = 'submitted][' . $parents;
  3570. }
  3571. }
  3572. // From address (author_mail).
  3573. if (!isset($form_info['mapping']['author_mail'])) {
  3574. $cid = $email['from_address'];
  3575. if (is_numeric($cid)) {
  3576. $parents = implode('][', webform_component_parent_keys($node, $node->webform['components'][$cid]));
  3577. $form_info['mapping']['author_mail'] = 'submitted][' . $parents;
  3578. }
  3579. }
  3580. }
  3581. return $form_info;
  3582. }