TeraWurflUtils
[ class tree: TeraWurflUtils ] [ index: TeraWurflUtils ] [ all elements ]

Source for file TeraWurflDeviceImage.php

Documentation is available at TeraWurflDeviceImage.php

  1. <?php
  2. /**
  3.  * Tera_WURFL - PHP MySQL driven WURFL
  4.  * 
  5.  * Tera-WURFL was written by Steve Kamerman, and is based on the
  6.  * Java WURFL Evolution package by Luca Passani and WURFL PHP Tools by Andrea Trassati.
  7.  * This version uses a MySQL database to store the entire WURFL file, multiple patch
  8.  * files, and a persistent caching mechanism to provide extreme performance increases.
  9.  * 
  10.  * @package TeraWurfl
  11.  * @author Steve Kamerman <stevekamerman AT gmail.com>
  12.  * @version Stable 2.1.3 $Date: 2010/09/18 15:43:21
  13.  * @license http://www.mozilla.org/MPL/ MPL Vesion 1.1
  14.  */
  15. /**
  16.  * Finds an image for devices detected with Tera-WURFL
  17.  * @package TeraWurflUtils
  18.  * 
  19.  */
  20.     
  21.     protected $baseURL;
  22.     protected $imagesDirectory;
  23.     protected $wurfl;
  24.     protected $deviceID;
  25.     protected $imageExt = '.gif';
  26.     protected $image;
  27.     protected $descend = true;
  28.     
  29.     /**
  30.      * Creates a new TeraWurflDeviceImage
  31.      * @param TeraWurfl The instance of TeraWurfl (or TeraWurflRemoteClient) that detected the device
  32.      * @return void 
  33.      */
  34.     public function __construct(&$wurfl){
  35.         $this->wurfl = $wurfl;
  36.         $this->deviceID = ($this->wurfl->getDeviceCapability('actual_root_device'))$this->wurfl->getDeviceCapability('actual_root_device')false;
  37.         $this->baseURL = '';
  38.         $this->imagesDirectory = dirname(__FILE__'/device_pix/';
  39.     }
  40.     /**
  41.      * Sets the base URL of the device images
  42.      * Must end with "/".
  43.      * @param string Web-accessible location of the device images  (e.g. "http://domain.com/device_pix/" or "../device_pix/")
  44.      * @return void 
  45.      */
  46.     public function setBaseURL($baseURL){
  47.         $this->baseURL = $baseURL;
  48.     }
  49.     /**
  50.      * Sets the local directoy of the device images on the filesystem
  51.      * @param string Local filesystem directory where the device images are located (e.g. "C:/device_pix/" or "../../device_pix/")
  52.      * @return void 
  53.      */
  54.     public function setImagesDirectory($dir){
  55.         $this->imagesDirectory = $dir;
  56.     }
  57.     /**
  58.      * If you set the BaseURL, returns the path and filename of the device image (http://domain.com/device_pix/apple_iphone_ver1.gif),
  59.      * otherwise returns only the filename (apple_iphone_ver1.gif)
  60.      * @return string Device image filename
  61.      */
  62.     public function getImage(){
  63.         if(is_null($this->image)) $this->setImage();
  64.         return $this->image;
  65.     }
  66.     /**
  67.      * Set to false to prevent the image searching function from looking through the device's parent devices to find
  68.      * a very similar device image if the exact device image is not found.
  69.      * @param bool false prevents using the device image from a different version of the device
  70.      * @return void 
  71.      */
  72.     public function setDescendToFindImage($descend){
  73.         $this->descend = (bool)$descend;
  74.     }
  75.     /**
  76.      * Sets the internal $this->image var with the complete path to the device image
  77.      * @return void 
  78.      */
  79.     protected function setImage(){
  80.         if($this->deviceID === false){
  81.             $this->image = null;
  82.             return;
  83.         }
  84.         if(!file_exists($this->imagesDirectory)){
  85.             $realpath @realpath($this->imagesDirectory);
  86.             if(!$realpath){
  87.                 if($this->imagesDirectory[0]=='.'){
  88.                     throw new Exception("Error: the local images directory was specified as a relative path ($this->imagesDirectory), but could not be resolved.  Current directory: ".getcwd());
  89.                     exit(1);
  90.                 }else{
  91.                     throw new Exception("Error: the local images directory specified does not exist: ".$this->imagesDirectory);
  92.                     exit(1);
  93.                 }
  94.             }
  95.         }
  96.         if(!$this->imageExists($this->deviceID)){
  97.             if($this->descend){
  98.                 // Check fall back tree for an alternate image, starting at the current device, working back to generic
  99.                 foreach(array_reverse(explode(',',$this->wurfl->capabilities['tera_wurfl']['fall_back_tree'])) as $parentID){
  100.                     if($this->imageExists($parentID)){
  101.                         $this->image = $this->baseURL . $parentID $this->imageExt;
  102.                         return;
  103.                     }
  104.                 }
  105.             }
  106.             $this->image = null;
  107.             return;
  108.         }
  109.         $this->image = $this->baseURL . $this->deviceID . $this->imageExt;
  110.     }
  111.     /**
  112.      * Check if a device image exists for the given deviceID
  113.      * @param string Device ID (WURFL ID)
  114.      * @return bool Device image exists
  115.      */
  116.     protected function imageExists($deviceID){
  117.         return file_exists(realpath($this->imagesDirectoryDIRECTORY_SEPARATOR $deviceID $this->imageExt);
  118.     }
  119.     public function __toString(){
  120.         return $this->getImage();
  121.     }
  122. }

Documentation generated on Sun, 19 Sep 2010 00:16:02 +0000 by phpDocumentor 1.4.3