webform.module 140 KB

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