Thursday, November 19, 2009

The Need for Mobile Optimization

This is definitely a NEED not a want.

Being in a profession of constant change I am always reading someone's blog, tutorial or latest trends in various subsets of the web.  Most of the time I just fire up Google Reader and start reading.  However, over the past 6 months I have found myself using my iPhone as my "reader" of choice.  Partly because of the convenience factor and in addition, I don't have to carry my laptop around everywhere I go.  Outside of Google Reader one of the best sources that I have for great articles are from some of the big names in web development that I follow on Twitter.

However, the sites and articles that people are linking to are still optimized for a standard browser, not the mobile experience which is HIGHLY annoying.  It is not that I am too lazy to wait for the page to load, but the lack of patience that I have for the delay in the page to finish loading due to 30+ stylesheets requested, some ad tracking pixel or any number of other tasks that are processing that have ZERO to do with what I, the user, is trying to accomplish.  What I find myself doing more and more is using the mobile app functionality of emailing the link to myself so that I can read it when I do get back to my laptop.

Should this mean that companies should have a regular/mobile site?  There is no right or wrong answer for that.  You have to do research and look at your visitor trends and who your target audience is.  I would venture to say that if you are a news organization then the answer would be yes.  However, even though currently you might have a low percentage of mobile visitors that number will only increase over time and your site should at the bare minimum be optimized for a mobile browsing experience.

Posted via email from Cory Wiles Blog

Friday, October 30, 2009

AVAudioRecorder prepareToRecord Silent Error

I was working on the voice record feature for one of my upcoming iPhone apps, but when I would try to start recording the app would crash.  During the adventure of debugging I couldn't trace anything back to my code as far as memory leaks, uninitialized properties, missing delegate methods, etc.  After about an hour I think I almost as many NSLog entries as I did code.  The two biggest troubling aspects where:
  1. prepareToRecord was the method that was failing
  2. AVAudioRecorder initialization wasn't throwing any errors
I turned to some sample code that I knew worked, but mine would still fail.  At this point I knew it was going to be something little that I was overlooking.  After reading through a few posts on StackOverFlow I noticed a difference in my audio configuration settings that was being passed to: initWithURL:settings:error:

I have a separate class where I set the settings argument.  The last two setting items are for: AVLinearPCMIsBigEndianKey and AVLinearPCMIsFloatKey.  Their values need to be NSNumbers so I was using [NSNumber numberWithBool:], but instead of passing in "YES" or "NO" I was passing in TRUE and FALSE, which is INCORRECT.  Just one of those oversights I forget about when switching from one language to another.

I am still not sure why my NSError wasn't populated with some information.  I don't know why the initializer didn't catch my syntax error either.

After I made my corrections, the recording worked perfectly.  Whew!  Now I just need to write the playback portion.

Posted via email from Cory Wiles Blog

Monday, October 26, 2009

Zend Framework Dynamic Site - In Production

Back in April I wrote a blog post discussing my concept of having a dynamic site using the Zend Framework.  In addition, I posted an some example code of how everything works.  I am a firm believer that one should practice what they preach and two different sites are now in production using the framework that I wrote and so far it works beautifully.  I had to make a few changes to the route to allow for module exceptions.

Towards the end of the project there was a request to have a search functionality and also custom forms.  Normally this wouldn't be an issue what so ever, but the way that the custom route is setup all requests are send to the default module/index controller/index action.  I modified the route to ignore any request that started with search or forms and route those requests to the search or forms module.  The regex is easily modified to allow for other exceptions.

Custom Regex:
<route>^(?!\bsearch|forms\b)(\w+-?[\w-?]*\/?)(.*)?</route>

I also setup the ability to add in meta keywords and meta description tags in the content.xml file.  Finally, both sites use the EXACT same doc root and dynamic site framework.  Since both sites use the same layouts, just different menus and different background images, I didn't want to duplicate a lot of code.  So in the setup page display plugin I am able to transverse the content mapping file based upon not only the request, but the url host name to display the proper layout.

Posted via email from Cory Wiles Blog

Thursday, October 8, 2009

SlideView Contribution

One of my favorite iPhone related sites to read is iPhone Developer Tips.  The tutorials are outstanding and most importantly "real world" useful.  Back in August there was a blog posted showing how to implement a "slideable" message that can be presented to a user.  After reading through it I decided to modify how it was used to make it a little more extensible for an iPhone project that I am working.  After I got everything working correctly and they way I wanted to I decided to contact the site owner, John, with all of my source code in case he wanted to use it.

After a little more tweaking to my code he posted my changes, as well as another developer, Greg's code in a new post.

This is a small example of the importance of using open source software, as well as, the importance for developers to use the knowledge and skills of other developers to make the good....GREAT.

Sliding Views On and Off Screen – Part 2 – Reader Contributions

Posted via email from Cory Wiles Blog

Monday, September 28, 2009

Best Practices That Work

Tonight I got an email from a friend that my site was down.  Sometimes, if Apache has been running for a long time on my vhost then for some reason the process id gets corrupted.  I don't know if that has to do with the VPS or what.  I never had that problem when I had my dedicated server.  It happens so infrequently I rarely even notice.  However, my usual task of just restarting Apache didn't work.  I got an error message that the main site's docroot didn't exist. That was very strange and very disturbing.  I got on the phone with the sysadmins at my hosting company and couldn't really find the culprit and they didn't have the automated backup of the site.

The good news, as I was told, was that they could recreate the directory structure within one minute and get Apache up and running. SWEET!  However, what about my site?!  Though they offer backups for the VPS they are not guaranteed.

I was not panicking because I keep all site changes backed up in subversion on a completed different host.  Once the directory structure was put back in place all I had to do was check the latest copy of my site and I was backup and running.  It was good to see and know that using best practices does ACTUALLY work.  Imagine that.

Posted via email from Cory Wiles Blog

Tuesday, September 22, 2009

Using NSNotificationCenter with UINavigation Controllers

When presenting an app to user with a large list of information, most developers will add in the ability to search the list inside of the table view.  To further increase usability it is a good practice to include different filters.  For example, a scope button that allows the user to filter by name, address, phone number, etc.  Implementing these features is pretty straight forward.  However, one of the filter options that was needed for a particular app that I am working on was to filter by department.

The complexity to this problem came in the fact that the search term couldn't be the department name, but the code.  Obviously, most users don't know the corresponding codes for a given department.  So I needed a way to present a list of "friendly" department names to the user to pick from, and once a given department was chosen then the corresponding department code would be populate the search bar field.

The first attempt was to use a UIPickerView.  Unfortunately, this wasn't the best solution mainly because you can't use key/value pairs with the UIPickerView.  Well...not in a very straight forward manner.  My second solution attempt was to use a modalViewController and then pass the selected object back to the parent controller.  Everything worked out just fine, but the parent object property wasn't being updated.

To solve this problem there are a couple of schools of thought.

  1. Set the parent controller as the delegate and setup a new protocol that the child controller implements.
  2. Use the NSNotificationCenter which uses the Observer Pattern to update another object based upon some action/method.
Most of the time I would say that the second option is overkill since it is designed for a one-to-many object broadcasts, but in this use case it is the preferred approach because "you would otherwise have to pass instances around of delegates just to connect objects". - http://alexvollmer.com/index.php/2009/06/24/cocoas-ways-of-talking/

FINAL SOLUTION:
// Parent UIViewControlller

- (void)viewWillAppear:(BOOL)animated {

  [[NSNotificationCenter defaultCenter] addObserver:self
                                        selector:@selector(refreshSearchBarControllerWithDepartmentCode:)
                                        name:@"DepartmentCodeNotification"
                                        object:nil];
 
  [super viewWillAppear:animated];
}


