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: class X2LeadsController extends x2base {
41:
42: public $modelClass = 'X2Leads';
43:
44: public function accessRules() {
45: return array(
46: array('allow',
47: 'actions'=>array('getItems'),
48: 'users'=>array('*'),
49: ),
50: array('allow',
51: 'actions'=>array('index','view','create','update','search',
52: 'saveChanges','delete','shareX2Leads','inlineEmail'),
53: 'users'=>array('@'),
54: ),
55: array('allow',
56: 'actions'=>array('admin','testScalability'),
57: 'users'=>array('admin'),
58: ),
59: array('deny',
60: 'users'=>array('*'),
61: ),
62: );
63: }
64:
65: public function behaviors(){
66: return array_merge(parent::behaviors(), array(
67: 'X2MobileControllerBehavior' => array(
68: 'class' =>
69: 'application.modules.mobile.components.behaviors.X2MobileControllerBehavior'
70: ),
71: 'QuickCreateRelationshipBehavior' => array(
72: 'class' => 'QuickCreateRelationshipBehavior',
73: 'attributesOfNewRecordToUpdate' => array (
74: 'Contacts' => array (
75: 'accountName' => 'company',
76: ),
77: )
78: ),
79: ));
80: }
81:
82:
83: public function actionGetItems($term){
84: X2LinkableBehavior::getItems ($term);
85: }
86:
87: 88: 89: 90: 91:
92: public function actionView($id, $opportunity=null) {
93: $type = 'x2Leads';
94: $model = $this->loadModel($id);
95: if($this->checkPermissions($model,'view')){
96:
97:
98: User::addRecentItem('l', $id, Yii::app()->user->getId());
99:
100: parent::view($model, $type);
101: }else{
102: $this->redirect('index');
103: }
104: }
105:
106: 107: 108: 109:
110: public function actionCreate() {
111: $model = new X2Leads;
112: $users = User::getNames();
113: foreach(Groups::model()->findAll() as $group){
114: $users[$group->id]=$group->name;
115: }
116: unset($users['admin']);
117: unset($users['']);
118:
119:
120:
121:
122: if(isset($_POST['X2Leads'])) {
123: $temp=$model->attributes;
124:
125: $model->setX2Fields($_POST['X2Leads']);
126:
127: if(isset($_POST['x2ajax'])) {
128: $ajaxErrors = $this->quickCreate ($model);
129: } else {
130: if($model->save())
131: $this->redirect(array('view','id'=>$model->id));
132: }
133: }
134:
135: if(isset($_POST['x2ajax'])){
136: $this->renderInlineCreateForm ($model, isset ($ajaxErrors) ? $ajaxErrors : false);
137: } else {
138: $this->render('create',array(
139: 'model'=>$model,
140: 'users'=>$users,
141: ));
142: }
143: }
144:
145: 146: 147: 148: 149:
150: public function actionUpdate($id) {
151: $model=$this->loadModel($id);
152:
153: if(isset($_POST['X2Leads'])) {
154: $model->setX2Fields($_POST['X2Leads']);
155: if(!empty($model->associatedContacts))
156: $model->associatedContacts=implode(', ',$model->associatedContacts);
157:
158: if ($model->save())
159: $this->redirect(array('view','id'=>$model->id));
160: }
161: 162:
163: $model->assignedTo = array_map(function($n){
164: return trim($n,',');
165: },explode(' ',$model->assignedTo));
166:
167: $this->render('update',array(
168: 'model'=>$model,
169: ));
170: }
171:
172: 173: 174:
175: public function actionIndex() {
176: $model=new X2Leads('search');
177: $this->render('index', array('model'=>$model));
178: }
179:
180: public function delete($id) {
181: $model = $this->loadModel($id);
182:
183: CActiveDataProvider::model('Actions')->deleteAllByAttributes(
184: array('associationType'=>'X2Leads','associationId'=>$id));
185:
186: $this->cleanUpTags($model);
187: $model->delete();
188: }
189:
190: public function actionDelete($id) {
191: $model=$this->loadModel($id);
192:
193: if(Yii::app()->request->isPostRequest) {
194: $event=new Events;
195: $event->type='record_deleted';
196: $event->associationType=$this->modelClass;
197: $event->associationId=$model->id;
198: $event->text=$model->name;
199: $event->user=Yii::app()->user->getName();
200: $event->save();
201: Actions::model()->deleteAll('associationId='.$id.' AND associationType=\'x2Leads\'');
202: $this->cleanUpTags($model);
203: $model->delete();
204: } else
205: throw new CHttpException(
206: 400,Yii::t('app','Invalid request. Please do not repeat this request again.'));
207:
208:
209:
210: if(!isset($_GET['ajax']))
211: $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
212: }
213:
214: public function actionGetTerms(){
215: $sql = 'SELECT id, name as value FROM x2_accounts WHERE name LIKE :qterm ORDER BY name ASC';
216: $command = Yii::app()->db->createCommand($sql);
217: $qterm = $_GET['term'].'%';
218: $command->bindParam(":qterm", $qterm, PDO::PARAM_STR);
219: $result = $command->queryAll();
220: echo CJSON::encode($result); exit;
221: }
222:
223: 224: 225: 226: 227: 228:
229: public function ($selectOptions = array(), $model = null, $menuParams = null) {
230: $Leads = Modules::displayName();
231: $Lead = Modules::displayName(false);
232: $modelId = isset($model) ? $model->id : 0;
233:
234: 235: 236: 237: 238: 239: 240:
241:
242: $menuItems = array(
243: array(
244: 'name'=>'index',
245: 'label'=>Yii::t('x2Leads','{leads} List', array(
246: '{leads}' => $Leads,
247: )),
248: 'url'=>array('index')
249: ),
250: array(
251: 'name'=>'create',
252: 'label'=>Yii::t('x2Leads','Create {lead}', array(
253: '{lead}' => $Lead,
254: )),
255: 'url'=>array('create')
256: ),
257: RecordViewLayoutManager::getViewActionMenuListItem ($modelId),
258: array(
259: 'name'=>'edit',
260: 'label'=>Yii::t('x2Leads','Edit {lead}', array(
261: '{lead}' => $Lead,
262: )),
263: 'url'=>array('update', 'id'=>$modelId)
264: ),
265: array(
266: 'name'=>'delete',
267: 'label'=>Yii::t('x2Leads','Delete {lead}', array(
268: '{lead}' => $Lead,
269: )),
270: 'url'=>'#',
271: 'linkOptions'=>array(
272: 'submit'=>array('delete','id'=>$modelId),
273: 'confirm'=>'Are you sure you want to delete this item?')
274: ),
275: ModelFileUploader::menuLink(),
276: array(
277: 'name'=>'quotes',
278: 'label' => Yii::t('quotes', 'Quotes/Invoices'),
279: 'url' => 'javascript:void(0)',
280: 'linkOptions' => array(
281: 'onclick' => 'x2.inlineQuotes.toggle(); return false;')
282: ),
283: array(
284: 'name'=>'convertToContact',
285: 'label' => Yii::t('x2Leads', 'Convert to {contact}', array(
286: '{contact}' => Modules::displayName(false, "Contacts"),
287: )),
288: 'url' => '#',
289: 'linkOptions' => array ('id' => 'convert-lead-to-contact-button'),
290: ),
291: array(
292: 'name'=>'convert',
293: 'label' => Yii::t('x2Leads', 'Convert to {opportunity}', array(
294: '{opportunity}' => Modules::displayName(false, "Opportunities"),
295: )),
296: 'url' => '#',
297: 'linkOptions' => array ('id' => 'convert-lead-button'),
298: ),
299: array(
300: 'name'=>'print',
301: 'label' => Yii::t('app', 'Print Record'),
302: 'url' => '#',
303: 'linkOptions' => array (
304: 'onClick'=>"window.open('".
305: Yii::app()->createUrl('/site/printRecord', array (
306: 'modelClass' => 'X2Leads',
307: 'id' => $modelId,
308: 'pageTitle' => Yii::t('app', 'Leads').': '.(isset($model) ?
309: $model->name : "")
310: ))."');"
311: )
312: ),
313: array(
314: 'name'=>'import',
315: 'label'=>Yii::t('x2Leads', 'Import {leads}', array(
316: '{leads}' => $Leads,
317: )),
318: 'url'=>array('admin/importModels', 'model'=>'X2Leads'),
319: 'visible'=>Yii::app()->params->isAdmin
320: ),
321: array(
322: 'name'=>'export',
323: 'label'=>Yii::t('x2Leads', 'Export {leads}', array(
324: '{leads}' => $Leads,
325: )),
326: 'url'=>array('admin/exportModels', 'model'=>'X2Leads'),
327: 'visible'=>Yii::app()->params->isAdmin
328: ),
329: RecordViewLayoutManager::getEditLayoutActionMenuListItem (),
330: );
331:
332: $this->prepareMenu($menuItems, $selectOptions);
333: $this->actionMenu = $this->formatMenu($menuItems, $menuParams);
334: }
335:
336: }
337: