DataObjects/Core_watch.php
[Pman.Core] / DataObjects / Core_watch.php
index 45c4038..9a40ce4 100644 (file)
  *   in which case it should create a core_notify event.
  *
  *
+ * Should 'event' trigger this..
+ *   -> eg. somebody makes a 'EDIT' on 'person'
+ *   -> a watch exists for
+ *        ontable=person,
+ *        onid = -1 <<-- every entry..
+ *        person_id -> who is goes to.
+ *        event = CRUD (eg. shortcut for edit/create/delete)
+ *        medium = "REVIEW" << eg. review needed..
+ *        
+ *
+ *  if onid == 0 then it will monitor all changes to that table..
+ *
+ *
+ * 
  * 
  */
 require_once 'DB/DataObject.php';
@@ -20,11 +34,12 @@ class Pman_Core_DataObjects_Core_watch extends DB_DataObject
     /* the code below is auto generated do not remove the above tag */
 
     public $__table = 'core_watch';                      // table name
-    public $ontable;                         // string(128)  not_null primary_key
-    public $onid;                            // int(11)  not_null primary_key
-    public $person_id;                         // int(11)  not_null primary_key
-    public $event;                           // string(128)  not_null primary_key
-    public $medium;                          // string(128)  not_null primary_key
+    public $id;                              // int(11)  not_null primary_key auto_increment
+    public $ontable;                         // string(128)  not_null
+    public $onid;                            // int(11)  not_null
+    public $person_id;                       // int(11)  not_null
+    public $event;                           // string(128)  not_null
+    public $medium;                          // string(128)  not_null
     public $active;                          // int(11)  not_null
 
     
@@ -32,9 +47,15 @@ class Pman_Core_DataObjects_Core_watch extends DB_DataObject
     ###END_AUTOCODE
     /** make sure there is a watch for this user.. */
     
+    /**
+     *
+     * Create a watch...
+     *
+     */
+    
     function ensureNotify(  $ontable, $onid, $person_id, $whereAdd)
     {
-        DB_DAtaObject::debugLevel(1);
+        //DB_DAtaObject::debugLevel(1);
         $w = DB_DataObject::factory('core_watch');
         $w->person_id = $person_id;
         if (empty($w->person_id)) {
@@ -54,88 +75,177 @@ class Pman_Core_DataObjects_Core_watch extends DB_DataObject
         $nw->medium = 'email';
         $nw->active = 1;
         $nw->insert();
-        
-        
+         
     }
-    
-    function notify($ontable , $onid, $whereAdd)
+    /**
+     * Generate a notify event based on watches (matching whereAdd)
+     *
+     * Usage: $core_watch->notify('mtrack_repos', 1, false, date()
+     *
+     * This can match any 'event' type - eg. it can be blank etc...
+     *   Generally used by non-event driven notifications, like our
+     *   Daily commit message.
+     *
+     *  @param string $ontable - the database table that has been updated/changed etc.
+     *  @param int    $onid    - the id of the row changed
+     *  @param string  $whereAdd (optiona) - a DB whereAdd() condition to filter the search for watches
+     *  @param datetime    $when   (default now)  - date/time to create the notification for (Eg. end of day..)
+     * 
+     */
+    function notify($ontable , $onid, $whereAdd = false, $when=false)
     {
-        $w = DB_DataObject::factory('core_watch');
-        $w->whereAdd($whereAdd);
-        $w->selectAdd();
-        $w->selectAdd('distinct(person_id) as person_id');
-        $people = $w->fetchAll('person_id');
-        $nn = DB_DataObject::Factory('core_notify');
-        $nn->ontable = $ontable;
-        $nn->onid = $onid;
-        foreach($people as $p) {
-            if (!$p) { // no people??? bugs in watch table
+        $w = DB_DataObject::factory('core_watch');   
+        if ($whereAdd !== false) { 
+            $w->whereAdd($whereAdd  );
+        }
+        $w->active =1;
+        
+        $w->whereAdd('onid = 0 OR onid='. ((int) $onid));
+       
+        
+        $w->ontable = $ontable;
+        //$w->selectAdd();
+        //$w->selectAdd('distinct(person_id) as person_id');
+        
+        foreach($w->fetchAll() as $w) { 
+            if (!$w->person_id) { // no people??? bugs in watch table
                 continue;
             }
-            $n = clone($nn);
-            $n->person_id = $p;
-            $nf = clone($n);
-            $nf->whereAdd('sent < act_when');
+         
+         
+         
+            $nn = DB_DataObject::Factory('core_notify');
+            $nn->ontable = $ontable;
+            $nn->onid = $onid;
+            $nn->evtype = $w->medium;
+            $nn->person_id = $w->person_id;
+            
+            $nf = clone($nn);
+            $nf->whereAdd("sent < '2000-01-01'");
             if ($nf->count()) {
                 // we have a item in the queue for that waiting to be sent..
                 continue;
             }
-            $n->act_when = date("Y-m-d H:i:s");
-            $n->insert();
-            
-            
+            $nn->act_start( date("Y-m-d H:i:s", $when !== false ? strtotime($when) : time()) );
+            $nn->insert();
         }
-        
-        
-        
+          
     }
-    /***
-     * The purpose of this is to gather all the events that have
-     * occured in the system (where watches exist)
-     * Do we want to send the user 1 email ?? or multiple...
-     * --> I guess multiple emails..
-     *
-     * so we need to return
-     *
-     *  array(
-          $USER_ID => array(
-                $OBJECT:$ID, $OBJECT:$ID, $OBJECT:$ID, .....
-          )
-     * )
+    // static really...
+    /**
      *
-     * The mailer can then go through and call each object ??
+     * This get's called by roo->jok()
      *
+     * And searches for matching '$watch->event' == $event->action
+     *  along with id/table etc..
      *
-     * -- Things we can watch..
-     *
-     * mtrack_change <- this is a neat log of all events.
-     *  which logs these things
-     *     Individual Ticket changes (already)
-     *     a Project -> which means ticket changes... which again can be discovered via mtrack_changes..
-     *     a Repo for Commits (-- which will be handled by mtrack_changes)
-     *     Wiki changes.. later...
-     *     
+     * it's basic usage is to fill in core_notify after an event has happend.
      *
+     * We can also use it to notify other modules if something has happend.
+     *  = eg. mtrack_ticket * watch will notify mtrack_jira::
      *
+     * @param Pman_Core_DataObject_Events $event - the Pman event dataobject that was created
+     * 
      */
     
-    function watched($medium, $watcher = null)
+    function notifyEvent($event)
     {
-        $w = DB_DataObject::factory('core_watch');
-        if ($watcher) {
-            $w->person_id = $watcher;
+        //DB_DataObject::DebugLevel(1);
+        // see if there are any watches on events..
+        // notify everyone flagged except the person doing it...
+        // this is very basic logic... -
+        //    if more intelligence is needed...
+        //    then it 'rules' will have to be added by the watched object....
+        //    anyway leave that for another day..
+        if (empty($event->action)) {
+            return;
         }
+        $w = DB_DataObject::factory('core_watch');
+        $w->ontable = $event->on_table;
+        $w->whereAdd('onid = 0 OR onid='. ((int) $event->on_id));
+       
+        $w->event  = $event->action;
         $w->active = 1;
-        $w->medium = $medium;
-        $ar = $w->fetchAll();
-        $ret = array();
-        foreach($ar as $o) {
-            if (!isset($ret[$o->person_id])) {
-                $ret[$o->person_id] = array();
+        
+        
+        $w->whereAdd('person_id != '. (int) $event->person_id);
+        $watches = $w->fetchAll();
+        
+      print_R($watches);exit;
+        
+        $nn = DB_DataObject::Factory('core_notify');
+        $nn->ontable    = $event->on_table;
+        $nn->onid       = $event->on_id;
+        
+        foreach($watches as $watch) {
+            $n = clone($nn);
+            if (!$watch->person_id) { // no people??? bugs in watch table
+                $dom = explode(':',$watch->medium);
+                if (count($dom) != 2) {
+                    continue;
+                }
+                // in some scenarios (like watching for new articles)
+                // we need to create a core, notify on the medium..
+                // in which case we set the  set $nn->evtype = medium..
+                // in that case - just let the called method generate the notify..
+                
+                
+                $do = DB_DataObject::factory($dom[0]);
+                if (!method_exists($do,$dom[1])) {
+                    continue;
+                }
+                // the triggered method, can either do something
+                // or modify the notify event..
+                if ($do->{$dom[1]}($event, $n) !== false) {
+                    continue;
+                }
+                
+                
             }
-            $ret[$o->person_id][] = $o->ontable .':'. $o->onid;
+            
+            
+            $n->trigger_person_id = $event->person_id;
+            $n->trigger_event_id = $event->id;
+            $n->person_id = $watch->person_id;
+            $n->watch_id =  $watch->id;
+            
+            // does this watch already have a flag...
+            $nf = clone($n);
+            $nf->whereAdd("sent < '2000-01-01'");
+            //$nf->whereAdd('sent < act_when');
+            if ($nf->count()) {
+                // we have a item in the queue for that waiting to be sent..
+                continue;
+            }
+            
+            $n->act_start( date("Y-m-d H:i:s") );
+            $n->insert();
+            
+            
         }
         
-        return $ret;
+        
+        
     }
+    function initDatabase($roo, $data) {
+        foreach($data as $d) {
+            $dd = $d;
+            if (isset($dd['active'])) {
+                unset($dd['active']);
+            }
+            $t = DB_DataObject::Factory($this->tableName());
+            $t->setFrom($dd);
+            if ($t->find(true)) {
+                continue;
+            }
+            $t = DB_DataObject::Factory($this->tableName());
+            $t->setFrom($d);
+            $t->insert();
+            
+            
+        }
+    }
+    
+     
 }