- (void) refreshSearchBarControllerWithDepartmentCode:(NSNotification *)notification {

  NSString *key;
  NSString *trimmedSearchString;

  for (key in [notification userInfo]) {
    trimmedSearchString = [[[notification userInfo] valueForKey:key] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  }
 
  self.searchDisplayController.searchBar.text = trimmedSearchString;

  [self.searchDisplayController.searchBar becomeFirstResponder];
  [self.searchDisplayController.searchResultsTableView reloadData];
}

- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope {

  if (selectedScope == DEPARTMENT_SCOPE_TITLE_INDEX && [[self.searchDisplayController.searchBar text] length] <= 2) {
   
    DepartmentsViewController *dvController = [[DepartmentsViewController alloc] initWithNibName:@"DepartmentsView" bundle:[NSBundle mainBundle]];

    [self presentModalViewController:dvController animated:YES];

    [dvController release];
   
  } else if ([[self.searchDisplayController.searchBar text] length] >= 2) {
   
    NSString *trimmedSearchString = [[self.searchDisplayController.searchBar text] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    [self getEmployees:trimmedSearchString method:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:selectedScope]];

    [self.searchDisplayController.searchResultsTableView reloadData];
  }
}

//Child UIViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

  NSString *code        = [self.tableView cellForRowAtIndexPath:indexPath].detailTextLabel.text;
  NSString *description = [self.tableView cellForRowAtIndexPath:indexPath].textLabel.text;
 
  NSDictionary *searchDepartmentCode = [NSDictionary dictionaryWithObjectsAndKeys: code, description, nil];

  [[NSNotificationCenter defaultCenter] postNotificationName:@"DepartmentCodeNotification"
                                                      object:nil
                                                    userInfo:searchDepartmentCode];

  [self dismissModalViewControllerAnimated:YES];
}

END RESULT:
After a user selects the appropriate department, the child controller is dismissed and the search bar text is updated with appropriate department code.

Posted via email from Cory Wiles Blog

Thursday, September 10, 2009

Microsoft and Enhanced Experience

I read a very interesting article yesterday that ranked the usability between Apple's website and Microsoft's website.  Granted, I am a little biased toward Apple, but I do feel that the author did a great job in their comparison. *spoiler: Apple won.  To add to the critique the Microsoft website does something that is a HUGE pet peeve of mine.

If a company claims to be in the "web" business then they need to create websites that look the same in all browsers.  Microsoft has a HORRIBLE reputation on releasing browsers that are not standards compliant, but are constantly giving non-IE users who visit their websites a second rate experience.  Case in point...their own website.  If you go their website with anything other than Internet Explorer, the visitor is presented with this ANNOYING alert in the upper left hand corner that is asks if you want to "Upgrade your Internet experience".  After few seconds it does minimize, but if you don't click the "Don't show me this EVER again link" the next page you navigate to will show it.

The fact is that I do want to upgrade my internet experience, but not by installing Silverlight, or IE7 or IE8 for that matter.  All of the before mentioned pieces of software are horrible, lack innovation, and do nothing but dumb down the web.  I want to upgrade my internet experience by not being harassed by Microsoft.

Posted via email from Cory Wiles Blog

Tuesday, September 8, 2009

Automate Adding Twitter Followers with Zend Framework

Adding followers to a user one at a time is sometimes a VERY lengthy process.  Last night the question was posed to me if there was a way to automate adding followers from users who follow someone else.

Example scenario:
John Doe follows Jane Doe
John Doe wants to follow at 600 followers of Jane Doe

Unfortunately, the Zend_Service_Twitter class doesn't offer any functionality to retrieve a list of followers from another user, but I was able to extend the class and add the functionality.

The particular user that I chose to test has roughly 6,000 users.  My client didn't want all 6,000, but the first 200.  Within 10 minutes the new custom class was written,  the script ran, and now my client was now following 200 new people of like mind. :)

I plan on submitting the feature request to the Zend Framework gurus to have the ability to find followers from other users so that it can be apart of the main service.

Until then here is the class and script:

require_once 'Zend/Service/Twitter.php';

class CW_Twitter extends Zend_Service_Twitter {

  public function __construct($username, $password) {
    parent::__construct($username, $password);
  }

  public function getOtherUserFriends($name) {
        $this->_init();
        $path = '/followers/ids/' . $name . '.json';
        $response = $this->restGet($path);
        return $response->getBody();
  }

}

$twitter = new CW_Twitter('username', 'password');
$response = $twitter->getOtherUserFriends('friendusername');

$arrayObject = new ArrayObject(Zend_Json::decode($response));
$limitIterator = new LimitIterator($arrayObject->getIterator(), 0, 200);

    foreach ($limitIterator as $value):
      $twitter->friendship->create($limitIterator->current());
    endforeach;

Posted via email from Cory Wiles Blog

Thursday, August 27, 2009

Google Interview - Could You Pass?

The gold standard of programmer interview questions.  It is not what they ask, but how they ask them.

http://courses.csail.mit.edu/iap/interview/materials.php

Posted via email from Cory Wiles Blog

Friday, August 21, 2009

Migrating to HTML5 and CSS3

It seems that in the past six months there has been more and more news, examples, and excitement concerning HTML5 and CSS3.  I must say that I too have been bitten by the bug.  What features that I have seen makes me very excited about the future of web development.  It is time to get back to basics and standards.  However,  because both are their infancy as far as adoption, not all browsers support the new technologies and the ones that do support them don't support all the features of HTML5 and/or CSS3.

Being, what most people consider, an earlier adopter I am going to be converting www.corywiles.com over to HTML5 layout using CSS3.  In order to not shut out those of you who don't have a supported browser, I will keep the old layout around until the percentage of those browsers falls below 5%.

Posted via email from Cory Wiles Blog

Friday, July 31, 2009

It's Not Easy to Get Mobile

Over the past two months I have been approached to develop at least three different iPhone apps.  Two out of three clients weren't as shocked with the price as they were with the time line.  While each developer out there has a different level of expertise, I would say that your average iPhone app will take 2 - 3 months of FULLTIME development and then at least 7 days for Apple to approve it.  Let me reiterate that the previously estimated time frame is not only a conservative estimate, but also based up a fairly simple app.

To define fairly simple, let's say that you want to have an app where you can search a database for a list of people based upon different criteria: name, age, state and country, have those displayed in a list where a user can then select one of the search results, view the person's complete detail information, and have the ability to copy that user's information to the phone's contacts.  Not that complicated as far as the requirements go.

However, once you start writing the app you will realize that as a developer you need to consider the asynchronous loading of the data, connection speed (Wifi, 3G, Edge or none at all), caching, processor limitations, memory limitations, phone orientation and interruptions of the app. For example, what if a call comes in while your user is trying to perform a save operation in the application or how should the app perform if there is a memory warning and/or crashes).  You have to keep those, and more, in mind just when you want to display a simple list of data.

Finally, after you have all your functionality worked out and you think that you are bug free the REAL fun begins.  The software that is used to develop for the iPhone and Macs has profiling and testing features.  At this point, the developer starts really digging into the bits and bytes of the app to make sure that it is not suffering from memory leaks, releasing objects too early/too late, and overall performance.  While the phone itself is VERY powerful, it is still a mobile device with limitations.

iPhone applications, in my development experience, are not usually offered as a standalone product or service, but as a companion to an existing or large product and/or service.  As a consequence, I have seen many applications rushed out the door on time for what was an unrealistic deadline.  The is usually driven by release date of the "big brother" to the application, which was set to meet a profit projection.  However, if the natural mobile development time is not taken into consideration then the app doesn't reach it's full potential.  At this point everyone loses.

If you decide that you want an iPhone app, and one that works, plan it taking sometime to develop.  Manage the expectations.

Posted via email from Cory Wiles Blog

Sunday, July 19, 2009

Speeding Up the Site

I have decided that since I don't have too much time in the next three months to update a lot of the UI for this site then I will be spending my time making it load faster, especially on the main page.  This first bottleneck that I noticed was when the feeds at the bottom of the page where being de-cached, the entire page was not being loaded until they were done.  This has been rectified by having those load separately through AJAX calls after the page has finished loading.


The next area of improvements will be the portfolio gallery.


It is all about getting to where you have to go faster and more efficient.


Thursday, July 16, 2009

Posterous Is Keeping It Simple

Leveraging social media outlet's to promote your site can be a love/hate relationship.  While the power of promotion is undeniable within the social networking outlets to spread the gospel of whatever business or service you are offering, it can be a daunting task to note only keep up with the number of sites out there, but also maintaing them.  You are seeing more and more companies having to extend their marketing or PR departments to include employees whose day to day tasks are to post and moderate these sites.

One of the statements that I hear most often from clients is that they want to have a presence on Facebook, Twitter, YouTube, LinkedIn, etc., as well as blog, but they don't want to  have to update twenty different sites.  The best solution by far for this is to use Posterous.

Posterous is by far the EASIEST service for not only, blogging, but also blasting out updates to individual services or groups of services at once.  Just send an email to post@posterous.com and you have an instant blog.  You don't even have to create an account first, but if want to have access to some of their other services then an account is required.  If you want to attach a video to blog or images...no problem. Just attach them to the email and embedded videos are added to the blog post as well as, images and audio files.

While some people require more advance features from a blog or need to have better control of their design, then there are plenty of great choices for them to choose from. However, I have found that most people don't have the time to do anything other than email.  Well your white horse has arrived.

Just send an email.  It doesn't get much simpler than that.

::NOTE:: This blog was posted using Posterous.

Posted via email from Cory Wiles Blog

Monday, June 8, 2009

WWDC Day One

While I can't disclose all that I learned from the WWDC, due to the NDA, I will say that it has been amazing.  Apple has not only put on a well oiled conference, but the new product lines as well as tools and resources available have just been amazing.

In case you didn't catch today's keynote here are some highlights:

  • Macbook Pros now come in 13in, 15in, 17in models
  • Macbook Pros can have up to 8gb of RAM
  • Macbook Pros have SD Slot
  • Snow Leopard has an almost seamless connection to Exchange
  • Snow Leopard and iPhone SDK use 90% of the same framework/APIs
  • Prices have dropped tremendously
  • iPhone 3g are now only $99
  • iPhone 3gp[s] are coming in 16gig/32gig
  • iPhone 3.0 now supports voice dialing, improved camera (with autofocus and highlighting), video camera with in-context editing and digital compass.
Obviously there was much more discussed so to get all the juicy details check out the keynote video.  

Friday, June 5, 2009

WWDC Next Week in San Francisco

The party will begin in roughly three days. I am one of the lucky individuals who is attending the 2009 WWDC in San Fransisco. With all the rumors flying around the web right now it is definitely hard to discern fact from fiction, but I have been working diligently on my first iPhone application using the 3.0Beta_3 SDK and while I can't speak specifically about a lot of the new features, because of the NDA, I can say that Apple once again has kept the users in mind with the new features. All will be pleased.

Everyone from developers to the casual user.

For up-to-date information on the conference just follow me on Twitter.

Friday, May 1, 2009

Wednesday, April 22, 2009

Targeting Nested LI's with jQuery

After two days of trying every DOM hierarchy transversile that I could think of or learn from I was about to give up. The task at hand was fairly simple. I was creating a TreeView menu for a given directory. The top level nodes where generated when the page loads and if the top level "resource" is a directory then when that item is clicked an AJAX call is made to get it's sub-resources which are nested ul/li.

In order to make subsequent calls for any directories/files that are > the first level I need to get the id of the li tag that is clicked (only directories have the ability to be clicked).

I thought I would be able to navigate through the DOM hiearchy to the nested li and retrieve the id, however, this becomes a problem with nested ul/li's because if you try to alert the id and expect the result to be of the nested element you will get the id of the parent element.

List Items


  • [+]applications


    • lib
    • index.php
    • settings
    • .htaccess
    • images
    • css
    • views
    • js
    • modules
    • layouts
  • [+]ops

// doesn't work

$(document).ready(function() {

$("li").click(function() {
alert($(this).attr('id'));

})
});


// does work
$(document).ready(function() {

$("li").click(function(event) {
alert(event.target.id);

})
});


The solution was to pass and event object to the click event handler. After I figured out that little nugget it only took me another hour to finish up my script.

Sunday, April 5, 2009

Zend Framework Dynamic Site

::UPDATE - 04.09.2009::
I finally was able to create an example of the below described site.  If you have any questions/comments please let me know.

