webform_phone.components.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. <?php
  2. /**
  3. * @file
  4. * Webform Component information for a phone number field type
  5. */
  6. /**
  7. * Implements _webform_defaults_component().
  8. */
  9. function _webform_defaults_phone() {
  10. return array(
  11. 'name' => '',
  12. 'form_key' => NULL,
  13. 'required' => 0,
  14. 'mandatory' => 0,
  15. 'pid' => 0,
  16. 'weight' => 0,
  17. 'value' => '',
  18. 'extra' => array(
  19. 'title_display' => 0,
  20. 'width' => '',
  21. 'disabled' => FALSE,
  22. 'private' => FALSE,
  23. 'attributes' => array(),
  24. 'description' => '',
  25. 'placeholder' => '',
  26. 'country' => 'ca',
  27. 'phone_country_code' => 0,
  28. 'phone_default_country_code' => 1,
  29. 'phone_int_max_length' => 15,
  30. 'ca_phone_separator' => '-',
  31. 'ca_phone_parentheses' => 1,
  32. ),
  33. );
  34. }
  35. /**
  36. * Implements _webform_theme_component().
  37. */
  38. function _webform_theme_phone() {
  39. return array( 'webform_display_phonefield' => array( 'render element' => 'element' ) );
  40. }
  41. /**
  42. * Generate the form for editing a component.
  43. * Create a set of form elements to be displayed on the form for editing this
  44. * component. Use care naming the form items, as this correlates directly to the
  45. * database schema. The component "Name" and "Description" fields are added to
  46. * every component type and are not necessary to specify here (although they
  47. * may be overridden if desired).
  48. *
  49. * @param $component
  50. * A Webform component array.
  51. *
  52. * @return
  53. * An array of form items to be displayed on the edit component page
  54. */
  55. function _webform_edit_phone($component) {
  56. $form = array();
  57. // General Options
  58. $form['extra']['country'] = array(
  59. '#type' => 'select',
  60. '#title' => t('Country'),
  61. '#options' => phone_countries(),
  62. '#default_value' => $component['extra']['country'],
  63. '#description' => t('Which country-specific rules should this field be validated against and formatted according to.'),
  64. '#required' => TRUE,
  65. );
  66. $form['extra']['phone_country_code'] = array(
  67. '#type' => 'checkbox',
  68. '#title' => t('Add the country code if not filled by the user'),
  69. '#default_value' => $component['extra']['phone_country_code'],
  70. );
  71. // International Options
  72. $form['extra']['phone_int_help'] = array(
  73. '#type' => 'markup',
  74. '#value' => t('International phone numbers are in the form +XX YYYYYYY where XX is a country code and YYYYYYY is the local number. This field type is based off of the <a href="http://www.itu.int/rec/T-REC-E.123/en">E.123 specification</a>.'),
  75. '#states' => array(
  76. 'visible' => array(
  77. ':input[name="extra[country]"]' => array( 'value' => 'int' ),
  78. ),
  79. ),
  80. );
  81. $form['extra']['phone_default_country_code'] = array(
  82. '#type' => 'textfield',
  83. '#title' => t('Default country code to add to international numbers without one (omit + sign)'),
  84. '#default_value' => $component['extra']['phone_default_country_code'],
  85. '#states' => array(
  86. 'visible' => array(
  87. ':input[name="extra[country]"]' => array( 'value' => 'int' ),
  88. ),
  89. ),
  90. );
  91. $form['extra']['phone_int_max_length'] = array(
  92. '#type' => 'textfield',
  93. '#title' => t('Maximum length of international numbers, according to the ITU this is 15'),
  94. '#default_value' => $component['extra']['phone_int_max_length'],
  95. '#states' => array(
  96. 'visible' => array(
  97. ':input[name="extra[country]"]' => array( 'value' => 'int' ),
  98. ),
  99. ),
  100. );
  101. // US/Canada Options
  102. $form['extra']['ca_phone_separator'] = array(
  103. '#type' => 'textfield',
  104. '#title' => t('Separator'),
  105. '#default_value' => $component['extra']['ca_phone_separator'],
  106. '#size' => 2,
  107. '#states' => array(
  108. 'visible' => array(
  109. ':input[name="extra[country]"]' => array( 'value' => 'ca' ),
  110. ),
  111. ),
  112. );
  113. $form['extra']['ca_phone_parentheses'] = array(
  114. '#type' => 'checkbox',
  115. '#title' => t('Use parentheses around area code'),
  116. '#default_value' => $component['extra']['ca_phone_parentheses'],
  117. '#states' => array(
  118. 'visible' => array(
  119. ':input[name="extra[country]"]' => array( 'value' => 'ca' ),
  120. ),
  121. ),
  122. );
  123. $form['value'] = array(
  124. '#type' => 'textfield',
  125. '#title' => t('Default value'),
  126. '#default_value' => $component['value'],
  127. '#description' => t('The default value of the field.') . theme('webform_token_help'),
  128. '#size' => 60,
  129. '#maxlength' => 1024,
  130. '#weight' => 0,
  131. );
  132. $form['display']['width'] = array(
  133. '#type' => 'textfield',
  134. '#title' => t('Width'),
  135. '#default_value' => $component['extra']['width'],
  136. '#description' => t('Width of the textfield.') . ' ' . t('Leaving blank will use the default size.'),
  137. '#size' => 5,
  138. '#maxlength' => 10,
  139. '#weight' => 0,
  140. '#parents' => array( 'extra', 'width' ),
  141. );
  142. $form['display']['placeholder'] = array(
  143. '#type' => 'textfield',
  144. '#title' => t('Placeholder'),
  145. '#default_value' => $component['extra']['placeholder'],
  146. '#description' => t('The text will be shown in the field until the user starts entering a value.'),
  147. '#weight' => 1,
  148. '#parents' => array( 'extra', 'placeholder' ),
  149. );
  150. $form['display']['disabled'] = array(
  151. '#type' => 'checkbox',
  152. '#title' => t('Disabled'),
  153. '#return_value' => 1,
  154. '#description' => t('Make this field non-editable. Useful for setting an unchangeable default value.'),
  155. '#weight' => 11,
  156. '#default_value' => $component['extra']['disabled'],
  157. '#parents' => array( 'extra', 'disabled' ),
  158. );
  159. return $form;
  160. }
  161. /**
  162. * Render a Webform component to be part of a form.
  163. *
  164. * @param $component
  165. * A Webform component array.
  166. * @param $value
  167. * If editing an existing submission or resuming a draft, this will contain
  168. * an array of values to be shown instead of the default in the component
  169. * configuration. This value will always be an array, keyed numerically for
  170. * each value saved in this field.
  171. * @param $filter
  172. * Whether or not to filter the contents of descriptions and values when
  173. * rendering the component. Values need to be unfiltered to be editable by
  174. * Form Builder.
  175. *
  176. * @see _webform_client_form_add_component()
  177. */
  178. function _webform_render_phone($component, $value = NULL, $filter = TRUE) {
  179. //TODO: change these to use non-private functions (no _) if/when webform 3.x is entirely deprecated
  180. $form_item = array(
  181. '#type' => module_exists('elements') ? 'telfield' : 'textfield',
  182. // '#default_value' => $filter ? webform_replace_tokens($component['value'], NULL, NULL, NULL, TRUE) : $component['value'],
  183. '#default_value' => $filter ? _webform_filter_values($component['value']) : $component['value'],
  184. '#attributes' => $component['extra']['attributes'],
  185. '#theme_wrappers' => array( 'webform_element' ),
  186. // '#description' => $filter ? webform_filter_descriptions($component['extra']['description']) : $component['extra']['description'],
  187. '#description' => $filter ? _webform_filter_descriptions($component['extra']['description']) : $component['extra']['description'],
  188. '#element_validate' => array( 'webform_validate_phone' ),
  189. '#maxlength' => ( $component['extra']['country'] == 'int' ? ( isset( $component['extra']['phone_int_max_length'] ) ? $component['extra']['phone_int_max_length'] : NULL ) : NULL ),
  190. '#required' => $component['required'] || $component['mandatory'], //Either one being true will could as required...because webform changed in 4.x-alpha8
  191. '#size' => 17,
  192. // '#title' => $filter ? webform_filter_xss($component['name']) : $component['name'],
  193. '#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
  194. '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
  195. '#weight' => $component['weight'],
  196. '#translatable' => array( 'title', 'description' ),
  197. );
  198. if (isset( $value )) {
  199. $form_item['#default_value'] = $value[0];
  200. }
  201. // Change the 'width' option to the correct 'size' option.
  202. if ($component['extra']['width'] > 0) {
  203. $form_item['#size'] = $component['extra']['width'];
  204. }
  205. // Show the placeholder text if used.
  206. if ($component['extra']['placeholder']) {
  207. $form_item['#attributes']['placeholder'] = $component['extra']['placeholder'];
  208. }
  209. if ($component['extra']['disabled']) {
  210. if ($filter) {
  211. $form_item['#attributes']['readonly'] = 'readonly';
  212. }
  213. else {
  214. $form_item['#disabled'] = TRUE;
  215. }
  216. }
  217. if (isset( $value[0] )) {
  218. $form_item['#default_value'] = $value[0];
  219. }
  220. return $form_item;
  221. }
  222. /**
  223. * Validation Callback for phone field
  224. */
  225. function webform_validate_phone($element, $form_state) {
  226. $value = $element['#value'];
  227. if (isset( $value ) && $value != '') {
  228. $ccode = $element['#webform_component']['extra']['country'];
  229. //run through 'phone' module's validation
  230. if (!valid_phone_number($ccode, $value)) {
  231. $country = phone_country_info($ccode);
  232. form_error($element, t($country['error'], array( '%value' => $value )));
  233. }
  234. }
  235. }
  236. /**
  237. * Display the result of a submission for a component.
  238. * The output of this function will be displayed under the "Results" tab then
  239. * "Submissions". This should output the saved data in some reasonable manner.
  240. *
  241. * @param $component
  242. * A Webform component array.
  243. * @param $value
  244. * An array of information containing the submission result, directly
  245. * correlating to the webform_submitted_data database table schema.
  246. * @param $format
  247. * Either 'html' or 'text'. Defines the format that the content should be
  248. * returned as. Make sure that returned content is run through check_plain()
  249. * or other filtering functions when returning HTML.
  250. *
  251. * @return
  252. * A renderable element containing at the very least these properties:
  253. * - #title
  254. * - #weight
  255. * - #component
  256. * - #format
  257. * - #value
  258. * Webform also uses #theme_wrappers to output the end result to the user,
  259. * which will properly format the label and content for use within an e-mail
  260. * (such as wrapping the text) or as HTML (ensuring consistent output).
  261. */
  262. function _webform_display_phone($component, $value, $format = 'html') {
  263. return array(
  264. '#title' => $component['name'],
  265. '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
  266. '#weight' => $component['weight'],
  267. '#theme' => 'webform_display_phonefield',
  268. '#theme_wrappers' => $format == 'html' ? array( 'webform_element' ) : array( 'webform_element_text' ),
  269. '#post_render' => array( 'webform_element_wrapper' ),
  270. '#component' => $component,
  271. '#format' => $format,
  272. '#value' => isset( $value[0] ) ? $value[0] : '',
  273. '#translatable' => array( 'title', 'description' ),
  274. );
  275. }
  276. /**
  277. * Format the output of data for this component.
  278. */
  279. function theme_webform_display_phonefield($variables) {
  280. $element = $variables['element'];
  281. $plain_value = check_plain($element['#value']);
  282. if ($element['#format'] == 'html') {
  283. //Use smarter detection if available for formatting the output
  284. $is_mobile_device = module_exists('mobile_tools') ? mobile_tools_is_mobile_device() : strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') !== FALSE || strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== FALSE;
  285. $value = ( $is_mobile_device ) ? '<a href="tel:' . $plain_value . '">' . $plain_value . '</a>' : $plain_value;
  286. }
  287. else {
  288. $value = $plain_value;
  289. }
  290. return $value;
  291. }
  292. /**
  293. * A hook for changing the input values before saving to the database.
  294. * Webform expects a component to consist of a single field, or a single array
  295. * of fields. If you have a component that requires a deeper form tree
  296. * you must flatten the data into a single array using this callback
  297. * or by setting #parents on each field to avoid data loss and/or unexpected
  298. * behavior.
  299. * Note that Webform will save the result of this function directly into the
  300. * database.
  301. *
  302. * @param $component
  303. * A Webform component array.
  304. * @param $value
  305. * The POST data associated with the user input.
  306. *
  307. * @return
  308. * An array of values to be saved into the database. Note that this should be
  309. * a numerically keyed array.
  310. */
  311. function _webform_submit_phone($component, $value) {
  312. $ccode = $component['extra']['country'];
  313. if (phone_countries($ccode) !== NULL) {
  314. if (isset( $value ) && !empty( $value )) {
  315. //Use 'phone' module to format the number
  316. return format_phone_number($ccode, $value, $component['extra']);
  317. }
  318. }
  319. return FALSE; //If we haven't returned already, something failed.
  320. }
  321. /**
  322. * Calculate and returns statistics about results for this component.
  323. * This takes into account all submissions to this webform. The output of this
  324. * function will be displayed under the "Results" tab then "Analysis".
  325. *
  326. * @param $component
  327. * An array of information describing the component, directly correlating to
  328. * the webform_component database schema.
  329. * @param $sids
  330. * An optional array of submission IDs (sid). If supplied, the analysis will
  331. * be limited to these sids.
  332. * @param $single
  333. * Boolean flag determining if the details about a single component are being
  334. * shown. May be used to provided detailed information about a single
  335. * component's analysis, such as showing "Other" options within a select list.
  336. *
  337. * @return
  338. * An array of data rows, each containing a statistic for this component's
  339. * submissions.
  340. */
  341. function _webform_analysis_phone($component, $sids = array(), $single = FALSE) {
  342. // Generate the list of options and questions.
  343. $query = db_select('webform_submitted_data', 'wsd', array( 'fetch' => PDO::FETCH_ASSOC ))->fields('wsd', array( 'data' ))->condition('nid', $component['nid'])->condition('cid', $component['cid']);
  344. if (count($sids)) {
  345. $query->condition('sid', $sids, 'IN');
  346. }
  347. $non_blanks = 0;
  348. $submissions = 0;
  349. $result = $query->execute();
  350. foreach ($result as $data) {
  351. if (drupal_strlen(trim($data['data'])) > 0) {
  352. $non_blanks++;
  353. }
  354. $submissions++;
  355. }
  356. $rows[0] = array(
  357. t('Left Blank'),
  358. ( $submissions - $non_blanks )
  359. );
  360. $rows[1] = array(
  361. t('User entered value'),
  362. $non_blanks
  363. );
  364. return $rows;
  365. }
  366. /**
  367. * Return the result of a component value for display in a table.
  368. * The output of this function will be displayed under the "Results" tab then
  369. * "Table".
  370. *
  371. * @param $component
  372. * A Webform component array.
  373. * @param $value
  374. * An array of information containing the submission result, directly
  375. * correlating to the webform_submitted_data database schema.
  376. *
  377. * @return
  378. * Textual output formatted for human reading.
  379. */
  380. function _webform_table_phone($component, $value) {
  381. return check_plain(empty( $value[0] ) ? '' : $value[0]);
  382. }
  383. /**
  384. * Return the header for this component to be displayed in a CSV file.
  385. * The output of this function will be displayed under the "Results" tab then
  386. * "Download".
  387. *
  388. * @param $component
  389. * A Webform component array.
  390. * @param $export_options
  391. * An array of options that may configure export of this field.
  392. *
  393. * @return
  394. * An array of data to be displayed in the first three rows of a CSV file, not
  395. * including either prefixed or trailing commas.
  396. */
  397. function _webform_csv_headers_phone($component, $export_options) {
  398. $header = array();
  399. $header[0] = '';
  400. $header[1] = '';
  401. $header[2] = $export_options['header_keys'] ? $component['form_key'] : $component['name'];
  402. return $header;
  403. }
  404. /**
  405. * Format the submitted data of a component for CSV downloading.
  406. * The output of this function will be displayed under the "Results" tab then
  407. * "Download".
  408. *
  409. * @param $component
  410. * A Webform component array.
  411. * @param $export_options
  412. * An array of options that may configure export of this field.
  413. * @param $value
  414. * An array of information containing the submission result, directly
  415. * correlating to the webform_submitted_data database schema.
  416. *
  417. * @return
  418. * An array of items to be added to the CSV file. Each value within the array
  419. * will be another column within the file. This function is called once for
  420. * every row of data.
  421. */
  422. function _webform_csv_data_phone($component, $export_options, $value) {
  423. return !isset( $value[0] ) ? '' : $value[0];
  424. }
  425. /**
  426. * The first hook provides the name and position of the field in the Form Builder palette, as well as a default element to display when the field is pulled out of the palette.
  427. * The second hook maps the component properties and options to FormAPI properties that Form Builder can manipulate.
  428. * Form Builder then will manage pulling the form out of the normal Webform configuration form, loading configuration, and saving it.
  429. * There are plenty of examples in the form_builder_webform.components.inc file that other modules (such as Webform Phone Number) can use as templates.
  430. * I'm moving this request over to that module's queue, and changing to a feature request.
  431. */
  432. /**
  433. * @defgroup form-builder-webform-phone-callbacks Callbacks for the Phone component
  434. * @{
  435. */
  436. /**
  437. * Implements _form_builder_webform_form_builder_types_component().
  438. */
  439. function _form_builder_webform_form_builder_types_phone() {
  440. drupal_add_css(drupal_get_path('module', 'webform_phone') . '/webform_phone.css');
  441. $fields = array();
  442. $fields['phone'] = array(
  443. 'title' => t('Phone Number'),
  444. 'properties' => array(
  445. 'country',
  446. 'phone_country_code',
  447. 'phone_default_country_code',
  448. 'phone_int_max_length',
  449. 'ca_phone_separator',
  450. 'ca_phone_parentheses',
  451. ),
  452. 'weight' => -17,
  453. //Doesn't make sense that modules get to weight themselves, why wouldn't everyone want to be first?
  454. );
  455. $defaults = _webform_defaults_phone();
  456. $fields['phone']['default'] = _form_builder_webform_default('phone');
  457. $fields['phone']['default']['#title'] = t('New Phone Number Field');
  458. $fields['phone']['default']['#country'] = $defaults['extra']['country'];
  459. $fields['phone']['default']['#phone_country_code'] = $defaults['extra']['phone_country_code'];
  460. $fields['phone']['default']['#phone_default_country_code'] = $defaults['extra']['phone_default_country_code'];
  461. $fields['phone']['default']['#phone_int_max_length'] = $defaults['extra']['phone_int_max_length'];
  462. $fields['phone']['default']['#ca_phone_separator'] = $defaults['extra']['ca_phone_separator'];
  463. $fields['phone']['default']['#ca_phone_parentheses'] = $defaults['extra']['ca_phone_parentheses'];
  464. return $fields;
  465. }
  466. /**
  467. * Implements _form_builder_webform_form_builder_map_component().
  468. */
  469. function _form_builder_webform_form_builder_map_phone() {
  470. return array(
  471. 'form_builder_type' => 'phone',
  472. 'properties' => array(
  473. 'country' => array(
  474. 'form_parents' => array(
  475. 'extra',
  476. 'country'
  477. ),
  478. 'storage_parents' => array(
  479. 'extra',
  480. 'country'
  481. ),
  482. ),
  483. 'phone_country_code' => array(
  484. 'form_parents' => array(
  485. 'extra',
  486. 'phone_country_code'
  487. ),
  488. 'storage_parents' => array(
  489. 'extra',
  490. 'phone_country_code'
  491. ),
  492. ),
  493. 'phone_default_country_code' => array(
  494. 'form_parents' => array(
  495. 'extra',
  496. 'phone_default_country_code'
  497. ),
  498. 'storage_parents' => array(
  499. 'extra',
  500. 'phone_default_country_code'
  501. ),
  502. ),
  503. 'phone_int_max_length' => array(
  504. 'form_parents' => array(
  505. 'extra',
  506. 'phone_int_max_length'
  507. ),
  508. 'storage_parents' => array(
  509. 'extra',
  510. 'phone_int_max_length'
  511. ),
  512. ),
  513. 'ca_phone_separator' => array(
  514. 'form_parents' => array(
  515. 'extra',
  516. 'ca_phone_separator'
  517. ),
  518. 'storage_parents' => array(
  519. 'extra',
  520. 'ca_phone_separator'
  521. ),
  522. ),
  523. 'ca_phone_parentheses' => array(
  524. 'form_parents' => array(
  525. 'extra',
  526. 'ca_phone_parentheses'
  527. ),
  528. 'storage_parents' => array(
  529. 'extra',
  530. 'ca_phone_parentheses'
  531. ),
  532. ),
  533. ),
  534. );
  535. }
  536. /**
  537. * @} End of "defgroup form-builder-webform-phone-callbacks"
  538. */