root/trunk/lib/model/Event.php

Revision 49, 3.7 kB (checked in by nperriault, 6 months ago)

Resolved the context "default" does not exist bug by removing calls to sfContext in the model.

Line 
1 <?php
2 /**
3  * Subclass for representing a row from the 'event' table.
4  *
5  * @package lib.model
6  */
7 class Event extends BaseEvent
8 {
9
10   public
11     $source_object = null,
12     $target_object = null;
13  
14   protected static $allowed_types = array(
15     'addition',
16     'connection',
17     'join',
18     'recommendation_addition',
19     'recommendation_update',
20     'relation',
21     'update',
22     'update_profile'
23   );
24  
25   /**
26    * Exclude admin event type handling for this event types
27    *
28    * @var array
29    */
30   protected static $admin_notif_exclude_for_types = array(
31     'addition',
32     'update_profile',
33     'relation',
34     'recommendation_addition',
35     'connection'
36   );
37  
38   /**
39    * Returns an array of allowed types
40    *
41    * @return array
42    */
43   public static function getAllowedTypes()
44   {
45     return self::$allowed_types;
46   }
47  
48   /**
49    * Retrieves related source object
50    *
51    * @return mixed
52    */
53   public function getSourceObject()
54   {
55     if (!$this->getSourceModel() or !$this->getSourceSlug())
56     {
57       $this->source_object = null;
58     }
59     else if (is_null($this->source_object))
60     {
61       $this->source_object = EventPeer::retrieveSourceObject($this->getSourceModel(), $this->getSourceSlug());
62     }
63     return $this->source_object;
64   }
65  
66   /**
67    * Retrieves related target object
68    *
69    * @return mixed
70    */
71   public function getTargetObject()
72   {
73     if (!$this->getTargetModel() or !$this->getTargetSlug())
74     {
75       $this->target_object = null;
76     }
77     else if (is_null($this->target_object))
78     {
79       $this->target_object = EventPeer::retrieveTargetObject($this->getTargetModel(), $this->getTargetSlug());
80     }
81     return $this->target_object;
82   }
83  
84   /**
85    * Checks if event has at least one duplicate entry in a given period of time
86    *
87    * @param  int      $period   Period, in seconds
88    * @return boolean
89    */
90   public function hasRecentDuplicate($period = 300)
91   {
92     $c = new Criteria();
93     $c->add(EventPeer::TYPE, $this->getType());
94     $c->add(EventPeer::SOURCE_MODEL, $this->getSourceModel());
95     $c->add(EventPeer::SOURCE_SLUG, $this->getSourceSlug());
96     if (!is_null($this->getTargetModel()))
97     {
98       $c->add(EventPeer::TARGET_MODEL, $this->getTargetModel());
99       $c->add(EventPeer::TARGET_SLUG, $this->getTargetSlug());
100     }
101     $c->add(EventPeer::OCCURED_AT, date('Y-m-d H:i:s', time() - $period), Criteria::GREATER_THAN);
102     return (EventPeer::doCount($c) > 0);
103   }
104  
105   /**
106    * Sets the Event type
107    *
108    * @param string  $type
109    */
110   public function setType($type)
111   {
112     $type = trim(strtolower($type));
113     if (!in_array($type, self::getAllowedTypes()))
114     {
115       throw new sfException(sprintf('"%s" is not a valid event type', $type));
116     }
117     parent::setType($type);
118   }
119  
120   /**
121    * Saved Event object
122    * FIXME: I don't like having to deal with session in the model
123    *
124    * @param  Connection $con
125    * @return int
126    */
127   public function save($con = null)
128   {
129     if (!$this->getType())
130     {
131       //sfContext::getInstance()->getLogger()->err('No type specified for event');
132       return 0;
133     }
134     
135     if ($this->hasRecentDuplicate(sfConfig::get('app_events_check_duplicates_period', 300)))
136     {
137       return 0;
138     }
139     
140     $this->setOccuredAt(time());
141     /*$context = @sfContext::getInstance();
142     if (!is_null($context))
143     {
144       $session = @$context->getUser();
145       $session_exists = !is_null($context->getStorage()) && !is_null($session);
146       $no_admin_notification = in_array($this->getType(), self::$admin_notif_exclude_for_types);
147       if ($session_exists && $session->isAdmin())
148       {
149         if (!$no_admin_notification)
150         {
151           $this->setIsAdmin(true);
152         }
153       }
154     }*/
155     return parent::save($con);
156   }
157  
158 }
159
Note: See TracBrowser for help on using the browser.