addressfield_tokens.module 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * Copyright 2011 New Signature
  6. * http://www.newsignature.com
  7. *
  8. * @author Andrew Marcus
  9. * @since Oct 4, 2011
  10. */
  11. function addressfield_tokens_menu() {
  12. $items = array();
  13. $items['admin/config/regional/address'] = array(
  14. 'title' => 'Addresses',
  15. 'description' => 'Settings for address fields and tokens',
  16. 'page callback' => 'drupal_get_form',
  17. 'page arguments' => array('addressfield_tokens_admin_form'),
  18. 'access arguments' => array('administer site configuration'),
  19. 'file' => 'addressfield_tokens.admin.inc',
  20. 'type' => MENU_NORMAL_ITEM,
  21. );
  22. return $items;
  23. }
  24. /**
  25. * Implements hook_theme().
  26. */
  27. function addressfield_tokens_theme($existing, $type, $theme, $path) {
  28. $theme = array(
  29. 'addressfield_formatter' => array(
  30. 'variables' => array('address' => NULL, 'handlers' => array('address')),
  31. 'file' => 'addressfield_tokens.theme.inc',
  32. ),
  33. 'addressfield_formatter__citystate' => array(
  34. 'variables' => array('address' => NULL),
  35. 'file' => 'addressfield_tokens.theme.inc',
  36. ),
  37. 'addressfield_formatter__linear' => array(
  38. 'variables' => array('address' => NULL, 'premise' => TRUE, 'organisation_name' => TRUE, 'name_line' => TRUE),
  39. 'file' => 'addressfield_tokens.theme.inc',
  40. ),
  41. 'addressfield_formatter__components' => array(
  42. 'variables' => array(
  43. 'address' => NULL,
  44. 'components' => array('thoroughfare', 'premise', 'locality', 'administrative_area', 'postal_code', 'country'),
  45. 'separator' => ', ',
  46. ),
  47. 'file' => 'addressfield_tokens.theme.inc',
  48. ),
  49. );
  50. return $theme;
  51. }
  52. /**
  53. * Implements hook_field_formatter_info().
  54. */
  55. function addressfield_tokens_field_formatter_info() {
  56. return array(
  57. 'addressfield_citystate' => array(
  58. 'label' => t('City/State'),
  59. 'field types' => array('addressfield'),
  60. ),
  61. 'addressfield_linear' => array(
  62. 'label' => t('One line'),
  63. 'field types' => array('addressfield'),
  64. ),
  65. 'addressfield_state' => array(
  66. 'label' => t('State'),
  67. 'field types' => array('addressfield'),
  68. ),
  69. 'addressfield_country' => array(
  70. 'label' => t('Country'),
  71. 'field types' => array('addressfield'),
  72. ),
  73. 'addressfield_components' => array(
  74. 'label' => t('Address components'),
  75. 'field types' => array('addressfield'),
  76. 'settings' => array(
  77. 'components' => array('thoroughfare', 'premise', 'locality', 'administrative_area', 'postal_code', 'country'),
  78. 'separator' => ', ',
  79. ),
  80. ),
  81. );
  82. }
  83. /**
  84. * Implements hook_field_formatter_settings_form().
  85. */
  86. function addressfield_tokens_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  87. $display = $instance['display'][$view_mode];
  88. $settings = $display['settings'];
  89. $element = array();
  90. if ($display['type'] == 'addressfield_components') {
  91. $element['components'] = array(
  92. '#type' => 'select',
  93. '#title' => t('Components to render'),
  94. '#multiple' => TRUE,
  95. '#rows' => 10,
  96. '#options' => addressfield_tokens_components(),
  97. '#default_value' => $settings['components'],
  98. '#required' => TRUE,
  99. );
  100. $element['separator'] = array(
  101. '#type' => 'textfield',
  102. '#title' => t('Separator'),
  103. '#description' => t('The separator to use between components. Use \n for a line break.'),
  104. '#default_value' => $settings['separator'],
  105. );
  106. }
  107. return $element;
  108. }
  109. /**
  110. * Implements hook_field_formatter_settings_summary().
  111. */
  112. function addressfield_tokens_field_formatter_settings_summary($field, $instance, $view_mode) {
  113. $display = $instance['display'][$view_mode];
  114. $settings = $display['settings'];
  115. $summary = '';
  116. if ($display['type'] == 'addressfield_components') {
  117. $comps = array_intersect_key(addressfield_tokens_components(), array_flip($settings['components']));
  118. $sep = str_replace('\n', '<br/>', $settings['separator']);
  119. $summary = implode($sep, $comps);
  120. }
  121. return $summary;
  122. }
  123. /**
  124. * Implements hook_field_formatter_view().
  125. */
  126. function addressfield_tokens_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  127. $element = array();
  128. switch ($display['type']) {
  129. case 'addressfield_citystate':
  130. $theme = array('addressfield_formatter__citystate', 'addressfield_formatter');
  131. if (!empty($item['country'])) {
  132. array_unshift($theme, 'addressfield_formatter__citystate__' . $item['country']);
  133. }
  134. foreach ($items as $delta => $item) {
  135. $element[$delta] = array(
  136. '#theme' => $theme,
  137. '#address' => $item,
  138. );
  139. }
  140. break;
  141. case 'addressfield_linear':
  142. $theme = array('addressfield_formatter__linear', 'addressfield_formatter');
  143. if (!empty($item['country'])) {
  144. array_unshift($theme, 'addressfield_formatter__linear__' . $item['country']);
  145. }
  146. foreach ($items as $delta => $item) {
  147. $element[$delta] = array(
  148. '#theme' => $theme,
  149. '#address' => $item,
  150. );
  151. }
  152. break;
  153. case 'addressfield_country':
  154. foreach ($items as $delta => $item) {
  155. if (!empty($item['country'])) {
  156. $country = _addressfield_tokens_country($item['country']);
  157. $element[$delta] = array(
  158. '#type' => 'markup',
  159. '#markup' => $country,
  160. '#prefix' => '<span class="addressfield-country">',
  161. '#suffix' => '</span>',
  162. );
  163. }
  164. }
  165. break;
  166. case 'addressfield_state':
  167. foreach ($items as $delta => $item) {
  168. if (!empty($item['country']) && !empty($item['administrative_area'])) {
  169. $state = _addressfield_tokens_state($item['country'], $item['administrative_area']);
  170. $element[$delta] = array(
  171. '#type' => 'markup',
  172. '#markup' => $state,
  173. '#prefix' => '<span class="addressfield-state">',
  174. '#suffix' => '</span>',
  175. );
  176. }
  177. }
  178. break;
  179. case 'addressfield_components':
  180. $theme = array('addressfield_formatter__components', 'addressfield_formatter');
  181. if (!empty($item['country'])) {
  182. array_unshift($theme, 'addressfield_formatter__components__' . $item['country']);
  183. }
  184. $settings = $display['settings'];
  185. foreach ($items as $delta => $item) {
  186. $element[$delta] = array(
  187. '#theme' => $theme,
  188. '#address' => $item,
  189. '#components' => $settings['components'],
  190. '#separator' => $settings['separator'],
  191. );
  192. }
  193. break;
  194. }
  195. return $element;
  196. }
  197. /**
  198. * Returns the country that has been configured as the default country.
  199. */
  200. function addressfield_tokens_default_country() {
  201. return variable_get('addressfield_tokens_default_country', 'US');
  202. }
  203. /**
  204. * Returns the names that have been configured for each address field.
  205. */
  206. function addressfield_tokens_property_names() {
  207. $names = variable_get('addressfield_tokens_property_names', array());
  208. if (empty($names)) {
  209. $props = addressfield_data_property_info();
  210. foreach($props as $name => $prop) {
  211. $names[$name] = $prop['label'];
  212. }
  213. }
  214. return $names;
  215. }
  216. function addressfield_tokens_components() {
  217. $comps = &drupal_static(__FUNCTION__, array());
  218. if (empty($comps)) {
  219. $names = addressfield_tokens_property_names();
  220. foreach (array('first_name', 'last_name', 'name_line', 'organisation_name', 'thoroughfare', 'premise', 'locality', 'dependent_locality', 'administrative_area', 'sub_administrative_area', 'postal_code', 'country') as $key) {
  221. $comps[$key] = $names[$key];
  222. if (in_array($key, array('administrative_area', 'country'))) {
  223. $comps[$key . '_full'] = t('@name (full)', array(
  224. '@name' => $names[$key],
  225. ));
  226. }
  227. }
  228. }
  229. return $comps;
  230. }
  231. /**
  232. * Gets the list of countries from the locale settings in Drupal.
  233. *
  234. * @return array An array of countries. The keys are the 2-letter
  235. * abbreviations and the values are the full country names.
  236. */
  237. function _addressfield_tokens_countries() {
  238. $countries = &drupal_static(__FUNCTION__);
  239. if (empty($countries)) {
  240. include_once('includes/locale.inc');
  241. $countries = country_get_list();
  242. }
  243. return $countries;
  244. }
  245. /**
  246. * Gets the name of the country with the given abbreviation.
  247. *
  248. * @param string $country The 2-letter abbreviation of the country.
  249. * @return string The name of the country, or FALSE.
  250. */
  251. function _addressfield_tokens_country($country) {
  252. $countries = _addressfield_tokens_countries();
  253. // Country abbreviations will always be two uppercase letters.
  254. $country = drupal_strtoupper($country);
  255. if (!empty($country) && isset($countries[$country])) {
  256. return $countries[$country];
  257. }
  258. return $country;
  259. }
  260. /**
  261. * Gets the abbreviation of the country with the given name
  262. *
  263. * @param string The name of the country
  264. * @return string $country The 2-letter abbreviation of the country, or FALSE.
  265. */
  266. function _addressfield_tokens_country_abbr($country) {
  267. $countries = array_flip(array_map('strtolower', _addressfield_tokens_countries()));
  268. if (isset($countries[strtolower($country)])) {
  269. return $countries[strtolower($country)];
  270. }
  271. return $country;
  272. }
  273. /**
  274. * Gets the list of states in the given country.
  275. *
  276. * @param string $country The 2-letter abbreviation of the country.
  277. *
  278. * @return array An array of countries. The keys are the 2-letter
  279. * abbreviations and the values are the full country names.
  280. */
  281. function _addressfield_tokens_states($country) {
  282. $states = &drupal_static(__FUNCTION__);
  283. $country = drupal_strtoupper($country);
  284. if (!isset($states[$country])) {
  285. $cache = cache_get('addressfield_tokens_states');
  286. if ($cache) {
  287. $states = $cache->data;
  288. }
  289. }
  290. if (!isset($states[$country])) {
  291. $format = addressfield_generate(array('country' => $country), array('address'), array('mode' => 'render'));
  292. if (isset($format['locality_block']['administrative_area']['#options'])) {
  293. $states[$country] = $format['locality_block']['administrative_area']['#options'];
  294. }
  295. else {
  296. $states[$country] = array();
  297. }
  298. cache_set('addressfield_tokens_states', $states);
  299. }
  300. return $states[$country];
  301. }
  302. /**
  303. * Gets the name of the state with the given abbreviation.
  304. *
  305. * @param string $country The 2-letter abbreviation of the country.
  306. * @param string $state The 2-letter abbreviation of the state.
  307. * @return string The name of the state, or FALSE.
  308. */
  309. function _addressfield_tokens_state($country, $state) {
  310. $states = _addressfield_tokens_states($country);
  311. // State abbreviations will usually be two uppercase letters.
  312. $state = drupal_strtoupper($state);
  313. if (!empty($state) && !empty($states[$state])) {
  314. return $states[$state];
  315. }
  316. return $state;
  317. }
  318. /**
  319. * Implements hook_webform_component_info().
  320. */
  321. function addressfield_tokens_webform_component_info() {
  322. $components = array ();
  323. $components['addressfield'] = array (
  324. 'label' => t('Address'),
  325. 'description' => t('Address field.'),
  326. 'features' => array (
  327. // Add content to CSV downloads. Defaults to TRUE.
  328. 'csv' => TRUE,
  329. // Show this component in e-mailed submissions. Defaults to TRUE.
  330. 'email' => TRUE,
  331. // Allow this component to be used as an e-mail FROM or TO address.
  332. // Defaults to FALSE.
  333. 'email_address' => FALSE,
  334. // Allow this component to be used as an e-mail SUBJECT or FROM name.
  335. // Defaults to FALSE.
  336. 'email_name' => FALSE,
  337. // This component may be toggled as required or not. Defaults to TRUE.
  338. 'required' => TRUE,
  339. // This component has a title that can be toggled as displayed or not.
  340. 'title_display' => TRUE,
  341. // This component has a title that can be displayed inline.
  342. 'title_inline' => FALSE,
  343. // If this component can be used as a conditional SOURCE. All components
  344. // may always be displayed conditionally, regardless of this setting.
  345. // Defaults to TRUE.
  346. 'conditional' => FALSE,
  347. // If this component allows other components to be grouped within it
  348. // (like a fieldset or tabs). Defaults to FALSE.
  349. 'group' => FALSE,
  350. // If this component can be used for SPAM analysis, usually with Mollom.
  351. 'spam_analysis' => FALSE,
  352. // If this component saves a file that can be used as an e-mail
  353. // attachment. Defaults to FALSE.
  354. 'attachment' => FALSE,
  355. ),
  356. 'file' => 'addressfield_tokens.components.inc',
  357. );
  358. return $components;
  359. }
  360. function _addressfield_tokens_webform_components($nid) {
  361. $components = &drupal_static(__FUNCTION__, array());
  362. if (!isset($components[$nid])) {
  363. $components[$nid] = db_select('webform_component')
  364. ->fields('webform_component')
  365. ->condition('type', 'addressfield')
  366. ->condition('nid', $nid)
  367. ->execute()
  368. ->fetchAllAssoc('cid', PDO::FETCH_ASSOC);
  369. }
  370. return $components[$nid];
  371. }
  372. /**
  373. * Implements hook_webform_submission_load().
  374. */
  375. function addressfield_tokens_webform_submission_load(&$submissions) {
  376. $submissions_reset = reset($submissions);
  377. $nid = $submissions_reset->nid;
  378. $components = _addressfield_tokens_webform_components($nid);
  379. foreach ($submissions as $sid => $submission) {
  380. foreach ($components as $cid => $component) {
  381. if (!empty($submission->data[$cid])) {
  382. $parents = array($cid);
  383. if (!empty($submission->data[$cid]['value'])) {
  384. $parents[] = 'value';
  385. }
  386. $addresses = array();
  387. foreach (drupal_array_get_nested_value($submission->data, $parents) as $delta => $data) {
  388. $data = empty($data) ? array() : unserialize($data);
  389. $addresses[$delta] = $data;
  390. }
  391. drupal_array_set_nested_value($submission->data, $parents, $addresses);
  392. }
  393. }
  394. }
  395. }
  396. /**
  397. * Implements hook_webform_validator_alter().
  398. */
  399. function addressfield_tokens_webform_validator_alter(&$validators) {
  400. $validators['unique']['component_types'][] = 'addressfield';
  401. $validators['oneoftwo']['component_types'][] = 'addressfield';
  402. $validators['oneofseveral']['component_types'][] = 'addressfield';
  403. }