1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36:
37:
38: Yii::import('application.models.X2Model');
39:
40: 41: 42: 43: 44:
45: class Accounts extends X2Model {
46:
47: 48: 49: 50:
51: public static function model($className = __CLASS__) {
52: return parent::model($className);
53: }
54:
55: 56: 57:
58: public function tableName() {
59: return 'x2_accounts';
60: }
61:
62: public function behaviors() {
63: return array_merge(parent::behaviors(), array(
64: 'X2LinkableBehavior' => array(
65: 'class' => 'X2LinkableBehavior',
66: 'module' => 'accounts',
67: 'icon' => 'accounts_icon.png',
68: ),
69: 'ERememberFiltersBehavior' => array(
70: 'class' => 'application.components.ERememberFiltersBehavior',
71: 'defaults' => array(),
72: 'defaultStickOnClear' => false
73: ),
74: 'InlineEmailModelBehavior' => array(
75: 'class' => 'application.components.InlineEmailModelBehavior',
76: ),
77: 'X2AddressBehavior' => array(
78: 'class' => 'application.components.X2AddressBehavior',
79: ),
80: 'X2DuplicateBehavior' => array(
81: 'class' => 'application.components.X2DuplicateBehavior',
82: ),
83: ));
84: }
85:
86: public function duplicateFields() {
87: return array_merge(array(
88: 'tickerSymbol',
89: 'website',
90: ), parent::duplicateFields());
91: }
92:
93: 94: 95: 96: 97: 98: 99: 100: 101: 102:
103:
104: public static function parseContacts($arr) {
105: $str = "";
106: foreach ($arr as $contact) {
107: $str.=$contact . " ";
108: }
109: return $str;
110: }
111:
112: public static function parseContactsTwo($arr) {
113: $str = "";
114: foreach ($arr as $id => $contact) {
115: $str.=$id . " ";
116: }
117: return $str;
118: }
119:
120: public static function editContactArray($arr, $model) {
121:
122: $pieces = explode(" ", $model->associatedContacts);
123: unset($arr[0]);
124:
125: foreach ($pieces as $contact) {
126: if (array_key_exists($contact, $arr)) {
127: unset($arr[$contact]);
128: }
129: }
130:
131: return $arr;
132: }
133:
134: public static function editUserArray($arr, $model) {
135:
136: $pieces = explode(', ', $model->assignedTo);
137: unset($arr['Anyone']);
138: unset($arr['admin']);
139: foreach ($pieces as $user) {
140: if (array_key_exists($user, $arr)) {
141: unset($arr[$user]);
142: }
143: }
144: return $arr;
145: }
146:
147: public static function editUsersInverse($arr) {
148:
149: $data = array();
150:
151: foreach ($arr as $username)
152: $data[] = CActiveRecord::model('User')->findByAttributes(array('username' => $username));
153:
154: $temp = array();
155: foreach ($data as $item) {
156: if (isset($item))
157: $temp[$item->username] = $item->firstName . ' ' . $item->lastName;
158: }
159: return $temp;
160: }
161:
162: public static function editContactsInverse($arr) {
163: $data = array();
164:
165: foreach ($arr as $id) {
166: if ($id != '')
167: $data[] = CActiveRecord::model('Contacts')->findByPk($id);
168: }
169: $temp = array();
170:
171: foreach ($data as $item) {
172: $temp[$item->id] = $item->firstName . ' ' . $item->lastName;
173: }
174: return $temp;
175: }
176:
177: public static function getAvailableContacts($accountId = 0) {
178:
179: $availableContacts = array();
180:
181: $criteria = new CDbCriteria;
182: $criteria->addCondition("accountId='$accountId'");
183: $criteria->addCondition(array("accountId=''"), 'OR');
184:
185:
186: $contactRecords = CActiveRecord::model('Contacts')->findAll($criteria);
187: foreach ($contactRecords as $record)
188: $availableContacts[$record->id] = $record->name;
189:
190: return $availableContacts;
191: }
192:
193: public static function getContacts($accountId) {
194: $contacts = array();
195: $contactRecords = CActiveRecord::model('Contacts')->findAllByAttributes(array('accountId' => $accountId));
196: if (!isset($contactRecords))
197: return array();
198:
199: foreach ($contactRecords as $record)
200: $contacts[$record->id] = $record->name;
201:
202: return $contacts;
203: }
204:
205: public static function setContacts($contactIds, $accountId) {
206:
207: $account = CActiveRecord::model('Accounts')->findByPk($accountId);
208:
209: if (!isset($account))
210: return false;
211:
212:
213: $oldContacts = CActiveRecord::model('Contacts')->findAllByAttributes(array('accountId' => $accountId));
214: foreach ($oldContacts as $contact) {
215: if (!in_array($contact->id, $contactIds)) {
216: $contact->accountId = 0;
217: $contact->company = '';
218: $contact->save();
219: }
220: }
221:
222:
223: foreach ($contactIds as $id) {
224: $contactRecord = CActiveRecord::model('Contacts')->findByPk($id);
225: $contactRecord->accountId = $account->id;
226: $contactRecord->company = $account->name;
227: $contactRecord->save();
228: }
229: return true;
230: }
231:
232: public function search($pageSize = null, $uniqueId = null) {
233: $criteria = new CDbCriteria;
234: return $this->searchBase($criteria, $pageSize);
235: }
236:
237: public function searchList($id, $pageSize = null) {
238: $list = X2List::model()->findByPk($id);
239:
240: if (isset($list)) {
241: $search = $list->queryCriteria();
242:
243: $this->compareAttributes($search);
244:
245: return new SmartActiveDataProvider('Accounts', array(
246: 'criteria' => $search,
247: 'sort' => array(
248: 'defaultOrder' => 't.lastUpdated DESC'
249: ),
250: 'pagination' => array(
251: 'pageSize' => isset($pageSize) ? $pageSize : Profile::getResultsPerPage(),
252: ),
253: ));
254: } else {
255: return $this->searchBase();
256: }
257: }
258:
259: }
260: