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: 39: 40:
41: class AccountsController extends x2base {
42:
43: public $modelClass = 'Accounts';
44:
45: public function accessRules() {
46: return array(
47: array('allow',
48: 'actions' => array('getItems'),
49: 'users' => array('*'),
50: ),
51: array('allow',
52: 'actions' => array('index', 'view', 'create', 'update', 'search', 'addUser', 'removeUser',
53: 'addNote', 'deleteNote', 'saveChanges', 'delete', 'shareAccount', 'inlineEmail', 'qtip'),
54: 'users' => array('@'),
55: ),
56: array('allow',
57: 'actions' => array('admin', 'testScalability'),
58: 'users' => array('admin'),
59: ),
60: array('deny',
61: 'users' => array('*'),
62: ),
63: );
64: }
65:
66: public function behaviors() {
67: return array_merge(parent::behaviors(), array(
68: 'X2MobileControllerBehavior' => array(
69: 'class' =>
70: 'application.modules.mobile.components.behaviors.X2MobileControllerBehavior'
71: ),
72: 'QuickCreateRelationshipBehavior' => array(
73: 'class' => 'QuickCreateRelationshipBehavior',
74: 'attributesOfNewRecordToUpdate' => array(
75: 'Contacts' => array(
76: 'nameId' => 'company',
77: 'website' => 'website',
78: 'phone' => 'phone',
79: ),
80: 'Opportunity' => array(
81: 'accountName' => 'id',
82: )
83: )
84: ),
85: ));
86: }
87:
88: public function actions() {
89: return array_merge(parent::actions(), array(
90: 'inlineEmail' => array(
91: 'class' => 'InlineEmailAction',
92: ),
93: 'accountsReport' => array(
94: 'class' => 'AccountsReportAction',
95: ),
96: 'exportAccountsReport' => array(
97: 'class' => 'ExportAccountsReportAction',
98: ),
99: 'accountsCampaign' => array(
100: 'class' => 'AccountCampaignAction',
101: ),
102: ));
103: }
104:
105: public function actionGetItems($term) {
106: X2LinkableBehavior::getItems ($term);
107: }
108:
109: 110: 111: 112:
113: public function actionView($id) {
114: $model = $this->loadModel($id);
115: if (!parent::checkPermissions($model, 'view'))
116: $this->denied();
117:
118:
119: User::addRecentItem('a', $id, Yii::app()->user->getId());
120: if ($model->checkForDuplicates()) {
121: $this->redirect($this->createUrl('/site/duplicateCheck', array(
122: 'moduleName' => 'accounts',
123: 'modelName' => 'Accounts',
124: 'id' => $id,
125: 'ref' => 'view',
126: )));
127: } else {
128: $model->duplicateChecked();
129: parent::view($model, 'accounts');
130: }
131: }
132:
133: public function actionShareAccount($id) {
134:
135: $model = $this->loadModel($id);
136: $body = "\n\n\n\n".Yii::t('accounts', '{module} Record Details', array(
137: '{module}'=>Modules::displayName(false)
138: ))." <br />
139: <br />".Yii::t('accounts', 'Name').": $model->name
140: <br />".Yii::t('accounts', 'Description').": $model->description
141: <br />".Yii::t('accounts', 'Revenue').": $model->annualRevenue
142: <br />".Yii::t('accounts', 'Phone').": $model->phone
143: <br />".Yii::t('accounts', 'Website').": $model->website
144: <br />".Yii::t('accounts', 'Type').": $model->type
145: <br />".Yii::t('app', 'Link') . ": " . CHtml::link($model->name,
146: array('/accounts/accounts/view', 'id'=>$model->id));
147: $body = trim($body);
148:
149: $errors = array();
150: $status = array();
151: $email = array();
152: if (isset($_POST['email'], $_POST['body'])) {
153:
154: $subject = Yii::t('accounts', "Account Record") . ": $model->name";
155: $email['to'] = $this->parseEmailTo($this->decodeQuotes($_POST['email']));
156: $body = $_POST['body'];
157:
158: if ($email['to'] === false)
159: $errors[] = 'email';
160: if (empty($body))
161: $errors[] = 'body';
162:
163: if (empty($errors))
164: $status = $this->sendUserEmail($email, $subject, $body);
165:
166: if (array_search('200', $status)) {
167: $this->redirect(array('view', 'id' => $model->id));
168: return;
169: }
170: if ($email['to'] === false)
171: $email = $_POST['email'];
172: else
173: $email = $this->mailingListToString($email['to']);
174: }
175: $this->render('shareAccount', array(
176: 'model' => $model,
177: 'body' => $body,
178: 'email' => $email,
179: 'status' => $status,
180: 'errors' => $errors
181: ));
182: }
183:
184: public function actionCreate() {
185: $model = new Accounts;
186: $users = User::getNames();
187: unset($users['admin']);
188: unset($users['']);
189: foreach (Groups::model()->findAll() as $group)
190: $users[$group->id] = $group->name;
191:
192:
193: if (isset($_POST['Accounts'])) {
194:
195: $model->setX2Fields($_POST['Accounts']);
196:
197: if (isset($_POST['x2ajax'])) {
198: $ajaxErrors = $this->quickCreate($model);
199: } else {
200: if ($model->validate () && $model->checkForDuplicates()) {
201: Yii::app()->user->setState('json_attributes', json_encode($model->attributes));
202: $this->redirect($this->createUrl('/site/duplicateCheck', array(
203: 'moduleName' => 'accounts',
204: 'modelName' => 'Accounts',
205: 'id' => null,
206: 'ref' => 'create',
207: )));
208: } else {
209: if ($model->save()) {
210: $this->redirect(array('view', 'id' => $model->id));
211: }
212: }
213: }
214: }
215:
216: if (isset($_POST['x2ajax'])) {
217: $this->renderInlineCreateForm($model, isset($ajaxErrors) ? $ajaxErrors : false);
218: } else {
219: $this->render('create', array(
220: 'model' => $model,
221: 'users' => $users,
222: ));
223: }
224: }
225:
226:
227: 228: 229: 230: 231: 232: 233: 234: 235:
236:
237: 238: 239: 240: 241:
242: public function actionUpdate($id) {
243: $model = $this->loadModel($id);
244: if (isset($_POST['Accounts'])) {
245: $model->setX2Fields($_POST['Accounts']);
246: if ($model->save())
247: $this->redirect(array('view', 'id' => $model->id));
248: }
249:
250: $this->render('update', array(
251: 'model' => $model,
252: ));
253: }
254:
255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274:
275:
276: public function actionAddUser($id) {
277: $users = User::getNames();
278: unset($users['admin']);
279: unset($users['']);
280: foreach (Groups::model()->findAll() as $group) {
281: $users[$group->id] = $group->name;
282: }
283:
284: $model = $this->loadModel($id);
285: $users = Accounts::editUserArray($users, $model);
286:
287:
288:
289:
290: if (isset($_POST['Accounts'])) {
291:
292: $temp = $model->assignedTo;
293: $tempArr = $model->attributes;
294: $model->attributes = $_POST['Accounts'];
295: $arr = $_POST['Accounts']['assignedTo'];
296: $model->assignedTo = Fields::parseUsers($arr);
297: if ($temp != "")
298: $temp.=", " . $model->assignedTo;
299: else
300: $temp = $model->assignedTo;
301: $model->assignedTo = $temp;
302:
303:
304: if ($model->save())
305: $this->redirect(array('view', 'id' => $model->id));
306: }
307:
308: $this->render('addUser', array(
309: 'model' => $model,
310: 'users' => $users,
311:
312: 'action' => 'Add'
313: ));
314: }
315:
316: public function actionRemoveUser($id) {
317:
318: $model = $this->loadModel($id);
319:
320: $pieces = explode(', ', $model->assignedTo);
321: $pieces = Opportunity::editUsersInverse($pieces);
322:
323:
324:
325:
326: if (isset($_POST['Accounts'])) {
327: $temp = $model->attributes;
328: $model->attributes = $_POST['Accounts'];
329: $arr = $_POST['Accounts']['assignedTo'];
330:
331: foreach ($arr as $id => $user) {
332: unset($pieces[$user]);
333: }
334:
335: $temp = Fields::parseUsersTwo($pieces);
336:
337: $model->assignedTo = $temp;
338:
339:
340: if ($model->save())
341: $this->redirect(array('view', 'id' => $model->id));
342: }
343:
344: $this->render('addUser', array(
345: 'model' => $model,
346: 'users' => $pieces,
347: 'action' => 'Remove'
348: ));
349: }
350:
351: public function delete($id) {
352:
353: $model = $this->loadModel($id);
354: $dataProvider = new CActiveDataProvider('Actions', array(
355: 'criteria' => array(
356: 'condition' => 'associationId=' . $id . ' AND associationType=\'account\'',
357: )));
358:
359: $actions = $dataProvider->getData();
360: foreach ($actions as $action) {
361: $action->delete();
362: }
363: $this->cleanUpTags($model);
364: $model->delete();
365: }
366:
367: 368: 369: 370: 371:
372: public function actionDelete($id) {
373: if (Yii::app()->request->isPostRequest) {
374: $model = $this->loadModel($id);
375: Actions::model()->deleteAll('associationId=' . $id . ' AND associationType=\'account\'');
376: $this->cleanUpTags($model);
377: $model->delete();
378: } else
379: throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
380:
381: if (!isset($_GET['ajax']))
382: $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
383: }
384:
385: 386: 387:
388: public function actionIndex() {
389:
390: $model = new Accounts('search');
391: $this->render('index', array('model' => $model));
392: }
393:
394: public function actionQtip($id) {
395: $model = $this->loadModel($id);
396: $this->renderPartial('qtip', array('model' => $model));
397: }
398:
399: 400: 401: 402: 403: 404:
405: public function ($selectOptions = array(), $model = null, $menuParams = null) {
406: $Accounts = Modules::displayName();
407: $Account = Modules::displayName(false);
408: $modelId = isset($model) ? $model->id : 0;
409: $modelName = isset($model) ? $model->name : "";
410:
411: 412: 413: 414: 415: 416: 417:
418:
419: $menuItems = array(
420: array(
421: 'name'=>'all',
422: 'label'=>Yii::t('accounts','All {module}', array('{module}'=>$Accounts)),
423: 'url' => array('index'),
424: ),
425: array(
426: 'name'=>'create',
427: 'label'=>Yii::t('accounts','Create {module}', array('{module}'=>$Account)),
428: 'url'=>array('create')
429: ),
430: array(
431: 'name'=>'report',
432: 'label'=>Yii::t('accounts','{module} Report', array('{module}'=>$Accounts)),
433: 'url'=>array('accountsReport')
434: ),
435: array(
436: 'name'=>'import',
437: 'label'=>Yii::t('accounts',"Import {module}", array('{module}'=>$Accounts)),
438: 'url'=>array('admin/importModels', 'model'=>'Accounts')
439: ),
440: array(
441: 'name'=>'export',
442: 'label'=>Yii::t('accounts','Export {module}', array('{module}'=>$Accounts)),
443: 'url'=>array('admin/exportModels', 'model'=>'Accounts')
444: ),
445: RecordViewLayoutManager::getViewActionMenuListItem ($modelId),
446: array(
447: 'name'=>'edit',
448: 'label'=>Yii::t('accounts','Edit {module}', array('{module}'=>$Account)),
449: 'url'=>array('update', 'id'=>$modelId)
450: ),
451: array(
452: 'name'=>'share',
453: 'label'=>Yii::t('accounts','Share {module}', array('{module}'=>$Account)),
454: 'url'=>array('shareAccount','id'=>$modelId)
455: ),
456: array(
457: 'name'=>'delete',
458: 'label'=>Yii::t('accounts','Delete {module}', array('{module}'=>$Account)),
459: 'url'=>'#',
460: 'linkOptions'=>array(
461: 'submit'=>array('delete','id'=>$modelId),
462: 'confirm'=>'Are you sure you want to delete this item?'
463: )
464: ),
465: array(
466: 'name'=>'email',
467: 'label' => Yii::t('app', 'Send Email'),
468: 'url' => '#',
469: 'linkOptions' => array('onclick' => 'toggleEmailForm(); return false;')
470: ),
471: ModelFileUploader::menuLink(),
472: array(
473: 'name'=>'quotes',
474: 'label' => Yii::t('quotes', 'Quotes/Invoices'),
475: 'url' => 'javascript:void(0)',
476: 'linkOptions' => array(
477: 'onclick' => 'x2.inlineQuotes.toggle(); return false;')
478: ),
479: array(
480: 'name'=>'quick',
481: 'label'=>Yii::t('app', 'Quick Create'),
482: 'url'=>array('/site/createRecords', 'ret'=>'accounts'),
483: 'linkOptions'=>array(
484: 'id'=>'x2-create-multiple-records-button',
485: 'class'=>'x2-hint',
486: 'title'=>Yii::t('app', 'Create a {contact}, {account}, and {opportunity}.', array(
487: '{account}' => $Account,
488: '{contact}' => Modules::displayName(false, "Contacts"),
489: '{opportunity}' => Modules::displayName(false, "Opportunities"),
490: )))
491: ),
492: array(
493: 'name'=>'print',
494: 'label' => Yii::t('app', 'Print Record'),
495: 'url' => '#',
496: 'linkOptions' => array (
497: 'onClick'=>"window.open('".
498: Yii::app()->createUrl('/site/printRecord', array (
499: 'modelClass' => 'Accounts',
500: 'id' => $modelId,
501: 'pageTitle' => Yii::t('app', 'Account').': '.$modelName
502: ))."');"
503: ),
504: ),
505: RecordViewLayoutManager::getEditLayoutActionMenuListItem (),
506: );
507:
508: $this->prepareMenu($menuItems, $selectOptions);
509: $this->actionMenu = $this->formatMenu($menuItems, $menuParams);
510: }
511: }
512: