Clinton Paquin

Code

I don't always make code public, but you can find other samples at GitHub

Here are some basic code samples, they aren't always production ready, but it gives you an entry talking point if you want to see my style.

PHP
MySQL
Javascript
  •                                         /**
     * Write an XML document based on another XML driver
     * Return the custom schema for processing in Controller layer
     */
    private function _formatForFacebook($arrFilenames=array())
    {
        // default output
        $strOutput = "";
    
        // filenames were passed in
        if (!empty($arrFilenames)) {
    
            // create an empty xml object
            $objXmlWriter = new XmlWriter();
            $objXmlWriter->openMemory();
            $objXmlWriter->setIndent(true);
            $objXmlWriter->setIndentString("\t");
            $objXmlWriter->startDocument('1.0', 'UTF-8');
            $objXmlWriter->startElement('upcoming');
    
            // loop through each filename and retrieve its data
            foreach ($arrFilenames as $strFilename) {
    
                // load up the file
                $objXML = simplexml_load_file($strFilename);
    
                // loop through file data
                foreach ($objXML->movie as $objElement) {
    
                    // get data to modify
                    $strImage    = (string) $objElement->attributes()->url;
                    $strShowTime = (string) $objElement->showtime;
    
                    // build out the correct image for facebook
                    $strFbImage = substr($strImage, 0, strrpos($strImage, '.')).'_fb'.substr($strImage, strrpos($strImage, '.'));
                    $strFbImage = str_replace('_images/moviesPage', 'assets/posters', $strFbImage);
    
                    // build out the correct airdate for facebook
                    $arrShowTime = explode(' ', $strShowTime);
                    array_pop($arrShowTime);
    
                    $objFbDate = new DateTime(implode(' ', $arrShowTime));
    
                    $strFbDate = $objFbDate->format('M d Y H:i:s');
    
                    // start element tag
                    $objXmlWriter->startElement('poster');
    
                    // add air date attribute
                    $objXmlWriter->startAttribute('airdate');
                    $objXmlWriter->text($strFbDate);
                    $objXmlWriter->endAttribute();
    
                    // create child xml tags
                    $objXmlWriter->writeElement('image', $strFbImage);
                    $objXmlWriter->writeElement('title', (string) $objElement->attributes()->title);
                    $objXmlWriter->writeElement('description', (string) $objElement->description);
    
                    // end element tag
                    $objXmlWriter->endElement();
                }
            }
    
            // ending root tag
            $objXmlWriter->endElement();
    
            // get XML output
            $strOutput = $objXmlWriter->outputMemory(true);
        }
    
        // return the output
        return $strOutput;
    }
    
                                        
                                            /**
     * Retrieve GetGlue comments for use in Controller layer
     */
    public function get_gg_comments($no_cache = false){
        $this->load->library('GetGlueHandler', $this->glueData);
    
        $data['comments'] = $this->load_gg_comments();
        $html = $this->load->view('getglue_comments', $data, true);
    
        $arr = array('success'=>'true','html'=>$html);
        $this->output->set_content_type('text/plain')->set_output(json_encode($arr));
    
        if ($no_cache) $this->my_output->clear_page_cache();
        $this->output->cache(2);
    }
    
    /**
     * Using the oauchlib, retrieve comments for use in the Controller layer
     */
    private function load_gg_comments() {
        $this->load->library('oauthlib',$this->glueData);
    
        $params = array(
            'objectId'=>$this->glueData['object_id'],
            'action' =>'commented',
            'numItems' => $this->glueData['comment_count']
        );
        $url = "http://api.getglue.com/v2/object/interesting"; //?action=commented&numItems=20%";
        $response = $this->oauthlib->api_call($url . "?" . http_build_query($params), $params);
    
        $data = array();
    
        if(isset($response['response'])){
            $data = $response['response']['totalActivity']['commentActivity']['interaction'];
            $data = $response['response']['interactions']['interaction'];
        }
    
        return $data;
    }
    
                                        
  •                                         /*
     * This is actually an MSSQL example (yeah, we have to use those other languages sometimes too …)
     * Select the ID, FirstName, LastName, and Number of subscriptions a user has (with promo code > 19)
     * Three tables
     *  - Promotion
     *  - Member
     *  - MemberPromotion (bridge table)
     */
    Select m.ID, m.FirstName, m.LastName, count(*) as Count
    From [FXNetworks].[dbo].[MemberPromotion] mp, [FXNetworks].[dbo].[Member] m, [FXNetworks].[dbo].[Promotion] p
    Where mp.PromotionID=p.ID and mp.MemberID=m.ID and mp.Subscribed='1' and p.ID >= '19'
    Group By m.ID, m.FirstName, m.LastName
    Order By Count ASC
                                        
                                            /*
     * This is actually an MSSQL example (yeah, we have to use those other languages sometimes too …)
     * Select a Promotion Description and Count of users subscribed to that Promotion (with promo code > 19)
     * Three tables
     *  - Promotion
     *  - Member
     *  - MemberPromotion (bridge table)
     */
    Select distinct Description, count(*) as Count
    From (Select m.ID, p.Description
    From [FXNetworks].[dbo].[MemberPromotion] mp, [FXNetworks].[dbo].[Member] m, [FXNetworks].[dbo].[Promotion] p
    Where mp.PromotionID=p.ID and mp.MemberID=m.ID and mp.Subscribed='1' and p.ID >= '19') as Foo
    Group by Description
                                        
                                            /*
     * This is actually an MSSQL example (yeah, we have to use those other languages sometimes too …)
     * Select the ID, FirstName, LastName, and LastLogin of users subscribed to newsletter (with promo = 19)
     * Three tables
     *  - Promotion
     *  - Member
     *  - MemberPromotion (bridge table)
     */
    Select Member.ID, Member.FirstName, Member.LastName, Member.LastLogin FROM Member, Promotion, MemberPromotion 
    WHERE Member.ID = MemberPromotion.MemberID and MemberPromotion.PromotionID='9' AND
    DATEADD(year, 1, Member.LastLogin) < getdate() 
    Group By Member.ID, Member.FirstName, Member.LastName, Member.LastLogin
    order by LastLogin DESC
                                        
  •                                         /**
     * Twitter API Sample
     * This is a quick code sample I whipped up and tested in about 30 minutes for a fellow dev over at forrst.com
     * It does a query to twitter.com looking for references to the term "TGIF" and returns the response in json
     * The response is parsed and output every 10 seconds, the old results slide down and new are placed at the top
     * of the stack.  Good fun for the whole family right?!
     **/
    <!DOCTYPE HTML>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <body>
    <style type="text/css">
    /** please for the love of John Elway put your css in a seperate file (it's inline here for demo reasons) **/
    #twitter ul{height:auto; padding:15px 0 0 15px; margin:0}
            #twitter li{float:left; margin:3px; color: #ccc; list-style-type:none; background: #666; display: block; 
                        width: 300px; height: 100px; padding: 7px; border: solid 2px #6fb2cd; display:none;}
                    #twitter li:hover {border: solid 2px #ccc;
                            -moz-box-shadow: 6px 6px 3px #888;
                            -webkit-box-shadow: 6px 6px 3px #888;
                            box-shadow: 6px 6px 3px #888;}
                    #twitter li a {color: #6fb2cd; text-decoration: none;}
                            #twitter li a:hover {text-decoration: underline;}
    
                    #twitter img {float: left; padding: 3px;}
    </style>
    <script type="text/javascript">
    $(function () {
        function getTweets() {
            var url = 'http://search.twitter.com/search.json?callback=?&q=%23TGIF&rpp=16';
            $.getJSON(url, function (json) {
                var output = [];
                for (var i = 0, len = json.results.length; i < len; i++) {
                    output.push('<li><img src="' + json.results[i].profile_image_url + '" width="48" height="48" />' + json.results[i].text + '</li>');
                }
                $("#twitter").html(output.join('');
                $("#twitter li").slideDown('slow');
            });
        }
        var timer = setInterval(getTweets, 10000);
        getTweets();
    });
    </script>
    <ul id="twitter"></ul>
    </body>
    </html>
                                        
                                            /*
     * This is a simple method to use the jQuery AJAX caller and post to an update on a user's profile
     * As is best practice, make sure you set a timeout, error handling method, and while your script is working
     * be sure to let your user know that something is going on by showing some type of a visual "loading" indicator
     *
     * @param array - data about the user
     */
    function updateUser(data) {
    
    	// disable submit button and hide it
    	$('#submit').attr('disabled', 'disabled').hide();
    
    	// show processing indicator
    	$('#processing').css('visibility', 'visible').show();
    
    	$.ajax({
    			url: 'users/update',
    			datatype: 'json',
    			type: 'POST',
    			data: {first_name : data.first_name, last_name: data.last_name, email: data.email, promo_code: data.promo_code}
    			timeout: 3000,
    			success: function(response) {
    
    				// hide processing indicator
    				$('#processing').css('visibility', 'visible').hide();
    
    				if(response.error == true) {
    					$('.error').html(response.message).css('color','red');
    					$('.error').css('visibility', 'visible').fadeIn();
    
    					// re-enable submit button and show
    					$('#submit').attr('disabled', '').show();
    				} else {
    					// everything is good, hide the form and display thankyou
    					$('#my_form').hide();
    					$('.thankyou').html(response.thankyou);
    				}
    			},
    			error: function( objAJAXRequest, strError ){
    				// hide processing indicator
    				$('#processing').css('visibility', 'visible').hide();
    
    				if(strError == 'timeout') {
    					var error_message = "Timeout: There was an error processing your request.";
    				} else {
    					var error_message = "There was an error processing your request."
    				}
    
    				// load and display error message
    				$('.error').html(error_message).css('color','red');
    				$('.error').css('visibility', 'visible').fadeIn();
    
    				// re-enable submit button and show
    				$('#submit').attr('disabled', '').show();
    			}
    
    		});
    }