root/trunk/plugins/sfPropelActAsLocalizableBehaviorPlugin/lib/sfPropelActAsLocalizableBehavior.class.php

Revision 2, 5.8 kB (checked in by nperriault, 10 months ago)

First commit of the extracted code from production, I hope no passwd has been forgotten :-)

Line 
1 <?php
2 /**
3  * Localizable behavior class for Propel
4  *
5  * @package plugins
6  * @authior Nicolas Perriault
7  */
8 class sfPropelActAsLocalizableBehavior
9 {
10  
11   /**
12    * Cached localizations
13    * @var array
14    */
15   protected static $localizations = array();
16  
17   /**
18    * Behavior activation status
19    * @var boolean
20    */
21   protected static $isActivated = true;
22  
23   /**
24    * Cache a localization
25    *
26    * @param sfLocalization $localization
27    */
28   public static function addCachedLocalization(sfLocalization $localization)
29   {
30     $model_name = $localization->getModelName();
31     $model_id = $localization->getModelId();
32     self::$localizations[$model_name][$model_id] = $localization;
33   }
34  
35   /**
36    * Deactivate behavior
37    *
38    */
39   public static function disable()
40   {
41     self::$isActivated = false;
42   }
43  
44   /**
45    * Activate behavior
46    *
47    */
48   public static function enable()
49   {
50     self::$isActivated = true;
51   }
52  
53   /**
54    * Get a cached localization
55    *
56    * @param  string  $model_name
57    * @param  string  $model_id
58    * @return mixed
59    */
60   public static function getCachedLocalization($model_name, $model_id)
61   {
62     if (self::hasCachedLocalization($model_name, $model_id))
63     {
64       return self::$localizations[$model_name][$model_id];
65     }
66     else
67     {
68       return null;
69     }
70   }
71  
72   /**
73    * Checks if a cached version of a localization exists
74    *
75    * @param string $model_name
76    * @param string $model_id
77    * @return boolean
78    */
79   public static function hasCachedLocalization($model_name, $model_id)
80   {
81     return isset(self::$localizations[$model_name])
82         && isset(self::$localizations[$model_name][$model_id])
83         && self::$localizations[$model_name][$model_id] instanceof sfLocalization;
84   }
85  
86   /**
87    * Actions to perform after an object has been deleted
88    *
89    * @param BaseObject $object
90    * @return mixed
91    */
92   public function postDelete(BaseObject $object)
93   {
94     $model_name = get_class($object);
95     $model_id = $object->getPrimaryKey();
96     $c = new Criteria;
97     $c->add(sfLocalizationPeer::MODEL_NAME, $model_name);
98     $c->add(sfLocalizationPeer::MODEL_ID, $model_id);
99     return sfLocalizationPeer::doDelete($c);
100   }
101  
102   /**
103    * Retrieves coordinates array for given object
104    *
105    * @param BaseObject $object
106    * @return array
107    */
108   public function getCoordinates(BaseObject $object)
109   {
110     $localization = $object->getLocalization();
111     if (is_null($localization))
112     {
113       return null;
114     }
115     return array('latitude'  => $localization->getLatitude(),
116                  'longitude' => $localization->getLongitude());
117   }
118  
119   /**
120    * Retrieves latitude for given object
121    *
122    * @param BaseObject $object
123    * @return float
124    */
125   public function getLatitude(BaseObject $object)
126   {
127     $coord = $this->getCoordinates($object);
128     if (is_array($coord) && count($coord) > 0 && isset($coord['latitude']))
129     {
130       return $coord['latitude'];
131     }
132   }
133  
134   /**
135    * Retrieves longitude for given object
136    *
137    * @param BaseObject $object
138    * @return float
139    */
140   public function getLongitude(BaseObject $object)
141   {
142     $coord = $this->getCoordinates($object);
143     if (is_array($coord) && count($coord) > 0 && isset($coord['longitude']))
144     {
145       return $coord['longitude'];
146     }
147   }
148  
149   /**
150    * Retrieves BaseObjet location string, according to passed parameter
151    *
152    * @param BaseObject $object
153    * @return string
154    */
155   public static function getLocationString(BaseObject $object)
156   {
157     $location_getter = sfConfig::get(
158       sprintf('propel_behavior_sfPropelActAsLocalizableBehavior_%s_location_getter',
159               get_class($object)));
160     if (!method_exists($object, $location_getter))
161     {
162       throw new sfPropelActAsLocalizableBehaviorException(
163                   sprintf('Location getter method "%s::%s" does not exist',
164                           get_class($object),
165                           $location_getter));
166     }
167     return trim((string) call_user_func(array($object, $location_getter)));
168   }
169  
170   /**
171    * Retrieve sfLocalization associated to current object
172    *
173    * @param  BaseObject $object
174    * @param  boolean    $reload  Forces data refresh (with a db query)
175    * @return sfLocalization
176    */
177   public function getLocalization(BaseObject $object, $reload = false)
178   {
179     $model_name = get_class($object);
180     $model_id = $object->getPrimaryKey();
181     if (!self::hasCachedLocalization($model_name, $model_id) or $reload)
182     {
183       $localization = sfLocalizationPeer::getOrCreate($model_name, $model_id);
184       self::addCachedLocalization($localization);
185       return $localization;
186     }
187     else
188     {
189       return self::getCachedLocalization($model_name, $model_id);
190     }
191   }
192  
193   /**
194    * Update localization associated to object
195    *
196    * @param  BaseObject $object
197    * @param  string     $location
198    * @return mixed
199    */
200   public function updateLocalization(BaseObject $object, $location = null)
201   {
202     if (is_null($location))
203     {
204       $location = self::getLocationString($object);
205     }
206     
207     if (!$location)
208     {
209       return null;
210     }
211     
212     $client = new YahooGeocodingApiClient();
213     try
214     {
215       $coord = $client->setLocation($location)->query()->getFirst();
216       if (!$coord)
217       {
218         sfLogger::getInstance()->info(sprintf('No geolocalization result found for location "%s"',
219                                               $location));
220         return null;
221       }
222     }
223     catch (Exception $e)
224     {
225       sfLogger::getInstance()->warning(sprintf('Error while localizing location "%s": %s',
226                                                $location,
227                                                $e->getMessage()));
228       return null;
229     }
230     
231     $localization = $object->getLocalization();
232     $localization->setLatitude($coord['latitude']);
233     $localization->setLongitude($coord['longitude']);
234     $res = $localization->save();
235     self::addCachedLocalization($localization);
236     return $res;
237   }
238
239 }
240
Note: See TracBrowser for help on using the browser.