When creating a website, especially in the corporate/enterprise world, it assumed that you have to use some sort of content management system.  The range, and subsequent debate, could be everything from free (Joomla, Wordpress, Drupal) to the very expensive (Vignette).  While I am in a firm believer in the use of a CMS it seems lately I have been asked to develop sites whose content doesn't change that often, but the responsibility still needs to fall into the hands of a content editor not a developer.  In addition, the designer that I work with wanted the freedom of creating as many templates as he wanted and tweak the design without having to consult me, the developer, nor have to work with a content editor to update any of the content pages.

I went back and forth how to best solve this solution.  While Joomla and Wordpress would have certainly handled all of these requirements there is the overhead that comes with any CMS and with the requirements that I had to work with I kept asking myself if using a CMS was lighting a grill with an atom bomb.

The end result was a hybrid between using static "html" and a mini-CMS....all using the Zend Framework.  I came up with a way that separated the content, layouts, and the developer maintained backedend of the site.

Basic requirements:
  1. SEF's
  2. Breadcrumb subheadings
  3. Flexible layouts - some pages might need a "one column content well", "one column content well with a side bar", etc
  4. Content pages should be kept in one location
  5. Templates should be independent of backend code and content
Directory Structure:
  • content
  • css
  • images
  • index.php
  • js
  • layouts
  • lib
  • modules
  • settings
How It Works
  • Creating/Editing Content


    1. Create the static phtml file in the content directory
    2. Edit the content.xml file in the content directory and add the following attributes and/or nodes: url - 1) what the request url will be. 2) file - the name of the phtml file (minus the extension). 3) pageTitle - self explainatory 4) layout - what template do you want to use for the page
  • Page Rendering


    1. Zend Layout
    2. Zend MVC
    3. Custom regex route
    4. Custom Site Page Render Plugin

Plugin and Go

The majority of the site dynamics happens in the custom plugin - routeShutdown method.
  1. Based upon the $_SERVER['REQUEST_URI'] the appropriate "page" node is located: "//pages/page[@url='{$_SERVER['REQUEST_URI']}']".
  2. The page title is set
  3. The name of the view (phtml) file is set
  4. The breadcrumbs array is populated. *
  5. All the attributes are then set as an array of params and set in the request.
  6. The IndexController/indexAction grabs the params, sets them to view properties to be displayed in the layout

*::NOTE::
Since there is technically not a hierarchy of files on the web server... the way that the url is decided upon is by the content editors.  Because of this there is a business rule in place that the "toplevel" navigation is the first set of characters in the url and child navigation thus follows.  In the method the breadcrumbs are created by parsing this pattern.

Conclusion
While there is definite room for improvement to how the site management is done we have seen great efficiency not only how the site is maintained and content is created, but also the amount of code that is used to generate the site.
  • 1 module, 1 controller, 1 action, 1 custom plugin
  • developers, content editors, and designers are able to work on their respective portions of the site without having to rework or update any other aspect of the site in 99% of use cases.

I finally was able to create an example of the below described site.  If you have any questions/comments please let me know.

Tuesday, February 24, 2009

Uh Oh

I realized yesterday that my site has been throwing an error for the past few days. The reason being was that the RSS feed url for Smashing Magazine had changed. I updated the footer feed display and now everything is all better.

Sunday, February 15, 2009

Internalization and Zend Form

One of the many growing requirements that I am experiencing at work is for internalization of our forms.  In this particular use case the languages available are limited to English and Spanish so I am not able to use Zend_Locale::BROWSER exclusively.  To make sure that correct form displays, in my form model class I created an array of allowed locales.  Before the form instantiated in the controller the language param is set the registry, and then that locale is checked against allowed locales.

References:
Translations - located in my models directory: Models/Languages/En.php and Es.php

// En.php

return array(
              'name'    => 'First Name',

              'address' => 'Address'
       );


// Es.php

return array (

         'name'     => 'nombre',         'address' => 'dirección'

       );



// Controller snippet
 /**
  * Add language to the registry
  */
  $locale = new Zend_Locale($this->_getParam('lang'));
  Zend_Registry::set('locale', $locale);

// Form snippet
  /**
   * Instance of Zend_Locale
   *
   * @var Zend_Locale
   */
  protected $_locale         = null;

  /**
   * Allowed locale regions
   *
   * @var array
   */
  protected $_allowedLocales = array('en', 'es');

  protected $_translations   = null;

    $this->addElement('note', 'quanityInstructions', array(
      'decorators'  => $this->_standardNoteDecorator,
      'description' => "{$this->_translations->_('quanityInstructions')}"
    ));

  /**
   * Prepare the form
   *
   * @return boolean
   */
  private function _prepare() {

    /**
     * Grab the browsers locale
     */
    $this->_locale = Zend_Registry::get('locale');

    /**
     * If the browser is locale isn't English or Spanish then default to
     * English
     */
    if (!$this->_checkForValidLocaleRegion($this->_locale)) {
      $this->_locale = new Zend_Locale('en_US');
    }

    $transFile = ucfirst($this->_locale->getLanguage()).'.php';

    $this->_translations = new Zend_Translate('array', realpath(dirname(__FILE__).'/../../Models/HemOnc/Languages/'.$transFile), $this->_locale);

    return true;
  }

  /**
   * Check to make sure that browser's locale is english or spanish.  The region
   * doesn't matter.
   *
   * @param Zend_Locale $zl
   * @return boolean
   */
  private function _checkForValidLocaleRegion(Zend_Locale $zl) {
    return in_array($zl->getLanguage(), $this->_allowedLocales) ? true : false;
  }


If locale is allowed then the form label translation is set.

For further info please contact me.

Thursday, January 22, 2009

Site Improvements

Last night I finished migrating www.corywiles.com to a complete Zend Framework MVC app.  Now that is done there should be significant site improvements mostly due to enhanced caching.

Saturday, January 10, 2009

PayPal Zend Framework Validator

I was presented with a project where the client wanted a form that would accept credit card payments via paypal.  I could either write it in PHP or Java. While I am a big fan of both languages I felt in this instance that PHP was the most time efficient route.   However, PayPal doesn't offer an API for PHP. Only Java and .NET.  As to not be discouraged I decided to spend a little time trying to come with a custom validator that would handle the validation of the credit card validation.

::Warning::
My use case was very specific.  I only needed to authorize credit card sales.  Basically their simpliest form of transations.

Since all the applications that I write now are us the Zend Framework I was able to reduce "inline-code" by creating a custom validator that I assign to my preValidation method inside of the model.  :: I worked on this concept with Jeremy Kendall ::

There is definitely room for improvement, but this is might be helpful to others.  For example, the paypal options should really not be declared as Zend_Config instance, but checked for an instance of Zend_config and then set as an array with the toArray method.


// Validator

/**
* @see Zend_Validate_Abstract
*/
require_once 'Zend/Validate/Abstract.php';

/**
* @see Zend_Http_Client
*/
require_once 'Zend/Http/Client.php';

