1: <?php
2: /*****************************************************************************************
3: * X2Engine Open Source Edition is a customer relationship management program developed by
4: * X2Engine, Inc. Copyright (C) 2011-2016 X2Engine Inc.
5: *
6: * This program is free software; you can redistribute it and/or modify it under
7: * the terms of the GNU Affero General Public License version 3 as published by the
8: * Free Software Foundation with the addition of the following permission added
9: * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10: * IN WHICH THE COPYRIGHT IS OWNED BY X2ENGINE, X2ENGINE DISCLAIMS THE WARRANTY
11: * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12: *
13: * This program is distributed in the hope that it will be useful, but WITHOUT
14: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15: * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16: * details.
17: *
18: * You should have received a copy of the GNU Affero General Public License along with
19: * this program; if not, see http://www.gnu.org/licenses or write to the Free
20: * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21: * 02110-1301 USA.
22: *
23: * You can contact X2Engine, Inc. P.O. Box 66752, Scotts Valley,
24: * California 95067, USA. or at email address [email protected].
25: *
26: * The interactive user interfaces in modified source and object code versions
27: * of this program must display Appropriate Legal Notices, as required under
28: * Section 5 of the GNU Affero General Public License version 3.
29: *
30: * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31: * these Appropriate Legal Notices must retain the display of the "Powered by
32: * X2Engine" logo. If the display of the logo is not reasonably feasible for
33: * technical reasons, the Appropriate Legal Notices must display the words
34: * "Powered by X2Engine".
35: *****************************************************************************************/
36:
37: Yii::import('application.components.leftWidget.LeftWidget');
38:
39: /**
40: * Widget for displaying the "Top Contacts" portlet
41: * @package application.components
42: */
43: class TopContacts extends LeftWidget {
44:
45: const ITEM_SEPARATOR = ',';
46: const PROP_SEPARATOR = ':';
47:
48: public $id = 'top-contacts';
49:
50: public $widgetLabel = 'Favorites';
51:
52: public static function addBookmark (CActiveRecord $record) {
53: $type = get_class ($record);
54: $id = $record->id;
55: $user = Yii::app()->params->profile->user;
56: if (empty ($user->topContacts)) {
57: $bookmarks = array ();
58: } else {
59: $bookmarks = explode (self::ITEM_SEPARATOR, $user->topContacts);
60: }
61: foreach ($bookmarks as $item) {
62: $parts = explode (self::PROP_SEPARATOR, $item);
63: if (count ($parts) === 1 && $type === 'Contacts' && $id === $item) {
64: return false;
65: }
66: if ($type === $parts[0] && $parts[1] === $id) {
67: return false;
68: }
69: }
70: $bookmarks[] = $type.':'.$id;
71: $user->topContacts = implode (self::ITEM_SEPARATOR, $bookmarks);
72: if ($user->update ('topContacts')) return true;
73: }
74:
75: public static function removeBookmark (CActiveRecord $record) {
76: $type = get_class ($record);
77: $id = $record->id;
78: $user = Yii::app()->params->profile->user;
79: if (empty ($user->topContacts)) {
80: $bookmarks = array ();
81: } else {
82: $bookmarks = explode (self::ITEM_SEPARATOR, $user->topContacts);
83: }
84: $found = false;
85: $count = count ($bookmarks);
86: for ($i = 0; $i < $count; $i++) {
87: $item = $bookmarks[$i];
88: $parts = explode (self::PROP_SEPARATOR, $item);
89: if (count ($parts) === 1 && $type === 'Contacts' && $id === $item) {
90: $found = true;
91: unset ($bookmarks[$i]);
92: break;
93: }
94: if ($type === $parts[0] && $parts[1] === $id) {
95: $found = true;
96: unset ($bookmarks[$i]);
97: break;
98: }
99: }
100: $user->topContacts = implode (self::ITEM_SEPARATOR, $bookmarks);
101: if ($found && $user->update ('topContacts')) return true;
102: }
103:
104: public static function getBookmarkedRecords () {
105: $user = Yii::app()->params->profile->user;
106:
107: $bookmarks = empty($user->topContacts) ?
108: array() : explode(TopContacts::ITEM_SEPARATOR, $user->topContacts);
109:
110: $bookmarkRecords = array();
111: foreach($bookmarks as $item){
112: $parts = explode (TopContacts::PROP_SEPARATOR, $item);
113: if (count ($parts) === 1) {
114: $record = X2Model::model ('Contacts')->findByPk($item);
115: } elseif (count ($parts) === 2) {
116: $type = $parts[0];
117: try {
118: $model = X2Model::model ($type, false);
119: } catch (CHttpException $e) {
120: continue;
121: }
122: $id = $parts[1];
123: $record = $model->findByPk ($id);
124: } else {
125: continue;
126: }
127: if(!is_null($record)) //only include contact if the contact ID exists
128: $bookmarkRecords[] = $record;
129: }
130: return $bookmarkRecords;
131: }
132:
133: protected function renderContent() {
134: Yii::t('app','Favorites');
135: $this->render('topContacts',array(
136: 'bookmarkRecords'=>User::getTopContacts()
137: ));
138: }
139: }
140:
141: ?>
142: