1: <?php
2:
3: /*****************************************************************************************
4: * X2Engine Open Source Edition is a customer relationship management program developed by
5: * X2Engine, Inc. Copyright (C) 2011-2016 X2Engine 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 X2ENGINE, X2ENGINE 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 X2Engine, Inc. P.O. Box 66752, Scotts Valley,
25: * California 95067, USA. or at email address [email protected].
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: * X2Engine" 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 X2Engine".
36: *****************************************************************************************/
37:
38: /**
39: * @package application.models
40: */
41: class Record {
42:
43: /**
44: * Merges records of different types
45: * @param array $recordArrs <type> => <array of records>
46: * @param array $attrUnion Union of attribute names of each record type
47: * @return array An array of records with keys equal to the keys specified in $attrUnion and
48: * with values corresponding to the values in $recordArrs. If a particular record type in
49: * $recordArrs does not have one of the attributes in $attrUnion, that attribute will be set
50: * to null;
51: */
52: public static function mergeMixedRecords (array $recordArrs, array $attrUnion) {
53: $recordTemplate = array ();
54: foreach ($attrUnion as $attr) {
55: $recordTemplate[$attr] = null;
56: }
57:
58: $combinedRecords = array ();
59:
60: foreach ($recordArrs as $recordType => $arr) {
61: foreach ($arr as $record) {
62: $combinedRecords[] = array_merge (
63: ArrayUtil::normalizeToArray ($recordTemplate, $record),
64: array ('recordType' => $recordType)
65: );
66: }
67: }
68:
69: return $combinedRecords;
70: }
71:
72: /**
73: * Compiles new actions and contacts into a list for the "What's New" page
74: * @param array $records
75: * @param boolean $whatsNew
76: * @return array
77: */
78: public static function convert($records, $whatsNew=true) {
79: $arr=array();
80:
81: foreach ($records as $record) {
82: $user=User::model()->findByAttributes(array('username'=>$record->updatedBy));
83: if(isset($user)) {
84: $name=$user->firstName." ".$user->lastName;
85: $userId=$user->id;
86: } else {
87: $name='web admin';
88: $userId=1;
89: }
90: $temp=array();
91: if($record->hasAttribute('assignedTo')){
92: $assignment=User::model()->findByAttributes(array('username'=>$record->assignedTo));
93: $temp['assignedTo']=isset($assignment)?CHtml::link($assignment->firstName." ".$assignment->lastName,array('/profile/view','id'=>$assignment->id)):"";
94: }else{
95: $temp['assignedTo']='';
96: }
97: $linkObject = $record->asa("X2LinkableBehavior");
98: if ($linkObject instanceof CBehavior) {
99: $temp['#recordLink'] = $linkObject->link;
100: $temp['#recordUrl'] = $linkObject->url;
101: }
102: else {
103: if ($record->hasAttribute('name'))
104: $temp['#recordLink'] = $record->name;
105: elseif($record->hasAttribute('id'))
106: $temp['#recordLink'] = '#'.$record->id;
107: else
108: $temp['#recordLink'] = '';
109: }
110: if($record instanceof Contacts) {
111: $temp['id']=$record->id;
112: $temp['name']=$record->firstName.' '.$record->lastName;
113: $temp['description']=$record->backgroundInfo;
114: $temp['link']=array('/contacts/contacts/view','id'=>$record->id);
115: $temp['type']='Contact';
116: $temp['lastUpdated']=$record->lastUpdated;
117: $temp['updatedBy']=CHtml::link($name,array('/profile/view','id'=>$userId));
118:
119: while(isset($arr[$temp['lastUpdated']]))
120: $temp['lastUpdated']++;
121: $arr[$temp['lastUpdated']]=$temp;
122:
123: } elseif($record instanceof Actions) {
124: $temp['id']=$record->id;
125: $temp['name']=empty($record->type)? Yii::t('actions','Action') : Yii::t('actions','Action: ').ucfirst($record->type);
126: $temp['description']=$record->actionDescription;
127: $temp['link']=array('/actions/actions/view','id'=>$record->id);
128: $temp['type']='Action';
129: $temp['lastUpdated']=$record->lastUpdated;
130: $temp['updatedBy']=$name;
131: while(isset($arr[$temp['lastUpdated']]))
132: $temp['lastUpdated']++;
133: $arr[$temp['lastUpdated']]=$temp;
134: } else {
135: $temp['id']=$record->id;
136: $temp['name']=$record->name;
137: if(!is_null($record->description))
138: $temp['description']=$record->description;
139: else
140: $temp['description']="";
141:
142: $temp['lastUpdated']=$record->lastUpdated;
143: $temp['updatedBy']=$name;
144:
145: if($record instanceof Opportunity) {
146: $temp['link']=array('/opportunities/opportunities/view','id'=>$record->id);
147: $temp['type']='Opportunity';
148: }
149: elseif($record instanceof Accounts) {
150: $temp['link']=array('/accounts/accounts/view','id'=>$record->id);
151: $temp['type']='Account';
152: } elseif($record instanceof Quote || $record instanceof Product){
153: $temp['type']=get_class($record);
154: $temp['link']=array(str_repeat('/'.strtolower(get_class($record)).'s',2).'/view','id'=>$record->id);
155: }else {
156: $temp['type']=get_class($record);
157: $temp['link']=array(str_repeat('/'.strtolower(get_class($record)),2).'/view','id'=>$record->id);
158: }
159:
160: while(isset($arr[$temp['lastUpdated']]))
161: $temp['lastUpdated']++;
162: if($whatsNew)
163: $arr[$temp['lastUpdated']]=$temp;
164: else
165: $arr[]=$temp;
166: }
167: }
168: if($whatsNew){
169: ksort($arr);
170: return array_values(array_reverse($arr));
171: }else{
172: return array_values($arr);
173: }
174: }
175: }
176: