root/trunk/lib/model/Company.php

Revision 75, 5.1 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 'company' table.
4  *
5  * @package lib.model
6  */
7 class Company extends BaseCompany
8 {
9
10   /**
11    * Returns company object string representation
12    *
13    * @return string
14    */
15   public function __toString()
16   {
17     return $this->getName();
18   }
19
20   /**
21    * Retrieves related applications
22    *
23    * @param  Criteria  $c
24    * @return array
25    */
26   public function getApplications(Criteria $c = null)
27   {
28     if (!$c instanceof Criteria)
29     {
30       $c = new Criteria;
31     }
32     $c->addJoin(ApplicationCompanyPeer::APPLICATION_ID, ApplicationPeer::ID);
33     $c->addJoin(ApplicationCompanyPeer::COMPANY_ID, CompanyPeer::ID);
34     $c->add(CompanyPeer::ID, $this->getId());
35     return ApplicationPeer::doSelect($c);
36   }
37
38   /**
39    * Retrieves related applications and return their names in a string
40    *
41    * @param  string  $separator
42    * @return string
43    */
44   public function getApplicationsString(Criteria $c = null, $separator = ', ')
45   {
46     if (!$c instanceof Criteria)
47     {
48       $c = new Criteria;
49     }
50     $applications = array();
51     foreach ($this->getApplications($c) as $application)
52     {
53      $applications[] = $application->getName();
54     }
55     return implode($separator, $applications);
56   }
57
58   /**
59    * Returns company country name
60    *
61    * @return string
62    */
63   public function getCountryName()
64   {
65     if ($this->getCountry())
66     {
67       sfLoader::loadHelpers('I18N');
68       return format_country($this->getCountry());
69     }
70   }
71
72   /**
73    * Retrieves related people for this company
74    *
75    * @param  Criteria  $c
76    * @return array
77    */
78   public function getPeople(Criteria $c = null)
79   {
80     if (!$c instanceof Criteria)
81     {
82       $c = new Criteria;
83     }
84     $c->addJoin(CompanyPersonPeer::COMPANY_ID, CompanyPeer::ID);
85     $c->addJoin(CompanyPersonPeer::USER_ID, sfGuardUserPeer::ID);
86     $c->add(CompanyPeer::ID, $this->getId());
87     $c->add(sfGuardUserPeer::IS_ACTIVE, true);
88     return sfGuardUserPeer::doSelect($c);
89   }
90
91   /**
92    * Retrieves related people and return their names in a string
93    *
94    * @param  Criteria  $c
95    * @param  string    $separator
96    * @return string
97    */
98   public function getPeopleString(Criteria $c = null, $separator = ', ')
99   {
100     if (!$c instanceof Criteria)
101     {
102       $c = new Criteria;
103     }
104     $people = array();
105     foreach ($this->getPeople($c) as $person)
106     {
107      $people[] = $person->getDisplayName();
108     }
109     return implode($separator, $people);
110   }
111
112   /**
113    * Returns full address in one line
114    *
115    * @return string
116    */
117   public function getFullAddress()
118   {
119     sfLoader::loadHelpers('I18N');
120     $address= sprintf('%s %s %s %s, %s (%s)',
121                       $this->getAddress(),
122                       $this->getZip(),
123                       $this->getCity(),
124                       $this->getState(),
125                       format_country($this->getCountry()),
126                       $this->getName());
127     return SymfoniansToolkit::onelinize($address);
128   }
129
130   /**
131    * Checks if company has an address
132    *
133    * @return string
134    */
135   public function hasAddress()
136   {
137     return $this->getCountry() && $this->getCity();
138   }
139
140   /**
141    * Checks if company is related to given person
142    *
143    * @param  mixed $user
144    * @return boolean
145    */
146   public function hasMember($user)
147   {
148     if (is_null($user))
149     {
150       return false;
151     }
152     elseif ($user instanceof sfGuardUser)
153     {
154       $user = $user->getId();
155     }
156     elseif (!is_int($user))
157     {
158       throw new sfException('Impossible to check user from value '.
159                             var_export($user, true));
160     }
161     $c = new Criteria();
162     $c->add(CompanyPersonPeer::USER_ID, $user);
163     return ($this->countCompanyPersons($c) > 0);
164   }
165
166   /**
167    * Saves Company instance
168    *
169    * @param  Connection $con
170    * @return int
171    */
172   public function save($con = null)
173   {
174     $new = $this->isNew();
175     $modified = $this->isModified();
176     $affectedRows = parent::save($con);
177
178     // Create "company created" event object
179     if ($modified)
180     {
181       $event = new Event();
182       $event->setSourceModel('sfGuardUser');
183       $event->setSourceSlug($this->getsfGuardUser()->getUsername());
184       $event->setSourceName($this->getsfGuardUser()->getDisplayName());
185       $event->setTargetModel('Company');
186       $event->setTargetSlug($this->getSlug());
187       $event->setTargetName($this->getName());
188       if ($new)
189       {
190         $event->setType('addition');
191       }
192       else
193       {
194         $event->setType('update');
195       }
196       $event->save();
197     }
198
199     return $affectedRows;
200   }
201
202 }
203
204 $columns_map = array('from' => CompanyPeer::NAME,
205                      'to'   => CompanyPeer::SLUG);
206
207 sfPropelBehavior::add('Company',
208                       array('sfPropelActAsSluggableBehavior' =>
209                               array('columns'   => $columns_map,
210                                     'separator' => '-',
211                                     'permanent' => true),
212                             'sfPropelActAsLocalizableBehavior' =>
213                               array('location_getter' => 'getFullAddress')));
214
215 //sfLucenePropelBehavior::getInitializer()->setupModel('Company');
Note: See TracBrowser for help on using the browser.