Cheetah
charts.json.php
Go to the documentation of this file.
1 <?php
2 
8 require_once( '../inc/header.inc.php' );
9 require_once( CH_DIRECTORY_PATH_INC . 'db.inc.php' );
10 require_once( CH_DIRECTORY_PATH_INC . 'design.inc.php' );
11 require_once( CH_DIRECTORY_PATH_CLASSES . 'ChWsbPFM.php' );
12 
14 
15 $logged['admin'] = member_auth( 1, true, true );
16 
17 switch(ch_get('action')) {
18  case 'get':
19  header( 'Content-Type:text/javascript' );
20  echo ch_charts_get_json(ch_get('o'), ch_get('from'), ch_get('to'));
21  break;
22 }
23 
24 function ch_charts_get_json($sObject, $sFrom, $sTo)
25 {
26  $aObject = $GLOBALS['MySQL']->getRow("SELECT * FROM `sys_objects_charts` WHERE `object` = ? AND `active` = ?", [$sObject, 1]);
27  if (!$aObject)
28  return json_encode(array('error' => _t('_Error Occured')));
29 
30  $iFrom = ch_charts_get_ts($sFrom);
31  $iTo = ch_charts_get_ts($sTo, true);
32  if (!$iFrom || !$iTo)
33  return json_encode(array('error' => _t('_Error Occured')));
34 
35  $aData = ch_charts_get_data($aObject, $iFrom, $iTo);
36  if (!$aData)
37  return json_encode(array('error' => _t('_Empty')));
38 
39  $aRet = array (
40  'title' => _t($aObject['title']),
41  'data' => $aData,
42  'hide_date_range' => $aObject['field_date_dt'] || $aObject['field_date_ts'] ? false : true,
43  'column_date' => $aObject['column_date'] >= 0 ? $aObject['column_date'] : false,
44  'column_count' => $aObject['column_count'] >= 0 ? $aObject['column_count'] : false,
45  'type' => $aObject['type'] ? $aObject['type'] : 'AreaChart',
46  'options' => $aObject['options'] ? unserialize($aObject['options']) : false,
47  );
48 
49  return json_encode($aRet);
50 }
51 
52 function ch_charts_get_ts($s, $isNowIfError = false)
53 {
54  $a = explode('-', $s); // YYYY-MM-DD
55  if (!$a || empty($a[0]) || empty($a[1]) || empty($a[2]) || !(int)$a[0] || !(int)$a[1] || !(int)$a[2])
56  return $isNowIfError ? time() : false;
57  return mktime(0, 0, 0, $a[1], $a[2], $a[0]);
58 }
59 
61 {
62  return date('Y-m-d', $iTs);
63 }
64 
65 function ch_charts_get_data($aObject, $iFrom, $iTo)
66 {
67  // build query
68  $sQuery = $aObject['query'] ? $aObject['query'] : "SELECT {field_date_formatted} AS `period`, COUNT(*) AS {object} FROM {table} WHERE {field_date} >= '{from}' AND {field_date} <= '{to}' GROUP BY `period` ORDER BY {field_date} ASC";
69  $a = array (
70  'field_date_formatted' => "DATE_FORMAT(" . ($aObject['field_date_dt'] ? "`{$aObject['field_date_dt']}`" : "FROM_UNIXTIME(`{$aObject['field_date_ts']}`)") . ", '%Y-%m-%d')",
71  'object' => $aObject['object'],
72  'table' => "`{$aObject['table']}`",
73  'field_date' => "`" . ($aObject['field_date_dt'] ? $aObject['field_date_dt'] : $aObject['field_date_ts']) . "`",
74  'from' => $aObject['field_date_dt'] ? ch_charts_get_dt_from_ts($iFrom) . ' 00:00:00' : $iFrom,
75  'to' => $aObject['field_date_dt'] ? ch_charts_get_dt_from_ts($iTo) . ' 23:59:59' : $iTo + 24*3600 - 1,
76  );
77  foreach ($a as $k => $v)
78  $sQuery = str_replace('{'.$k.'}', $v, $sQuery);
79 
80  // get data
81  if ($aObject['column_date'] >= 0)
82  $aData = $GLOBALS['MySQL']->getAllWithKey($sQuery, $aObject['column_date'], [], PDO::FETCH_NUM);
83  else
84  $aData = $GLOBALS['MySQL']->getAll($sQuery, [], PDO::FETCH_NUM);
85  if (!$aData)
86  return false;
87 
88  // fill in missed days and convert values to numbers
89  if ($aObject['column_date'] >= 0) {
90  $iColumnsNum = count(array_pop(array_slice($aData, 0, 1)));
91  for ($i = $iFrom ; $i <= ($iTo + 24*3600 - 1); $i += 24*60*60) {
92  $sDate = date('Y-m-d', $i);
93  $aRow = array ();
94  for ($j = 0 ; $j < $iColumnsNum ; ++$j) {
95  $v = isset($aData[$sDate]) ? (int)$aData[$sDate][$j] : 0;
96  $aRow[$j] = ($j == $aObject['column_date'] ? $sDate : $v);
97  }
98  $aData[$sDate] = $aRow;
99  }
100  }
101  else {
102  foreach ($aData as $k => $v)
103  foreach ($aData[$k] as $kk => $vv)
104  if ($kk > 0)
105  $aData[$k][$kk] = (int)$aData[$k][$kk];
106  }
107 
108  // return values only
109  ksort($aData);
110  return array_values($aData);
111 }
header
</code > Be careful enabling this directive if you have a redirector script that does not use the< code > Location</code > HTTP header
Definition: URI.MungeResources.txt:10
ch_charts_get_json
switch(ch_get('action')) ch_charts_get_json($sObject, $sFrom, $sTo)
Definition: charts.json.php:24
ch_charts_get_ts
ch_charts_get_ts($s, $isNowIfError=false)
Definition: charts.json.php:52
ch_charts_get_data
ch_charts_get_data($aObject, $iFrom, $iTo)
Definition: charts.json.php:65
ch_get
ch_get($sName)
Definition: utils.inc.php:1664
php
send_headers_page_changed
send_headers_page_changed()
Definition: design.inc.php:99
$logged
$logged['admin']
Definition: charts.json.php:15
_t
_t($key, $arg0="", $arg1="", $arg2="")
Definition: languages.inc.php:509
time
that in the case of a Adaptation or at a minimum such credit will if a credit for all contributing authors of the Adaptation or Collection then as part of these credits and in a manner at least as prominent as the credits for the other contributing authors For the avoidance of You may only use the credit required by this Section for the purpose of attribution in the manner set out above by exercising Your rights under this You may not implicitly or explicitly assert or imply any connection sponsorship or endorsement by the Original Licensor and or Attribution as of You or Your use of the without the express prior written permission of the Original Licensor and or Attribution Parties Except as otherwise agreed in writing by the Licensor or as may be otherwise permitted by applicable if You Distribute or Publicly Perform the Work either by itself or as part of any Adaptations or You must not modify or take other derogatory action in relation to the Work which would be prejudicial to the Original Author s honor or reputation Licensor agrees that in those in which any exercise of the right granted in modification or other derogatory action prejudicial to the Original Author s honor and the Licensor will waive or not as this to the fullest extent permitted by the applicable national to enable You to reasonably exercise Your right under Warranties and Disclaimer UNLESS OTHERWISE MUTUALLY AGREED TO BY THE PARTIES IN LICENSOR OFFERS THE WORK AS IS AND MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE STATUTORY OR WITHOUT WARRANTIES OF FITNESS FOR A PARTICULAR OR THE ABSENCE OF LATENT OR OTHER OR THE PRESENCE OF ABSENCE OF WHETHER OR NOT DISCOVERABLE SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF IMPLIED SO SUCH EXCLUSION MAY NOT APPLY TO YOU Limitation on Liability EXCEPT TO THE EXTENT REQUIRED BY APPLICABLE IN NO EVENT WILL LICENSOR BE LIABLE TO YOU ON ANY LEGAL THEORY FOR ANY PUNITIVE OR EXEMPLARY DAMAGES ARISING OUT OF THIS LICENSE OR THE USE OF THE EVEN IF LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES Termination This License and the rights granted hereunder will terminate automatically upon any breach by You of the terms of this License Individuals or entities who have received Adaptations or Collections from You under this will not have their licenses terminated provided such individuals or entities remain in full compliance with those licenses and will survive any termination of this License Subject to the above terms and the license granted here is Licensor reserves the right to release the Work under different license terms or to stop distributing the Work at any time
Definition: license.txt:56
member_auth
member_auth($member=0, $error_handle=true, $bAjx=false)
Definition: admin.inc.php:262
$s
$s
Definition: embed.php:13
ch_charts_get_dt_from_ts
ch_charts_get_dt_from_ts($iTs)
Definition: charts.json.php:60
empty
Attr AllowedRel this is empty
Definition: Attr.AllowedRel.txt:7
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
$GLOBALS
$GLOBALS['iAdminPage']
Definition: advanced_settings.php:10