We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14050
    • 788 Posts
    I was a Computer Science Minor in Undergrad, but haven’t coded anything for over four years. I was looking to get my hands wet with possibly coding some snippets for ModX. Now let me know if I am completely wrong, but the way I understand it is that the snippets should be raw PHP code and return the necessary data, and if that data is to be formatted with HTML, it should be done inside chunks. I have read through the material in the Developer’s Guide, and I think that a helpful tutorial, would be to take an existing PHP Script that that doesn’t separate the data returned from the formatting, and put it into the ModX framework of chunks and snippets.

    Here is an example script, although the Tutorial could be based on anything that does something similar:

    http://53x11.com/projects/php_scripts/weatherxmlparser_14.zip

    If I am correct in my understanding, this script returns the data already wrapped in HTML, and this isn’t how the ModX framework would ideally handle such a situation.
      Jesse R.
      Consider trying something new and extraordinary.
      Illinois Wine

      Have you considered donating to MODx lately?
      Donate now. Every contribution helps.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      The beauty of MODx is that you can do anything you like. The framework does not force you into any HTML style, nor any PHP coding style. Not only is it free as in beer, it’s truly free as in speech as well.

      Most of us do strive to practice "good" coding style and separate layout from presentation from logic, but it’s really a matter of personal taste and preference.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 7923
        • 4,213 Posts
        Correct and if you are willing to write up such tutorial, here is where you should do it:

        http://wiki.modxcms.com/index.php/Creating_Snippets

        It doesn’t matter if it’s not 100% correct or nicely formatted or anything at first. We can do it together. There is some common coding conventions listed on that page allready what should be used in the tutorial too..


          "He can have a lollipop any time he wants to. That's what it means to be a programmer."
          • 14050
          • 788 Posts
          Alright, well I just got the author’s permission to use the Weather XML Parser has a Snippet that I can try to make a tutorial of and upload for everyone to use if I manage to be successful :-D
            Jesse R.
            Consider trying something new and extraordinary.
            Illinois Wine

            Have you considered donating to MODx lately?
            Donate now. Every contribution helps.
            • 14050
            • 788 Posts
            OK, I am working on having this code work efficiently and moving more towards the coding standards supplied in the Wiki. This is what I have so far:

            Snippet:

            <?php
            // WEATHER.COM XML PARSER
            // Version 1.4
            // Copyright 2005 Nick Schaffner
            // http://53x11.com
            
            //Resource Name: WeatherX
            
            
            //////// Set Varibles Below
            $wxZipcode = 'XXXXX'; // Zipcode
            $wxPartner = 'XXXXXXXXXX'; // Partner ID
            $wxLicense = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';// License Key
            $wxIcons = 'weather'; // Path to Weather Icons from Include
            $wxUnits = 'english'; // Set to 'english' (Fahrenheit & MPH) or 'metric' (Celsius & KMH)
            $wxClock = 12; // Set to '12' or '24' hour format
            $wxUrl	 = 'http://xoap.weather.com/weather/local/';	// URL to xoap.weather
            
            
            //////// Parse XML
            
            $file = $wxUrl . $wxZipcode . '?cc=*&&prod=xoap&par=' . $wxPartner . '&key=' . $wxLicense;
            
            $xml_parser = xml_parser_create();
            
                 if (file_exists($file)){
                        $data = implode('', file($file));
                     } else {
                        $fp = fopen($file,'r');
                        while(!feof($fp)){
                           $data = $data . fread($fp, 1024);
                        }
                        fclose($fp);
            	}
            
            xml_parse_into_struct( $xml_parser, $data, $vals, $index );
            xml_parser_free( $xml_parser );
                
            $t = true;
            
            foreach ($vals as $key => $i) {
            
            if ($vals[$key]['value'] != false) {
            
            	if ($vals[$key]['tag'] != 'T') { 
            	
            		$temp = strtolower($vals[$key]['tag']);
            		$$temp = $vals[$key]['value'];
            
            	} elseif ($t == true) { 
            
            		if ($vals[$key]["level"] == 3) $conditions = $vals[$key]['value']; 
            		else { $windir = $vals[$key]['value']; $t = false; break; }
            
            	} 
            }
            
            }
            
            //////// Make sense of the varibles
            
            $sunr = str_replace (' ','',$sunr);
            $suns = str_replace (' ','',$suns);
            $date = date("l, F jS Y @ g:ia");
            
            if ($wxUnits == 'metric') { // Set Metric
            	$tmp = round((($tmp - 32)/9) * 5);
            	$flik = round((($flik - 32)/9) * 5);
            	$s = round($s * 1.609);
            }
            
            if ($wxClock == 24)  {
            	$sunr = date("H:i",strtotime($sunr));
            	$suns = date("H:i",strtotime($suns));
            	
            }
            
            
            if ($s == 'calm') $wind = 'Calm';
            
            $wxIcon = $modx->config['base_url'] . $wxIcons . '/' . $icon . '.png';
            
            $modx->setPlaceholder('wxIcon', $wxIcon);
            $modx->setPlaceholder('wxSunr', $sunr);
            $modx->setPlaceholder('wxSuns', $suns);
            $modx->setPlaceholder('wxDate', $date);
            $modx->setPlaceholder('wxWindSpeed', $s);
            $modx->setPlaceholder('wxConditions', $conditions);
            $modx->setPlaceholder('wxTemp', $tmp);
            $modx->setPlaceholder('wxWindDir', $windir);
            ?>


            Chunk:

            [!weather!]
            <div>
              <p>Temp: [+wxTemp+]°F</p>
              <p>Wind: [+wxWindDir+] @ [+wxWindSpeed+]mph</p>
              <p>Conditions: [+wxConditions+]</p>
              <p>Date: [+wxDate+]</p>
              <img src="[+wxIcon+]">
            </div>


            This allows me to output a majority of the information I would imagine someone would want. The only problem is that the way it is setup now, the current weather will be grabbed everytime someone loads the page. This would likely cause the license from Weather.com to be terminated. To avoid this the original script was intended to be used with a Cron Job that would invoke the Script every 10 or 15 minutes to write to a file that and the the locally stored text file would be served up to the users when the load the page. Is there a way to have ModX invoke this script only if it hasn’t been invoked by any user loading the page in the last 10 to 15 minutes? Also, any suggestions on adding in/removing some of the functionality I took out of the original script?
              Jesse R.
              Consider trying something new and extraordinary.
              Illinois Wine

              Have you considered donating to MODx lately?
              Donate now. Every contribution helps.
              • 7923
              • 4,213 Posts
              Make it save the wheather content to a file and check for its timestamp every time the script runs to see if it needs to get the info from weather.com again or not..


                "He can have a lollipop any time he wants to. That's what it means to be a programmer."
                • 14050
                • 788 Posts
                Doze,

                I am not exactly sure I know how to write it to the file, then read that file first when the snippet is initialized to do my timestamp check?
                  Jesse R.
                  Consider trying something new and extraordinary.
                  Illinois Wine

                  Have you considered donating to MODx lately?
                  Donate now. Every contribution helps.
                  • 15987
                  • 786 Posts
                  All you would need to do is have the file be read in at the beginning of the snippet by using fopen (http://us2.php.net/fopen) and fread (http://us2.php.net/manual/hk/function.fread.php).

                  Then determine if it needs updated, if an update occurs, then write back to the file using fwrite (http://us2.php.net/manual/hk/function.fwrite.php) with the new update date.

                  When creating files for snippets you will want to put them in: assets/snippets/name of snippet

                  Hope that helps, looks like a neeat snippet.
                    • 14050
                    • 788 Posts
                    I just wanted to thank everyone for their help! I have developed a WeatherX 1.0 Beta, available in the Snippet Development Forum. After it is polished, I will work on doing a wiki article to document the steps I took.
                      Jesse R.
                      Consider trying something new and extraordinary.
                      Illinois Wine

                      Have you considered donating to MODx lately?
                      Donate now. Every contribution helps.