]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Trackers/BreadCrumbStack.php
Release 6.5.0
[Github/sugarcrm.git] / modules / Trackers / BreadCrumbStack.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38
39 class BreadCrumbStack {
40
41    /**
42         * Maintain an ordered list of items in the breadcrumbs
43         *
44         * @var unknown_type
45         */
46    private $stack;
47    /**
48     * Maps an item_id to the position index in stack
49     *
50     * @var unknown_type
51     */
52    private $stackMap;
53    /**
54     * Boolean flag to determine whether or not entries not visible should be removed
55     * 
56     * @var 
57     */
58    private $deleteInvisible = false;
59    
60    
61    /**
62     * BreadCrumbStack
63     * Constructor for BreadCrumbStack that builds list of breadcrumbs using tracker table
64     * 
65     * @param $user_id String value of user id to get bread crumb items for
66     * @param $modules mixed value of module name(s) to provide extra filtering
67     */
68    public function BreadCrumbStack($user_id, $modules='') {
69       $this->stack = array();
70       $this->stackMap = array();
71       
72       $admin = new Administration();
73           $admin->retrieveSettings('tracker');      
74  
75       $this->deleteInvisible = !empty($admin->settings['tracker_Tracker']);
76       $db = DBManagerFactory::getInstance();
77       
78       $module_query = '';
79       if(!empty($modules)) {
80          $history_max_viewed = 10;
81          $module_query = is_array($modules) ? ' AND module_name IN (\'' . implode("','" , $modules) . '\')' :  ' AND module_name = \'' . $modules . '\'';
82       } else {
83          $history_max_viewed = (!empty($GLOBALS['sugar_config']['history_max_viewed']))? $GLOBALS['sugar_config']['history_max_viewed'] : 50;
84       }         
85       
86       $query = 'SELECT distinct item_id AS item_id, id, item_summary, module_name, monitor_id, date_modified FROM tracker WHERE user_id = \'' . $user_id . '\' AND deleted = 0 AND visible = 1 ' . $module_query . ' ORDER BY date_modified DESC';      
87       $result = $db->limitQuery($query, 0, $history_max_viewed);
88       $items = array();
89       while(($row = $db->fetchByAssoc($result))) {           
90                 $items[] = $row;
91       }
92       $items = array_reverse($items);
93       foreach($items as $item) {
94           $this->push($item);
95       }
96    }
97    
98    /**
99     * contains
100     * Returns true if the stack contains the specified item_id, false otherwise.
101     * 
102     * @param item_id the item id to search for
103     * @return id of the first item on the stack
104     */
105    public function contains($item_id) {
106                 if(!empty($this->stackMap)){
107                         return array_key_exists($item_id, $this->stackMap);
108                 }else
109                         return false;
110    }
111    
112    /**
113     * Push an element onto the stack.
114     * This will only maintain a list of unique item_ids, if an item_id is found to 
115     * already exist in the stack, we want to remove it and update the database to reflect it's
116     * visibility.
117     *
118     * @param array $row - a trackable item to store in memory
119     */
120    public function push($row) {
121           if(is_array($row) && !empty($row['item_id'])) {
122                   if($this->contains($row['item_id'])) {
123                         //if this item already exists in the stack then update the found items
124                         //to visible = 0 and add our new item to the stack
125                         $item = $this->stack[$this->stackMap[$row['item_id']]];
126                         if(!empty($item['id']) && $row['id'] != $item['id']){
127                                 $this->makeItemInvisible($item['id'], 0);
128                         }
129                         $this->popItem($item['item_id']);
130                   }
131                   //If we reach the max count, shift the first element off the stack
132                   $history_max_viewed = (!empty($GLOBALS['sugar_config']['history_max_viewed']))? $GLOBALS['sugar_config']['history_max_viewed'] : 50;
133
134                   if($this->length() >= $history_max_viewed) {
135                         $this->pop();
136                   }
137                   //Push the element into the stack
138                   $this->addItem($row);
139           }
140    }
141    
142    /**
143     * Pop an item off the stack
144     *
145     */
146    public function pop(){
147                 $item = array_shift($this->stack);
148                 if(!empty($item['item_id']) && isset($this->stackMap[$item['item_id']])){
149                         unset($this->stackMap[$item['item_id']]);
150                         $this->heal();
151                 }
152    }
153    
154    /**
155     * Change the visibility of an item
156     *
157     * @param int $id
158     */
159    private function makeItemInvisible($id){
160             if($this->deleteInvisible) {
161               $query = "DELETE FROM tracker where id = '{$id}'";
162             } else {
163                   $query = "UPDATE tracker SET visible = 0 WHERE id = '{$id}'";
164             }
165         $GLOBALS['db']->query($query, true);
166    }
167    
168    /**
169     * Pop an Item off the stack. Call heal to reconstruct the indices properly
170     *
171     * @param string $item_id - the item id to remove from the stack
172     */
173    public function popItem($item_id){
174                 if(isset($this->stackMap[$item_id])){
175                         $idx = $this->stackMap[$item_id];
176                         unset($this->stack[$idx]);
177                         unset($this->stackMap[$item_id]);
178                         $this->heal();
179                 }
180    }
181    
182    /**
183     * Add an item to the stack
184     *
185     * @param array $row - the row from the db query
186     */
187    private function addItem($row){
188                 $this->stack[] = $row;
189                 $this->stackMap[$row['item_id']] = ($this->length() - 1);
190    }
191    
192    /**
193     * Once we have removed an item from the stack we need to be sure to have the 
194     * ids and indices match up properly.  Heal takes care of that.  This method should only 
195     * be called when an item_id is already in the stack and needs to be removed
196     *
197     */
198    private function heal(){
199                 $vals = array_values($this->stack);
200                 $this->stack = array();
201                 $this->stackMap = array();
202                 foreach($vals as $key => $val){
203                         $this->addItem($val);
204                 }
205    }
206    
207    /**
208     * Return the number of elements in the stack
209     *
210     * @return int - the number of elements in the stack
211     */
212    public function length(){
213                 return count($this->stack);
214    }
215    
216    /**
217     * Return the list of breadcrubmbs currently in memory
218     *
219     * @return array of breadcrumbs
220     */
221    public function getBreadCrumbList($filter_module='') {
222           if(!empty($filter_module)) {
223                  $s2 = array();
224                  if(is_array($filter_module)) {
225                          foreach($this->stack as $entry) {
226                             if(in_array($entry['module_name'], $filter_module)) {
227                                $s2[$entry['item_id']] = $entry;
228                             }
229                          }                      
230                  } else {
231                          foreach($this->stack as $entry) {
232                             if($entry['module_name'] == $filter_module) {
233                                $s2[$entry['item_id']] = $entry;
234                             }
235                          }
236                  }
237                  
238                  $s2 = array_reverse($s2);
239              if(count($s2) > 10) {
240                         $s2 = array_slice($s2, 0, 10);
241                  }
242                  return $s2;             
243           }
244           
245           $s = $this->stack;
246           $s = array_reverse($s);
247           if(count($s) > 10) {
248                  $s = array_slice($s, 0, 10);
249           }
250       return $s;
251    }
252 }
253
254 ?>