Php webtags: Difference between revisions

From Cumulus Wiki
Jump to navigationJump to search
4,215 bytes added ,  20:27, 17 May 2021
m
m (delete)
 
(12 intermediate revisions by the same user not shown)
Line 72: Line 72:


== Option 3: JavaScript Object Notation ==
== Option 3: JavaScript Object Notation ==
Anybody using Cumulus MX is already using this option if they are set up to use the example web pages provided in the software package.


This is a lightweight data-interchange format (JSON) that is easy for humans to read and write, but critically it is easy for machines to parse and generate for transferring the variables in a portable way.
This is a lightweight data-interchange format (JSON) that is easy for humans to read and write, but critically it is easy for machines to parse and generate for transferring the variables in a portable way.


Cumulus MX uses this approach to provide variables for plotting the charts on your web site (Trends web page).
The 3.10.1 release of MX has functionality to optionally generate and upload many more json files for transfers to the external web server - see [[:Category:JSON_Files#Data_Transfer_Format]].
 
<div style="background: LemonChiffon;padding:5px; margin:2px;">
[[File:Crystal Clear info.png|40px]] This document was written for a MX release that is no longer latest!
 
'''The 3.10.1 release of MX changes the settings referenced below'''
</div>
 
Cumulus MX beta (and releases up to 3.9.8) uses this approach solely to provide variables for plotting the charts on your web site (Trends web page).
*To have the json files uploaded to your web site, you must enable '''Include standard files''' within the ''Web/FTP settings'' section of '''Internet Settings'''.
*To have the json files uploaded to your web site, you must enable '''Include standard files''' within the ''Web/FTP settings'' section of '''Internet Settings'''.
*Cumulus MX has one json file per weather chart
*Cumulus MX has one json file per weather chart
Line 82: Line 92:
*The interval between sub-arrays (time and value pairs) depends on the chart, for daily rain and daily temperature there is a sub array for each day; but for most there is a sub-array for every minute (assuming Cumulus is left running for long enough) for a full 24 hours.  
*The interval between sub-arrays (time and value pairs) depends on the chart, for daily rain and daily temperature there is a sub array for each day; but for most there is a sub-array for every minute (assuming Cumulus is left running for long enough) for a full 24 hours.  


For the admin interface, the Cumulus MX engine has to pass information to and from the admin interface, this is done using an application programming interface for each transfer (and each interface uses json format).
For the admin interface, the Cumulus MX engine has to pass information to and from the admin interface, this is done using an [[MX_Administrative_Interface#The_API_interface|application programming interface]] for each transfer (and each interface uses json format).


If you want to set up your own json files, you may follow the same approach but need to choose the interval between such sub-arrays that suits your use in your web pages.
If you want to set up your own json files, you may follow the same approach but need to choose the interval between such sub-arrays that suits your use in your web pages.
Line 159: Line 169:
** Check your choice of read-made script can output dates in the format you prefer.
** Check your choice of read-made script can output dates in the format you prefer.


==== Time format =====
==== Time format ====
*Do you prefer 24-hour clock (some call this timetable format or military format) or 12 hour clock with am/pm?
*Do you prefer 24-hour clock (some call this timetable format or military format) or 12 hour clock with am/pm?
**Some ready made scripts may express times in both formats so you can choose the format you want
**Some ready made scripts may express times in both formats so you can choose the format you want
Line 202: Line 212:
*Cumulus MX web tags do allow embedding of HTML, see the [[Webtags|Web tags article]] for details.
*Cumulus MX web tags do allow embedding of HTML, see the [[Webtags|Web tags article]] for details.


== Ready made PHP Web Tag templates ==
== Producing your own script ==
 
It is perfectly possible to produce your own templates, following the advice above, I know that might scare you.
 
== Modifying a ready made script ==


It is perfectly possible to produce your own templates, following the advice in '''Option 2''' below, I know that might scare you.
*One approach, that is perhaps easier is to modify a ready-made script (as has happened for some of those listed below) by adding new lines, each with a PHP variable name (it must start with "$") on the left of an equals sign and a web tag (<#tag_name>) on the right, and end the line with a ";".  
*One approach, that is perhaps easier is to modify a ready-made script (as has happened for some of those listed below) by adding new lines, each with a PHP variable name (it must start with "$") on the left of an equals sign and a web tag (<#tag_name>) on the right, and end the line with a ";".  
*You can add a comment at end of line using prefix "//".  
*You can add a comment at end of line using prefix "//".  
Line 214: Line 227:
*The scary bit is understanding the right format to use in those web tags for the output parameters because it is easy to specify items like minutes, hour, or month, incorrectly, especially if you have moved from Cumulus 1 to MX.
*The scary bit is understanding the right format to use in those web tags for the output parameters because it is easy to specify items like minutes, hour, or month, incorrectly, especially if you have moved from Cumulus 1 to MX.


== Ready made PHP Web Tag templates ==


*Be aware that if you are using Cumulus 1, when processing it ignores any web tags it does not recognise.  
*Be aware that if you are using Cumulus 1, when processing it ignores any web tags it does not recognise.  
Line 291: Line 305:
**That download includes just 10 elements for the recent history tags at 5, 10, 15, 20, 30, 45, 60, 75, 90, 105 and 120 minutes. If you want older values or values at other times, then you will need to add these to the array yourself.
**That download includes just 10 elements for the recent history tags at 5, 10, 15, 20, 30, 45, 60, 75, 90, 105 and 120 minutes. If you want older values or values at other times, then you will need to add these to the array yourself.


=== Alternatives ===
=== Ready-made Templates not based on original ===


These templates have not been influenced by the original template, but instead have tried to cover what suited each author at the time the template was written, and each author has made their own independent decision about what parameters to use.
These templates have not been influenced by the original template, but instead have tried to cover what suited each author at the time the template was written, and each author has made their own independent decision about what parameters to use.


==== Simplest ====
*The simplest template of all, it essentially reproduces the layout used for tables on the [[Webtags|web tags wiki page]], so it is very nicely laid out. It was written from scratch by Brandon using all web tags available in November 2014 with Cumulus 1, although MX beta was available then, he has not identified the tags not available in MX at that time. Because Brandon has not used using any output parameters, it will work with Cumulus 1 and MX, but as it is also without any input parameters none of his Recent History PHP variables actually contain anything but rubbish. It can be downloaded in [https://cumulus.hosiene.co.uk/viewtopic.php?f=6&t=10424#p99549 this storm rain topic].
*The simplest template of all, it essentially reproduces the layout used for tables on the [[Webtags|web tags wiki page]], so it is very nicely laid out. It was written from scratch by Brandon using all web tags available in November 2014 with Cumulus 1, although MX beta was available then, he has not identified the tags not available in MX at that time. Because Brandon has not used using any output parameters, it will work with Cumulus 1 and MX, but as it is also without any input parameters none of his Recent History PHP variables actually contain anything but rubbish. It can be downloaded in [https://cumulus.hosiene.co.uk/viewtopic.php?f=6&t=10424#p99549 this storm rain topic].
==== Smallest number of web tags ====
* Another simple template, but this one includes far fewer web tags, is called ''CUtags.txt'', it was produced by BCJKiwi as a single template file able to work with both Cumulus 1 and MX. It can be downloaded as part of his [[CumulusMX_and_Cumulus1_UI_style_Multilingual_Websites|package]] or separately at [https://cumulus.hosiene.co.uk/viewtopic.php?f=40&t=17985&p=140688#p140688 this post of mine in a zip].
* Another simple template, but this one includes far fewer web tags, is called ''CUtags.txt'', it was produced by BCJKiwi as a single template file able to work with both Cumulus 1 and MX. It can be downloaded as part of his [[CumulusMX_and_Cumulus1_UI_style_Multilingual_Websites|package]] or separately at [https://cumulus.hosiene.co.uk/viewtopic.php?f=40&t=17985&p=140688#p140688 this post of mine in a zip].
**This template does not contain any web tags that are only available in MX, nor does it contain any web tags that require input or output parameters. The PHP variable names match Cumulus web tag names, but some of those PHP variables are set to "not used", instead of to the web tag contents, because this template file is designed for a specific set of web pages.
**This template does not contain any web tags that are only available in MX, nor does it contain any web tags that require input or output parameters. The PHP variable names match Cumulus web tag names, but some of those PHP variables are set to "not used", instead of to the web tag contents, because this template file is designed for a specific set of web pages.
*This [[File:Web tag templates.zip|zip]] contains a set of templates, each updated at different frequencies. It includes all web tags for either Cumulus 1 or MX. You may get an error reported locally when Cumulus processes an individual file as your version of Cumulus does not include every tag it will try to process. However, once it has been converted to a PHP script, it will test the version and should not give any errors as you use it on your production web site. I have taken the decision to write all the templates assuming your locale uses a fullstop as decimal separator. I am working on templates that use the Remove Comma options that Cumulus 1 and MX provide, but those are not ready to release.
 
==== Minimised Upload size ====
 
You can reduce your consumption of your cap on uploading by not uploading at real time any PHP variables based on web tags that don't alter that frequently.
 
In fact, you can define and upload some variables for web tags that change less frequently during the day at the standard uploading interval, and PHP variables based on yesterday's web tags (and some others) just once a day.
 
So what you need is a set of scripts, and one such ready made set can be found in [[File:Web tag templates.zip|this zip]].
*It contains a set of templates, each updated at different frequencies.  
*It includes all web tags for either Cumulus 1 or MX.  
*You may get an error reported locally when Cumulus processes an individual file as your version of Cumulus does not include every tag it will try to process.  
*However, once it has been converted to a PHP script, it will test the version and should not give any errors as you use it on your production web site.  
*Most of the templates are written assuming that either your locale uses a fullstop as decimal separator, or you don't need to do any calculation with the PHP variables.
*But for the most frequently changing web tags, there is a script for Cumulus 1 and another for MX that do indeed remove commas and produce PHP variables that can safely be used for calculations.


== Usage ==
== Usage ==
Line 373: Line 403:


== Add your template(s) to Extra Files processing ==
== Add your template(s) to Extra Files processing ==
Please note that a template file for Cumulus to process (local name in the following) can have any file extension you want. Some example show ".txt" being used, others show ".cum" being used, but some ready made templates might use ".php".  It really does not matter what the file extension is, but obviously what you enter in the local name "slot" must match the actual file you are referencing.
Similarly, a file that you are going to include in other scripts can have any extension you like, it makes no difference to the PHP parser. Thus what you put in the remote name "slot" must also match what you are going to use in your "include" (or "require") statements. One convention is that the extension should be ".inc" to make it clear it is an include file, not a web page as might be implied by using ".php".  It is the "<?php" at the start of the file that the PHP parser will recognise when it brings the contents of the uploaded file into another script.
   
   
=== Cumulus 1 ===
=== Cumulus 1 ===
Line 380: Line 414:
#Edit the screen columns as indicated by red in the figure:
#Edit the screen columns as indicated by red in the figure:
#*in one of the local side boxes (under the 'Local filenames' header), enter the path (directory\file) to where you have stored the template:<br>e.g. '''your_customised_template_location\cumuluswebtags.txt'''<br> (you can use the 'Browse' button to find and select the exact location of the file);<br><br>
#*in one of the local side boxes (under the 'Local filenames' header), enter the path (directory\file) to where you have stored the template:<br>e.g. '''your_customised_template_location\cumuluswebtags.txt'''<br> (you can use the 'Browse' button to find and select the exact location of the file);<br><br>
#*in one of the remote side boxes (under the 'Remote filenames' header), enter the name '''cumuluswebtags.php''':
#*in one of the remote side boxes (under the 'Remote filenames' header), enter the name '''cumuluswebtags.inc''' (in image it shows ''cumuluswebtags.php'', both are acceptable as explained above):
#** -- if needed add the relative server path (see Cumulus Help for explanation) before the file name;
#** -- if needed add the relative server path (see Cumulus 1 Help for explanation) before the file name;
#** (-- if you are using a local server, and want Cumulus to do a 'copy' instead of using 'FTP' then specify the full path for the destination file here);
#** (-- if you are using a local server, and want Cumulus to do a 'copy' instead of using 'FTP' then specify the full path for the destination file here);
#** -- ''notice that the file extension has been changed from'' '''txt''' (on the left side) to '''php''' (on the right side) ''as the remote file needs to be recognised by the PHP processor on the web server;''
#** -- ''notice that the file extension has been changed from'' '''txt''' (on the left side) to '''inc''' (on the right side) ''simply to help the remote file to be recognised on the web server;''
#* place a check mark in the box under the 'Process?' header;
#* place a check mark in the box under the 'Process?' header;
#** -- this tells Cumulus that the file '''cumuluswebtags.txt''' contains tags which need to be replaced by actual values when it processes the template into a web page;
#** -- this tells Cumulus that the file '''cumuluswebtags.txt''' contains tags which need to be replaced by actual values when it processes the template into a web page;
#*(there is no red arrow under the 'Realtime' header as it may not be appropriate),
#*(there is no red arrow under the 'Realtime' header as it may not be appropriate),
#** -- If any of your PHP pages require the latest information at your chosen real-time interval (rather than the 'normal' web site '''updating''' interval) they could be using this file because they require any web tags not in the realtime file, then you might need to select this option (indeed I have on my implementation);
#** -- If any of your PHP pages require the latest information at your chosen real-time interval (rather than the 'normal' web site '''updating''' interval) they could be using this file because they require any web tags not in the real-time file, then you might need to select this option (indeed I have on my implementation);
#*place a check mark in the box under the 'UTF-8' header;
#* place a check mark in the box under the 'FTP?' header;
#* place a check mark in the box under the 'FTP?' header;
#** -- this tells Cumulus that the processed file must be uploaded by file transfer to the remote Web server with all the other files;
#** -- this tells Cumulus that the processed file must be uploaded by file transfer to the remote Web server with all the other files;
Line 397: Line 430:


=== Cumulus MX ===
=== Cumulus MX ===
It is broadly similar for Cumulus MX.
It is broadly similar for Cumulus MX.
# Select '''Settings''' menu in the user interface, then select the ''Extra Files'' page. Scroll through to a page with empty rows if necessary.[[File:Extra.PNG|right|'Screenshot Cumulus v3.5.3']]
 
# The local file name column does not have the browse selection available in the equivalent Cumulus 1 feature. So be very careful that you type in the full path correctly. You don't need a drive selector, Cumulus will assume the file is on same drive as the MX executable, so only specify drive if it is somewhere else. You might have a feature that allows you to do a list of files and for the file picked show its path, if so you can copy that path into the box in the Local files column.
# Select '''Settings''' menu in the admin interface, then select the ''Extra Files'' page.  
#Scroll through to a page with empty rows if necessary.[[File:Extra.PNG|right|'Screenshot Cumulus v3.5.3']]
#*The local file name column does not have the browse selection available in the equivalent Cumulus 1 feature. So be very careful that you type in the full path correctly. You don't need a drive selector, Cumulus will assume the file is on same drive as the MX executable, so only specify drive if it is somewhere else.  
#*You might have a feature that allows you to do a list of files and for the file picked show its path, if so you can copy that path into the box in the Local files column.
#*In the illustration (that does not have the default background for admin interface, but ignore that) each local filename uses ".cum" extension, but as already mentioned just match whatever file(s) you are using.
#* In the example illustrated there are 4 web tag templates, each containing the web tags that are to be processed at a different frequency, all following advice on this page:
#* In the example illustrated there are 4 web tag templates, each containing the web tags that are to be processed at a different frequency, all following advice on this page:
#*# one ('static') only needs to be processed once, it is shown as using EOD here, but actually you would use real-time once, and then delete the local name so Cumulus did not try to process it again.
#*# one ('static') only needs to be processed once, it is shown as using EOD here, but actually you would use real-time once, and then delete the local name so Cumulus did not try to process it again.
Line 405: Line 443:
#*# one ('standard_update') is processed and uploaded at the standard uploading interval
#*# one ('standard_update') is processed and uploaded at the standard uploading interval
#*# the remaining one is processed and uploaded at the last task when [[Cumulus_MX#MX_End_of_Day_Process|the MX_End_of_Day_Process]] runs.
#*# the remaining one is processed and uploaded at the last task when [[Cumulus_MX#MX_End_of_Day_Process|the MX_End_of_Day_Process]] runs.
# The remote filename column needs to contain the FTP path and required destination file name if you want Cumulus MX to FTP the processed file to your web server. If you have your web server on the same local network as the device where you run the MX engine, then MX can use copy to transfer the file to your web server and you need the full path and file name relative to where the Cumulus executable is stored (again you don't need the drive if same drive).
# The remote filename column needs to contain the FTP path and required destination file name if you want Cumulus MX to FTP the processed file to your web server.
# You must have a tick in the process column, your template file (or files) will not be valid PHP until Cumulus MX has processed it and each PHP variable has a value.
#*As already said, the filename used here needs to match what you will use in your includes, the file extension could be ".inc" to make its usage clear or (as illustrated) it could be ".php" to remind you of the language used in the file.
# If you want your file processed and uploaded at the real-time interval you put a tick in the realtime column.
#*If you have your web server on the same local network as the device where you run the MX engine, then MX can use copy to transfer the file to your web server and you need the full path and file name relative to where the Cumulus executable is stored (again you don't need the drive if same drive as executable).
#*Note that Cumulus cannot process a file at the real-time interval unless you have enabled the real-time timer ('''Internet settings''' page, ''Web/FTP settings'' section, tick '''Enable Realtime''')
# You must have a tick in the process column, your template file (or files) will not contain any values until Cumulus MX has processed it and replaced each web tag.
#*Note that Cumulus cannot upload a file by FTP at the real-time interval unless you have set a FTP process to happen at real-time interval ('''Internet settings''' page, ''Web/FTP settings'' section, tick '''Enable realtime FTP''')
#Unless your web server is on the same local network as your Cumulus software, tick the '''FTP''' column.
#If you have followed the instructions above and saved your file in UTF-8 encoding, tick the '''UTF8''' column.
#If you have followed the instructions above and saved your file in UTF-8 encoding, tick the '''UTF8''' column.
#Leave the '''Binary''' column unticked.
#Leave the '''Binary''' column unticked.
#If you have not ticked the realtime column, your file will be uploaded at the normal uploading interval unless you tick the '''End of Day''' column. If you do tick here, then your file will be uploaded right at the end of the rollover process, at this stage Cumulus will have the new date in every date type tag, it will have initialised the monthly tags to be empty, and the yesterday tags will have been set. In other words, you will only use this option if you have other web tag templates being uploaded during the day.
#Unless your web server is on the same local network as your Cumulus software, tick the '''FTP''' column.


'''Choosing the uploading interval'''
# If you want your file processed and uploaded at the real-time interval you put a tick in the real-time column. You would choose this for all tags representing current conditions or related to today and so likely to keep updating during the day.
#*Note that Cumulus cannot process a file at the real-time interval unless you have enabled the real-time timer ('''Internet settings''' page, ''Web/FTP settings'' section, tick '''Enable Realtime''')
#*Note that Cumulus cannot upload a file by FTP at the real-time interval unless you have set a FTP process to happen at real-time interval ('''Internet settings''' page, ''Web/FTP settings'' section, tick '''Enable realtime FTP''')
#If you want your file processed and uploaded at the start of a new meteorological day (when the end of previous day has been completed) you put a tick in the '''End of Day''' column. Remember, at this stage Cumulus will have the new date in every date type tag, it will have initialised the monthly tags to be empty (so don't include those tags if processing at this interval), and the yesterday tags will have been set (so those tags should be processed at this interval).
#If you have not ticked the real-time column, nor ticked the EOD column, your file will be uploaded at the normal uploading interval. You will probably use this interval for tags relating to month and year that would benefit from being updated more trhan just at end of day, but don't really need to be updated as often as web tags for current conditions.


==Example==
==Example==
Line 422: Line 464:
<pre>
<pre>
<?php  
<?php  
  require_once("cumuluswebtags.php");
  require_once("cumuluswebtags.inc");
  echo $time;
  echo $time;
  echo "<br/>";
  echo "<br/>";
Line 438: Line 480:
See [[Sensor_Contact_PHP]] for another example script that uses this file.
See [[Sensor_Contact_PHP]] for another example script that uses this file.


==Debugging==
==Viewing the PHP==
 
Any PHP only exists on the web server, all a browser can see is the generated HTML, so this is what you see if you use the web browser's View Source option. As explained in [[PHP]] article not showing PHP has security advantages protecting content that might include stuff you don't want the public to see and preventing unauthorised changing of the script file. (Javascript files can be seen normally and most browsers provide an option to edit them).
 
Some authors when writing a script using PHP do include a downloading option at the start of the script before any other output. These downloaders allow you to see the actual PHP script. Unfortunately, there is no rule on how you invoke this downloader. I tried hard to get standardisation; see [https://cumulus.hosiene.co.uk/viewtopic.php?f=14&t=16425 Source Listing suggestion] in the forum. Another problem my suggestion addressed was the possibility that a web page includes multiple PHP scripts, and the downloader may list a different one to that you wanted to see.
 
Here are some querystrings you might try, in the brandon script download the PHP by appending the querystring <tt>?source=view</tt> to the end of the URL. Other templates might expect you to add the querystring <tt>?parse=source</tt>, <tt>download=source</tt>, <tt>sce=view</tt>,  or reversing the first idea <tt>?view=source</tt>. There are several other variants including <tt>"?view=getorfmiland</tt>.
 


If you wish to view all the values contained within a template file , some files (e.g. the brandon script) let you do this by appending the querystring <tt>?source=view</tt> to the end of the URL. Other templates expect you to add the querystring <code>?parse=source</code> and still others use <tt>?view=source</tt>. There may even be other variants. I tried hard to get standardisation see [https://cumulus.hosiene.co.uk/viewtopic.php?f=14&t=16425 Source Listing suggestion] in the forum, but I know of at least five other variants, source is abbreviated to 'sce' or 'src', and as well as parse, people use 'download'.


Example
Example
Line 447: Line 495:
This will display the source code of the file, listing all the variable names and the values Cumulus has assigned to the web tags during it's processing cycle. It won't show you the original template with web tags in it.
This will display the source code of the file, listing all the variable names and the values Cumulus has assigned to the web tags during it's processing cycle. It won't show you the original template with web tags in it.


[[Category:WebTools]][[Category:User Contributions]]
[[Category:WebTools]][[Category:User Contributions]][[Category:WebTips]]
5,838

edits

Navigation menu