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 ProductsController extends x2base {
 41:     public $modelClass = 'Product';
 42: 
 43:     public function accessRules() {
 44:         return array(
 45:             array('allow', 
 46:                 'actions'=>array('index', 'view', 'search','getItems'),
 47:                 'users'=>array('@'),
 48:             ),
 49:             array('allow', 
 50:                 'actions'=>array('admin','testScalability', 'create', 'update', 'delete'),
 51:                 'users'=>array('admin'),
 52:             ),
 53:             array('deny',  
 54:                 'users'=>array('*'),
 55:             ),
 56:         );
 57:     }
 58: 
 59:     public function behaviors(){
 60:         return array_merge(parent::behaviors(), array(
 61:             'X2MobileControllerBehavior' => array(
 62:                 'class' => 
 63:                     'application.modules.mobile.components.behaviors.X2MobileControllerBehavior'
 64:             ),
 65:             'QuickCreateRelationshipBehavior' => array(
 66:                 'class' => 'QuickCreateRelationshipBehavior',
 67:             ),
 68:         ));
 69:     }
 70: 
 71:     public function actionGetItems($term){
 72:         X2LinkableBehavior::getItems ($term);
 73:     }
 74:      75:  76:  77: 
 78:     public function actionView($id) {
 79:         $model = $this->loadModel($id);
 80:         if (!$this->checkPermissions($model, 'view')) $this->denied ();
 81: 
 82:         
 83:         User::addRecentItem('r', $id, Yii::app()->user->getId()); 
 84: 
 85:         parent::view($model);
 86:     }
 87: 
 88:      89:  90:  91: 
 92:     public function actionCreate() {
 93:         $model=new Product;
 94:         $users=User::getNames();
 95:         if(isset($_POST['Product'])) {
 96:             $model->setX2Fields($_POST['Product']);
 97:             
 98:             $model->createDate=time();
 99: 
100:             if(isset($_POST['x2ajax'])){
101:                 $ajaxErrors = $this->quickCreate ($model);
102:             }else{
103:                 if($model->save()) {
104:                     $this->redirect(array('view', 'id' => $model->id));
105:                 }
106:             }
107:         }
108: 
109:         if(isset($_POST['x2ajax'])){
110:             $this->renderInlineCreateForm ($model, isset ($ajaxErrors) ? $ajaxErrors : false);
111:         } else {
112:             $this->render('create',array(
113:                 'model'=>$model,
114:                 'users'=>$users,
115:             ));
116:         }
117:     }
118: 
119:     120: 121: 122: 123: 
124:     public function actionUpdate($id) {
125:         $model = $this->loadModel($id);
126:         $users=User::getNames(); 
127:         $fields=Fields::model()->findAllByAttributes(array('modelName'=>'Product'));
128:         foreach($fields as $field){
129:             if($field->type=='link'){
130:                 $fieldName=$field->fieldName;
131:                 $type=ucfirst($field->linkType);
132:                 if(is_numeric($model->$fieldName) && $model->$fieldName!=0){
133:                     eval("\$lookupModel=$type::model()->findByPk(".$model->$fieldName.");");
134:                     if(isset($lookupModel))
135:                         $model->$fieldName=$lookupModel->name;
136:                 }
137:             }
138:         }
139:         if(isset($_POST['Product'])) {
140:             $temp=$model->attributes;
141:             $model->setX2Fields($_POST['Product']);
142:             
143:             
144:             $action = new Actions;
145:             $action->associationType = 'product';
146:             $action->associationId = $model->id;
147:             $action->associationName = $model->name;
148:             $action->assignedTo = Yii::app()->user->getName();
149:             $action->completedBy=Yii::app()->user->getName();
150:             $action->dueDate = time();
151:             $action->completeDate = time();
152:             $action->visibility = 1;
153:             $action->complete='Yes';
154:         
155:             $action->actionDescription = "Update: {$model->name}
156:             Type: {$model->type}
157:             Price: {$model->price}
158:             Currency: {$model->currency}
159:             Inventory: {$model->inventory}";
160:             $action->save();         
161:             parent::update($model,$temp,'0');
162:         }
163: 
164:         $this->render('update',array(
165:             'model'=>$model,
166:             'users'=>$users,
167:         ));
168:     }
169:     170: 171: 172: 173: 
174:     public function actionDelete($id) {
175:         if(Yii::app()->request->isPostRequest) {    
176:             $model = $this->loadModel($id);
177:             
178:             $model->clearTags();
179:             $model->delete();
180:             
181:             
182:             if(!isset($_GET['ajax']))
183:                 $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
184:         } else {
185:             throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
186:         }
187:     }
188: 
189:     190: 191: 
192:     public function actionIndex() {
193:         $model=new Product('search');
194:         $this->render('index', array('model'=>$model));
195:     }
196: 
197:     198: 199: 200: 
201:     protected function performAjaxValidation($model) {
202:         if(isset($_POST['ajax']) && $_POST['ajax']==='product-form') {
203:             echo CActiveForm::validate($model);
204:             Yii::app()->end();
205:         }
206:     }
207: 
208:     209: 210: 211: 212: 
213:     public function actionGetItems2 () {
214:         $prefix = isset ($_POST['prefix']) ? $_POST['prefix'] : '';
215:         $page = isset ($_POST['page']) ? $_POST['page'] : 0;
216:         $pageSize = isset ($_POST['pageSize']) ? $_POST['pageSize'] : 20;
217: 
218:         $items = X2Model::model ($this->modelClass)->getItems2 (
219:             $prefix, $page, $pageSize, array ('id', 'description', 'price'), 'name');
220:         echo CJSON::encode ($items);
221:     }
222: 
223:     224: 225: 226: 227: 228: 
229:     public function ($selectOptions = array(), $model = null, $menuParams = null) {
230:         $Products = Modules::displayName();
231:         $Product = Modules::displayName(false);
232:         $modelId = isset($model) ? $model->id : 0;
233: 
234:         235: 236: 237: 238: 239: 
240: 
241:         $menuItems = array(
242:             array(
243:                 'name'=>'index',
244:                 'label'=>Yii::t('products','{module} List', array(
245:                     '{module}'=>$Product,
246:                 )),
247:                 'url'=>array('index')
248:             ),
249:             array(
250:                 'name'=>'create',
251:                 'label'=>Yii::t('products','Create'),
252:                 'url'=>array('create')
253:             ),
254:             RecordViewLayoutManager::getViewActionMenuListItem ($modelId),
255:             array(
256:                 'name'=>'edit',
257:                 'label'=>Yii::t('products','Update'),
258:                 'url'=>array('update', 'id'=>$modelId)
259:             ),
260:             array(
261:                 'name'=>'delete',
262:                 'label'=>Yii::t('products','Delete'),
263:                 'url'=>'#',
264:                 'linkOptions'=>array(
265:                     'submit'=>array('delete','id'=>$modelId),
266:                     'confirm'=>Yii::t('app','Are you sure you want to delete this item?')
267:                 )
268:             ),
269:             array(
270:                 'name' => 'print',
271:                 'label' => Yii::t('app', 'Print Record'),
272:                 'url' => '#',
273:                 'linkOptions' => array (
274:                     'onClick'=>"window.open('".
275:                         Yii::app()->createUrl('/site/printRecord', array (
276:                             'modelClass' => 'Product',
277:                             'id' => $modelId,
278:                             'pageTitle' => Yii::t('app', '{module}', array(
279:                                 '{module}' => $Product,
280:                             )).': '.(isset($model) ? $model->name : "")
281:                     ))."');"
282:                 ),
283:             ),
284:             array(
285:                 'name'=>'import',
286:                 'label'=>Yii::t('products', 'Import {module}', array(
287:                     '{module}' => $Products,
288:                 )),
289:                 'url'=>array('admin/importModels', 'model'=>'Product'),
290:             ),
291:             array(
292:                 'name'=>'export',
293:                 'label'=>Yii::t('products', 'Export {module}',  array(
294:                     '{module}' => $Products,
295:                 )),
296:                  'url'=>array('admin/exportModels', 'model'=>'Product'),
297:             ),
298:             RecordViewLayoutManager::getEditLayoutActionMenuListItem (),
299:         );
300: 
301:         $this->prepareMenu($menuItems, $selectOptions);
302:         $this->actionMenu = $this->formatMenu($menuItems, $menuParams);
303:     }
304: 
305: 
306: }
307: