root/trunk/lib/model/Application.php

Revision 75, 7.7 kB (checked in by nperriault, 3 months ago)

[1.1]:

  • Companies and people modules migrated, with according form classes and templates, and functional test suite
  • Enhanced SymfoniansTestBrowser?
Line 
1 <?php
2 /**
3  * Subclass for representing a row from the 'applications' table.
4  *
5  * @package lib.model
6  */
7 class Application extends BaseApplication
8 {
9
10   /**
11    * Returns a string representation of current object
12    *
13    * @return string
14    */
15   public function __toString()
16   {
17     return $this->getName();
18   }
19
20   /**
21    * Fills current Application with array parameters, including tags
22    *
23    * @param  array   $arr
24    * @param  string  $keyType
25    */
26   public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME)
27   {
28     parent::fromArray($arr, $keyType);
29
30     foreach (array('tags', 'Tags') as $tagField)
31     {
32       if (array_key_exists($tagField, $arr))
33       {
34         $this->setTags($arr[$tagField]);
35       }
36     }
37   }
38
39   /**
40    * Converts current Application to an associative array
41    *
42    * @param  string $keyType
43    * @return array
44    */
45   public function toArray($keyType = BasePeer::TYPE_PHPNAME)
46   {
47     $arr = parent::toArray($keyType);
48
49     if (BasePeer::TYPE_FIELDNAME == $keyType)
50     {
51       $arr['tags'] = $this->getTagsString();
52     }
53     else if (BasePeer::TYPE_PHPNAME == $keyType)
54     {
55       $arr['Tags'] = $this->getTagsString();
56     }
57
58     return $arr;
59   }
60
61   /**
62    * Retrieves related companies for this app
63    *
64    * @param  Criteria  $c
65    * @return array
66    */
67   public function getCompanies(Criteria $c = null)
68   {
69     if (!$c instanceof Criteria)
70     {
71       $c = new Criteria;
72     }
73     $c->addJoin(ApplicationCompanyPeer::APPLICATION_ID, ApplicationPeer::ID);
74     $c->addJoin(ApplicationCompanyPeer::COMPANY_ID, CompanyPeer::ID);
75     $c->add(ApplicationPeer::ID, $this->getId());
76     $c->add(CompanyPeer::IS_ACTIVE, true);
77     return CompanyPeer::doSelect($c);
78   }
79
80   /**
81    * Retrieves related companies and return their names in a string
82    *
83    * @param  string  $separator
84    * @return string
85    */
86   public function getCompaniesString(Criteria $c = null, $separator = ', ')
87   {
88     if (!$c instanceof Criteria)
89     {
90       $c = new Criteria;
91     }
92     $companies = array();
93     foreach ($this->getCompanies($c) as $company)
94     {
95      $companies[] = $company->getName();
96     }
97     return implode($separator, $companies);
98   }
99
100   /**
101    * Returns app country name
102    *
103    * @return string
104    */
105   public function getCountryName()
106   {
107     if ($this->getCountry())
108     {
109       sfLoader::loadHelpers('I18N');
110       return format_country($this->getCountry());
111     }
112   }
113
114   /**
115    * Retrieves related developpers for this app
116    *
117    * @param  Criteria  $c
118    * @return array
119    */
120   public function getDevelopers(Criteria $c = null)
121   {
122     if (!$c instanceof Criteria)
123     {
124       $c = new Criteria;
125     }
126     $c->addJoin(ApplicationDeveloperPeer::APPLICATION_ID, ApplicationPeer::ID);
127     $c->addJoin(ApplicationDeveloperPeer::DEVELOPER_ID, sfGuardUserPeer::ID);
128     $c->add(ApplicationPeer::ID, $this->getId());
129     $c->add(sfGuardUserPeer::IS_ACTIVE, true);
130     return sfGuardUserPeer::doSelect($c);
131   }
132
133   /**
134    * Retrieves related developpers and return their names in a string
135    *
136    * @param  Criteria  $c
137    * @param  string    $separator
138    * @return string
139    */
140   public function getDevelopersString(Criteria $c = null, $separator = ', ')
141   {
142     if (!$c instanceof Criteria)
143     {
144       $c = new Criteria;
145     }
146     $devs = array();
147     foreach ($this->getDevelopers($c) as $developer)
148     {
149      $devs[] = $developer->getDisplayName();
150     }
151     return implode($separator, $devs);
152   }
153
154   /**
155    * Returns a string containing ap tags
156    *
157    * @param  string  $separator
158    * @return string
159    */
160   public function getTagsString($separator = ', ')
161   {
162     return implode($separator, $this->getTags());
163   }
164
165   /**
166    * Check if current app has given user as related developer
167    *
168    * @param  $user
169    * @return boolean
170    */
171   public function hasDeveloper($user)
172   {
173     if (is_null($user))
174     {
175       return false;
176     }
177     elseif ($user instanceof sfGuardUser)
178     {
179       $user = $user->getId();
180     }
181     elseif (!is_int($user))
182     {
183       throw new sfException('Impossible to check user from value '.
184                             var_export($user, true));
185     }
186     $c = new Criteria();
187     $c->add(ApplicationDeveloperPeer::DEVELOPER_ID, $user);
188     return ($this->countApplicationDevelopers($c) > 0);
189   }
190
191   /**
192    * Checks if current Application has a related Company
193    *
194    * @param  mixed   $company  Can be an integer or a BaseCompany object instance
195    * @return boolean
196    */
197   public function hasCompanyConnected($company)
198   {
199     if (!$company)
200     {
201       return false;
202     }
203
204     if ($company instanceof BaseCompany)
205     {
206       $company_id = $company->getId();
207     }
208     else
209     {
210       $company_id = $company;
211     }
212
213     $c = new Criteria;
214     $c->add(ApplicationCompanyPeer::COMPANY_ID, $company_id);
215
216     return ($this->countApplicationCompanys($c) > 0);
217   }
218
219   /**
220    * Checks if an application is owned by a given user
221    *
222    * @param int|sfGuardUser $user
223    */
224   public function isOwnedBy($user_id)
225   {
226     if ($user_id instanceof sfGuardUser)
227     {
228       $user_id = $user_id->getId();
229     }
230
231     return ($this->getSubmitterId() === $user_id);
232   }
233
234   /**
235    * Deletes app
236    *
237    * @param  Connection $con
238    * @return int
239    */
240   public function delete($con = null)
241   {
242     $this->removeAllTags();
243     $this->save();
244     $delete = parent::delete($con);
245     return $delete;
246   }
247
248   /**
249    * Saves app
250    *
251    * @param  Connection $con
252    * @return int
253    */
254   public function save($con = null)
255   {
256     $new = $this->isNew();
257     $modified = $this->isModified();
258     $save = parent::save($con);
259
260     // Create "app created" event object
261     if ($modified && $this->getSubmitterId())
262     {
263       $event = new Event();
264       $event->setSourceModel('sfGuardUser');
265       $event->setSourceSlug($this->getsfGuardUser()->getUsername());
266       $event->setSourceName($this->getsfGuardUser()->getDisplayName());
267       $event->setTargetModel('Application');
268       $event->setTargetSlug($this->getSlug());
269       $event->setTargetName($this->getName());
270       if ($new)
271       {
272         $event->setType('addition');
273       }
274       else
275       {
276         $event->setType('update');
277       }
278       $event->save();
279     }
280
281     return $save;
282   }
283
284   /**
285    * Sets the feed url value
286    *
287    * @param string $v
288    */
289   public function setFeedUrl($v)
290   {
291     if ('http://' == trim(strtolower($v)))
292     {
293       $v = null;
294     }
295
296     return parent::setFeedUrl($v);
297   }
298
299   /**
300    * Sets the homepage url value
301    *
302    * @param string $v
303    */
304   public function setHomepage($v)
305   {
306     if ('http://' == trim(strtolower($v)))
307     {
308       $v = null;
309     }
310
311     return parent::setHomepage($v);
312   }
313
314   /**
315    * Sets is_featured field and update according relations
316    *
317    * @param  boolean $flag
318    */
319   public function setIsFeatured($flag)
320   {
321     if ($flag && !$this->getIsFeatured())
322     {
323       // Flag all apps as unfeatured
324       $c = new Criteria();
325       $c->add(ApplicationPeer::IS_FEATURED, false);
326       ApplicationPeer::doUpdate($c);
327     }
328     parent::setIsFeatured($flag);
329   }
330
331   /**
332    * Sets application tags, only if application has already been saved
333    *
334    * @param array|string $tags  Tags for the application
335    */
336   public function setTags($tags)
337   {
338     if ($this->isNew())
339     {
340       return;
341     }
342
343     $this->removeAllTags();
344     $this->addTag($tags);
345   }
346
347 }
348
349 $columns_map = array('from' => ApplicationPeer::NAME,
350                      'to'   => ApplicationPeer::SLUG);
351
352 sfPropelBehavior::add('Application',
353                       array('sfPropelActAsSluggableBehavior' =>
354                             array('columns'   => $columns_map,
355                                   'separator' => '-',
356                                   'permanent' => true),
357                             'sfPropelActAsTaggableBehavior'));
358
359 //sfLucenePropelBehavior::getInitializer()->setupModel('Application');
Note: See TracBrowser for help on using the browser.