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-2011 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
38 /*********************************************************************************
41 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc. All Rights
42 * Reserved. Contributor(s): ______________________________________..
43 *********************************************************************************/
45 require_once("include/ytree/Tree.php");
46 require_once("include/ytree/ExtNode.php");
49 * Polymorphic buckets - place any item in a folder
57 var $has_child = 0; // flag node has child
60 var $dynamic_query = '';
70 var $new_with_id = false;
73 var $core = "SELECT f.id, f.name, f.has_child, f.is_group, f.is_dynamic, f.dynamic_query, f.folder_type, f.created_by, i.deleted FROM folders f left join inbound_email i on f.id = i.groupfolder_id ";
74 var $coreSubscribed = "SELECT f.id, f.name, f.has_child, f.is_group, f.is_dynamic, f.dynamic_query, f.folder_type, f.created_by, i.deleted FROM folders f LEFT JOIN folders_subscriptions fs ON f.id = fs.folder_id LEFT JOIN inbound_email i on i.groupfolder_id = f.id ";
75 var $coreWhere = "WHERE f.deleted = 0 ";
76 var $coreWhereSubscribed = "WHERE f.deleted = 0 AND fs.assigned_user_id = ";
77 var $coreOrderBy = " ORDER BY f.is_dynamic, f.is_group, f.name ASC ";
79 var $hrSortLocal = array(
81 'status' => 'reply_to_status',
82 'from' => 'emails_text.from_addr',
84 'date' => 'date_sent',
85 'AssignedTo' => 'assigned_user_id',
86 'flagged' => 'flagged'
88 var $defaultSort = 'date';
89 var $defaultDirection = "DESC";
97 function SugarFolder() {
98 $this->db = DBManagerFactory::getInstance();
101 function deleteEmailFromAllFolder($id) {
102 $q = "delete from folders_rel where polymorphic_module = 'Emails' AND polymorphic_id = '{$id}' ";
103 $r = $this->db->query($q);
106 function deleteEmailFromFolder($id) {
107 $q = "delete from folders_rel where polymorphic_module = 'Emails' AND polymorphic_id = '{$id}' AND folder_id = '{$this->id}'";
108 $r = $this->db->query($q);
111 function checkEmailExistForFolder($id) {
112 $q = "SELECT COUNT(*) c from folders_rel where polymorphic_module = 'Emails' AND polymorphic_id = '{$id}' AND folder_id = '{$this->id}'";
113 $r = $this->db->query($q);
114 $a = $this->db->fetchByAssoc($r);
122 * Moves beans from one folder to another folder
123 * @param string fromFolder GUID of source folder
124 * @param string toFolder GUID of destination folder
125 * @param string beanId GUID of SugarBean being moved
127 function move($fromFolder, $toFolder, $beanId) {
128 $q = "UPDATE folders_rel SET folder_id = '{$toFolder}' WHERE folder_id = '{$fromFolder}' AND polymorphic_id = '{$beanId}' AND deleted = 0";
129 $r = $this->db->query($q);
133 * Copies one bean from one folder to another
135 function copyBean($fromFolder, $toFolder, $beanId, $module) {
136 $q = "INSERT INTO folders_rel (id, folder_id, polymorphic_module, polymorphic_id, deleted)
137 VALUES('{$guid}', '{$toFolder}', '{$module}', '{$beanId}', 0)";
138 $r = $this->db->query($q);
142 * Creates a new group Folder from the passed fields
143 * @param array fields
145 function setFolder($fields) {
147 global $current_user;
148 if(empty($fields['groupFoldersUser'])) {
149 $fields['groupFoldersUser'] = $current_user->id;
152 $this->name = $fields['name'];
153 $this->parent_folder = $fields['parent_folder'];
154 $this->has_child = 0;
156 $this->assign_to_id = $fields['groupFoldersUser'];
162 * Returns GUIDs of folders that the user in focus is subscribed to
163 * @param object user User object in focus
166 function getSubscriptions($user) {
168 global $current_user;
169 $user = $current_user;
172 $q = "SELECT folder_id FROM folders_subscriptions WHERE assigned_user_id = '{$user->id}'";
173 $r = $this->db->query($q);
175 while($a = $this->db->fetchByAssoc($r)) {
176 $ret[] = $a['folder_id'];
182 * Sets a user's preferences for subscribe folders (Sugar only)
183 * @param array subs Array of IDs for subscribed folders
185 function setSubscriptions($subs) {
186 global $current_user;
188 if(empty($current_user->id)) {
189 $GLOBALS['log']->fatal("*** FOLDERS: tried to update folder subscriptions for a user with no ID");
193 $cleanSubscriptions = array();
195 // ensure parent folders are selected, regardless.
196 foreach($subs as $id) {
199 $cleanSubscriptions[] = $id;
200 $qChk = "SELECT parent_folder FROM folders WHERE id = '{$id}'";
201 $rChk = $this->db->query($qChk);
202 $aChk = $this->db->fetchByAssoc($rChk);
204 if(!empty($aChk['parent_folder'])) {
205 $cleanSubscriptions = $this->getParentIDRecursive($aChk['parent_folder'], $cleanSubscriptions);
210 $this->clearSubscriptions();
212 foreach($cleanSubscriptions as $id) {
213 $this->insertFolderSubscription($id, $current_user->id);
218 * Given a folder id and user id, create a folder subscription entry.
220 * @param String $folderId
221 * @param String $userID
222 * @return String The id of the newly created folder subscription.
224 function insertFolderSubscription($folderId, $userID)
226 $guid = create_guid();
227 $query = "INSERT INTO folders_subscriptions (id, folder_id, assigned_user_id) VALUES ('{$guid}', '{$folderId}', '{$userID}')";
228 $r = $this->db->query($query);
232 * Recursively finds parent node until it hits root
233 * @param string id Starting id to follow up
234 * @param array ret collected ids
235 * @return array of IDs
237 function getParentIDRecursive($id, $ret=array()) {
238 $q = "SELECT * FROM folders WHERE id = '{$id}' AND deleted = 0";
239 $r = $this->db->query($q);
240 $a = $this->db->fetchByAssoc($r);
242 if(!in_array($id, $ret)) {
246 if($a['parent_folder'] != '') {
247 $qChk = "SELECT parent_folder FROM folders WHERE id = '{$id}'";
248 $rChk = $this->db->query($qChk);
249 $aChk = $this->db->fetchByAssoc($rChk);
251 if(!empty($aChk['parent_folder'])) {
252 $ret = $this->getParentIDRecursive($aChk['parent_folder'], $ret);
260 * Deletes subscriptions to folders in preparation for reset
262 function clearSubscriptions() {
263 global $current_user;
265 if(!empty($current_user->id)) {
266 $q = "DELETE FROM folders_subscriptions WHERE assigned_user_id = '{$current_user->id}'";
267 $r = $this->db->query($q);
273 * Deletes all subscriptions for a particular folder id
277 function clearSubscriptionsForFolder($folderID)
279 $query = "DELETE FROM folders_subscriptions WHERE folder_id = '$folderID'";
280 $r = $this->db->query($query);
283 function generateSugarsDynamicFolderQuery() {
284 global $current_user;
285 $type = $this->folder_type;
287 if($type == "sent") {
290 if($type == 'inbound') {
291 $ret = " AND status NOT IN ('sent', 'archived', 'draft') AND type NOT IN ('out', 'archived', 'draft')";
293 $ret = " AND status NOT IN ('archived') AND type NOT IN ('archived')";
295 $q = "SELECT emails.id , emails.name, emails.date_sent, emails.status, emails.type, emails.flagged, emails.reply_to_status, emails_text.from_addr, emails_text.to_addrs, 'Emails' polymorphic_module FROM emails" .
296 " JOIN emails_text on emails.id = emails_text.email_id
297 WHERE (type = '{$type}' OR status = '{$status}') AND assigned_user_id = '{$current_user->id}' AND emails.deleted = '0'";
303 * returns array of items for listView display in yui-ext Grid
305 function getListItemsForEmailXML($folderId, $page = 1, $pageSize = 10, $sort = '', $direction='') {
306 require_once('include/TimeDate.php');
308 global $current_user;
310 global $sugar_config;
313 $this->retrieve($folderId);
314 $start = ($page - 1) * $pageSize;
316 $sort = (empty($sort)) ? $this->defaultSort : $sort;
317 $direction = (empty($direction)) ? $this->defaultDirection : $direction;
318 $order = " ORDER BY {$this->hrSortLocal[$sort]} {$direction}";
320 if($this->is_dynamic) {
321 $r = $this->db->limitQuery(from_html($this->generateSugarsDynamicFolderQuery() . $order), $start, $pageSize);
323 // get items and iterate through them
324 $q = "SELECT emails.id , emails.name, emails.date_sent, emails.status, emails.type, emails.flagged, emails.reply_to_status, emails_text.from_addr, emails_text.to_addrs, 'Emails' polymorphic_module FROM emails JOIN folders_rel ON emails.id = folders_rel.polymorphic_id" .
325 " JOIN emails_text on emails.id = emails_text.email_id
326 WHERE folders_rel.folder_id = '{$folderId}' AND folders_rel.deleted = 0 AND emails.deleted = 0";
327 if ($this->is_group) {
328 $q = $q . " AND emails.assigned_user_id is null";
330 $r = $this->db->limitQuery($q . $order, $start, $pageSize);
335 $email = new Email(); //Needed for email specific functions.
337 while($a = $this->db->fetchByAssoc($r)) {
340 $temp['flagged'] = (is_null($a['flagged']) || $a['flagged'] == '0') ? '' : 1;
341 $temp['status'] = (is_null($a['reply_to_status']) || $a['reply_to_status'] == '0') ? '' : 1;
342 $temp['from'] = preg_replace('/[\x00-\x08\x0B-\x1F]/', '', $a['from_addr']);
343 $temp['subject'] = $a['name'];
344 $temp['date'] = $timedate->to_display_date_time($this->db->fromConvert($a['date_sent'], 'datetime'));
345 $temp['uid'] = $a['id'];
346 $temp['mbox'] = 'sugar::'.$a['polymorphic_module'];
347 $temp['ieId'] = $folderId;
348 $temp['site_url'] = $sugar_config['site_url'];
349 $temp['seen'] = ($a['status'] == 'unread') ? 0 : 1;
350 $temp['type'] = $a['type'];
351 $temp['hasAttach'] = $email->doesImportedEmailHaveAttachment($a['id']);
352 $temp['to_addrs'] = preg_replace('/[\x00-\x08\x0B-\x1F]/', '', $a['to_addrs']);
358 $metadata['mbox'] = $app_strings['LBL_EMAIL_SUGAR_FOLDER'].': '.$this->name;
359 $metadata['ieId'] = $folderId;
360 $metadata['name'] = $this->name;
361 $metadata['unreadChecked'] = ($current_user->getPreference('showUnreadOnly', 'Emails') == 1) ? 'CHECKED' : '';
362 $metadata['out'] = $return;
367 function getCountItems ( $folderId ) {
368 global $current_user ;
370 global $sugar_config ;
371 global $app_strings ;
373 $this->retrieve ( $folderId ) ;
374 if ($this->is_dynamic) {
375 $pattern = '/SELECT(.*?)(\s){1}FROM(\s){1}/is'; // ignores the case
376 $replacement = 'SELECT count(*) c FROM ';
377 $modified_select_query = preg_replace($pattern, $replacement, $this->generateSugarsDynamicFolderQuery(), 1);
378 $r = $this->db->query ( from_html ( $modified_select_query )) ;
380 // get items and iterate through them
381 $q = "SELECT count(*) c FROM folders_rel JOIN emails ON emails.id = folders_rel.polymorphic_id" .
382 " WHERE folder_id = '{$folderId}' AND folders_rel.deleted = 0 AND emails.deleted = 0" ;
383 if ($this->is_group) {
384 $q .= " AND emails.assigned_user_id IS null";
386 $r = $this->db->query ( $q ) ;
389 $a = $this->db->fetchByAssoc($r);
393 function getCountUnread ( $folderId ) {
394 global $current_user ;
396 global $sugar_config ;
397 global $app_strings ;
399 $this->retrieve ( $folderId ) ;
400 if ($this->is_dynamic) {
401 $pattern = '/SELECT(.*?)(\s){1}FROM(\s){1}/is'; // ignores the case
402 $replacement = 'SELECT count(*) c FROM ';
403 $modified_select_query = preg_replace($pattern, $replacement, $this->generateSugarsDynamicFolderQuery(), 1);
404 $r = $this->db->query (from_html($modified_select_query) . " AND emails.status = 'unread'") ;
406 // get items and iterate through them
407 $q = "SELECT count(*) c FROM folders_rel fr JOIN emails on fr.folder_id = '{$folderId}' AND fr.deleted = 0 " .
408 "AND fr.polymorphic_id = emails.id AND emails.status = 'unread' AND emails.deleted = 0" ;
409 if ($this->is_group) {
410 $q .= " AND emails.assigned_user_id IS null";
412 $r = $this->db->query ( $q ) ;
415 $a = $this->db->fetchByAssoc($r);
421 * Convenience method, pass a SugarBean and User to this to add anything to a given folder
423 function addBean($bean, $user=null) {
424 if(empty($bean->id) || empty($bean->module_dir)) {
425 $GLOBALS['log']->fatal("*** FOLDERS: addBean() got empty bean - not saving");
427 } elseif(empty($this->id)) {
428 $GLOBALS['log']->fatal("*** FOLDERS: addBean() is trying to save to a non-saved or non-existent folder");
432 global $current_user;
434 $user = $current_user;
437 $guid = create_guid();
439 $q = "INSERT INTO folders_rel (id, folder_id, polymorphic_module, polymorphic_id, deleted)
440 VALUES('{$guid}', '{$this->id}', '{$bean->module_dir}', '{$bean->id}', 0)";
441 $r = $this->db->query($q);
446 * Builds up a metacollection of user/group folders to be passed to processor methods
447 * @param object User object, defaults to $current_user
448 * @return array Array of abstract folder objects
450 function retrieveFoldersForProcessing($user, $subscribed=true) {
451 global $sugar_config;
452 global $current_language, $current_user;
454 $emails_mod_strings = return_module_language($current_language, "Emails");
455 $myEmailTypeString = 'inbound';
456 $myDraftsTypeString = 'draft';
457 $mySentEmailTypeString = 'sent';
460 global $current_user;
461 $user = $current_user;
464 $teamSecurityClause = '';
468 $rootWhere .= "AND (f.parent_folder IS NULL OR f.parent_folder = '')";
471 $q = $this->coreSubscribed.$teamSecurityClause.$this->coreWhereSubscribed."'{$user->id}' ".$rootWhere.$this->coreOrderBy;
473 $q = $this->core.$teamSecurityClause.$this->coreWhere.$rootWhere.$this->coreOrderBy;
475 $r = $this->db->query($q);
479 while($a = $this->db->fetchByAssoc($r)) {
480 if ((($a['folder_type'] == $myEmailTypeString) ||
481 ($a['folder_type'] == $myDraftsTypeString) ||
482 ($a['folder_type'] == $mySentEmailTypeString)) &&
483 ($a['created_by'] != $current_user->id)) {
487 if (!isset($found[$a['id']])) {
488 $found[$a['id']] = true;
495 * Preps object array for async call from user's Settings->Folders
497 function getGroupFoldersForSettings($focusUser=null) {
502 $folders = $this->retrieveFoldersForProcessing($focusUser, false);
503 $subscriptions = $this->getSubscriptions($focusUser);
505 foreach($folders as $a) {
506 $a['selected'] = (in_array($a['id'], $subscriptions)) ? true : false;
507 $a['origName'] = $a['name'];
509 if($a['is_group'] == 1)
510 if ($a['deleted'] != 1)
517 * Preps object array for async call from user's Settings->Folders
519 function getFoldersForSettings($focusUser=null) {
524 $user[] = array('id' => '', 'name' => $app_strings['LBL_NONE'], 'has_child' => 0, 'is_group' => 0, 'selected' => false);
525 $grp[] = array('id' => '', 'name' => $app_strings['LBL_NONE'], 'has_child' => 0, 'is_group' => 1, 'selected' => false, 'origName' => "");
527 $folders = $this->retrieveFoldersForProcessing($focusUser, false);
528 $subscriptions = $this->getSubscriptions($focusUser);
530 foreach($folders as $a) {
531 $a['selected'] = (in_array($a['id'], $subscriptions)) ? true : false;
532 $a['origName'] = $a['name'];
533 if( isset($a['dynamic_query']) )
534 unset($a['dynamic_query']);
535 if($a['is_group'] == 1) {
541 if($a['has_child'] == 1) {
542 $qGetChildren = $this->core.$this->coreWhere."AND parent_folder = '{$a['id']}'";
543 $rGetChildren = $this->db->query($qGetChildren);
545 while($aGetChildren = $this->db->fetchByAssoc($rGetChildren)) {
548 $grp = $this->getFoldersChildForSettings($aGetChildren, $grp, $subscriptions);
551 $user = $this->getFoldersChildForSettings($aGetChildren, $user, $subscriptions);
558 'userFolders' => $user,
559 'groupFolders' => $grp,
564 function getFoldersChildForSettings($a, $collection, $subscriptions) {
565 $a['selected'] = (in_array($a['id'], $subscriptions)) ? true : false;
566 $a['origName'] = $a['name'];
568 if(isset($a['dynamic_query']))
570 unset($a['dynamic_query']);
573 for($i=0; $i<$this->_depth; $i++)
575 $a['name'] = ".".$a['name'];
580 if($a['has_child'] == 1) {
582 $qGetChildren = $this->core.$this->coreWhere."AND parent_folder = '{$a['id']}'";
583 $rGetChildren = $this->db->query($qGetChildren);
584 while($aGetChildren = $this->db->fetchByAssoc($rGetChildren)) {
585 $collection = $this->getFoldersChildForSettings($aGetChildren, $collection, $subscriptions);
593 * Returns the number of "new" items (based on passed criteria)
594 * @param string id ID of folder
595 * @param array criteria
597 * array('field' => 'status',
598 * 'value' => 'unread');
602 function getCountNewItems($id, $criteria, $folder) {
603 global $current_user;
605 $sugarFolder = new SugarFolder();
606 return $sugarFolder->getCountUnread($id);
610 * Collects, sorts, and builds tree of user's folders
611 * @param objec $rootNode Reference to tree root node
612 * @param array $folderStates User pref folder open/closed states
613 * @param object $user Optional User in focus, default current_user
616 function getUserFolders(&$rootNode, $folderStates, $user=null, $forRefresh=false) {
618 global $current_user;
619 $user = $current_user;
622 $folders = $this->retrieveFoldersForProcessing($user, true);
623 $subscriptions = $this->getSubscriptions($user);
625 $refresh = ($forRefresh) ? array() : null;
627 if(!is_array($folderStates)) {
628 $folderStates = array();
631 foreach($folders as $a) {
632 if ($a['deleted'] == 1)
634 $label = ($a['name'] == 'My Email' ? $mod_strings['LNK_MY_INBOX'] : $a['name']);
636 $unseen = $this->getCountNewItems($a['id'], array('field' => 'status', 'value' => 'unread'), $a);
638 $folderNode = new ExtNode($a['id'], $label);
639 $folderNode->dynamicloadfunction = '';
640 $folderNode->expanded = false;
642 if(array_key_exists('Home::'.$a['id'], $folderStates)) {
643 if($folderStates['Home::'.$a['id']] == 'open') {
644 $folderNode->expanded = true;
647 $nodePath = "Home::".$folderNode->_properties['id'];
649 $folderNode->dynamic_load = true;
650 //$folderNode->set_property('click', " SUGAR.email2.listView.populateListFrameSugarFolder(YAHOO.namespace('frameFolders').selectednode, '{$a['id']}', 'false');");
651 $folderNode->set_property('ieId', 'folder');
652 $folderNode->set_property('is_group', ($a['is_group'] == 1) ? 'true' : 'false');
653 $folderNode->set_property('is_dynamic', ($a['is_dynamic'] == 1) ? 'true' : 'false');
654 $folderNode->set_property('mbox', $folderNode->_properties['id']);
655 $folderNode->set_property('unseen', $unseen);
656 $folderNode->set_property('id', $a['id']);
657 $folderNode->set_property('folder_type', $a['folder_type']);
658 $folderNode->set_property('children', array());
660 if(in_array($a['id'], $subscriptions) && $a['has_child'] == 1) {
661 $qGetChildren = $this->core.$this->coreWhere."AND parent_folder = '{$a['id']}'";
662 $rGetChildren = $this->db->query($qGetChildren);
664 while($aGetChildren = $this->db->fetchByAssoc($rGetChildren)) {
665 if(in_array($aGetChildren['id'], $subscriptions)) {
666 $folderNode->add_node($this->buildTreeNodeFolders($aGetChildren, $nodePath, $folderStates, $subscriptions));
670 $rootNode->add_node($folderNode);
673 /* the code below is called only by Settings->Folders when selecting folders to subscribe to */
677 if(!empty($rootNode->nodes)) {
678 foreach($rootNode->nodes as $node) {
679 $metaNode[] = $this->buildTreeNodeRefresh($node, $subscriptions);
687 * Builds up a metanode for folder refresh (Sugar folders only)
689 function buildTreeNodeRefresh($folderNode, $subscriptions) {
690 $metaNode = $folderNode->_properties;
691 $metaNode['expanded'] = $folderNode->expanded;
692 $metaNode['text'] = $folderNode->_label;
693 if($metaNode['is_group'] == 'true') {
694 $metaNode['cls'] = 'groupFolder';
696 $metaNode['cls'] = 'sugarFolder';
698 $metaNode['id'] = $folderNode->_properties['id'];
699 $metaNode['children'] = array();
700 $metaNode['type'] = 1;
701 $metaNode['leaf'] = false;
702 $metaNode['isTarget'] = true;
703 $metaNode['allowChildren'] = true;
705 if(!empty($folderNode->nodes)) {
706 foreach($folderNode->nodes as $node) {
707 if(in_array($node->_properties['id'], $subscriptions))
708 $metaNode['children'][] = $this->buildTreeNodeRefresh($node, $subscriptions);
715 * Builds children nodes for folders for TreeView
716 * @return $folderNode TreeView node
718 function buildTreeNodeFolders($a, $nodePath, $folderStates, $subscriptions) {
721 if($a['name'] == 'My Drafts') {
722 $label = $mod_strings['LBL_LIST_TITLE_MY_DRAFTS'];
724 if($a['name'] == 'Sent Emails') {
725 $label = $mod_strings['LBL_LIST_TITLE_MY_SENT'];
727 $unseen = $this->getCountNewItems($a['id'], array('field' => 'status', 'value' => 'unread'), $a);
729 $folderNode = new ExtNode($a['id'], $label);
730 $folderNode->dynamicloadfunction = '';
731 $folderNode->expanded = false;
733 $nodePath .= "::{$a['id']}";
735 if(array_key_exists($nodePath, $folderStates)) {
736 if($folderStates[$nodePath] == 'open') {
737 $folderNode->expanded = true;
741 $folderNode->dynamic_load = true;
742 $folderNode->set_property('click', "SUGAR.email2.listView.populateListFrameSugarFolder(YAHOO.namespace('frameFolders').selectednode, '{$a['id']}', 'false');");
743 $folderNode->set_property('ieId', 'folder');
744 $folderNode->set_property('mbox', $a['id']);
745 $folderNode->set_property('is_group', ($a['is_group'] == 1) ? 'true' : 'false');
746 $folderNode->set_property('is_dynamic', ($a['is_dynamic'] == 1) ? 'true' : 'false');
747 $folderNode->set_property('unseen', $unseen);
748 $folderNode->set_property('folder_type', $a['folder_type']);
750 if(in_array($a['id'], $subscriptions) && $a['has_child'] == 1) {
751 $qGetChildren = $this->core.$this->coreWhere."AND parent_folder = '{$a['id']}' ".$this->coreOrderBy;
752 $rGetChildren = $this->db->query($qGetChildren);
754 while($aGetChildren = $this->db->fetchByAssoc($rGetChildren)) {
755 $folderNode->add_node($this->buildTreeNodeFolders($aGetChildren, $nodePath, $folderStates, $subscriptions));
762 * Flags a folder as deleted
763 * @return bool True on success
766 global $current_user;
768 if(!empty($this->id)) {
769 if($this->has_child) {
770 $this->deleteChildrenCascade($this->id);
773 $ownerCheck = ($current_user->is_admin == 0) ? " AND created_by = '{$current_user->id}'" : "";
774 $q = "UPDATE folders SET deleted = 1 WHERE id = '{$this->id}'{$ownerCheck}";
775 $r = $this->db->query($q);
782 * Deletes all children in a cascade
783 * @param string $id ID of parent
784 * @return bool True on success
786 function deleteChildrenCascade($id) {
787 global $current_user;
790 $checkInboundQuery = "SELECT count(*) c FROM inbound_email WHERE groupfolder_id = '{$id}' and deleted = 0";
791 $resultSet = $this->db->query($checkInboundQuery);
792 $a = $this->db->fetchByAssoc($resultSet);
797 $q = "SELECT COUNT(*) c from folders_rel where polymorphic_module = 'Emails' AND polymorphic_id = '{$id}' AND folder_id = '{$this->id}'";
799 $checkEmailQuery = "SELECT count(*) c FROM folders_rel where polymorphic_module = 'Emails' and folder_id = '{$id}' and deleted = 0";
800 $resultSet = $this->db->query($checkEmailQuery);
801 $a = $this->db->fetchByAssoc($resultSet);
806 $q = "SELECT * FROM folders WHERE id = '{$id}'";
807 $r = $this->db->query($q);
808 $a = $this->db->fetchByAssoc($r);
810 if($a['has_child'] == 1) {
811 $q2 = "SELECT id FROM folders WHERE parent_folder = '{$id}'";
812 $r2 = $this->db->query($q2);
814 while($a2 = $this->db->fetchByAssoc($r2)) {
815 $canContinue = $this->deleteChildrenCascade($a2['id']);
821 $ownerCheck = ($current_user->is_admin == 0) ? " AND created_by = '{$current_user->id}'" : "";
822 $q3 = "UPDATE folders SET deleted = 1 WHERE id = '{$id}'{$ownerCheck}";
823 $r3 = $this->db->query($q3);
826 $qRel = "UPDATE folders_rel SET deleted = 1 WHERE folder_id = '{$id}'";
827 $rRel = $this->db->query($qRel);
829 // delete subscriptions
830 $qSub = "DELETE FROM folders_subscriptions WHERE folder_id = '{$id}'";
831 $rSub = $this->db->query($qSub);
834 //_pp($q3);_pp($qRel);_pp($qSub);
842 function save($addSubscriptions = TRUE) {
843 global $current_user;
845 $this->dynamic_query = $this->db->quote($this->dynamic_query);
847 if((empty($this->id) && $this->new_with_id == false) || (!empty($this->id) && $this->new_with_id == true))
850 if( empty($this->id) )
852 $guid = create_guid();
856 $q = "INSERT INTO folders(id, name, folder_type, parent_folder, has_child, is_group, is_dynamic, dynamic_query, assign_to_id, ".
857 "created_by, modified_by, deleted)".
859 " VALUES('{$this->id}', '{$this->name}', '{$this->folder_type}', '{$this->parent_folder}', {$this->has_child}, {$this->is_group}, {$this->is_dynamic}, '{$this->dynamic_query}', '{$this->assign_to_id}', " .
860 "'{$current_user->id}', '{$current_user->id}', 0)";
863 if($addSubscriptions)
865 // create default subscription
866 $this->addSubscriptionsToGroupFolder();
869 // if parent_id is set, update parent's has_child flag
870 $q3 = "UPDATE folders SET has_child = 1 WHERE id = '{$this->parent_folder}'";
871 $r3 = $this->db->query($q3);
874 $q = "UPDATE folders SET name = '{$this->name}', parent_folder = '{$this->parent_folder}', dynamic_query = '{$this->dynamic_query}', assign_to_id = '{$this->assign_to_id}', " .
875 "modified_by = '{$current_user->id}' WHERE id = '{$this->id}'";
878 $this->db->query($q, true);
884 * Add subscriptions to this group folder.
887 function addSubscriptionsToGroupFolder()
889 global $current_user;
891 $this->createSubscriptionForUser($current_user->id);
898 * Add subscriptions to this group folder.
901 function createSubscriptionForUser($user_id)
903 $guid2 = create_guid();
904 $query = "INSERT INTO folders_subscriptions VALUES('{$guid2}', '{$this->id}', '{$user_id}')";
905 $this->db->query($query);
909 function updateFolder($fields) {
910 global $current_user;
912 $this->dynamic_query = $this->db->quote($this->dynamic_query);
913 $id = $fields['record'];
914 $name = $fields['name'];
915 $parent_folder = $fields['parent_folder'];
916 // first do the retrieve
917 $this->retrieve($id);
918 if ($this->has_child) {
919 $childrenArray = array();
920 $this->findAllChildren($id, $childrenArray);
921 if (in_array($parent_folder, $childrenArray)) {
922 return array('status' => "failed", 'message' => "Can not add this folder to its children");
925 // update has_child to 0 for this parent folder if this is the only child it has
926 $q1 = "select count(*) count from folders where deleted = 0 AND parent_folder = '{$this->parent_folder}'";
927 $r1 = $this->db->query($q1);
928 $a1 = $this->db->fetchByAssoc($r1);
929 if ($a1['count'] == 1) {
930 $q1 = "UPDATE folders SET has_child = 0 WHERE id = '{$this->parent_folder}'";
931 $r1 = $this->db->query($q1);
936 $this->parent_folder = $parent_folder;
937 $q2 = "UPDATE folders SET name = '{$this->name}', parent_folder = '{$this->parent_folder}', dynamic_query = '{$this->dynamic_query}', " .
938 "modified_by = '{$current_user->id}' WHERE id = '{$this->id}'";
939 $r2 = $this->db->query($q2);
940 if (!empty($this->parent_folder)) {
941 $q3 = "UPDATE folders SET has_child = 1 WHERE id = '{$this->parent_folder}'";
942 $r3 = $this->db->query($q3);
944 return array('status' => "done");
948 function findAllChildren($folderId, &$childrenArray) {
949 $q = "SELECT * FROM folders WHERE id = '{$folderId}'";
950 $r = $this->db->query($q);
951 $a = $this->db->fetchByAssoc($r);
953 if($a['has_child'] == 1) {
954 $q2 = "SELECT id FROM folders WHERE deleted = 0 AND parent_folder = '{$folderId}'";
955 $r2 = $this->db->query($q2);
957 while($a2 = $this->db->fetchByAssoc($r2)) {
958 $childrenArray[] = $a2['id'];
959 $this->findAllChildren($a2['id'], $childrenArray);
966 * Retrieves and populates object
967 * @param string $id ID of folder
968 * @return bool True on success
970 function retrieve($id) {
971 $q = "SELECT * FROM folders WHERE id = '{$id}' AND deleted = 0";
972 $r = $this->db->query($q);
973 $a = $this->db->fetchByAssoc($r);
976 foreach($a as $k => $v) {
977 if($k == 'dynamic_query') {