-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleads_json.module
138 lines (121 loc) · 3.65 KB
/
simpleads_json.module
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* @file
*
* Provides JSON data on SimpleAds.
*/
/**
* Implements hook_permission
*/
function simpleads_json_permission() {
return array(
// Permission for setting SimpleAds JSON access key.
'simpleads_json accesskey' => array(
'title' => t('Administer SimpleAds JSON'),
'description' => t('Perform administration task for SimpleAds JSON'),
),
);
}
/**
* Implements hook_menu().
*/
function simpleads_json_menu() {
return array(
// Menu for JSON output.
'simpleads/json' => array(
'page callback' => 'simpleads_json_output',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
),
// Menu for SimpleAds JSON configuration page.
'admin/config/services/simpleads-json' => array(
'title' => 'SimpleAds JSON',
'description' => 'SimpleAds JSON configuration',
'page callback' => 'drupal_get_form',
'page arguments' => array('simpleads_json_admin_config_form'),
'access arguments' => array('simpleads_json accesskey'),
'file' => 'simpleads_json.admin.inc',
'file path' => drupal_get_path('module', 'simpleads_json') . '/includes/',
),
);
}
/**
* Page Callback.
*
* @return type json mixed
* JSON result related to query
*/
function simpleads_json_output() {
// Check if key is set or key matches to our key else return access denied page.
if (!isset($_GET['key']) ||
$_GET['key'] != variable_get('simpleads_json_key')) {
return MENU_ACCESS_DENIED;
}
// Variable for storing JSON result.
$output = '';
// Save the request value in array.
$data = array(
'start' => isset($_GET['start']) ? $_GET['start'] : '',
'end' => isset($_GET['end']) ? $_GET['end'] : '',
);
// Check if date is in correct format.
$valid = (isset($_GET['start']) && _simpleads_json_check_format($data))
? TRUE : FALSE;
switch ($valid) {
// If date format is correct.
case TRUE:
// Send out the result we obtain from query.
$output = drupal_json_output(_simpleads_json_query_result($data));
break;
// If date format is incorrect.
case FALSE:
// Send out invalid request in JSON.
$output = drupal_json_output(array('error' => 'invalid request',));
break;
}
// Final result to be displayed on page.
return $output;
}
/**
* Check supplied date format.
*
* @param type $date array mixed
* contains start date and probably end date.
*
* @return type boolean
* - TRUE
* if date format is correct.
* - FALSE
* if date format is incorrect.
*/
function _simpleads_json_check_format($date) {
return ($date['end'] != '') ?
((((DateTime::createFromFormat('Y-m-d', $date['start']) !== FALSE)
&& DateTime::createFromFormat('Y-m-d', $date['end']) !== FALSE)) ?
TRUE : FALSE) :
((DateTime::createFromFormat('Y-m-d', $date['start']) !== FALSE) ?
TRUE : FALSE);
}
/**
* Returns the fetched query from database.
*
* @param type $date array mixed
* start date and probably end date
*
* @return type array mixed
* an array contaning information on published ads.
*/
function _simpleads_json_query_result($date) {
$node = array('title', 'nid', 'status');
$field_ad_date = array('field_ad_date_value', 'field_ad_date_value2');
$query = db_select('node', 'n');
$query->join('field_data_field_ad_date', 'd', 'n.nid = d.entity_id');
$query->fields('n', $node)
->fields('d', $field_ad_date)
->condition('d.field_ad_date_value', $date['start'], '>=');
if($date['end'] != '') {
$query->condition('d.field_ad_date_value2', $date['end'], '<=');
}
$query->orderBy('n.nid');
return $query->execute()->fetchall();
}