class SJCRH_Validate_PayFlow extends Zend_Validate_Regex {

const CHARGE_FAILED = "ppChargeFailed";

const CHARGE_EXCEPTED_CODE = "0";

/**
* Error message to display to the user if the credit card transaction fails
*
* @access protected
* @var array
*/
protected $_messageTemplates = array(
self::CHARGE_FAILED => "There was an error processing your card. Error code: '%code%' Error message: '%msg%'"
);

/**
* @var array
*/
protected $_messageVariables = array(
'code' => '_code',
'msg' => '_msg'
);

/**
* Error Code value
*
* @var mixed
*/
protected $_code;

/**
* Error Message
*
* @param Zend_Config $options
*/
protected $_msg;


/**
* CC validation using PayFlow
*
* @param Zend_Config $options
* @param string $ccnum
* @param string $exp
* @param array $billing
* @param string $amount
* @param string $country
*/
public function __construct(Zend_Config $paypaloptions, $ccnum, $amount, $exp, $billing = array(), $country = 'US') {

if (!$paypaloptions instanceof Zend_Config) {
throw new Exception("Options must be an instance of Zend Config");
} else {
$options = $paypaloptions->toArray();
}

/**
* Random string used to error checking with payflow - DUPLICATION TRANSACTIONS
*/
$requestId = md5(date('YmdGis'));

$zfClient = new Zend_Http_Client($options['url']);

$zfClient->setHeaders(array('X-VPS-Request-ID' => $requestId));

/**
* Recommended from their documentation to change the timeout from 30 seconds (default)
* to 45
*/
$zfClient->setConfig(array('timeout' => 45));

$zfClient->setMethod(Zend_Http_Client::POST);

$zfClient->setParameterPost(array(
'USER' => $options['username'],
'VENDOR' => $options['vendor'],
'PARTNER' => $options['partner'],
'PWD' => $options['password'],
'FIRSTNAME' => $billing['firstname'],
'LASTNAME' => $billing['lastname'],
'STREET' => $billing['street'],
'ZIP' => $billing['zip'],
'TENDER' => 'C',
'TRXTYPE' => 'S',
'ACCT' => $ccnum,
'EXPDATE' => $exp,
'AMT' => $amount,
'CURRENCY' => 'USD',
'COUNTRY' => $country,
'CLIENTIP' => $_SERVER['REMOTE_ADDR'],
'VERBOSITY' => 'MEDIUM'
));

/**
* The response key/value pairs are seperated by ampersands.
*
* I extract the RESULT and RESPMSG
*
* If anything but a RESULT=0 is found then the code and error message are
* set to the validator's variables to display to the form user.
*/

$payFlowResponse = explode("&", $zfClient->request()->getBody());
$regex = "/(\w*)=(.*)/i";

@preg_match($regex, $payFlowResponse[0], $matches);

$this->setCode($matches[2]);

if ($this->getCode() !== self::CHARGE_EXCEPTED_CODE) {
@preg_match($regex, $payFlowResponse[2], $matches);
$this->setMsg($matches[2]);
}
}

public function isValid($value) {

$this->_setValue($value);

if ($this->getCode() !== self::CHARGE_EXCEPTED_CODE) {
$this->_error();
return false;
}

return true;
}

public function setCode($code) {
$this->_code = $code;
}

public function setMsg($msg) {
$this->_msg = $msg;
}

public function getCode() {
return $this->_code;
}

public function getMsg() {
return $this->_msg;
}
}

Using Zend View for Email Message Body in Your Model

Lately in the PHP/Zend Framework blog world there has been much discussion concerning what constitutes a model in an MVC app.  In the current implementation of the MVC apps at work our ZF Form implementations are processed in a corresponding model class as well as a 'notify' method which handles emailing a response after successful submission.  I was able to abstract all aspects of the email properties except for the message body.

Most of the time the requirement is to have, basically, the same form view but with the values populate.  That usually means creating some long and kludgy looking heredoc or worse a huge string of crazy html intermingled with escapes.  Neither solution was very appealing to me.  I kept thinking more and more that the email body was really a view.  So I treated as such.  So my solution was to create a global views directory and a view script that was the email body template and passed it's render method to Zend_Mail instance.

This allows for further separation of models and views.  I know that technically the email body should be apart of the model, but far too many times I have to change how the body of the mail looks, not the data, so it lends itself to more of a view.

Please feel free to comment.

::NOTE::
Forms as model architecture taken from http://weierophinney.net/matthew/archives/200-Using-Zend_Form-in-Your-Models.html

//Example Code
private function _notifyGroup() {
 
  $data = array();
  $data = $this->getForm()->getValues();
 
  /**
   *  Setup view instance to pass to notifier
   */
  $view = new Zend_View();

  /**
   * Assign all form data to view property
   */
  $view->assign("formvalues", $data);

 
 /**
   * Location of view scripts
   */
  $view->addScriptPath(dirname(__FILE__).'/../../../../views');

  $body = $view->render('email-templates/mir.phtml');

  /**
   *  Custom notify class that abstracts different 'notify' methodologies.  For example you can notfiy
   *  by writing to a log or email.  Email can be Zend_Mail, PEAR or plain ol' php mail() 
   */
  $notifier = CW_Notify::factory('mail_zend', $body, $options['email']);

  $notifier->notify();

}