webform.module 146 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983
  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 or deny file 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. else {
  990. return -1;
  991. }
  992. }
  993. // Grant access to files uploaded by a user before the submission is saved.
  994. elseif (!empty($file) && !empty($_SESSION['webform_files'][$file->fid])) {
  995. return file_get_content_headers($file);
  996. }
  997. // Ensure we never completely ignore a webform file request.
  998. if (strpos(file_uri_target($uri), 'webform/') === 0) {
  999. // The file is not part of a submission or a submission-in-progress (by
  1000. // the current user), however it may be part of a submission-in-progress
  1001. // (or an abandoned submission) by another user. We assume that all files
  1002. // under our enforced directory prefix are in fact webform files, and so
  1003. // we deny access to the file. Abandoned uploads will be deleted by
  1004. // system_cron() in due course.
  1005. return -1;
  1006. }
  1007. }
  1008. /**
  1009. * Implements hook_node_type().
  1010. *
  1011. * Not a real hook in Drupal 7. Re-used for consistency with the D6 version.
  1012. */
  1013. function webform_node_type($op, $info) {
  1014. $webform_types = webform_variable_get('webform_node_types');
  1015. $affected_type = isset($info->old_type) ? $info->old_type : $info->type;
  1016. $key = array_search($affected_type, $webform_types);
  1017. if ($key !== FALSE) {
  1018. if ($op == 'update') {
  1019. $webform_types[$key] = $info->type;
  1020. }
  1021. if ($op == 'delete') {
  1022. unset($webform_types[$key]);
  1023. }
  1024. variable_set('webform_node_types', $webform_types);
  1025. }
  1026. }
  1027. /**
  1028. * Implements hook_node_type_update().
  1029. */
  1030. function webform_node_type_update($info) {
  1031. webform_node_type('update', $info);
  1032. }
  1033. /**
  1034. * Implements hook_node_type_delete().
  1035. */
  1036. function webform_node_type_delete($info) {
  1037. webform_node_type('delete', $info);
  1038. }
  1039. /**
  1040. * Implements hook_node_insert().
  1041. */
  1042. function webform_node_insert($node) {
  1043. if (!in_array($node->type, webform_variable_get('webform_node_types'))) {
  1044. return;
  1045. }
  1046. // If added directly through node_save(), set defaults for the node.
  1047. if (!isset($node->webform)) {
  1048. $node->webform = webform_node_defaults();
  1049. }
  1050. // Do not make an entry if this node does not have any Webform settings.
  1051. if ($node->webform == webform_node_defaults() && !in_array($node->type, webform_variable_get('webform_node_types_primary'))) {
  1052. return;
  1053. }
  1054. module_load_include('inc', 'webform', 'includes/webform.components');
  1055. module_load_include('inc', 'webform', 'includes/webform.emails');
  1056. // Insert the webform.
  1057. $node->webform['nid'] = $node->nid;
  1058. $node->webform['record_exists'] = (bool) drupal_write_record('webform', $node->webform);
  1059. // Insert the components into the database. Used with clone.module.
  1060. if (isset($node->webform['components']) && !empty($node->webform['components'])) {
  1061. foreach ($node->webform['components'] as $cid => $component) {
  1062. $component['nid'] = $node->nid; // Required for clone.module.
  1063. webform_component_insert($component);
  1064. }
  1065. }
  1066. // Insert emails. Also used with clone.module.
  1067. if (isset($node->webform['emails']) && !empty($node->webform['emails'])) {
  1068. foreach ($node->webform['emails'] as $eid => $email) {
  1069. $email['nid'] = $node->nid;
  1070. webform_email_insert($email);
  1071. }
  1072. }
  1073. // Set the per-role submission access control.
  1074. foreach (array_filter($node->webform['roles']) as $rid) {
  1075. db_insert('webform_roles')->fields(array('nid' => $node->nid, 'rid' => $rid))->execute();
  1076. }
  1077. // Flush the block cache if creating a block.
  1078. if ($node->webform['block']) {
  1079. block_flush_caches();
  1080. }
  1081. }
  1082. /**
  1083. * Implements hook_node_update().
  1084. */
  1085. function webform_node_update($node) {
  1086. if (!in_array($node->type, webform_variable_get('webform_node_types'))) {
  1087. return;
  1088. }
  1089. // Check if this node needs a webform record at all. If it matches the
  1090. // defaults, any existing record will be deleted.
  1091. webform_check_record($node);
  1092. // If a webform row doesn't even exist, we can assume it needs to be inserted.
  1093. // If the the webform matches the defaults, no row will be inserted.
  1094. if (!$node->webform['record_exists']) {
  1095. webform_node_insert($node);
  1096. return;
  1097. }
  1098. // Update the webform entry.
  1099. $node->webform['nid'] = $node->nid;
  1100. drupal_write_record('webform', $node->webform, array('nid'));
  1101. // Compare the webform components and don't do anything if it's not needed.
  1102. // The internal cache needs to be reset here so that the cached node entity
  1103. // does not get loaded and invalidate the comparisons.
  1104. $original = node_load($node->nid, NULL, TRUE);
  1105. if ($original->webform['components'] != $node->webform['components']) {
  1106. module_load_include('inc', 'webform', 'includes/webform.components');
  1107. $original_cids = array_keys($original->webform['components']);
  1108. $current_cids = array_keys($node->webform['components']);
  1109. $all_cids = array_unique(array_merge($original_cids, $current_cids));
  1110. $deleted_cids = array_diff($original_cids, $current_cids);
  1111. $inserted_cids = array_diff($current_cids, $original_cids);
  1112. foreach ($all_cids as $cid) {
  1113. if (in_array($cid, $inserted_cids)) {
  1114. webform_component_insert($node->webform['components'][$cid]);
  1115. }
  1116. elseif (in_array($cid, $deleted_cids)) {
  1117. webform_component_delete($node, $original->webform['components'][$cid]);
  1118. }
  1119. elseif ($node->webform['components'][$cid] != $original->webform['components'][$cid]) {
  1120. $node->webform['components'][$cid]['nid'] = $node->nid;
  1121. webform_component_update($node->webform['components'][$cid]);
  1122. }
  1123. }
  1124. }
  1125. // Compare the webform e-mails and don't do anything if it's not needed.
  1126. if ($original->webform['emails'] != $node->webform['emails']) {
  1127. module_load_include('inc', 'webform', 'includes/webform.emails');
  1128. $original_eids = array_keys($original->webform['emails']);
  1129. $current_eids = array_keys($node->webform['emails']);
  1130. $all_eids = array_unique(array_merge($original_eids, $current_eids));
  1131. $deleted_eids = array_diff($original_eids, $current_eids);
  1132. $inserted_eids = array_diff($current_eids, $original_eids);
  1133. foreach ($all_eids as $eid) {
  1134. if (in_array($eid, $inserted_eids)) {
  1135. webform_email_insert($node->webform['emails'][$eid]);
  1136. }
  1137. elseif (in_array($eid, $deleted_eids)) {
  1138. webform_email_delete($node, $original->webform['emails'][$eid]);
  1139. }
  1140. elseif ($node->webform['emails'][$eid] != $original->webform['emails'][$eid]) {
  1141. $node->webform['emails'][$eid]['nid'] = $node->nid;
  1142. webform_email_update($node->webform['emails'][$eid]);
  1143. }
  1144. }
  1145. }
  1146. // Just delete and re-insert roles if they've changed.
  1147. if ($original->webform['roles'] != $node->webform['roles']) {
  1148. db_delete('webform_roles')->condition('nid', $node->nid)->execute();
  1149. foreach (array_filter($node->webform['roles']) as $rid) {
  1150. db_insert('webform_roles')->fields(array('nid' => $node->nid, 'rid' => $rid))->execute();
  1151. }
  1152. }
  1153. // Flush the block cache if block settings have been changed.
  1154. if ($node->webform['block'] != $original->webform['block']) {
  1155. block_flush_caches();
  1156. }
  1157. }
  1158. /**
  1159. * Implements hook_node_delete().
  1160. */
  1161. function webform_node_delete($node) {
  1162. if (!in_array($node->type, webform_variable_get('webform_node_types'))) {
  1163. return;
  1164. }
  1165. // Allow components clean up extra data, such as uploaded files.
  1166. module_load_include('inc', 'webform', 'includes/webform.components');
  1167. foreach ($node->webform['components'] as $cid => $component) {
  1168. webform_component_delete($node, $component);
  1169. }
  1170. // Remove any trace of webform data from the database.
  1171. db_delete('webform')->condition('nid', $node->nid)->execute();
  1172. db_delete('webform_component')->condition('nid', $node->nid)->execute();
  1173. db_delete('webform_emails')->condition('nid', $node->nid)->execute();
  1174. db_delete('webform_roles')->condition('nid', $node->nid)->execute();
  1175. db_delete('webform_submissions')->condition('nid', $node->nid)->execute();
  1176. db_delete('webform_submitted_data')->condition('nid', $node->nid)->execute();
  1177. db_delete('webform_last_download')->condition('nid', $node->nid)->execute();
  1178. }
  1179. /**
  1180. * Default settings for a newly created webform node.
  1181. */
  1182. function webform_node_defaults() {
  1183. $defaults = array(
  1184. 'confirmation' => '',
  1185. 'confirmation_format' => NULL,
  1186. 'redirect_url' => '<confirmation>',
  1187. 'teaser' => '0',
  1188. 'block' => '0',
  1189. 'allow_draft' => '0',
  1190. 'auto_save' => '0',
  1191. 'submit_notice' => '1',
  1192. 'submit_text' => '',
  1193. 'submit_limit' => '-1',
  1194. 'submit_interval' => '-1',
  1195. 'total_submit_limit' => '-1',
  1196. 'total_submit_interval' => '-1',
  1197. 'status' => '1',
  1198. 'record_exists' => FALSE,
  1199. 'roles' => array('1', '2'),
  1200. 'emails' => array(),
  1201. 'components' => array(),
  1202. );
  1203. drupal_alter('webform_node_defaults', $defaults);
  1204. return $defaults;
  1205. }
  1206. /**
  1207. * Implements hook_node_prepare().
  1208. */
  1209. function webform_node_prepare($node) {
  1210. $webform_types = webform_variable_get('webform_node_types');
  1211. if (in_array($node->type, $webform_types) && !isset($node->webform)) {
  1212. $node->webform = webform_node_defaults();
  1213. }
  1214. }
  1215. /**
  1216. * Implements hook_node_load().
  1217. */
  1218. function webform_node_load($nodes, $types) {
  1219. // Quick check to see if we need to do anything at all for these nodes.
  1220. $webform_types = webform_variable_get('webform_node_types');
  1221. if (count(array_intersect($types, $webform_types)) == 0) {
  1222. return;
  1223. }
  1224. module_load_include('inc', 'webform', 'includes/webform.components');
  1225. // Select all webforms that match these node IDs.
  1226. $result = db_select('webform')
  1227. ->fields('webform')
  1228. ->condition('nid', array_keys($nodes), 'IN')
  1229. ->execute()
  1230. ->fetchAllAssoc('nid', PDO::FETCH_ASSOC);
  1231. foreach ($result as $nid => $webform) {
  1232. // Load the basic information for each node.
  1233. $nodes[$nid]->webform = $webform;
  1234. $nodes[$nid]->webform['record_exists'] = TRUE;
  1235. }
  1236. // Load the components, emails, and defaults for all webform-enabled nodes.
  1237. // TODO: Increase efficiency here by pulling in all information all at once
  1238. // instead of individual queries.
  1239. foreach ($nodes as $nid => $node) {
  1240. if (!in_array($node->type, $webform_types)) {
  1241. continue;
  1242. }
  1243. // If a webform record doesn't exist, just return the defaults.
  1244. if (!isset($nodes[$nid]->webform)) {
  1245. $nodes[$nid]->webform = webform_node_defaults();
  1246. continue;
  1247. }
  1248. $nodes[$nid]->webform['roles'] = db_select('webform_roles')
  1249. ->fields('webform_roles', array('rid'))
  1250. ->condition('nid', $nid)
  1251. ->execute()
  1252. ->fetchCol();
  1253. $nodes[$nid]->webform['emails'] = db_select('webform_emails')
  1254. ->fields('webform_emails')
  1255. ->condition('nid', $nid)
  1256. ->execute()
  1257. ->fetchAllAssoc('eid', PDO::FETCH_ASSOC);
  1258. // Unserialize the exclude component list for e-mails.
  1259. foreach ($nodes[$nid]->webform['emails'] as $eid => $email) {
  1260. $nodes[$nid]->webform['emails'][$eid]['excluded_components'] = array_filter(explode(',', $email['excluded_components']));
  1261. if (variable_get('webform_format_override', 0)) {
  1262. $nodes[$nid]->webform['emails'][$eid]['html'] = variable_get('webform_default_format', 0);
  1263. }
  1264. }
  1265. // Load components for each node.
  1266. $nodes[$nid]->webform['components'] = db_select('webform_component')
  1267. ->fields('webform_component')
  1268. ->condition('nid', $nid)
  1269. ->orderBy('weight')
  1270. ->orderBy('name')
  1271. ->execute()
  1272. ->fetchAllAssoc('cid', PDO::FETCH_ASSOC);
  1273. // Do a little cleanup on each component.
  1274. foreach ($nodes[$nid]->webform['components'] as $cid => $component) {
  1275. $nodes[$nid]->webform['components'][$cid]['nid'] = $nid;
  1276. $nodes[$nid]->webform['components'][$cid]['extra'] = unserialize($component['extra']);
  1277. webform_component_defaults($nodes[$nid]->webform['components'][$cid]);
  1278. }
  1279. // Organize the components into a fieldset-based order.
  1280. if (!empty($nodes[$nid]->webform['components'])) {
  1281. $component_tree = array();
  1282. $page_count = 1;
  1283. _webform_components_tree_build($nodes[$nid]->webform['components'], $component_tree, 0, $page_count);
  1284. $nodes[$nid]->webform['components'] = _webform_components_tree_flatten($component_tree['children']);
  1285. }
  1286. }
  1287. }
  1288. /**
  1289. * Implements hook_form_alter().
  1290. */
  1291. function webform_form_alter(&$form, $form_state, $form_id) {
  1292. $matches = array();
  1293. if (isset($form['#node']->type) && $form_id == $form['#node']->type . '_node_form' && in_array($form['#node']->type, webform_variable_get('webform_node_types'))) {
  1294. $node = $form['#node'];
  1295. // Preserve all Webform options currently set on the node.
  1296. $form['webform'] = array(
  1297. '#type' => 'value',
  1298. '#value' => $node->webform,
  1299. );
  1300. // If a new node, redirect the user to the components form after save.
  1301. if (empty($node->nid) && in_array($node->type, webform_variable_get('webform_node_types_primary'))) {
  1302. $form['actions']['submit']['#submit'][] = 'webform_form_submit';
  1303. }
  1304. }
  1305. }
  1306. /**
  1307. * Submit handler for the webform node form.
  1308. *
  1309. * Redirect the user to the components form on new node inserts. Note that this
  1310. * fires after the hook_submit() function above.
  1311. */
  1312. function webform_form_submit($form, &$form_state) {
  1313. 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'])));
  1314. $form_state['redirect'] = 'node/' . $form_state['nid'] . '/webform/components';
  1315. }
  1316. /**
  1317. * Implements hook_node_view().
  1318. */
  1319. function webform_node_view($node, $view_mode) {
  1320. global $user;
  1321. if (!in_array($node->type, webform_variable_get('webform_node_types'))) {
  1322. return;
  1323. }
  1324. // Set teaser and page variables a la Drupal 6.
  1325. $teaser = $view_mode == 'teaser';
  1326. $page = arg(0) == 'node' && arg(1) == $node->nid;
  1327. // If empty, a teaser, or a new node (during preview) do not display.
  1328. if (empty($node->webform['components']) || ($teaser && !$node->webform['teaser']) || empty($node->nid)) {
  1329. return;
  1330. }
  1331. // Do not include the form in the search index if indexing is disabled.
  1332. if (module_exists('search') && $view_mode == 'search_index' && !variable_get('webform_search_index', 1)) {
  1333. return;
  1334. }
  1335. // If the webform is not set to display in this view mode, return early.
  1336. // View mode of 'form' is exempted to allow blocks and views to force display.
  1337. $extra_fields = field_extra_fields_get_display('node', $node->type, $view_mode);
  1338. if ($view_mode != 'form' && empty($extra_fields['webform']['visible'])) {
  1339. return;
  1340. }
  1341. $info = array();
  1342. $submission = array();
  1343. $submission_count = 0;
  1344. $enabled = TRUE;
  1345. $logging_in = FALSE;
  1346. $total_limit_exceeded = FALSE;
  1347. $user_limit_exceeded = FALSE;
  1348. $closed = FALSE;
  1349. $allowed_roles = array();
  1350. // If a teaser, tell the form to load subsequent pages on the node page.
  1351. if ($teaser && !isset($node->webform['action'])) {
  1352. $query = array_diff_key($_GET, array('q' => ''));
  1353. $node->webform['action'] = url('node/' . $node->nid, array('query' => $query));
  1354. }
  1355. // When logging in using a form on the same page as a webform node, suppress
  1356. // output messages so that they don't show up after the user has logged in.
  1357. // See http://drupal.org/node/239343.
  1358. if (isset($_POST['op']) && isset($_POST['name']) && isset($_POST['pass'])) {
  1359. $logging_in = TRUE;
  1360. }
  1361. if ($node->webform['status'] == 0) {
  1362. $closed = TRUE;
  1363. $enabled = FALSE;
  1364. }
  1365. else {
  1366. // Check if the user's role can submit this webform.
  1367. if (variable_get('webform_submission_access_control', 1)) {
  1368. foreach ($node->webform['roles'] as $rid) {
  1369. $allowed_roles[$rid] = isset($user->roles[$rid]) ? TRUE : FALSE;
  1370. }
  1371. if (array_search(TRUE, $allowed_roles) === FALSE && $user->uid != 1) {
  1372. $enabled = FALSE;
  1373. }
  1374. }
  1375. else {
  1376. // If not using Webform submission access control, allow for all roles.
  1377. $allowed_roles = array_keys(user_roles());
  1378. }
  1379. }
  1380. // Get a count of previous submissions by this user. Note that the
  1381. // webform_submission_access() function may disable the page cache for
  1382. // anonymous users if they are allowed to edit their own submissions!
  1383. if ($page && webform_submission_access($node, NULL, 'list')) {
  1384. module_load_include('inc', 'webform', 'includes/webform.submissions');
  1385. $submission_count = webform_get_submission_count($node->nid, $user->uid);
  1386. }
  1387. // Check if this page is cached or not.
  1388. $cached = $user->uid == 0 && (variable_get('cache', 0) || drupal_page_is_cacheable() === FALSE);
  1389. // Check if the user can add another submission.
  1390. if ($node->webform['submit_limit'] != -1) { // -1: Submissions are never throttled.
  1391. module_load_include('inc', 'webform', 'includes/webform.submissions');
  1392. // Disable the form if the limit is exceeded and page cache is not active.
  1393. if (($user_limit_exceeded = _webform_submission_user_limit_check($node)) && !$cached) {
  1394. $enabled = FALSE;
  1395. }
  1396. }
  1397. // Check if the user can add another submission if there is a limit on total
  1398. // submissions.
  1399. if ($node->webform['total_submit_limit'] != -1) { // -1: Submissions are never throttled.
  1400. module_load_include('inc', 'webform', 'includes/webform.submissions');
  1401. // Disable the form if the limit is exceeded and page cache is not active.
  1402. if (($total_limit_exceeded = _webform_submission_total_limit_check($node)) && !$cached) {
  1403. $enabled = FALSE;
  1404. }
  1405. }
  1406. // Check if this user has a draft for this webform.
  1407. $is_draft = FALSE;
  1408. if (($node->webform['allow_draft'] || $node->webform['auto_save']) && $user->uid != 0) {
  1409. // Draft found - display form with draft data for further editing.
  1410. if ($draft_sid = _webform_fetch_draft_sid($node->nid, $user->uid)) {
  1411. module_load_include('inc', 'webform', 'includes/webform.submissions');
  1412. $submission = webform_get_submission($node->nid, $draft_sid);
  1413. $enabled = TRUE;
  1414. $is_draft = TRUE;
  1415. }
  1416. }
  1417. // Avoid building the same form twice on the same page request (which can
  1418. // happen if the webform is displayed in a panel or block) because this
  1419. // causes multistep forms to build incorrectly the second time.
  1420. $cached_forms = &drupal_static(__FUNCTION__, array());
  1421. if (isset($cached_forms[$node->nid])) {
  1422. $form = $cached_forms[$node->nid];
  1423. }
  1424. // If this is the first time, generate the form array.
  1425. else {
  1426. $form = drupal_get_form('webform_client_form_' . $node->nid, $node, $submission, $is_draft);
  1427. $cached_forms[$node->nid] = $form;
  1428. }
  1429. // Remove the surrounding <form> tag if this is a preview.
  1430. if (!empty($node->in_preview)) {
  1431. $form['#type'] = 'markup';
  1432. }
  1433. // Print out messages for the webform.
  1434. if (empty($node->in_preview) && !isset($node->webform_block) && !$logging_in) {
  1435. 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));
  1436. }
  1437. // Add the output to the node.
  1438. $node->content['webform'] = array(
  1439. '#theme' => 'webform_view',
  1440. '#node' => $node,
  1441. '#teaser' => $teaser,
  1442. '#page' => $page,
  1443. '#form' => $form,
  1444. '#enabled' => $enabled,
  1445. '#weight' => 10,
  1446. );
  1447. }
  1448. /**
  1449. * Output the Webform into the node content.
  1450. *
  1451. * @param $node
  1452. * The webform node object.
  1453. * @param $teaser
  1454. * If this webform is being displayed as the teaser view of the node.
  1455. * @param $page
  1456. * If this webform node is being viewed as the main content of the page.
  1457. * @param $form
  1458. * The rendered form.
  1459. * @param $enabled
  1460. * If the form allowed to be completed by the current user.
  1461. */
  1462. function theme_webform_view($variables) {
  1463. // Only show the form if this user is allowed access.
  1464. if ($variables['webform']['#enabled']) {
  1465. return drupal_render($variables['webform']['#form']);
  1466. }
  1467. }
  1468. /**
  1469. * Display a message to a user if they are not allowed to fill out a form.
  1470. *
  1471. * @param $node
  1472. * The webform node object.
  1473. * @param $teaser
  1474. * If this webform is being displayed as the teaser view of the node.
  1475. * @param $page
  1476. * If this webform node is being viewed as the main content of the page.
  1477. * @param $submission_count
  1478. * The number of submissions this user has already submitted. Not calculated
  1479. * for anonymous users.
  1480. * @param $user_limit_exceeded
  1481. * Boolean value if the submission limit for this user has been exceeded.
  1482. * @param $total_limit_exceeded
  1483. * Boolean value if the total submission limit has been exceeded.
  1484. * @param $allowed_roles
  1485. * A list of user roles that are allowed to submit this webform.
  1486. * @param $closed
  1487. * Boolean value if submissions are closed.
  1488. */
  1489. function theme_webform_view_messages($variables) {
  1490. global $user;
  1491. $node = $variables['node'];
  1492. $teaser = $variables['teaser'];
  1493. $page = $variables['page'];
  1494. $submission_count = $variables['submission_count'];
  1495. $user_limit_exceeded = $variables['user_limit_exceeded'];
  1496. $total_limit_exceeded = $variables['total_limit_exceeded'];
  1497. $allowed_roles = $variables['allowed_roles'];
  1498. $closed = $variables['closed'];
  1499. $cached = $variables['cached'];
  1500. $type = 'status';
  1501. if ($closed) {
  1502. $message = t('Submissions for this form are closed.');
  1503. }
  1504. // If open and not allowed to submit the form, give an explanation.
  1505. elseif (array_search(TRUE, $allowed_roles) === FALSE && $user->uid != 1) {
  1506. if (empty($allowed_roles)) {
  1507. // No roles are allowed to submit the form.
  1508. $message = t('Submissions for this form are closed.');
  1509. }
  1510. elseif (isset($allowed_roles[2])) {
  1511. // The "authenticated user" role is allowed to submit and the user is currently logged-out.
  1512. $login = url('user/login', array('query' => drupal_get_destination()));
  1513. $register = url('user/register', array('query' => drupal_get_destination()));
  1514. if (variable_get('user_register', 1) == 0) {
  1515. $message = t('You must <a href="!login">login</a> to view this form.', array('!login' => $login));
  1516. }
  1517. else {
  1518. $message = t('You must <a href="!login">login</a> or <a href="!register">register</a> to view this form.', array('!login' => $login, '!register' => $register));
  1519. }
  1520. }
  1521. else {
  1522. // The user must be some other role to submit.
  1523. $message = t('You do not have permission to view this form.');
  1524. $type = 'error';
  1525. }
  1526. }
  1527. // If the user has exceeded the limit of submissions, explain the limit.
  1528. elseif ($user_limit_exceeded && !$cached) {
  1529. if ($node->webform['submit_interval'] == -1 && $node->webform['submit_limit'] > 1) {
  1530. $message = t('You have submitted this form the maximum number of times (@count).', array('@count' => $node->webform['submit_limit']));
  1531. }
  1532. elseif ($node->webform['submit_interval'] == -1 && $node->webform['submit_limit'] == 1) {
  1533. $message = t('You have already submitted this form.');
  1534. }
  1535. else {
  1536. $message = t('You may not submit another entry at this time.');
  1537. }
  1538. $type = 'error';
  1539. }
  1540. elseif ($total_limit_exceeded && !$cached) {
  1541. if ($node->webform['total_submit_interval'] == -1 && $node->webform['total_submit_limit'] > 1) {
  1542. $message = t('This form has received the maximum number of entries.');
  1543. }
  1544. else {
  1545. $message = t('You may not submit another entry at this time.');
  1546. }
  1547. }
  1548. // If the user has submitted before, give them a link to their submissions.
  1549. if ($submission_count > 0 && $node->webform['submit_notice'] == 1 && !$cached) {
  1550. if (empty($message)) {
  1551. $message = t('You have already submitted this form.') . ' ' . t('<a href="!url">View your previous submissions</a>.', array('!url' => url('node/' . $node->nid . '/submissions')));
  1552. }
  1553. else {
  1554. $message .= ' ' . t('<a href="!url">View your previous submissions</a>.', array('!url' => url('node/' . $node->nid . '/submissions')));
  1555. }
  1556. }
  1557. if ($page && isset($message)) {
  1558. drupal_set_message($message, $type, FALSE);
  1559. }
  1560. }
  1561. /**
  1562. * Implements hook_mail().
  1563. */
  1564. function webform_mail($key, &$message, $params) {
  1565. $message['headers'] = array_merge($message['headers'], $params['headers']);
  1566. $message['subject'] = $params['subject'];
  1567. $message['body'][] = $params['message'];
  1568. }
  1569. /**
  1570. * Implements hook_block_info().
  1571. */
  1572. function webform_block_info() {
  1573. $blocks = array();
  1574. $webform_node_types = webform_variable_get('webform_node_types');
  1575. if (!empty($webform_node_types)) {
  1576. $query = db_select('webform', 'w')->fields('w')->fields('n', array('title'));
  1577. $query->leftJoin('node', 'n', 'w.nid = n.nid');
  1578. $query->condition('w.block', 1);
  1579. $query->condition('n.type', $webform_node_types, 'IN');
  1580. $result = $query->execute();
  1581. foreach ($result as $data) {
  1582. $blocks['client-block-' . $data->nid] = array(
  1583. 'info' => t('Webform: !title', array('!title' => $data->title)),
  1584. 'cache' => DRUPAL_NO_CACHE,
  1585. );
  1586. }
  1587. }
  1588. return $blocks;
  1589. }
  1590. /**
  1591. * Implements hook_block_view().
  1592. */
  1593. function webform_block_view($delta = '') {
  1594. global $user;
  1595. // Load the block-specific configuration settings.
  1596. $webform_blocks = variable_get('webform_blocks', array());
  1597. $settings = isset($webform_blocks[$delta]) ? $webform_blocks[$delta] : array();
  1598. $settings += array(
  1599. 'display' => 'form',
  1600. 'pages_block' => 0,
  1601. );
  1602. // Get the node ID from delta.
  1603. $nid = drupal_substr($delta, strrpos($delta, '-') + 1);
  1604. // Load node in current language.
  1605. if (module_exists('translation')) {
  1606. global $language;
  1607. if (($translations = translation_node_get_translations($nid)) && (isset($translations[$language->language]))) {
  1608. $nid = $translations[$language->language]->nid;
  1609. }
  1610. }
  1611. // The webform node to display in the block.
  1612. $node = node_load($nid);
  1613. // Return if user has no access to the webform node.
  1614. if (!node_access('view', $node)) {
  1615. return;
  1616. }
  1617. // This is a webform node block.
  1618. $node->webform_block = TRUE;
  1619. // If not displaying pages in the block, set the #action property on the form.
  1620. if ($settings['pages_block']) {
  1621. $node->webform['action'] = FALSE;
  1622. }
  1623. else {
  1624. $query = array_diff_key($_GET, array('q' => ''));
  1625. $node->webform['action'] = url('node/' . $node->nid, array('query' => $query));
  1626. }
  1627. // Generate the content of the block based on display settings.
  1628. if ($settings['display'] == 'form') {
  1629. webform_node_view($node, 'form');
  1630. $content = isset($node->content['webform']) ? $node->content['webform'] : array();
  1631. }
  1632. else {
  1633. $teaser = ($settings['display'] == 'teaser') ? 'teaser' : 'full';
  1634. $content = node_view($node, $teaser);
  1635. }
  1636. // Add contextual links for the webform node if they aren't already there.
  1637. if (!isset($content['#contextual_links']['node'])) {
  1638. $content['#contextual_links']['node'] = array('node', array($node->nid));
  1639. }
  1640. // Create the block, using the node title for the block title.
  1641. // Note that we render the content immediately here rather than passing back
  1642. // a renderable so that if the block is empty it is hidden.
  1643. $block = array(
  1644. 'subject' => check_plain($node->title),
  1645. 'content' => drupal_render($content),
  1646. );
  1647. return $block;
  1648. }
  1649. /**
  1650. * Implements hook_block_configure().
  1651. */
  1652. function webform_block_configure($delta = '') {
  1653. // Load the block-specific configuration settings.
  1654. $webform_blocks = variable_get('webform_blocks', array());
  1655. $settings = isset($webform_blocks[$delta]) ? $webform_blocks[$delta] : array();
  1656. $settings += array(
  1657. 'display' => 'form',
  1658. 'pages_block' => 0,
  1659. );
  1660. $form = array();
  1661. $form['display'] = array(
  1662. '#type' => 'radios',
  1663. '#title' => t('Display mode'),
  1664. '#default_value' => $settings['display'],
  1665. '#options' => array(
  1666. 'form' => t('Form only'),
  1667. 'full' => t('Full node'),
  1668. 'teaser' => t('Teaser'),
  1669. ),
  1670. '#description' => t('The display mode determines how much of the webform to show within the block.'),
  1671. );
  1672. $form['pages_block'] = array(
  1673. '#type' => 'checkbox',
  1674. '#title' => t('Show all webform pages in block'),
  1675. '#default_value' => $settings['pages_block'],
  1676. '#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.'),
  1677. );
  1678. return $form;
  1679. }
  1680. /**
  1681. * Implements hook_block_save().
  1682. */
  1683. function webform_block_save($delta = '', $edit = array()) {
  1684. // Load the previously defined block-specific configuration settings.
  1685. $settings = variable_get('webform_blocks', array());
  1686. // Build the settings array.
  1687. $new_settings[$delta] = array(
  1688. 'display' => $edit['display'],
  1689. 'pages_block' => $edit['pages_block'],
  1690. );
  1691. // We store settings for multiple blocks in just one variable
  1692. // so we merge the existing settings with the new ones before save.
  1693. variable_set('webform_blocks', array_merge($settings, $new_settings));
  1694. }
  1695. /**
  1696. * Client form generation function. If this is displaying an existing
  1697. * submission, pass in the $submission variable with the contents of the
  1698. * submission to be displayed.
  1699. *
  1700. * @param $form
  1701. * The current form array (always empty).
  1702. * @param $form_state
  1703. * The current form values of a submission, used in multipage webforms.
  1704. * @param $node
  1705. * The current webform node.
  1706. * @param $submission
  1707. * An object containing information about the form submission if we're
  1708. * displaying a result.
  1709. * @param $is_draft
  1710. * Optional. Set to TRUE if displaying a draft.
  1711. * @param $filter
  1712. * Whether or not to filter the contents of descriptions and values when
  1713. * building the form. Values need to be unfiltered to be editable by
  1714. * Form Builder.
  1715. */
  1716. function webform_client_form($form, &$form_state, $node, $submission, $is_draft = FALSE, $filter = TRUE) {
  1717. global $user;
  1718. // Attach necessary JavaScript and CSS.
  1719. $form['#attached'] = array(
  1720. 'css' => array(drupal_get_path('module', 'webform') . '/css/webform.css'),
  1721. 'js' => array(drupal_get_path('module', 'webform') . '/js/webform.js'),
  1722. );
  1723. form_load_include($form_state, 'inc', 'webform', 'includes/webform.components');
  1724. form_load_include($form_state, 'inc', 'webform', 'includes/webform.submissions');
  1725. $form['#process'] = array(
  1726. 'webform_client_form_includes',
  1727. );
  1728. // If in a multi-step form, a submission ID may be specified in form state.
  1729. // Load this submission. This allows anonymous users to use auto-save.
  1730. if (empty($submission) && !empty($form_state['values']['details']['sid'])) {
  1731. $submission = webform_get_submission($node->nid, $form_state['values']['details']['sid']);
  1732. $is_draft = $submission->is_draft;
  1733. }
  1734. // Bind arguments to $form to make them available in theming and form_alter.
  1735. $form['#node'] = $node;
  1736. $form['#submission'] = $submission;
  1737. $form['#is_draft'] = $is_draft;
  1738. $form['#filter'] = $filter;
  1739. // Add a theme function for this form.
  1740. $form['#theme'] = array('webform_form_' . $node->nid, 'webform_form');
  1741. // Add a css class for all client forms.
  1742. $form['#attributes'] = array('class' => array('webform-client-form'));
  1743. // Set the encoding type (necessary for file uploads).
  1744. $form['#attributes']['enctype'] = 'multipart/form-data';
  1745. // Sometimes when displaying a webform as a teaser or block, a custom action
  1746. // property is set to direct the user to the node page.
  1747. if (!empty($node->webform['action'])) {
  1748. $form['#action'] = $node->webform['action'];
  1749. }
  1750. $form['#submit'] = array('webform_client_form_pages', 'webform_client_form_submit');
  1751. $form['#validate'] = array('webform_client_form_validate');
  1752. if (is_array($node->webform['components']) && !empty($node->webform['components'])) {
  1753. // Prepare a new form array.
  1754. $form['submitted'] = array(
  1755. '#tree' => TRUE
  1756. );
  1757. $form['details'] = array(
  1758. '#tree' => TRUE,
  1759. );
  1760. // Put the components into a tree structure.
  1761. if (!isset($form_state['storage']['component_tree'])) {
  1762. $form_state['webform']['component_tree'] = array();
  1763. $form_state['webform']['page_count'] = 1;
  1764. $form_state['webform']['page_num'] = 1;
  1765. _webform_components_tree_build($node->webform['components'], $form_state['webform']['component_tree'], 0, $form_state['webform']['page_count']);
  1766. }
  1767. else {
  1768. $form_state['webform']['component_tree'] = $form_state['storage']['component_tree'];
  1769. $form_state['webform']['page_count'] = $form_state['storage']['page_count'];
  1770. $form_state['webform']['page_num'] = $form_state['storage']['page_num'];
  1771. }
  1772. // Shorten up our variable names.
  1773. $component_tree = $form_state['webform']['component_tree'];
  1774. $page_count = $form_state['webform']['page_count'];
  1775. $page_num = $form_state['webform']['page_num'];
  1776. if ($page_count > 1) {
  1777. $next_page_labels = array();
  1778. $prev_page_labels = array();
  1779. }
  1780. // Recursively add components to the form. The unfiltered version of the
  1781. // form (typically used in Form Builder), includes all components.
  1782. foreach ($component_tree['children'] as $cid => $component) {
  1783. $component_value = isset($form_state['values']['submitted'][$cid]) ? $form_state['values']['submitted'][$cid] : NULL;
  1784. if ($filter == FALSE || _webform_client_form_rule_check($node, $component, $page_num, $form_state)) {
  1785. if ($component['type'] == 'pagebreak') {
  1786. $next_page_labels[$component['page_num'] - 1] = !empty($component['extra']['next_page_label']) ? t($component['extra']['next_page_label']) : t('Next Page >');
  1787. $prev_page_labels[$component['page_num']] = !empty($component['extra']['prev_page_label']) ? t($component['extra']['prev_page_label']) : t('< Previous Page');
  1788. }
  1789. _webform_client_form_add_component($node, $component, $component_value, $form['submitted'], $form, $form_state, $submission, 'form', $page_num, $filter);
  1790. }
  1791. }
  1792. // These form details help managing data upon submission.
  1793. $form['details']['nid'] = array(
  1794. '#type' => 'value',
  1795. '#value' => $node->nid,
  1796. );
  1797. $form['details']['sid'] = array(
  1798. '#type' => 'hidden',
  1799. '#value' => isset($submission->sid) ? $submission->sid : NULL,
  1800. );
  1801. $form['details']['uid'] = array(
  1802. '#type' => 'value',
  1803. '#value' => isset($submission->uid) ? $submission->uid : $user->uid,
  1804. );
  1805. $form['details']['page_num'] = array(
  1806. '#type' => 'hidden',
  1807. '#value' => $page_num,
  1808. );
  1809. $form['details']['page_count'] = array(
  1810. '#type' => 'hidden',
  1811. '#value' => $page_count,
  1812. );
  1813. $form['details']['finished'] = array(
  1814. '#type' => 'hidden',
  1815. '#value' => isset($submission->is_draft) ? (!$submission->is_draft) : 0,
  1816. );
  1817. // Add buttons for pages, drafts, and submissions.
  1818. $form['actions'] = array(
  1819. '#type' => 'actions',
  1820. '#weight' => 1000,
  1821. );
  1822. // Add the draft button.
  1823. if ($node->webform['allow_draft'] && (empty($submission) || $submission->is_draft) && $user->uid != 0) {
  1824. $form['actions']['draft'] = array(
  1825. '#type' => 'submit',
  1826. '#value' => t('Save Draft'),
  1827. '#weight' => -2,
  1828. '#validate' => array(),
  1829. '#attributes' => array('formnovalidate' => 'formnovalidate'),
  1830. );
  1831. }
  1832. if ($page_count > 1) {
  1833. // Add the submit button(s).
  1834. if ($page_num > 1) {
  1835. $form['actions']['previous'] = array(
  1836. '#type' => 'submit',
  1837. '#value' => $prev_page_labels[$page_num],
  1838. '#weight' => 5,
  1839. '#validate' => array(),
  1840. '#attributes' => array('formnovalidate' => 'formnovalidate'),
  1841. );
  1842. }
  1843. if ($page_num == $page_count) {
  1844. $form['actions']['submit'] = array(
  1845. '#type' => 'submit',
  1846. '#value' => empty($node->webform['submit_text']) ? t('Submit') : t($node->webform['submit_text']),
  1847. '#weight' => 10,
  1848. );
  1849. }
  1850. elseif ($page_num < $page_count) {
  1851. $form['actions']['next'] = array(
  1852. '#type' => 'submit',
  1853. '#value' => $next_page_labels[$page_num],
  1854. '#weight' => 10,
  1855. );
  1856. }
  1857. }
  1858. else {
  1859. // Add the submit button.
  1860. $form['actions']['submit'] = array(
  1861. '#type' => 'submit',
  1862. '#value' => empty($node->webform['submit_text']) ? t('Submit') : t($node->webform['submit_text']),
  1863. '#weight' => 10,
  1864. );
  1865. }
  1866. }
  1867. return $form;
  1868. }
  1869. /**
  1870. * Process function for webform_client_form().
  1871. *
  1872. * Include all the enabled components for this form to ensure availability.
  1873. */
  1874. function webform_client_form_includes($form, $form_state) {
  1875. $components = webform_components();
  1876. foreach ($components as $component_type => $component) {
  1877. webform_component_include($component_type);
  1878. }
  1879. return $form;
  1880. }
  1881. /**
  1882. * Check if a component should be displayed on the current page.
  1883. */
  1884. function _webform_client_form_rule_check($node, $component, $page_num, $form_state = NULL, $submission = NULL) {
  1885. $conditional_values = isset($component['extra']['conditional_values']) ? $component['extra']['conditional_values'] : NULL;
  1886. $conditional_component = isset($component['extra']['conditional_component']) && isset($node->webform['components'][$component['extra']['conditional_component']]) ? $node->webform['components'][$component['extra']['conditional_component']] : NULL;
  1887. $conditional_cid = $conditional_component['cid'];
  1888. // Check the rules for this entire page. Note individual page breaks are
  1889. // checked down below in the individual component rule checks.
  1890. $show_page = TRUE;
  1891. if ($component['page_num'] > 1 && $component['type'] != 'pagebreak') {
  1892. foreach ($node->webform['components'] as $cid => $page_component) {
  1893. if ($page_component['type'] == 'pagebreak' && $page_component['page_num'] == $page_num) {
  1894. $show_page = _webform_client_form_rule_check($node, $page_component, $page_num, $form_state, $submission);
  1895. break;
  1896. }
  1897. }
  1898. }
  1899. // Check any parents' visibility rules.
  1900. $show_parent = $show_page;
  1901. if ($show_parent && $component['pid'] && isset($node->webform['components'][$component['pid']])) {
  1902. $parent_component = $node->webform['components'][$component['pid']];
  1903. $show_parent = _webform_client_form_rule_check($node, $parent_component, $page_num, $form_state, $submission);
  1904. }
  1905. // Check the individual component rules.
  1906. $show_component = $show_parent;
  1907. if ($show_component && ($page_num == 0 || $component['page_num'] == $page_num) && $conditional_component && strlen(trim($conditional_values))) {
  1908. $input_values = array();
  1909. if (isset($form_state)) {
  1910. $input_value = isset($form_state['values']['submitted'][$conditional_cid]) ? $form_state['values']['submitted'][$conditional_cid] : NULL;
  1911. $input_values = is_array($input_value) ? $input_value : array($input_value);
  1912. }
  1913. elseif (isset($submission)) {
  1914. $input_values = isset($submission->data[$conditional_cid]['value']) ? $submission->data[$conditional_cid]['value'] : array();
  1915. }
  1916. $test_values = array_map('trim', explode("\n", $conditional_values));
  1917. if (empty($input_values) && !empty($test_values)) {
  1918. $show_component = FALSE;
  1919. }
  1920. else {
  1921. foreach ($input_values as $input_value) {
  1922. if ($show_component = in_array($input_value, $test_values)) {
  1923. break;
  1924. }
  1925. }
  1926. }
  1927. if ($component['extra']['conditional_operator'] == '!=') {
  1928. $show_component = !$show_component;
  1929. }
  1930. }
  1931. return $show_component;
  1932. }
  1933. /**
  1934. * Add a component to a renderable array. Called recursively for fieldsets.
  1935. *
  1936. * This function assists in the building of the client form, as well as the
  1937. * display of results, and the text of e-mails.
  1938. *
  1939. * @param $component
  1940. * The component to be added to the form.
  1941. * @param $component_value
  1942. * The components current value if known.
  1943. * @param $parent_fieldset
  1944. * The fieldset to which this element will be added.
  1945. * @param $form
  1946. * The entire form array.
  1947. * @param $form_state
  1948. * The form state.
  1949. * @param $submission
  1950. * The Webform submission as retrieved from the database.
  1951. * @param $format
  1952. * The format the form should be displayed as. May be one of the following:
  1953. * - form: Show as an editable form.
  1954. * - html: Show as HTML results.
  1955. * - text: Show as plain text.
  1956. * @param $filter
  1957. * Whether the form element properties should be filtered. Only set to FALSE
  1958. * if needing the raw properties for editing.
  1959. *
  1960. * @see webform_client_form()
  1961. * @see webform_submission_render()
  1962. */
  1963. function _webform_client_form_add_component($node, $component, $component_value, &$parent_fieldset, &$form, $form_state, $submission, $format = 'form', $page_num = 0, $filter = TRUE) {
  1964. $cid = $component['cid'];
  1965. $component_access = empty($component['extra']['private']) || webform_results_access($node);
  1966. // Load with submission information if necessary.
  1967. if ($format != 'form') {
  1968. // This component is display only.
  1969. $data = empty($submission->data[$cid]['value']) ? NULL : $submission->data[$cid]['value'];
  1970. if ($display_element = webform_component_invoke($component['type'], 'display', $component, $data, $format)) {
  1971. // Set access based on the private property.
  1972. $element['#access'] = $component_access;
  1973. // Ensure the component is added as a property.
  1974. $display_element['#webform_component'] = $component;
  1975. // Allow modules to modify a "display only" webform component.
  1976. drupal_alter('webform_component_display', $display_element, $component);
  1977. // The form_builder() function usually adds #parents and #id for us, but
  1978. // because these are not marked for #input, we need to add them manually.
  1979. if (!isset($display_element['#parents'])) {
  1980. $parents = isset($parent_fieldset['#parents']) ? $parent_fieldset['#parents'] : array('submitted');
  1981. $parents[] = $component['form_key'];
  1982. $display_element['#parents'] = $parents;
  1983. }
  1984. if (!isset($display_element['#id'])) {
  1985. $display_element['#id'] = drupal_clean_css_identifier('edit-' . implode('-', $display_element['#parents']));
  1986. }
  1987. // Add the element into the proper parent in the display.
  1988. $parent_fieldset[$component['form_key']] = $display_element;
  1989. }
  1990. }
  1991. // Show the component only on its form page, or if building an unfiltered
  1992. // version of the form (such as for Form Builder).
  1993. elseif ($component['page_num'] == $page_num || $filter == FALSE) {
  1994. // Add this user-defined field to the form (with all the values that are always available).
  1995. $data = isset($submission->data[$cid]['value']) ? $submission->data[$cid]['value'] : NULL;
  1996. if ($element = webform_component_invoke($component['type'], 'render', $component, $data, $filter)) {
  1997. // Set access based on the private property.
  1998. $element['#access'] = $component_access;
  1999. // Ensure the component is added as a property.
  2000. $element['#webform_component'] = $component;
  2001. // The 'private' option is in most components, but it's not a real
  2002. // property. Add it for Form Builder compatibility.
  2003. if (webform_component_feature($component['type'], 'private')) {
  2004. $element['#webform_private'] = $component['extra']['private'];
  2005. }
  2006. // Allow modules to modify a webform component that is going to be render in a form.
  2007. drupal_alter('webform_component_render', $element, $component);
  2008. // Add the element into the proper parent in the form.
  2009. $parent_fieldset[$component['form_key']] = $element;
  2010. // Override the value if one already exists in the form state.
  2011. if (isset($component_value)) {
  2012. $parent_fieldset[$component['form_key']]['#default_value'] = $component_value;
  2013. if (is_array($component_value)) {
  2014. foreach ($component_value as $key => $value) {
  2015. if (isset($parent_fieldset[$component['form_key']][$key])) {
  2016. $parent_fieldset[$component['form_key']][$key]['#default_value'] = $value;
  2017. }
  2018. }
  2019. }
  2020. }
  2021. }
  2022. else {
  2023. drupal_set_message(t('The webform component @type is not able to be displayed', array('@type' => $component['type'])));
  2024. }
  2025. }
  2026. // Disable validation initially on all elements. We manually validate
  2027. // all webform elements in webform_client_form_validate().
  2028. if (isset($parent_fieldset[$component['form_key']])) {
  2029. $parent_fieldset[$component['form_key']]['#validated'] = TRUE;
  2030. $parent_fieldset[$component['form_key']]['#webform_validated'] = FALSE;
  2031. }
  2032. if (isset($component['children']) && is_array($component['children'])) {
  2033. foreach ($component['children'] as $scid => $subcomponent) {
  2034. $subcomponent_value = isset($form_state['values']['submitted'][$scid]) ? $form_state['values']['submitted'][$scid] : NULL;
  2035. if (_webform_client_form_rule_check($node, $subcomponent, $page_num, $form_state, $submission)) {
  2036. _webform_client_form_add_component($node, $subcomponent, $subcomponent_value, $parent_fieldset[$component['form_key']], $form, $form_state, $submission, $format, $page_num, $filter);
  2037. }
  2038. }
  2039. }
  2040. }
  2041. function webform_client_form_validate($form, &$form_state) {
  2042. $node = node_load($form_state['values']['details']['nid']);
  2043. $finished = $form_state['values']['details']['finished'];
  2044. // Check that the submissions have not exceeded the total submission limit.
  2045. if ($node->webform['total_submit_limit'] != -1) {
  2046. module_load_include('inc', 'webform', 'includes/webform.submissions');
  2047. // Check if the total number of entries was reached before the user submitted
  2048. // the form.
  2049. if (!$finished && $total_limit_exceeded = _webform_submission_total_limit_check($node)) {
  2050. // Show the user the limit has exceeded.
  2051. 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));
  2052. form_set_error('', NULL);
  2053. return;
  2054. }
  2055. }
  2056. // Check that the user has not exceeded the submission limit.
  2057. // This usually will only apply to anonymous users when the page cache is
  2058. // enabled, because they may submit the form even if they do not have access.
  2059. if ($node->webform['submit_limit'] != -1) { // -1: Submissions are never throttled.
  2060. module_load_include('inc', 'webform', 'includes/webform.submissions');
  2061. if (!$finished && $user_limit_exceeded = _webform_submission_user_limit_check($node)) {
  2062. // Assume that webform_view_messages will print out the necessary message,
  2063. // then stop the processing of the form with an empty form error.
  2064. 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));
  2065. form_set_error('', NULL);
  2066. return;
  2067. }
  2068. }
  2069. // Run all #element_validate and #required checks. These are skipped initially
  2070. // by setting #validated = TRUE on all components when they are added.
  2071. _webform_client_form_validate($form, $form_state);
  2072. }
  2073. /**
  2074. * Recursive validation function to trigger normal Drupal validation.
  2075. *
  2076. * This function imitates _form_validate in Drupal's form.inc, only it sets
  2077. * a different property to ensure that validation has occurred.
  2078. */
  2079. function _webform_client_form_validate($elements, &$form_state, $first_run = TRUE) {
  2080. static $form;
  2081. if ($first_run) {
  2082. $form = $elements;
  2083. }
  2084. // Recurse through all children.
  2085. foreach (element_children($elements) as $key) {
  2086. if (isset($elements[$key]) && $elements[$key]) {
  2087. _webform_client_form_validate($elements[$key], $form_state, FALSE);
  2088. }
  2089. }
  2090. // Validate the current input.
  2091. if (isset($elements['#webform_validated']) && $elements['#webform_validated'] == FALSE) {
  2092. if (isset($elements['#needs_validation'])) {
  2093. // Make sure a value is passed when the field is required.
  2094. // A simple call to empty() will not cut it here as some fields, like
  2095. // checkboxes, can return a valid value of '0'. Instead, check the
  2096. // length if it's a string, and the item count if it's an array. For
  2097. // radios, FALSE means that no value was submitted, so check that too.
  2098. if ($elements['#required'] && (!count($elements['#value']) || (is_string($elements['#value']) && strlen(trim($elements['#value'])) == 0) || $elements['#value'] === FALSE)) {
  2099. form_error($elements, t('!name field is required.', array('!name' => $elements['#title'])));
  2100. }
  2101. // Verify that the value is not longer than #maxlength.
  2102. if (isset($elements['#maxlength']) && drupal_strlen($elements['#value']) > $elements['#maxlength']) {
  2103. 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']))));
  2104. }
  2105. if (isset($elements['#options']) && isset($elements['#value'])) {
  2106. if ($elements['#type'] == 'select') {
  2107. $options = form_options_flatten($elements['#options']);
  2108. }
  2109. else {
  2110. $options = $elements['#options'];
  2111. }
  2112. if (is_array($elements['#value'])) {
  2113. $value = $elements['#type'] == 'checkboxes' ? array_keys(array_filter($elements['#value'])) : $elements['#value'];
  2114. foreach ($value as $v) {
  2115. if (!isset($options[$v])) {
  2116. form_error($elements, t('An illegal choice has been detected. Please contact the site administrator.'));
  2117. watchdog('form', 'Illegal choice %choice in !name element.', array('%choice' => $v, '!name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title']), WATCHDOG_ERROR);
  2118. }
  2119. }
  2120. }
  2121. elseif ($elements['#value'] !== '' && !isset($options[$elements['#value']])) {
  2122. form_error($elements, t('An illegal choice has been detected. Please contact the site administrator.'));
  2123. watchdog('form', 'Illegal choice %choice in %name element.', array('%choice' => $elements['#value'], '%name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title']), WATCHDOG_ERROR);
  2124. }
  2125. }
  2126. }
  2127. // Call any element-specific validators. These must act on the element
  2128. // #value data.
  2129. if (isset($elements['#element_validate'])) {
  2130. foreach ($elements['#element_validate'] as $function) {
  2131. if (function_exists($function)) {
  2132. $function($elements, $form_state, $form);
  2133. }
  2134. }
  2135. }
  2136. $elements['#webform_validated'] = TRUE;
  2137. }
  2138. }
  2139. /**
  2140. * Handle the processing of pages and conditional logic.
  2141. */
  2142. function webform_client_form_pages($form, &$form_state) {
  2143. $node = node_load($form_state['values']['details']['nid']);
  2144. // Multistep forms may not have any components on the first page.
  2145. if (!isset($form_state['values']['submitted'])) {
  2146. $form_state['values']['submitted'] = array();
  2147. }
  2148. // Move special settings to storage.
  2149. if (isset($form_state['webform']['component_tree'])) {
  2150. $form_state['storage']['component_tree'] = $form_state['webform']['component_tree'];
  2151. $form_state['storage']['page_count'] = $form_state['webform']['page_count'];
  2152. $form_state['storage']['page_num'] = $form_state['webform']['page_num'];
  2153. }
  2154. // Perform post processing by components.
  2155. _webform_client_form_submit_process($node, $form_state['values']['submitted']);
  2156. // Flatten trees within the submission.
  2157. $form_state['values']['submitted_tree'] = $form_state['values']['submitted'];
  2158. $form_state['values']['submitted'] = _webform_client_form_submit_flatten($node, $form_state['values']['submitted']);
  2159. // Assume the form is completed unless the page logic says otherwise.
  2160. $form_state['webform_completed'] = TRUE;
  2161. // Check for a multi-page form that is not yet complete.
  2162. $submit_op = !empty($form['actions']['submit']['#value']) ? $form['actions']['submit']['#value'] : t('Submit');
  2163. $draft_op = !empty($form['actions']['draft']['#value']) ? $form['actions']['draft']['#value'] : t('Save Draft');
  2164. if (!in_array($form_state['values']['op'], array($submit_op, $draft_op))) {
  2165. // Store values from the current page in the form state storage.
  2166. if (is_array($form_state['values']['submitted'])) {
  2167. foreach ($form_state['values']['submitted'] as $key => $val) {
  2168. $form_state['storage']['submitted'][$key] = $val;
  2169. }
  2170. }
  2171. // Update form state values with those from storage.
  2172. if (isset($form_state['storage']['submitted'])) {
  2173. foreach ($form_state['storage']['submitted'] as $key => $val) {
  2174. $form_state['values']['submitted'][$key] = $val;
  2175. }
  2176. }
  2177. // Set the page number.
  2178. if (!isset($form_state['storage']['page_num'])) {
  2179. $form_state['storage']['page_num'] = 1;
  2180. }
  2181. if (end($form_state['clicked_button']['#parents']) == 'next') {
  2182. $direction = 1;
  2183. }
  2184. else {
  2185. $direction = 0;
  2186. }
  2187. // If the next page has no components that need to be displayed, skip it.
  2188. if (isset($direction)) {
  2189. $components = $direction ? $node->webform['components'] : array_reverse($node->webform['components'], TRUE);
  2190. $last_component = end($node->webform['components']);
  2191. foreach ($components as $component) {
  2192. if ($component['type'] == 'pagebreak' && (
  2193. $direction == 1 && $component['page_num'] > $form_state['storage']['page_num'] ||
  2194. $direction == 0 && $component['page_num'] <= $form_state['storage']['page_num'])) {
  2195. $previous_pagebreak = $component;
  2196. continue;
  2197. }
  2198. if (isset($previous_pagebreak)) {
  2199. $page_num = $previous_pagebreak['page_num'] + $direction - 1;
  2200. // If we've found an component on this page, advance to that page.
  2201. if ($component['page_num'] == $page_num && _webform_client_form_rule_check($node, $component, $page_num, $form_state)) {
  2202. $form_state['storage']['page_num'] = $page_num;
  2203. break;
  2204. }
  2205. // If we've gotten to the end of the form without finding any more
  2206. // components, set the page number more than the max, ending the form.
  2207. elseif ($direction && $component['cid'] == $last_component['cid']) {
  2208. $form_state['storage']['page_num'] = $page_num + 1;
  2209. }
  2210. }
  2211. }
  2212. }
  2213. // The form is done if the page number is greater than the page count.
  2214. $form_state['webform_completed'] = $form_state['storage']['page_num'] > $form_state['storage']['page_count'];
  2215. }
  2216. // Merge any stored submission data for multistep forms.
  2217. if (isset($form_state['storage']['submitted'])) {
  2218. $original_values = is_array($form_state['values']['submitted']) ? $form_state['values']['submitted'] : array();
  2219. unset($form_state['values']['submitted']);
  2220. foreach ($form_state['storage']['submitted'] as $key => $val) {
  2221. $form_state['values']['submitted'][$key] = $val;
  2222. }
  2223. foreach ($original_values as $key => $val) {
  2224. $form_state['values']['submitted'][$key] = $val;
  2225. }
  2226. // Remove the variable so it doesn't show up in the additional processing.
  2227. unset($original_values);
  2228. }
  2229. // Inform the submit handlers that a draft will be saved.
  2230. $form_state['save_draft'] = $form_state['values']['op'] == $draft_op || ($node->webform['auto_save'] && !$form_state['webform_completed']);
  2231. // Determine what we need to do on the next page.
  2232. if (!empty($form_state['save_draft']) || !$form_state['webform_completed']) {
  2233. // Rebuild the form and display the current (on drafts) or next page.
  2234. $form_state['rebuild'] = TRUE;
  2235. }
  2236. else {
  2237. // Remove the form state storage now that we're done with the pages.
  2238. $form_state['rebuild'] = FALSE;
  2239. unset($form_state['storage']);
  2240. }
  2241. }
  2242. /**
  2243. * Submit handler for saving the form values and sending e-mails.
  2244. */
  2245. function webform_client_form_submit($form, &$form_state) {
  2246. module_load_include('inc', 'webform', 'includes/webform.submissions');
  2247. module_load_include('inc', 'webform', 'includes/webform.components');
  2248. global $user;
  2249. if (empty($form_state['save_draft']) && empty($form_state['webform_completed'])) {
  2250. return;
  2251. }
  2252. $node = $form['#node'];
  2253. $sid = $form_state['values']['details']['sid'] ? (int) $form_state['values']['details']['sid'] : NULL;
  2254. // Check if user is submitting as a draft.
  2255. $is_draft = (int) !empty($form_state['save_draft']);
  2256. if (!$sid) {
  2257. // Create a new submission object.
  2258. $submission = (object) array(
  2259. 'nid' => $node->nid,
  2260. 'uid' => $form_state['values']['details']['uid'],
  2261. 'submitted' => REQUEST_TIME,
  2262. 'remote_addr' => ip_address(),
  2263. 'is_draft' => $is_draft,
  2264. 'data' => webform_submission_data($node, $form_state['values']['submitted']),
  2265. );
  2266. }
  2267. else {
  2268. // To maintain time and user information, load the existing submission.
  2269. $submission = webform_get_submission($node->webform['nid'], $sid);
  2270. $submission->is_draft = $is_draft;
  2271. // Merge with new submission data. The + operator maintains numeric keys.
  2272. // This maintains existing data with just-submitted data when a user resumes
  2273. // a submission previously saved as a draft.
  2274. $new_data = webform_submission_data($node, $form_state['values']['submitted']);
  2275. $submission->data = $new_data + $submission->data;
  2276. }
  2277. // If there is no data to be saved (such as on a multipage form with no fields
  2278. // on the first page), process no further. Submissions with no data cannot
  2279. // be loaded from the database as efficiently, so we don't save them at all.
  2280. if (empty($submission->data)) {
  2281. return;
  2282. }
  2283. // Save the submission to the database.
  2284. if (!$sid) {
  2285. // No sid was found thus insert it in the dataabase.
  2286. $form_state['values']['details']['sid'] = $sid = webform_submission_insert($node, $submission);
  2287. $form_state['values']['details']['is_new'] = TRUE;
  2288. // Set a cookie including the server's submission time.
  2289. // The cookie expires in the length of the interval plus a day to compensate for different timezones.
  2290. if (variable_get('webform_use_cookies', 0)) {
  2291. $cookie_name = 'webform-' . $node->nid;
  2292. $time = REQUEST_TIME;
  2293. $params = session_get_cookie_params();
  2294. setcookie($cookie_name . '[' . $time . ']', $time, $time + $node->webform['submit_interval'] + 86400, $params['path'], $params['domain'], $params['secure'], $params['httponly']);
  2295. }
  2296. // Save session information about this submission for anonymous users,
  2297. // allowing them to access or edit their submissions.
  2298. if (!$user->uid && user_access('access own webform submissions')) {
  2299. $_SESSION['webform_submission'][$form_state['values']['details']['sid']] = $node->nid;
  2300. }
  2301. }
  2302. else {
  2303. // Sid was found thus update the existing sid in the database.
  2304. webform_submission_update($node, $submission);
  2305. $form_state['values']['details']['is_new'] = FALSE;
  2306. }
  2307. // Check if this form is sending an email.
  2308. if (!$is_draft && !$form_state['values']['details']['finished']) {
  2309. $submission = webform_get_submission($node->webform['nid'], $sid, TRUE);
  2310. webform_submission_send_mail($node, $submission);
  2311. }
  2312. // Strip out empty tags added by WYSIWYG editors if needed.
  2313. $confirmation = strlen(trim(strip_tags($node->webform['confirmation']))) ? $node->webform['confirmation'] : '';
  2314. // Clean up the redirect URL and filter it for webform tokens.
  2315. $redirect_url = trim($node->webform['redirect_url']);
  2316. $redirect_url = _webform_filter_values($redirect_url, $node, $submission, NULL, FALSE, TRUE);
  2317. // Remove the domain name from the redirect.
  2318. $redirect_url = preg_replace('/^' . preg_quote($GLOBALS['base_url'], '/') . '\//', '', $redirect_url);
  2319. // Check confirmation and redirect_url fields.
  2320. $message = NULL;
  2321. $redirect = NULL;
  2322. $external_url = FALSE;
  2323. if (isset($form['actions']['draft']['#value']) && $form_state['values']['op'] == $form['actions']['draft']['#value']) {
  2324. $message = t('Submission saved. You may return to this form later and it will restore the current values.');
  2325. }
  2326. elseif ($is_draft) {
  2327. $redirect = NULL;
  2328. }
  2329. elseif (!empty($form_state['values']['details']['finished'])) {
  2330. $message = t('Submission updated.');
  2331. }
  2332. elseif ($redirect_url == '<none>') {
  2333. $redirect = NULL;
  2334. }
  2335. elseif ($redirect_url == '<confirmation>') {
  2336. $redirect = array('node/' . $node->nid . '/done', array('query' => array('sid' => $sid)));
  2337. }
  2338. elseif (valid_url($redirect_url, TRUE)) {
  2339. $redirect = $redirect_url;
  2340. $external_url = TRUE;
  2341. }
  2342. elseif ($redirect_url && strpos($redirect_url, 'http') !== 0) {
  2343. $parts = drupal_parse_url($redirect_url);
  2344. $parts['query'] ? ($parts['query']['sid'] = $sid) : ($parts['query'] = array('sid' => $sid));
  2345. $query = $parts['query'];
  2346. $redirect = array($parts['path'], array('query' => $query, 'fragment' => $parts['fragment']));
  2347. }
  2348. // Show a message if manually set.
  2349. if (isset($message)) {
  2350. drupal_set_message($message);
  2351. }
  2352. // If redirecting and we have a confirmation message, show it as a message.
  2353. elseif (!$is_draft && !$external_url && (!empty($redirect_url) && $redirect_url != '<confirmation>') && !empty($confirmation)) {
  2354. drupal_set_message(check_markup($confirmation, $node->webform['confirmation_format'], '', TRUE));
  2355. }
  2356. $form_state['redirect'] = $redirect;
  2357. }
  2358. /**
  2359. * Post processes the submission tree with any updates from components.
  2360. *
  2361. * @param $node
  2362. * The full webform node.
  2363. * @param $form_values
  2364. * The form values for the form.
  2365. * @param $types
  2366. * Optional. Specific types to perform processing.
  2367. * @param $parent
  2368. * Internal use. The current parent CID whose children are being processed.
  2369. */
  2370. function _webform_client_form_submit_process($node, &$form_values, $types = NULL, $parent = 0) {
  2371. if (is_array($form_values)) {
  2372. foreach ($form_values as $form_key => $value) {
  2373. $cid = webform_get_cid($node, $form_key, $parent);
  2374. if (is_array($value) && isset($node->webform['components'][$cid]['type']) && webform_component_feature($node->webform['components'][$cid]['type'], 'group')) {
  2375. _webform_client_form_submit_process($node, $form_values[$form_key], $types, $cid);
  2376. }
  2377. if (isset($node->webform['components'][$cid])) {
  2378. // Call the component process submission function.
  2379. $component = $node->webform['components'][$cid];
  2380. if ((!isset($types) || in_array($component['type'], $types)) && webform_component_implements($component['type'], 'submit')) {
  2381. $form_values[$component['form_key']] = webform_component_invoke($component['type'], 'submit', $component, $form_values[$component['form_key']]);
  2382. }
  2383. }
  2384. }
  2385. }
  2386. }
  2387. /**
  2388. * Flattens a submitted form back into a single array representation (rather than nested fields)
  2389. */
  2390. function _webform_client_form_submit_flatten($node, $fieldset, $parent = 0) {
  2391. $values = array();
  2392. if (is_array($fieldset)) {
  2393. foreach ($fieldset as $form_key => $value) {
  2394. $cid = webform_get_cid($node, $form_key, $parent);
  2395. if (is_array($value) && webform_component_feature($node->webform['components'][$cid]['type'], 'group')) {
  2396. $values += _webform_client_form_submit_flatten($node, $value, $cid);
  2397. }
  2398. else {
  2399. $values[$cid] = $value;
  2400. }
  2401. }
  2402. }
  2403. return $values;
  2404. }
  2405. /**
  2406. * Prints the confirmation message after a successful submission.
  2407. */
  2408. function _webform_confirmation($node) {
  2409. drupal_set_title($node->title);
  2410. webform_set_breadcrumb($node, TRUE);
  2411. $sid = isset($_GET['sid']) ? $_GET['sid'] : NULL;
  2412. return theme(array('webform_confirmation_' . $node->nid, 'webform_confirmation'), array('node' => $node, 'sid' => $sid));
  2413. }
  2414. /**
  2415. * Prepare for theming of the webform form.
  2416. */
  2417. function template_preprocess_webform_form(&$vars) {
  2418. if (isset($vars['form']['details']['nid']['#value'])) {
  2419. $vars['nid'] = $vars['form']['details']['nid']['#value'];
  2420. }
  2421. elseif (isset($vars['form']['submission']['#value'])) {
  2422. $vars['nid'] = $vars['form']['submission']['#value']->nid;
  2423. }
  2424. }
  2425. /**
  2426. * Prepare for theming of the webform submission confirmation.
  2427. */
  2428. function template_preprocess_webform_confirmation(&$vars) {
  2429. $confirmation = check_markup($vars['node']->webform['confirmation'], $vars['node']->webform['confirmation_format'], '', TRUE);
  2430. // Strip out empty tags added by WYSIWYG editors if needed.
  2431. $vars['confirmation_message'] = strlen(trim(strip_tags($confirmation))) ? $confirmation : '';
  2432. }
  2433. /**
  2434. * Prepare to theme the contents of e-mails sent by webform.
  2435. */
  2436. function template_preprocess_webform_mail_message(&$vars) {
  2437. global $user;
  2438. $vars['user'] = $user;
  2439. $vars['ip_address'] = ip_address();
  2440. }
  2441. /**
  2442. * A Form API #pre_render function. Sets display based on #title_display.
  2443. *
  2444. * This function is used regularly in D6 for all elements, but specifically for
  2445. * fieldsets in D7, which don't support #title_display natively.
  2446. */
  2447. function webform_element_title_display($element) {
  2448. if (isset($element['#title_display']) && strcmp($element['#title_display'], 'none') === 0) {
  2449. $element['#title'] = NULL;
  2450. }
  2451. return $element;
  2452. }
  2453. /**
  2454. * Replacement for theme_form_element().
  2455. */
  2456. function theme_webform_element($variables) {
  2457. // Ensure defaults.
  2458. $variables['element'] += array(
  2459. '#title_display' => 'before',
  2460. );
  2461. $element = $variables['element'];
  2462. // All elements using this for display only are given the "display" type.
  2463. if (isset($element['#format']) && $element['#format'] == 'html') {
  2464. $type = 'display';
  2465. }
  2466. else {
  2467. $type = (isset($element['#type']) && !in_array($element['#type'], array('markup', 'textfield', 'webform_email', 'webform_number'))) ? $element['#type'] : $element['#webform_component']['type'];
  2468. }
  2469. // Convert the parents array into a string, excluding the "submitted" wrapper.
  2470. $nested_level = $element['#parents'][0] == 'submitted' ? 1 : 0;
  2471. $parents = str_replace('_', '-', implode('--', array_slice($element['#parents'], $nested_level)));
  2472. $wrapper_classes = array(
  2473. 'form-item',
  2474. 'webform-component',
  2475. 'webform-component-' . $type,
  2476. );
  2477. if (isset($element['#title_display']) && strcmp($element['#title_display'], 'inline') === 0) {
  2478. $wrapper_classes[] = 'webform-container-inline';
  2479. }
  2480. $output = '<div class="' . implode(' ', $wrapper_classes) . '" id="webform-component-' . $parents . '">' . "\n";
  2481. // If #title_display is none, set it to invisible instead - none only used if
  2482. // we have no title at all to use.
  2483. if ($element['#title_display'] == 'none') {
  2484. $variables['element']['#title_display'] = 'invisible';
  2485. $element['#title_display'] = 'invisible';
  2486. }
  2487. // If #title is not set, we don't display any label or required marker.
  2488. if (!isset($element['#title'])) {
  2489. $element['#title_display'] = 'none';
  2490. }
  2491. $prefix = isset($element['#field_prefix']) ? '<span class="field-prefix">' . _webform_filter_xss($element['#field_prefix']) . '</span> ' : '';
  2492. $suffix = isset($element['#field_suffix']) ? ' <span class="field-suffix">' . _webform_filter_xss($element['#field_suffix']) . '</span>' : '';
  2493. switch ($element['#title_display']) {
  2494. case 'inline':
  2495. case 'before':
  2496. case 'invisible':
  2497. $output .= ' ' . theme('form_element_label', $variables);
  2498. $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
  2499. break;
  2500. case 'after':
  2501. $output .= ' ' . $prefix . $element['#children'] . $suffix;
  2502. $output .= ' ' . theme('form_element_label', $variables) . "\n";
  2503. break;
  2504. case 'none':
  2505. case 'attribute':
  2506. // Output no label and no required marker, only the children.
  2507. $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
  2508. break;
  2509. }
  2510. if (!empty($element['#description'])) {
  2511. $output .= ' <div class="description">' . $element['#description'] . "</div>\n";
  2512. }
  2513. $output .= "</div>\n";
  2514. return $output;
  2515. }
  2516. /**
  2517. * Output a form element in plain text format.
  2518. */
  2519. function theme_webform_element_text($variables) {
  2520. $element = $variables['element'];
  2521. $value = $variables['element']['#children'];
  2522. $output = '';
  2523. $is_group = webform_component_feature($element['#webform_component']['type'], 'group');
  2524. // Output the element title.
  2525. if (isset($element['#title'])) {
  2526. if ($is_group) {
  2527. $output .= '--' . $element['#title'] . '--';
  2528. }
  2529. elseif (!in_array(drupal_substr($element['#title'], -1), array('?', ':', '!', '%', ';', '@'))) {
  2530. $output .= $element['#title'] . ':';
  2531. }
  2532. else {
  2533. $output .= $element['#title'];
  2534. }
  2535. }
  2536. // Wrap long values at 65 characters, allowing for a few fieldset indents.
  2537. // It's common courtesy to wrap at 75 characters in e-mails.
  2538. if ($is_group && drupal_strlen($value) > 65) {
  2539. $value = wordwrap($value, 65, "\n");
  2540. $lines = explode("\n", $value);
  2541. foreach ($lines as $key => $line) {
  2542. $lines[$key] = ' ' . $line;
  2543. }
  2544. $value = implode("\n", $lines);
  2545. }
  2546. // Add the value to the output. Add a newline before the response if needed.
  2547. $output .= (strpos($value, "\n") === FALSE ? ' ' : "\n") . $value;
  2548. // Indent fieldsets.
  2549. if ($is_group) {
  2550. $lines = explode("\n", $output);
  2551. foreach ($lines as $number => $line) {
  2552. if (strlen($line)) {
  2553. $lines[$number] = ' ' . $line;
  2554. }
  2555. }
  2556. $output = implode("\n", $lines);
  2557. $output .= "\n";
  2558. }
  2559. if ($output) {
  2560. $output .= "\n";
  2561. }
  2562. return $output;
  2563. }
  2564. /**
  2565. * Theme a radio button and another element together.
  2566. *
  2567. * This is used in the e-mail configuration to show a radio button and a text
  2568. * field or select list on the same line.
  2569. */
  2570. function theme_webform_inline_radio($variables) {
  2571. $element = $variables['element'];
  2572. // Add element's #type and #name as class to aid with JS/CSS selectors.
  2573. $class = array('form-item');
  2574. if (!empty($element['#type'])) {
  2575. $class[] = 'form-type-' . strtr($element['#type'], '_', '-');
  2576. }
  2577. if (!empty($element['#name'])) {
  2578. $class[] = 'form-item-' . strtr($element['#name'], array(' ' => '-', '_' => '-', '[' => '-', ']' => ''));
  2579. }
  2580. // Add container-inline to all elements.
  2581. $class[] = 'webform-container-inline';
  2582. if (isset($element['#inline_element']) && isset($variables['element']['#title'])) {
  2583. $variables['element']['#title'] .= ': ';
  2584. }
  2585. $output = '<div class="' . implode(' ', $class) . '">' . "\n";
  2586. $output .= ' ' . $element['#children'];
  2587. if (!empty($element['#title'])) {
  2588. $output .= ' ' . theme('form_element_label', $variables) . "\n";
  2589. }
  2590. if (isset($element['#inline_element'])) {
  2591. $output .= ' ' . $element['#inline_element'] . "\n";
  2592. }
  2593. if (!empty($element['#description'])) {
  2594. $output .= ' <div class="description">' . $element['#description'] . "</div>\n";
  2595. }
  2596. $output .= "</div>\n";
  2597. return $output;
  2598. }
  2599. /**
  2600. * Theme the headers when sending an email from webform.
  2601. *
  2602. * @param $node
  2603. * The complete node object for the webform.
  2604. * @param $submission
  2605. * The webform submission of the user.
  2606. * @param $email
  2607. * If you desire to make different e-mail headers depending on the recipient,
  2608. * you can check the $email['email'] property to output different content.
  2609. * This will be the ID of the component that is a conditional e-mail
  2610. * recipient. For the normal e-mails, it will have the value of 'default'.
  2611. * @return
  2612. * An array of headers to be used when sending a webform email. If headers
  2613. * for "From", "To", or "Subject" are set, they will take precedence over
  2614. * the values set in the webform configuration.
  2615. */
  2616. function theme_webform_mail_headers($variables) {
  2617. $headers = array(
  2618. 'X-Mailer' => 'Drupal Webform (PHP/' . phpversion() . ')',
  2619. );
  2620. return $headers;
  2621. }
  2622. /**
  2623. * Check if current user has a draft of this webform, and return the sid.
  2624. */
  2625. function _webform_fetch_draft_sid($nid, $uid) {
  2626. return db_select('webform_submissions')
  2627. ->fields('webform_submissions', array('sid'))
  2628. ->condition('nid', $nid)
  2629. ->condition('uid', $uid)
  2630. ->condition('is_draft', 1)
  2631. ->orderBy('submitted', 'DESC')
  2632. ->execute()
  2633. ->fetchField();
  2634. }
  2635. /**
  2636. * Filters all special tokens provided by webform, such as %post and %profile.
  2637. *
  2638. * @param $string
  2639. * The string to have its tokens replaced.
  2640. * @param $node
  2641. * If replacing node-level tokens, the node for which tokens will be created.
  2642. * @param $submission
  2643. * If replacing submission-level tokens, the submission for which tokens will
  2644. * be created.
  2645. * @param $email
  2646. * If replacing tokens within the context of an e-mail, the Webform e-mail
  2647. * settings array.
  2648. * @param $strict
  2649. * Boolean value indicating if the results should be run through check_plain.
  2650. * This is used any time the values will be output as HTML, but not in
  2651. * default values or e-mails.
  2652. * @param $allow_anonymous
  2653. * Boolean value indicating if all tokens should be replaced for anonymous
  2654. * users, even if they contain sensitive user information such as %session or
  2655. * %ip_address. This is disabled by default to prevent user data from being
  2656. * preserved in the anonymous page cache and should only be used in
  2657. * non-cached situations, such as e-mails.
  2658. */
  2659. function _webform_filter_values($string, $node = NULL, $submission = NULL, $email = NULL, $strict = TRUE, $allow_anonymous = FALSE) {
  2660. global $user;
  2661. $replacements = &drupal_static(__FUNCTION__);
  2662. // Don't do any filtering if the string is empty.
  2663. if (strlen(trim($string)) == 0) {
  2664. return $string;
  2665. }
  2666. // Setup default token replacements.
  2667. if (!isset($replacements)) {
  2668. $replacements['unsafe'] = array();
  2669. $replacements['safe']['%site'] = variable_get('site_name', 'drupal');
  2670. $replacements['safe']['%date'] = format_date(REQUEST_TIME, 'long');
  2671. }
  2672. // Node replacements.
  2673. if (isset($node) && !array_key_exists('%nid', $replacements['safe'])) {
  2674. $replacements['safe']['%nid'] = $node->nid;
  2675. $replacements['safe']['%title'] = $node->title;
  2676. }
  2677. // Determine the display format.
  2678. $format = isset($email['html']) && $email['html'] ? 'html' : 'text';
  2679. // Submission replacements.
  2680. if (isset($submission) && (!isset($replacements['email'][$format]) || (isset($replacements['unsafe']['%sid']) && $replacements['unsafe']['%sid'] != $submission->sid))) {
  2681. module_load_include('inc', 'webform', 'includes/webform.components');
  2682. // Set the submission ID.
  2683. $replacements['unsafe']['%sid'] = $submission->sid;
  2684. // E-mails may be sent in two formats, keep tokens separate for each one.
  2685. $replacements['email'][$format] = array();
  2686. // Populate token values for each component.
  2687. foreach ($node->webform['components'] as $cid => $component) {
  2688. // Find by form key.
  2689. $parents = webform_component_parent_keys($node, $component);
  2690. $form_key = implode('][', $parents);
  2691. if (isset($submission->data[$cid])) {
  2692. $value = $submission->data[$cid];
  2693. $display_element = webform_component_invoke($component['type'], 'display', $component, $value['value'], $format);
  2694. // Ensure the component is added as a property.
  2695. $display_element['#webform_component'] = $component;
  2696. if (empty($display_element['#parents'])) {
  2697. $display_element['#parents'] = array_merge(array('submitted'), $parents);
  2698. }
  2699. if (empty($display_element['#id'])) {
  2700. $display_element['#id'] = drupal_html_id('edit-' . implode('-', $display_element['#parents']));
  2701. }
  2702. $replacements['email'][$format]['%email[' . $form_key . ']'] = render($display_element);
  2703. $display_element['#theme_wrappers'] = array(); // Remove label and wrappers.
  2704. $replacements['email'][$format]['%value[' . $form_key . ']'] = render($display_element);
  2705. }
  2706. else {
  2707. // Provide an empty value for components without submitted data.
  2708. $replacements['email'][$format]['%email[' . $form_key . ']'] = '';
  2709. $replacements['email'][$format]['%value[' . $form_key . ']'] = '';
  2710. }
  2711. }
  2712. // Reverse the order of tokens so that nested tokens (ie. inside fieldsets)
  2713. // come before their parents.
  2714. $replacements['email'][$format] = array_reverse($replacements['email'][$format]);
  2715. // Submission edit URL.
  2716. $replacements['unsafe']['%submission_url'] = url('node/' . $node->nid . '/submission/' . $submission->sid, array('absolute' => TRUE));
  2717. }
  2718. // Token for the entire form tree for e-mails.
  2719. if (isset($submission) && isset($email)) {
  2720. $replacements['email'][$format]['%email_values'] = webform_submission_render($node, $submission, $email, $format);
  2721. }
  2722. // Provide a list of candidates for token replacement.
  2723. $special_tokens = array(
  2724. 'safe' => array(
  2725. '%get' => $_GET,
  2726. '%post' => $_POST,
  2727. ),
  2728. 'unsafe' => array(
  2729. '%cookie' => isset($_COOKIE) ? $_COOKIE : array(),
  2730. '%session' => isset($_SESSION) ? $_SESSION : array(),
  2731. '%request' => $_REQUEST,
  2732. '%server' => $_SERVER,
  2733. '%profile' => (array) $user,
  2734. ),
  2735. );
  2736. // Replacements of global variable tokens.
  2737. if (!isset($replacements['specials_set'])) {
  2738. $replacements['specials_set'] = TRUE;
  2739. // Load profile information if available.
  2740. if ($user->uid) {
  2741. $account = user_load($user->uid);
  2742. $special_tokens['unsafe']['%profile'] = (array) $account;
  2743. }
  2744. // User replacements.
  2745. if (!array_key_exists('%uid', $replacements['unsafe'])) {
  2746. $replacements['unsafe']['%uid'] = !empty($user->uid) ? $user->uid : '';
  2747. $replacements['unsafe']['%username'] = isset($user->name) ? $user->name : '';
  2748. $replacements['unsafe']['%useremail'] = isset($user->mail) ? $user->mail : '';
  2749. $replacements['unsafe']['%ip_address'] = ip_address();
  2750. }
  2751. // Populate the replacements array with special variables.
  2752. foreach ($special_tokens as $safe_state => $tokens) {
  2753. foreach ($tokens as $token => $variable) {
  2754. // Safety check in case $_POST or some other global has been removed
  2755. // by a naughty module, in which case $variable may be NULL.
  2756. if (!is_array($variable)) {
  2757. continue;
  2758. }
  2759. foreach ($variable as $key => $value) {
  2760. // This special case for profile module dates.
  2761. if ($token == '%profile' && is_array($value) && isset($value['year'])) {
  2762. $replacement = webform_strtodate(webform_date_format(), $value['month'] . '/' . $value['day'] . '/' . $value['year'], 'UTC');
  2763. }
  2764. else {
  2765. // Checking for complex types (arrays and objects) fails here with
  2766. // incomplete objects (see http://php.net/is_object), so we check
  2767. // for simple types instead.
  2768. $replacement = (is_string($value) || is_bool($value) || is_numeric($value)) ? $value : '';
  2769. }
  2770. $replacements[$safe_state][$token . '[' . $key . ']'] = $replacement;
  2771. }
  2772. }
  2773. }
  2774. }
  2775. // Make a copy of the replacements so we don't affect the static version.
  2776. $safe_replacements = $replacements['safe'];
  2777. // Restrict replacements for anonymous users. Not all tokens can be used
  2778. // because they may expose session or other private data to other users when
  2779. // anonymous page caching is enabled.
  2780. if ($user->uid || $allow_anonymous) {
  2781. $safe_replacements += $replacements['unsafe'];
  2782. if (isset($replacements['email'][$format])) {
  2783. $safe_replacements += $replacements['email'][$format];
  2784. }
  2785. }
  2786. else {
  2787. foreach ($replacements['unsafe'] as $key => $value) {
  2788. $safe_replacements[$key] = '';
  2789. }
  2790. }
  2791. $find = array_keys($safe_replacements);
  2792. $replace = array_values($safe_replacements);
  2793. $string = str_replace($find, $replace, $string);
  2794. // Clean up any unused tokens.
  2795. foreach ($special_tokens as $safe_state => $tokens) {
  2796. foreach (array_keys($tokens) as $token) {
  2797. $string = preg_replace('/\\' . $token . '\[\w+\]/', '', $string);
  2798. }
  2799. }
  2800. return $strict ? _webform_filter_xss($string) : $string;
  2801. }
  2802. /**
  2803. * Filters all special tokens provided by webform, and allows basic layout in descriptions.
  2804. */
  2805. function _webform_filter_descriptions($string, $node = NULL, $submission = NULL) {
  2806. return strlen($string) == 0 ? '' : _webform_filter_xss(_webform_filter_values($string, $node, $submission, NULL, FALSE));
  2807. }
  2808. /**
  2809. * Filter labels for display by running through XSS checks.
  2810. */
  2811. function _webform_filter_xss($string) {
  2812. static $allowed_tags;
  2813. $allowed_tags = isset($allowed_tags) ? $allowed_tags : webform_variable_get('webform_allowed_tags');
  2814. return filter_xss($string, $allowed_tags);
  2815. }
  2816. /**
  2817. * Utility function to ensure that a webform record exists in the database.
  2818. *
  2819. * @param $node
  2820. * The node object to check if a database entry exists.
  2821. * @return
  2822. * This function should always return TRUE if no errors were encountered,
  2823. * ensuring that a webform table row has been created. Will return FALSE if
  2824. * a record does not exist and a new one could not be created.
  2825. */
  2826. function webform_ensure_record(&$node) {
  2827. if (!$node->webform['record_exists']) {
  2828. // Even though webform_node_insert() would set this property to TRUE,
  2829. // we set record_exists to trigger a difference from the defaults.
  2830. $node->webform['record_exists'] = TRUE;
  2831. webform_node_insert($node);
  2832. }
  2833. return $node->webform['record_exists'];
  2834. }
  2835. /**
  2836. * Utility function to check if a webform record is necessary in the database.
  2837. *
  2838. * If the node is no longer using any webform settings, this function will
  2839. * delete the settings from the webform table. Note that this function will NOT
  2840. * delete rows from the webform table if the node-type is exclusively used for
  2841. * webforms (per the "webform_node_types_primary" variable).
  2842. *
  2843. * @param $node
  2844. * The node object to check if a database entry is still required.
  2845. * @return
  2846. * Returns TRUE if the webform still has a record in the database. Returns
  2847. * FALSE if the webform does not have a record or if the previously existing
  2848. * record was just deleted.
  2849. */
  2850. function webform_check_record(&$node) {
  2851. $webform = $node->webform;
  2852. $webform['record_exists'] = FALSE;
  2853. unset($webform['nid']);
  2854. // Don't include empty values in the comparison, this makes it so modules that
  2855. // extend Webform with empty defaults won't affect cleanup of rows.
  2856. $webform = array_filter($webform);
  2857. $defaults = array_filter(webform_node_defaults());
  2858. if ($webform == $defaults && !in_array($node->type, webform_variable_get('webform_node_types_primary'))) {
  2859. webform_node_delete($node);
  2860. $node->webform = webform_node_defaults();
  2861. }
  2862. return $node->webform['record_exists'];
  2863. }
  2864. /**
  2865. * Given a form_key and a list of form_key parents, determine the cid.
  2866. *
  2867. * @param $node
  2868. * A fully loaded node object.
  2869. * @param $form_key
  2870. * The form key for which we're finding a cid.
  2871. * @param $parent
  2872. * The cid of the parent component.
  2873. */
  2874. function webform_get_cid(&$node, $form_key, $pid) {
  2875. foreach ($node->webform['components'] as $cid => $component) {
  2876. if ($component['form_key'] == $form_key && $component['pid'] == $pid) {
  2877. return $cid;
  2878. }
  2879. }
  2880. }
  2881. /**
  2882. * Retreive a Drupal variable with the appropriate default value.
  2883. */
  2884. function webform_variable_get($variable) {
  2885. switch ($variable) {
  2886. case 'webform_allowed_tags':
  2887. $result = variable_get('webform_allowed_tags', array('a', 'em', 'strong', 'code', 'img'));
  2888. break;
  2889. case 'webform_default_from_name':
  2890. $result = variable_get('webform_default_from_name', variable_get('site_name', ''));
  2891. break;
  2892. case 'webform_default_from_address':
  2893. $result = variable_get('webform_default_from_address', variable_get('site_mail', ini_get('sendmail_from')));
  2894. break;
  2895. case 'webform_default_subject':
  2896. $result = variable_get('webform_default_subject', t('Form submission from: %title'));
  2897. break;
  2898. case 'webform_node_types':
  2899. $result = variable_get('webform_node_types', array('webform'));
  2900. break;
  2901. case 'webform_node_types_primary':
  2902. $result = variable_get('webform_node_types_primary', array('webform'));
  2903. break;
  2904. }
  2905. return $result;
  2906. }
  2907. function theme_webform_token_help($variables) {
  2908. $groups = $variables['groups'];
  2909. $groups = empty($groups) ? array('basic', 'node', 'special') : $groups;
  2910. static $tokens = array();
  2911. if (empty($tokens)) {
  2912. $tokens['basic'] = array(
  2913. 'title' => t('Basic tokens'),
  2914. 'tokens' => array(
  2915. '%username' => t('The name of the user if logged in. Blank for anonymous users.'),
  2916. '%useremail' => t('The e-mail address of the user if logged in. Blank for anonymous users.'),
  2917. '%ip_address' => t('The IP address of the user.'),
  2918. '%site' => t('The name of the site (i.e. %site_name)', array('%site_name' => variable_get('site_name', ''))),
  2919. '%date' => t('The current date, formatted according to the site settings.'),
  2920. ),
  2921. );
  2922. $tokens['node'] = array(
  2923. 'title' => t('Node tokens'),
  2924. 'tokens' => array(
  2925. '%nid' => t('The node ID.'),
  2926. '%title' => t('The node title.'),
  2927. ),
  2928. );
  2929. $tokens['special'] = array(
  2930. 'title' => t('Special tokens'),
  2931. 'tokens' => array(
  2932. '%profile[' . t('key') . ']' => t('Any user profile field or value, such as %profile[name] or %profile[profile_first_name]'),
  2933. '%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".'),
  2934. '%post[' . t('key') . ']' => t('Tokens may also be populated from POST values that are submitted by forms.'),
  2935. ),
  2936. '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].'),
  2937. );
  2938. $tokens['email'] = array(
  2939. 'title' => t('E-mail tokens'),
  2940. 'tokens' => array(
  2941. '%email_values' => t('All included components in a hierarchical structure.'),
  2942. '%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.'),
  2943. '%submission_url' => t('The URL for viewing the completed submission.'),
  2944. ),
  2945. );
  2946. $tokens['submission'] = array(
  2947. 'title' => t('Submission tokens'),
  2948. 'tokens' => array(
  2949. '%sid' => t('The unique submission ID.'),
  2950. '%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.'),
  2951. ),
  2952. );
  2953. }
  2954. $output = '';
  2955. $output .= '<p>' . t('You may use special tokens in this field that will be replaced with dynamic values.') . '</p>';
  2956. foreach ($tokens as $group_name => $group) {
  2957. if (!is_array($groups) || in_array($group_name, $groups)) {
  2958. $items = array();
  2959. foreach ($group['tokens'] as $token => $token_description) {
  2960. $items[] = $token . ' - ' . $token_description;
  2961. }
  2962. $output .= theme('item_list', array('items' => $items, 'title' => $group['title']));
  2963. $output .= isset($group['description']) ? '<p>' . $group['description'] . '</p>' : '';
  2964. }
  2965. }
  2966. $fieldset = array(
  2967. '#title' => t('Token values'),
  2968. '#type' => 'fieldset',
  2969. '#collapsible' => TRUE,
  2970. '#collapsed' => TRUE,
  2971. '#children' => '<div>' . $output . '</div>',
  2972. '#attributes' => array('class' => array('collapsible', 'collapsed')),
  2973. );
  2974. return theme('fieldset', array('element' => $fieldset));
  2975. }
  2976. function _webform_safe_name($name) {
  2977. $new = trim($name);
  2978. // If transliteration is available, use it to convert names to ASCII.
  2979. if (function_exists('transliteration_get')) {
  2980. $new = transliteration_get($new, '');
  2981. $new = str_replace(array(' ', '-', '/'), array('_', '_', '_'), $new);
  2982. }
  2983. else {
  2984. $new = str_replace(
  2985. array(' ', '-', '/', '€', 'ƒ', 'Š', 'Ž', 'š', 'ž', 'Ÿ', '¢', '¥', 'µ', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'à', 'á', 'â', 'ã', 'ä', 'å', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'Œ', 'œ', 'Æ', 'Ð', 'Þ', 'ß', 'æ', 'ð', 'þ'),
  2986. 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'),
  2987. $new);
  2988. }
  2989. $new = drupal_strtolower($new);
  2990. $new = preg_replace('/[^a-z0-9_]/', '', $new);
  2991. return $new;
  2992. }
  2993. /**
  2994. * Given an email address and a name, format an e-mail address.
  2995. *
  2996. * @param $address
  2997. * The e-mail address.
  2998. * @param $name
  2999. * The name to be used in the formatted address.
  3000. * @param $node
  3001. * The webform node if replacements will be done.
  3002. * @param $submission
  3003. * The webform submission values if replacements will be done.
  3004. * @param $encode
  3005. * Encode the text for use in an e-mail.
  3006. * @param $single
  3007. * Force a single value to be returned, even if a component expands to
  3008. * multiple addresses. This is useful to ensure a single e-mail will be
  3009. * returned for the "From" address.
  3010. * @param $format
  3011. * The e-mail format, defaults to the site-wide setting. May be either "short"
  3012. * or "long".
  3013. */
  3014. function webform_format_email_address($address, $name, $node = NULL, $submission = NULL, $encode = TRUE, $single = TRUE, $format = NULL) {
  3015. if (!isset($format)) {
  3016. $format = variable_get('webform_email_address_format', 'long');
  3017. }
  3018. if ($name == 'default') {
  3019. $name = webform_variable_get('webform_default_from_name');
  3020. }
  3021. elseif (is_numeric($name) && isset($node->webform['components'][$name])) {
  3022. if (isset($submission->data[$name]['value'])) {
  3023. $name = $submission->data[$name]['value'];
  3024. }
  3025. else {
  3026. $name = t('Value of !component', array('!component' => $node->webform['components'][$name]['name']));
  3027. }
  3028. }
  3029. if ($address == 'default') {
  3030. $address = webform_variable_get('webform_default_from_address');
  3031. }
  3032. elseif (is_numeric($address) && isset($node->webform['components'][$address])) {
  3033. if (isset($submission->data[$address]['value'])) {
  3034. $values = $submission->data[$address]['value'];;
  3035. $address = array();
  3036. foreach ($values as $value) {
  3037. $address = array_merge($address, explode(',', $value));
  3038. }
  3039. }
  3040. else {
  3041. $address = t('Value of "!component"', array('!component' => $node->webform['components'][$address]['name']));
  3042. }
  3043. }
  3044. // Convert arrays into a single value for From values.
  3045. if ($single) {
  3046. $address = is_array($address) ? reset($address) : $address;
  3047. $name = is_array($name) ? reset($name) : $name;
  3048. }
  3049. // Address may be an array if a component value was used on checkboxes.
  3050. if (is_array($address)) {
  3051. foreach ($address as $key => $individual_address) {
  3052. $address[$key] = _webform_filter_values($individual_address, $node, $submission, NULL, FALSE, TRUE);
  3053. }
  3054. }
  3055. else {
  3056. $address = _webform_filter_values($address, $node, $submission, NULL, FALSE, TRUE);
  3057. }
  3058. if ($format == 'long' && !empty($name)) {
  3059. $name = _webform_filter_values($name, $node, $submission, NULL, FALSE, TRUE);
  3060. if ($encode) {
  3061. $name = mime_header_encode($name);
  3062. }
  3063. $name = trim($name);
  3064. return '"' . $name . '" <' . $address . '>';
  3065. }
  3066. else {
  3067. return $address;
  3068. }
  3069. }
  3070. /**
  3071. * Given an email subject, format it with any needed replacements.
  3072. */
  3073. function webform_format_email_subject($subject, $node = NULL, $submission = NULL) {
  3074. if ($subject == 'default') {
  3075. $subject = webform_variable_get('webform_default_subject');
  3076. }
  3077. elseif (is_numeric($subject) && isset($node->webform['components'][$subject])) {
  3078. $component = $node->webform['components'][$subject];
  3079. if (isset($submission->data[$subject]['value'])) {
  3080. $display_function = '_webform_display_' . $component['type'];
  3081. $value = $submission->data[$subject]['value'];
  3082. // Convert the value to a clean text representation if possible.
  3083. if (function_exists($display_function)) {
  3084. $display = $display_function($component, $value, 'text');
  3085. $display['#theme_wrappers'] = array();
  3086. $display['#webform_component'] = $component;
  3087. $subject = str_replace("\n", ' ', drupal_render($display));
  3088. }
  3089. else {
  3090. $subject = $value;
  3091. }
  3092. }
  3093. else {
  3094. $subject = t('Value of "!component"', array('!component' => $component['name']));
  3095. }
  3096. }
  3097. // Convert arrays to strings (may happen if checkboxes are used as the value).
  3098. if (is_array($subject)) {
  3099. $subject = reset($subject);
  3100. }
  3101. return _webform_filter_values($subject, $node, $submission, NULL, FALSE, TRUE);
  3102. }
  3103. /**
  3104. * Convert an array of components into a tree
  3105. */
  3106. function _webform_components_tree_build($src, &$tree, $parent, &$page_count) {
  3107. foreach ($src as $cid => $component) {
  3108. if ($component['pid'] == $parent) {
  3109. _webform_components_tree_build($src, $component, $cid, $page_count);
  3110. if ($component['type'] == 'pagebreak') {
  3111. $page_count++;
  3112. }
  3113. $tree['children'][$cid] = $component;
  3114. $tree['children'][$cid]['page_num'] = $page_count;
  3115. }
  3116. }
  3117. return $tree;
  3118. }
  3119. /**
  3120. * Flatten a component tree into a flat list.
  3121. */
  3122. function _webform_components_tree_flatten($tree) {
  3123. $components = array();
  3124. foreach ($tree as $cid => $component) {
  3125. if (isset($component['children'])) {
  3126. unset($component['children']);
  3127. $components[$cid] = $component;
  3128. // array_merge() can't be used here because the keys are numeric.
  3129. $children = _webform_components_tree_flatten($tree[$cid]['children']);
  3130. foreach ($children as $ccid => $ccomponent) {
  3131. $components[$ccid] = $ccomponent;
  3132. }
  3133. }
  3134. else {
  3135. $components[$cid] = $component;
  3136. }
  3137. }
  3138. return $components;
  3139. }
  3140. /**
  3141. * Helper for the uasort in webform_tree_sort()
  3142. */
  3143. function _webform_components_sort($a, $b) {
  3144. if ($a['weight'] == $b['weight']) {
  3145. return strcasecmp($a['name'], $b['name']);
  3146. }
  3147. return ($a['weight'] < $b['weight']) ? -1 : 1;
  3148. }
  3149. /**
  3150. * Sort each level of a component tree by weight and name
  3151. */
  3152. function _webform_components_tree_sort($tree) {
  3153. if (isset($tree['children']) && is_array($tree['children'])) {
  3154. $children = array();
  3155. uasort($tree['children'], '_webform_components_sort');
  3156. foreach ($tree['children'] as $cid => $component) {
  3157. $children[$cid] = _webform_components_tree_sort($component);
  3158. }
  3159. $tree['children'] = $children;
  3160. }
  3161. return $tree;
  3162. }
  3163. /**
  3164. * Get a list of all available component definitions.
  3165. */
  3166. function webform_components($include_disabled = FALSE, $reset = FALSE) {
  3167. static $components, $disabled;
  3168. if (!isset($components) || $reset) {
  3169. $components = array();
  3170. $disabled = array_flip(variable_get('webform_disabled_components', array()));
  3171. foreach (module_implements('webform_component_info') as $module) {
  3172. $module_components = module_invoke($module, 'webform_component_info');
  3173. foreach ($module_components as $type => $info) {
  3174. $module_components[$type]['module'] = $module;
  3175. $module_components[$type]['enabled'] = !array_key_exists($type, $disabled);
  3176. }
  3177. $components += $module_components;
  3178. }
  3179. drupal_alter('webform_component_info', $components);
  3180. ksort($components);
  3181. }
  3182. return $include_disabled ? $components : array_diff_key($components, $disabled);
  3183. }
  3184. /**
  3185. * Build a list of components suitable for use as select list options.
  3186. */
  3187. function webform_component_options($include_disabled = FALSE) {
  3188. $component_info = webform_components($include_disabled);
  3189. $options = array();
  3190. foreach ($component_info as $type => $info) {
  3191. $options[$type] = $info['label'];
  3192. }
  3193. return $options;
  3194. }
  3195. /**
  3196. * Load a component file into memory.
  3197. *
  3198. * @param $component_type
  3199. * The string machine name of a component.
  3200. */
  3201. function webform_component_include($component_type) {
  3202. static $included = array();
  3203. // No need to load components that have already been added once.
  3204. if (!isset($included[$component_type])) {
  3205. $components = webform_components(TRUE);
  3206. $included[$component_type] = TRUE;
  3207. if (($info = $components[$component_type]) && isset($info['file'])) {
  3208. $pathinfo = pathinfo($info['file']);
  3209. $basename = basename($pathinfo['basename'], '.' . $pathinfo['extension']);
  3210. $path = (!empty($pathinfo['dirname']) ? $pathinfo['dirname'] . '/' : '') . $basename;
  3211. module_load_include($pathinfo['extension'], $info['module'], $path);
  3212. }
  3213. }
  3214. }
  3215. /**
  3216. * Invoke a component callback.
  3217. *
  3218. * @param $type
  3219. * The component type as a string.
  3220. * @param $callback
  3221. * The callback to execute.
  3222. * @param ...
  3223. * Any additional parameters required by the $callback.
  3224. */
  3225. function webform_component_invoke($type, $callback) {
  3226. $args = func_get_args();
  3227. $type = array_shift($args);
  3228. $callback = array_shift($args);
  3229. $function = '_webform_' . $callback . '_' . $type;
  3230. webform_component_include($type);
  3231. if (function_exists($function)) {
  3232. return call_user_func_array($function, $args);
  3233. }
  3234. }
  3235. /**
  3236. * Check if a component implements a particular hook.
  3237. *
  3238. * @param $type
  3239. * The component type as a string.
  3240. * @param $callback
  3241. * The callback to check.
  3242. */
  3243. function webform_component_implements($type, $callback) {
  3244. $function = '_webform_' . $callback . '_' . $type;
  3245. webform_component_include($type);
  3246. return function_exists($function);
  3247. }
  3248. /**
  3249. * Disable the Drupal page cache.
  3250. */
  3251. function webform_disable_page_cache() {
  3252. drupal_page_is_cacheable(FALSE);
  3253. }
  3254. /**
  3255. * Set the necessary breadcrumb for the page we are on.
  3256. *
  3257. * @param object $node
  3258. * The loaded webform node.
  3259. * @param boolean|object $submission
  3260. * The submission if the current page is viewing or dealing with a submission,
  3261. * or TRUE to just include the webform node in the breadcrumbs (used for
  3262. * the submission completion confirmation page), or NULL for no extra
  3263. * processing
  3264. */
  3265. function webform_set_breadcrumb($node, $submission = NULL) {
  3266. $node_path = "node/{$node->nid}";
  3267. // Set the href of the current menu item to be the node's path. This has two
  3268. // effects. The active trail will be to the node's prefered menu tree location,
  3269. // expanding the menu as appropriate. And the breadcrumbs will be set as if
  3270. // the current page were under the node's preferred location.
  3271. // Note that menu_tree_set_path() could be used to set the path for the menu,
  3272. // but it will not affect the breadcrumbs when the webform is not in the
  3273. // default menu.
  3274. menu_set_item(NULL, array('href' => $node_path) + menu_get_item());
  3275. if ($submission) {
  3276. $breadcrumb = menu_get_active_breadcrumb();
  3277. // Append the node title (or its menu name), in case it isn't in the path already.
  3278. $active_trail = menu_get_active_trail();
  3279. $last_active = end($active_trail);
  3280. $breadcrumb[] = $last_active['href'] === $node_path && !empty($last_active['in_active_trail'])
  3281. ? l($last_active['title'], $node_path, $last_active['localized_options'])
  3282. : l($node->title, $node_path);
  3283. // Setting the current menu href will cause the submission title and current
  3284. // tab (if not the default tab) to be added to the active path when the
  3285. // webform is in the default location in the menu (node/NID). The title
  3286. // is desirable, but the tab name (e.g. Edit or Delete) isn't.
  3287. if (preg_match('/href=".*"/', end($breadcrumb), $matches)) {
  3288. foreach ($breadcrumb as $index => $link) {
  3289. if (stripos($link, $matches[0]) !== FALSE) {
  3290. $breadcrumb = array_slice($breadcrumb, 0, $index + 1);
  3291. break;
  3292. }
  3293. }
  3294. }
  3295. // If the user is dealing with a submission, then the breadcrumb should
  3296. // be fudged to allow them to return to a likely list of webforms.
  3297. // Note that this isn't necessarily where they came from, but it's the
  3298. // best guess available.
  3299. if (is_object($submission)) {
  3300. if (webform_results_access($node)) {
  3301. $breadcrumb[] = l(t('Webform results'), $node_path . '/webform-results');
  3302. }
  3303. elseif (user_access('access own webform results')) {
  3304. $breadcrumb[] = l(t('Submissions'), $node_path . '/submissions');
  3305. }
  3306. }
  3307. drupal_set_breadcrumb($breadcrumb);
  3308. }
  3309. }
  3310. /**
  3311. * Convert an ISO 8601 date or time into an array.
  3312. *
  3313. * This converts full format dates or times. Either a date or time may be
  3314. * provided, in which case only those portions will be returned. Dashes and
  3315. * colons must be used, never implied.
  3316. *
  3317. * Formats:
  3318. * Dates: YYYY-MM-DD
  3319. * Times: HH:MM:SS
  3320. * Datetimes: YYYY-MM-DDTHH:MM:SS
  3321. *
  3322. * @param $string
  3323. * An ISO 8601 date, time, or datetime.
  3324. * @param $type
  3325. * If wanting only specific fields back, specify either "date" or "time".
  3326. * Leaving empty will return an array with both date and time keys, even if
  3327. * some are empty. Returns an array with the following keys:
  3328. * - year
  3329. * - month
  3330. * - day
  3331. * - hour (in 24hr notation)
  3332. * - minute
  3333. * - second
  3334. */
  3335. function webform_date_array($string, $type = NULL) {
  3336. $pattern = '/((\d{4}?)-(\d{2}?)-(\d{2}?))?(T?(\d{2}?):(\d{2}?):(\d{2}?))?/';
  3337. $matches = array();
  3338. preg_match($pattern, $string, $matches);
  3339. $matches += array_fill(0, 9, '');
  3340. $return = array();
  3341. // Check for a date string.
  3342. if ($type == 'date' || !isset($type)) {
  3343. $return['year'] = $matches[2] !== '' ? (int) $matches[2] : '';
  3344. $return['month'] = $matches[3] !== '' ? (int) $matches[3] : '';
  3345. $return['day'] = $matches[4] !== '' ? (int) $matches[4] : '';
  3346. }
  3347. // Check for a time string.
  3348. if ($type == 'time' || !isset($type)) {
  3349. $return['hour'] = $matches[6] !== '' ? (int) $matches[6] : '';
  3350. $return['minute'] = $matches[7] !== '' ? (int) $matches[7] : '';
  3351. $return['second'] = $matches[8] !== '' ? (int) $matches[8] : '';
  3352. }
  3353. return $return;
  3354. }
  3355. /**
  3356. * Convert an array of a date or time into an ISO 8601 compatible string.
  3357. *
  3358. * @param $array
  3359. * The array to convert to a date or time string.
  3360. * @param $type
  3361. * If wanting a specific string format back specify either "date" or "time".
  3362. * Otherwise a full ISO 8601 date and time string will be returned.
  3363. */
  3364. function webform_date_string($array, $type = NULL) {
  3365. $string = '';
  3366. if ($type == 'date' || !isset($type)) {
  3367. $string .= empty($array['year']) ? '0000' : sprintf('%04d', $array['year']);
  3368. $string .= '-';
  3369. $string .= empty($array['month']) ? '00' : sprintf('%02d', $array['month']);
  3370. $string .= '-';
  3371. $string .= empty($array['day']) ? '00' : sprintf('%02d', $array['day']);
  3372. }
  3373. if (!isset($type)) {
  3374. $string .= 'T';
  3375. }
  3376. if ($type == 'time' || !isset($type)) {
  3377. $string .= empty($array['hour']) ? '00' : sprintf('%02d', $array['hour']);
  3378. $string .= ':';
  3379. $string .= empty($array['minute']) ? '00' : sprintf('%02d', $array['minute']);
  3380. $string .= ':';
  3381. $string .= empty($array['second']) ? '00' : sprintf('%02d', $array['second']);
  3382. }
  3383. return $string;
  3384. }
  3385. /**
  3386. * Get a date format according to the site settings.
  3387. *
  3388. * @param $size
  3389. * A choice of 'short', 'medium', or 'long' date formats.
  3390. */
  3391. function webform_date_format($size = 'medium') {
  3392. // Format date according to site's given format.
  3393. $format = variable_get('date_format_' . $size, 'D, m/d/Y - H:i');
  3394. $time = 'aABgGhHisueIOPTZ';
  3395. $day_of_week = 'Dlw';
  3396. $special = ',-: ';
  3397. $date_format = trim($format, $time . $day_of_week . $special);
  3398. // Ensure that a day, month, and year value are present. Use a default
  3399. // format if all the values are not found.
  3400. if (!preg_match('/[dj]/', $date_format) || !preg_match('/[FmMn]/', $date_format) || !preg_match('/[oYy]/', $date_format)) {
  3401. $date_format = 'm/d/Y';
  3402. }
  3403. return $date_format;
  3404. }
  3405. /**
  3406. * Return a date in the desired format taking into consideration user timezones.
  3407. */
  3408. function webform_strtodate($format, $string, $timezone_name = NULL) {
  3409. global $user;
  3410. // Adjust the time based on the user or site timezone.
  3411. if (variable_get('configurable_timezones', 1) && $timezone_name == 'user' && $user->uid) {
  3412. $timezone_name = isset($GLOBALS['user']->timezone) ? $GLOBALS['user']->timezone : 'UTC';
  3413. }
  3414. // If the timezone is still empty or not set, use the site timezone.
  3415. if (empty($timezone_name) || $timezone_name == 'user') {
  3416. $timezone_name = variable_get('date_default_timezone', 'UTC');
  3417. }
  3418. if (!empty($timezone_name) && class_exists('DateTimeZone')) {
  3419. // Suppress errors if encountered during string conversion. Exceptions are
  3420. // only supported for DateTime in PHP 5.3 and higher.
  3421. try {
  3422. @$timezone = new DateTimeZone($timezone_name);
  3423. @$datetime = new DateTime($string, $timezone);
  3424. return @$datetime->format($format);
  3425. }
  3426. catch (Exception $e) {
  3427. return '';
  3428. }
  3429. }
  3430. else {
  3431. return date($format, strtotime($string));
  3432. }
  3433. }
  3434. /**
  3435. * Get a timestamp in GMT time, ensuring timezone accuracy.
  3436. */
  3437. function webform_strtotime($date) {
  3438. $current_tz = date_default_timezone_get();
  3439. date_default_timezone_set('UTC');
  3440. $timestamp = strtotime($date);
  3441. date_default_timezone_set($current_tz);
  3442. return $timestamp;
  3443. }
  3444. /**
  3445. * Wrapper function for i18n_string() if i18nstrings enabled.
  3446. */
  3447. function webform_tt($name, $string, $langcode = NULL, $update = FALSE) {
  3448. if (function_exists('i18n_string')) {
  3449. $options = array(
  3450. 'langcode' => $langcode,
  3451. 'update' => $update,
  3452. );
  3453. return i18n_string($name, $string, $options);
  3454. }
  3455. else {
  3456. return $string;
  3457. }
  3458. }
  3459. /**
  3460. * Check if any available HTML mail handlers are available for Webform to use.
  3461. */
  3462. function webform_email_html_capable() {
  3463. // TODO: Right now we only support MIME Mail and HTML Mail. Support others
  3464. // if available through a hook?
  3465. $supported_html_modules = array(
  3466. 'mimemail' => 'MimeMailSystem',
  3467. 'htmlmail' => 'HTMLMailSystem',
  3468. );
  3469. foreach ($supported_html_modules as $mail_module => $mail_system_name) {
  3470. if (module_exists($mail_module)) {
  3471. $mail_systems = variable_get('mail_system', array('default-system' => 'DefaultMailSystem'));
  3472. if (isset($mail_systems['webform'])) {
  3473. $enable = strpos($mail_systems['webform'], $mail_system_name) !== FALSE ? $mail_systems['webform'] : FALSE;
  3474. }
  3475. else {
  3476. $enable = $mail_system_name;
  3477. }
  3478. }
  3479. }
  3480. if (!empty($enable)) {
  3481. // We assume that if a solution exists even if it's not specified we should
  3482. // use it. Webform will specify if e-mails sent with the system are plain-
  3483. // text or not when sending each e-mail.
  3484. $GLOBALS['conf']['mail_system']['webform'] = $enable;
  3485. return TRUE;
  3486. }
  3487. return FALSE;
  3488. }
  3489. /**
  3490. * Implements hook_views_api().
  3491. */
  3492. function webform_views_api() {
  3493. return array(
  3494. 'api' => 2.0,
  3495. 'path' => drupal_get_path('module', 'webform') . '/views',
  3496. );
  3497. }
  3498. /**
  3499. * Implements hook_field_extra_fields().
  3500. */
  3501. function webform_field_extra_fields() {
  3502. $extra = array();
  3503. foreach (webform_variable_get('webform_node_types') as $type) {
  3504. $extra['node'][$type]['display']['webform'] = array(
  3505. 'label' => t('Webform'),
  3506. 'description' => t('Webform client form.'),
  3507. 'weight' => 10,
  3508. );
  3509. }
  3510. return $extra;
  3511. }
  3512. /**
  3513. * Implements hook_mollom_form_list().
  3514. */
  3515. function webform_mollom_form_list() {
  3516. $forms = array();
  3517. $webform_types = webform_variable_get('webform_node_types');
  3518. if (empty($webform_types)) {
  3519. return $forms;
  3520. }
  3521. $query = db_select('webform', 'w');
  3522. $query->innerJoin('node', 'n', 'n.nid = w.nid');
  3523. $query->fields('n', array('nid', 'title'));
  3524. $query->condition('n.type', $webform_types, 'IN');
  3525. $result = $query->execute();
  3526. foreach ($result as $node) {
  3527. $form_id = 'webform_client_form_' . $node->nid;
  3528. $forms[$form_id] = array(
  3529. 'title' => t('@name form', array('@name' => $node->title)),
  3530. 'entity' => 'webform',
  3531. 'delete form' => 'webform_submission_delete_form',
  3532. );
  3533. }
  3534. return $forms;
  3535. }
  3536. /**
  3537. * Implements hook_mollom_form_info().
  3538. */
  3539. function webform_mollom_form_info($form_id) {
  3540. module_load_include('inc', 'webform', 'includes/webform.components');
  3541. $nid = drupal_substr($form_id, 20);
  3542. $node = node_load($nid);
  3543. $form_info = array(
  3544. 'title' => t('@name form', array('@name' => $node->title)),
  3545. 'mode' => MOLLOM_MODE_ANALYSIS,
  3546. 'bypass access' => array('edit all webform submissions', 'edit any webform content'),
  3547. 'entity' => 'webform',
  3548. 'elements' => array(),
  3549. 'mapping' => array(
  3550. 'post_id' => 'details][sid',
  3551. 'author_id' => 'details][uid',
  3552. ),
  3553. );
  3554. // Add components as elements.
  3555. // These components can be enabled for textual analysis (when not using a
  3556. // CAPTCHA-only protection) in Mollom's form configuration.
  3557. foreach ($node->webform['components'] as $cid => $component) {
  3558. if (webform_component_feature($component['type'], 'spam_analysis')) {
  3559. $parents = implode('][', webform_component_parent_keys($node, $component));
  3560. $form_info['elements']['submitted][' . $parents] = check_plain(t($component['name']));
  3561. }
  3562. }
  3563. // Assign field mappings based on webform configuration.
  3564. // Since multiple emails can be configured, we iterate over all and take
  3565. // over the assigned component for the field mapping in any email, unless
  3566. // we already assigned one. We are not interested in administratively
  3567. // configured static strings, only user-submitted values.
  3568. foreach ($node->webform['emails'] as $email) {
  3569. // Subject (post_title).
  3570. if (!isset($form_info['mapping']['post_title'])) {
  3571. $cid = $email['subject'];
  3572. if (is_numeric($cid)) {
  3573. $parents = implode('][', webform_component_parent_keys($node, $node->webform['components'][$cid]));
  3574. $form_info['mapping']['post_title'] = 'submitted][' . $parents;
  3575. }
  3576. }
  3577. // From name (author_name).
  3578. if (!isset($form_info['mapping']['author_name'])) {
  3579. $cid = $email['from_name'];
  3580. if (is_numeric($cid)) {
  3581. $parents = implode('][', webform_component_parent_keys($node, $node->webform['components'][$cid]));
  3582. $form_info['mapping']['author_name'] = 'submitted][' . $parents;
  3583. }
  3584. }
  3585. // From address (author_mail).
  3586. if (!isset($form_info['mapping']['author_mail'])) {
  3587. $cid = $email['from_address'];
  3588. if (is_numeric($cid)) {
  3589. $parents = implode('][', webform_component_parent_keys($node, $node->webform['components'][$cid]));
  3590. $form_info['mapping']['author_mail'] = 'submitted][' . $parents;
  3591. }
  3592. }
  3593. }
  3594. return $form_info;
  3595. }