/
geopicker.php
119 lines (94 loc) · 4.2 KB
/
geopicker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
class modules_geopicker {
/**
* @brief The base URL to the datepicker module. This will be correct whether it is in the
* application modules directory or the xataface modules directory.
*
* @see getBaseURL()
*/
private $baseURL = null;
private $pathsRegistered = false;
/**
* @brief Initializes the datepicker module and registers all of the event listener.
*
*/
function __construct(){
$app = Dataface_Application::getInstance();
// Now work on our dependencies
$mt = Dataface_ModuleTool::getInstance();
// We require the XataJax module
// The XataJax module activates and embeds the Javascript and CSS tools
$mt->loadModule('modules_XataJax', 'modules/XataJax/XataJax.php');
// Register the geopicker widget with the form tool so that it responds
// to widget:type=geopicker
import('Dataface/FormTool.php');
$ft = Dataface_FormTool::getInstance();
$ft->registerWidgetHandler('geopicker', dirname(__FILE__).DIRECTORY_SEPARATOR.'widget.php', 'Dataface_FormTool_geopicker');
if ( !@$app->_conf['modules_geopicker'] or !@$app->_conf['modules_geopicker']['key'])
{
$msg = <<<END
<p>Google Maps Module is installed but no API key is specified.</p>
<p>For information about obtaining your API key see <a href="https://developers.google.com/maps/documentation/javascript/tutorial#api_key">this page</a>.</p>
<p>After obtaining your key, add the following section to your application's conf.ini file:</p>
<p><code><pre>
[modules_geopicker]
key=YOUR_API_KEY_HERE
</pre></code></p>
END;
die($msg);
}
$app->addHeadContent('<script>XF_GEOPICKER_API_KEY="'.htmlspecialchars($app->_conf['modules_geopicker']['key']).'";</script>');
foreach ( Dataface_Table::loadTable('', df_db(), true) as $t ){
$evt = new StdClass;
$evt->table = $t;
$this->afterTableInit($evt);
}
$app->registerEventListener("afterTableInit", array($this, 'afterTableInit'));
$app->registerEventListener("Dataface_Record__htmlValue", array($this, 'Dataface_Record__htmlValue'));
}
function registerPaths(){
if ( !$this->pathsRegistered ){
$mod = $this;
$jt = Dataface_JavascriptTool::getInstance();
$jt->addPath(dirname(__FILE__).'/js', $mod->getBaseURL().'/js');
$ct = Dataface_CSSTool::getInstance();
$ct->addPath(dirname(__FILE__).'/css', $mod->getBaseURL().'/css');
}
}
//called when rendered in view mode
public function Dataface_Record__htmlValue($event){
$fieldname = $event->fieldname;
$record = $event->record;
$field =& $record->table()->getField($fieldname);
if ( $field['widget']['type'] === 'geopicker' ){
$this->registerPaths();
Dataface_JavascriptTool::getInstance()->import('xataface/modules/geopicker/widgets/geopicker.js');
$val = $record->val($fieldname);
if ( !trim($val) ){
$event->out = '';
} else {
$event->out = '<input type="hidden" value="'.df_escape($val).'" class="xf-geopicker" data-geopicker-read-only="1"/>'; //out geopicker data in a hidden file (js code will substitute the textbox with the map)
}
}
}
public function afterTableInit($event){
$fields =& $event->table->fields();
foreach ( $fields as $k=>$f ){
if ( @$fields[$k]['widget']['type'] === 'geopicker' ){
$fields[$k]['struct'] = 1;
$fields[$k]['visibility']['list'] = 'hidden'; //hide geopicker fields in table view
}
}
}
/**
* @brief Returns the base URL to this module's directory. Useful for including
* Javascripts and CSS.
*
*/
public function getBaseURL(){
if ( !isset($this->baseURL) ){
$this->baseURL = Dataface_ModuleTool::getInstance()->getModuleURL(__FILE__);
}
return $this->baseURL;
}
}