php8
[web.mtrack] / MTrackWeb / Watch.php
1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
3 /**
4  * watch code 
5  * - reponsilbe for
6  * a) listing watchers.
7  * 
8  * b) updateding watchers.
9  * 
10  */
11  
12 require_once 'MTrackWeb.php';
13 class MTrackWeb_Watch extends MTrackWeb
14 {
15     var $id; // 0 = new
16     var $issue = false;
17     var $preview;
18     var $error;
19     var $editable;
20     var $tid = 0;
21     
22     var $masterTemplate = 'watch.html';
23     
24     
25     
26     function get()
27     {
28   
29         // watcher list
30         // who is currently watching
31         if (!$this->authUser) {
32             $this->jerr("not authenticated");
33             return ''; // no subscriptin data available to public..
34         }
35         
36         
37         $this->objname = empty($_REQUEST['objname']) ? '' :   $_REQUEST['objname'];
38         $this->objid = empty($_REQUEST['objid']) ? '' : $_REQUEST['objid'];
39         
40         $this->objname = preg_replace('/[^a-z_]+/i', '', $this->objname );
41         $obj = DB_DataObject::factory($this->objname );
42         
43         // if it's a project watch.. do not do this..
44         if (!$obj->get($this->objid)) {
45             $this->jerr("invalid object");
46         }
47         if ($this->objname == 'Projects') {
48             if ($this->objid != $this->currentProject()) {
49                 $this->jerr("invalid project id");
50             }
51         } else {
52             if ($obj->project_id != $this->currentProject()) {
53                 $this->jerr("invalid project id on object");
54             }
55         }
56         
57         
58        // DB_DataObject::debugLevel(1);
59         $w = DB_DataObjecT::Factory('core_watch');
60         $w->ontable = $this->objname;
61         $w->onid = $this->objid;
62         
63         $w->autoJoin();
64        // $w->orderBy('Person.name ASC');
65         
66         $existing = $w->fetchAll();
67         
68         $this->selfsubscribe = true;
69         foreach($existing as $w) {
70             if ($this->authUser->id == $w->person_id) {
71                 $this->selfsubscribe = false;
72             }
73         }
74         $this->subscribers = $existing;
75         
76         if ($this->objname != 'Projects') {
77             $w = DB_DataObjecT::Factory('core_watch');
78             $w->ontable = 'Projects';
79             $w->onid = $this->currentProject();
80             $w->autoJoin();
81             $existing = $w->fetchAll();
82             
83             $this->selfsubscribe = true;
84             foreach($existing as $w) {
85                 if ($this->authUser->id == $w->person_id) {
86                     $this->selfsubscribe = false;
87                 }
88             }
89             $this->project_subscribers = $existing;
90
91         }
92         //print_r($existing);
93         /* 
94         require_once 'HTML/Template/Flexy/Element.php';
95         $this->elements['subscribe-add'] = new HTML_Template_Flexy_Element('select');
96         $this->elements['subscribe-add']->setOptions($users);
97         $this->addsubscribe = true;
98         if (count(array_keys($users)) == 1) {
99             $this->addsubscribe = false;
100         }
101         // never inherit..
102         $this->elements['subscribe-add']->setValue('');
103         */
104         
105         //$this->renderEvents();
106         
107     }
108      
109     function post()
110     {
111         
112         if (!$this->authUser) {
113             $this->jerr("not authenticated");
114             return ''; // no subscriptin data available to public..
115         }
116         
117         $table = empty($_REQUEST['objname']) ? '' : $_REQUEST['objname'];
118         $id = empty($_REQUEST['objid']) ? '' : $_REQUEST['objid'];
119         
120         if (empty($table) || empty($id)) {
121             die("invalid");
122         }
123         
124         if (empty($_REQUEST['userid'])) {
125             die("INVALID USER ID");
126         }
127         
128         if ($this->authUser->company()->comptype != 'OWNER') {
129             if ($_REQUEST['userid'] != $this->authUser->id) {
130                 die("INVALID REQUEST.");
131             }
132         }
133         $table = preg_replace('/[^a-z_]+/i', '', $table);
134         
135         // question ... who do we allow to watch what..
136         $o = DB_DataObject::factory($table);
137         if (!is_a($o, 'DB_DataObject')) {
138             die('invalid table');
139         }
140         $o->get((int)$id);
141         if ($table != 'Projects') {
142             if (empty($o->project_id) || $o->project_id != $this->currentProject()) {
143                 die("invalid - project id does not match");
144                 // unless it's a project..
145             }
146         }  else {
147             if ($id != $this->currentProject()) {
148                 die("invalid - project id does not match");
149             }
150         }
151         
152         
153         
154         $w = DB_DataObjecT::Factory('core_watch');
155         $w->ontable = $table;
156         $w->onid = $id;
157         $w->person_id = $_REQUEST['userid'];
158         $w->medium = 'email';
159         if (!$w->count()) {
160             $w->insert();
161         }
162          
163         return $this->get(); 
164         // carry on and show get(..
165     }
166      
167 }