PageRenderTime 47ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/geo-mashup/edit-form.php

http://wordpress-geo-mashup.googlecode.com/
PHP | 237 lines | 184 code | 21 blank | 32 comment | 25 complexity | 2c1651166947f9464a691112328cc33d MD5 | raw file
  1. <?php
  2. /**
  3. * A function wrapper for the location editor HTML.
  4. *
  5. * @package GeoMashup
  6. */
  7. /**
  8. * Print the Geo Mashup location editor HTML for an object.
  9. *
  10. * Goals for this interface are to make it usable for any kind of locatable
  11. * object, to be usable without javascript, functional on the front end or admin,
  12. * and eventually adaptable to editing multiple locations for an object.
  13. *
  14. * It's assumed this will go inside an existing form for editing the object,
  15. * such as the WordPress admin post edit form.
  16. *
  17. * @since 1.2
  18. * @see geo-mashup-ui-managers.php
  19. * @see geo-mashup-location-editor.js
  20. * @uses edit-form.css
  21. * @access public
  22. *
  23. * @param string $object_name The type of object, e.g. 'post', 'user', etc.
  24. * @param string $object_id The ID of the object being edited.
  25. * @param string $ui_manager Optionally the name of UI Manager class to use for AJAX operations.
  26. */
  27. function geo_mashup_edit_form( $object_name, $object_id, $ui_manager = '' ) {
  28. global $geo_mashup_options;
  29. $help_class = 'geo-mashup-js';
  30. $add_input_style = 'style="display:none;"';
  31. $update_input_style = $delete_input_style = '';
  32. $coordinate_string = '';
  33. // Load any existing location for the object
  34. $location = GeoMashupDB::get_object_location( $object_name, $object_id );
  35. if ( empty( $location ) ) {
  36. $location = GeoMashupDB::blank_object_location();
  37. $help_class = '';
  38. $add_input_style = '';
  39. $update_input_style = $delete_input_style = 'style="display:none;"';
  40. } else {
  41. $coordinate_string = $location->lat . ',' . $location->lng;
  42. }
  43. $post_location_name = $location->saved_name;
  44. $kml_url = '';
  45. // Set a Geo date default when needed & possible
  46. $date_missing = ( empty( $location->geo_date ) || '0000-00-00 00:00:00' == $location->geo_date );
  47. if ( 'post' == $object_name) {
  48. if ( $date_missing ) {
  49. // Geo date defaults to post date
  50. $post = get_post( $object_id );
  51. $location->geo_date = $post->post_date;
  52. if ( !empty( $location->id ) ) {
  53. GeoMashupDB::set_object_location( $object_name, $object_id, $location->id, false, $location->geo_date );
  54. }
  55. }
  56. // For posts, look for a KML attachment
  57. $kml_urls = GeoMashup::get_kml_attachment_urls( $object_id );
  58. if (count($kml_urls)>0) {
  59. $kml_url = array_pop($kml_urls);
  60. }
  61. } else if ( 'user' == $object_name && $date_missing ) {
  62. // Geo date defaults to registration date
  63. $user = get_userdata( $object_id );
  64. $location->geo_date = $user->user_registered;
  65. if ( !empty( $location->id ) ) {
  66. GeoMashupDB::set_object_location( $object_name, $object_id, $location->id, false, $location->geo_date );
  67. }
  68. } else if ( 'comment' == $object_name && $date_missing ) {
  69. // Geo date defaults to comment date
  70. $comment = get_comment( $object_id );
  71. $location->geo_date = $comment->comment_date;
  72. if ( !empty( $location->id ) ) {
  73. GeoMashupDB::set_object_location( $object_name, $object_id, $location->id, false, $location->geo_date );
  74. }
  75. }
  76. if ( empty( $location->geo_date ) ) {
  77. $location_datetime = mktime();
  78. } else {
  79. $location_datetime = strtotime( $location->geo_date );
  80. }
  81. $location_date = date( 'M j, Y', $location_datetime );
  82. $location_hour = date( 'G', $location_datetime );
  83. $location_minute = date( 'i', $location_datetime );
  84. // Load saved locations
  85. $saved_locations = GeoMashupDB::get_saved_locations( );
  86. $saved_location_options = array();
  87. if ( ! empty( $saved_locations ) ) {
  88. foreach ( $saved_locations as $saved_location ) {
  89. $escaped_name = str_replace( array( "\r\n", "\r", "\n" ), '', $saved_location->saved_name );
  90. if ( $saved_location->id != $location->id )
  91. $selected = '';
  92. else
  93. $selected = ' selected="selected"';
  94. $saved_location_options[] = '<option value="' . esc_attr( $saved_location->id . '|' . $saved_location->lat . '|' .
  95. $saved_location->lng . '|' . $saved_location->address ) . '"' . $selected . '>' . esc_html( $escaped_name ) . '</option>';
  96. }
  97. }
  98. $saved_location_options = implode( '', $saved_location_options );
  99. $nonce = wp_create_nonce('geo-mashup-edit');
  100. $static_maps_base_url = 'http://maps.google.com/maps/api/staticmap?sensor=false&amp;key=' .
  101. $geo_mashup_options->get( 'overall', 'google_key' );
  102. ?>
  103. <div id="geo_mashup_location_editor">
  104. <div id="geo_mashup_ajax_message" class="geo-mashup-js ui-state-highlight"></div>
  105. <input id="geo_mashup_nonce" name="geo_mashup_nonce" type="hidden" value="<?php echo $nonce; ?>" />
  106. <input id="geo_mashup_changed" name="geo_mashup_changed" type="hidden" value="" />
  107. <?php ob_start(); ?>
  108. <table id="geo-mashup-location-table">
  109. <thead class="ui-widget-header">
  110. <tr>
  111. <th><?php _e( 'Address', 'GeoMashup' ); ?></th>
  112. <th><?php _e( 'Saved Name', 'GeoMashup' ); ?></th>
  113. <th><?php _e( 'Geo Date', 'GeoMashup' ); ?></th>
  114. </tr>
  115. </thead>
  116. <tbody class="ui-widget-content">
  117. <tr id="geo_mashup_display" class="geo-mashup-display-row">
  118. <td class="geo-mashup-info">
  119. <div class="geo-mashup-address"><?php echo esc_html( $location->address ); ?></div>
  120. <div class="geo-mashup-coordinates"><?php echo esc_attr( $coordinate_string ); ?></div>
  121. </td>
  122. <td id="geo_mashup_saved_name_ui">
  123. <input id="geo_mashup_location_name" name="geo_mashup_location_name" size="50" type="text" value="<?php echo esc_attr( $post_location_name ); ?>" />
  124. </td>
  125. <td id="geo_mashup_date_ui">
  126. <input id="geo_mashup_date" name="geo_mashup_date" type="text" size="20" value="<?php echo esc_attr( $location_date ); ?>" /><br />
  127. @
  128. <input id="geo_mashup_hour" name="geo_mashup_hour" type="text" size="2" maxlength="2" value="<?php echo esc_attr( $location_hour ); ?>" />
  129. :
  130. <input id="geo_mashup_minute" name="geo_mashup_minute" type="text" size="2" maxlength="2" value="<?php echo esc_attr( $location_minute ); ?>" />
  131. </td>
  132. <td id="geo_mashup_ajax_buttons">
  133. </td>
  134. </tr>
  135. </tbody>
  136. </table>
  137. <?php $location_table_html = ob_get_clean(); ?>
  138. <?php ob_start(); ?>
  139. <div id="geo_mashup_map" class="geo-mashup-js">
  140. <?php _e('Loading Google map. Check Geo Mashup options if the map fails to load.', 'GeoMashup'); ?>
  141. </div>
  142. <?php if ( ! empty( $location->id ) ) : ?>
  143. <noscript>
  144. <div id="geo_mashup_static_map">
  145. <img src="<?php echo $static_maps_base_url; ?>&amp;size=400x300&amp;zoom=4&amp;markers=size:small|color:green|<?php echo esc_attr( $location->lat . ',' . $location->lng ); ?>"
  146. alt="<?php _e( 'Location Map Image', 'GeoMashup' ); ?>" />
  147. </div>
  148. </noscript>
  149. <?php endif; ?>
  150. <?php $map_html = ob_get_clean(); ?>
  151. <?php ob_start(); ?>
  152. <label for="geo_mashup_search"><?php _e('Find a new location:', 'GeoMashup'); ?>
  153. <input id="geo_mashup_search" name="geo_mashup_search" type="text" size="35" />
  154. </label>
  155. <?php _e( 'or select from', 'GeoMashup' ); ?>
  156. <select id="geo_mashup_select" name="geo_mashup_select">
  157. <option value=""><?php _e('[Saved Locations]','GeoMashup'); ?></option>
  158. <?php echo $saved_location_options; ?>
  159. </select>
  160. <?php $search_html = ob_get_clean(); ?>
  161. <?php echo empty( $location->id ) ? $search_html . $map_html . $location_table_html : $location_table_html . $map_html . $search_html; ?>
  162. <input id="geo_mashup_ui_manager" name="geo_mashup_ui_manager" type="hidden" value="<?php echo $ui_manager; ?>" />
  163. <input id="geo_mashup_object_id" name="geo_mashup_object_id" type="hidden" value="<?php echo $object_id; ?>" />
  164. <input id="geo_mashup_no_js" name="geo_mashup_no_js" type="hidden" value="true" />
  165. <input id="geo_mashup_location_id" name="geo_mashup_location_id" type="hidden" value="<?php echo esc_attr( $location->id ); ?>" />
  166. <input id="geo_mashup_location" name="geo_mashup_location" type="hidden" value="<?php echo esc_attr( $coordinate_string ); ?>" />
  167. <input id="geo_mashup_geoname" name="geo_mashup_geoname" type="hidden" value="<?php echo esc_attr( $location->geoname ); ?>" />
  168. <input id="geo_mashup_address" name="geo_mashup_address" type="hidden" value="<?php echo esc_attr( $location->address ); ?>" />
  169. <input id="geo_mashup_postal_code" name="geo_mashup_postal_code" type="hidden" value="<?php echo esc_attr( $location->postal_code ); ?>" />
  170. <input id="geo_mashup_country_code" name="geo_mashup_country_code" type="hidden" value="<?php echo esc_attr( $location->country_code ); ?>" />
  171. <input id="geo_mashup_admin_code" name="geo_mashup_admin_code" type="hidden" value="<?php echo esc_attr( $location->admin_code ); ?>" />
  172. <input id="geo_mashup_admin_name" name="geo_mashup_admin_name" type="hidden" value="" />
  173. <input id="geo_mashup_kml_url" name="geo_mashup_kml_url" type="hidden" value="<?php echo $kml_url; ?>" />
  174. <input id="geo_mashup_sub_admin_code" name="geo_mashup_sub_admin_code" type="hidden" value="<?php echo esc_attr( $location->sub_admin_code ); ?>" />
  175. <input id="geo_mashup_sub_admin_name" name="geo_mashup_sub_admin_name" type="hidden" value="" />
  176. <input id="geo_mashup_locality_name" name="geo_mashup_locality_name" type="hidden" value="<?php echo esc_attr( $location->locality_name ); ?>" />
  177. <div id="geo_mashup_submit" class="submit">
  178. <input id="geo_mashup_add_location" name="geo_mashup_add_location" type="submit" <?php echo $add_input_style; ?> value="<?php _e( 'Add Location', 'GeoMashup' ); ?>" />
  179. <input id="geo_mashup_delete_location" name="geo_mashup_delete_location" type="submit" <?php echo $delete_input_style; ?> value="<?php _e( 'Delete', 'GeoMashup' ); ?>" />
  180. <input id="geo_mashup_update_location" name="geo_mashup_update_location" type="submit" <?php echo $update_input_style; ?> value="<?php _e( 'Save', 'GeoMashup' ); ?>" />
  181. </div>
  182. <div id="geo-mashup-inline-help-link-wrap" class="geo-mashup-js">
  183. <a href="#geo-mashup-inline-help" id="geo-mashup-inline-help-link"><?php _e('help', 'GeoMashup'); ?><span class="ui-icon ui-icon-triangle-1-s"></span></a>
  184. </div>
  185. <div id="geo-mashup-inline-help" class="<?php echo $help_class; ?> ui-widget-content">
  186. <p><?php _e( '<em>Saved Name</em> is an optional name you may use to add entries to the Saved Locations menu.', 'GeoMashup' ); ?></p>
  187. <p><?php _e( '<em>Geo Date</em> associates a date (most formats work) and time with a location. Leave the default value if uncertain.', 'GeoMashup' ); ?></p>
  188. <div class="geo-mashup-js">
  189. <p><?php _e('Put a green pin at a new location. There are many ways to do it:', 'GeoMashup'); ?></p>
  190. <ul>
  191. <li><?php _e('Search for a location name.', 'GeoMashup'); ?></li>
  192. <li><?php _e('For multiple search results, mouse over pins to see location names, and click a result pin to select that location.', 'GeoMashup'); ?></li>
  193. <li><?php _e('Search for a decimal latitude and longitude separated by a comma, like <em>40.123,-105.456</em>. Seven decimal places are stored. Negative latitude is used for the southern hemisphere, and negative longitude for the western hemisphere.', 'GeoMashup'); ?></li>
  194. <li><?php _e('Search for a street address, like <em>123 main st, anytown, acity</em>.', 'GeoMashup'); ?></li>
  195. <li><?php _e('Click on the location. Zoom in if necessary so you can refine the location by dragging it or clicking a new location.', 'GeoMashup'); ?></li>
  196. </ul>
  197. <p><?php _e('To execute a search, type search text into the Find Location box and hit the enter key. If you type a name next to "Save As", the location will be saved under that name and added to the Saved Locations dropdown list.', 'GeoMashup'); ?></p>
  198. <p><?php _e('To remove the location (green pin), clear the search box and hit the enter key.', 'GeoMashup'); ?></p>
  199. <p><?php _e('When you are satisfied with the location, save or update.', 'GeoMashup'); ?></p>
  200. </div>
  201. <noscript>
  202. <div>
  203. <p><?php _e( 'To add or update location choose a saved location, or find a new location using one of these formats:', 'GeoMashup' ); ?></p>
  204. <ul>
  205. <li><?php _e('A place name like <em>Yellowstone National Park</em>', 'GeoMashup'); ?></li>
  206. <li><?php _e('A decimal latitude and longitude, like <em>40.123,-105.456</em>.', 'GeoMashup'); ?></li>
  207. <li><?php _e('A full or partial street address, like <em>123 main st, anytown, acity 12345 USA</em>.', 'GeoMashup'); ?></li>
  208. </ul>
  209. <p><?php _e( 'When you save or update, the closest match available will be saved as the location.', 'GeoMashup' ); ?></p>
  210. </div>
  211. </noscript>
  212. </div>
  213. </div><!-- id="geo_mashup_location_editor" -->
  214. <?php
  215. }
  216. ?>