PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/modules/admin/controllers/BannersController.php

https://bitbucket.org/ALandazabal/software-seminario
PHP | 322 lines | 211 code | 30 blank | 81 comment | 54 complexity | fb4406786b482666a37d694fff3ad06d MD5 | raw file
Possible License(s): MIT, 0BSD, Apache-2.0, JSON, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. class BannersController extends Controller
  3. {
  4. /**
  5. * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
  6. * using two-column layout. See 'protected/views/layouts/column2.php'.
  7. */
  8. public $layout='//layouts/adminwithmenu';
  9. /**
  10. * @return array action filters
  11. */
  12. public function filters()
  13. {
  14. return array(
  15. 'accessControl', // perform access control for CRUD operations
  16. 'postOnly + delete', // we only allow deletion via POST request
  17. );
  18. }
  19. /**
  20. * Specifies the access control rules.
  21. * This method is used by the 'accessControl' filter.
  22. * @return array access control rules
  23. */
  24. protected function beforeAction($action)
  25. {
  26. if (!parent::beforeAction($action)) {
  27. return false;
  28. }
  29. $user = Yii::app()->adminUser;
  30. if(isset(Yii::app()->adminUser->id)){
  31. //permisologia
  32. $model=Admins::model()->findByPk(Yii::app()->adminUser->id);
  33. $cont=Yii::app()->controller->id.'controller';
  34. $controllers=explode(',',$model->permiso);
  35. $unico=array_unique($controllers);
  36. $cont_act=array();
  37. foreach($unico as $controller){ $cont_act[]=explode('-',$controller); }
  38. foreach($cont_act as $contro){
  39. if(strtolower($contro[0])==$cont){
  40. if(strtolower($contro[1])==strtolower($action->id)){
  41. $ok=true;
  42. }
  43. }
  44. }
  45. if(!$ok){ echo 'Denied access'; exit(); }
  46. //FIN permisologia
  47. }
  48. if($user->isGuest && Yii::app()->controller->action->id != 'index'&& Yii::app()->controller->action->id != 'resized') {
  49. $this->redirect(Yii::app()->adminUser->loginUrl);
  50. return false;
  51. }elseif(isset(Yii::app()->adminUser->id) && Yii::app()->controller->action->id == 'index'){
  52. $this->redirect(array('/admin/action/dashboard'));
  53. return false;
  54. }
  55. return true;
  56. }
  57. /**
  58. * Displays a particular model.
  59. * @param integer $id the ID of the model to be displayed
  60. */
  61. public function actionView($id)
  62. {
  63. $this->render('view',array(
  64. 'model'=>$this->loadModel($id),
  65. ));
  66. }
  67. /**
  68. * Creates a new model.
  69. * If creation is successful, the browser will be redirected to the 'view' page.
  70. */
  71. public function actionCreate()
  72. {
  73. $models=new Banners;
  74. // Uncomment the following line if AJAX validation is needed
  75. // $this->performAjaxValidation($model);
  76. if(isset($_POST['Banners']))
  77. {
  78. $models->attributes=$_POST['Banners'];
  79. /*$bannerUpload = CUploadedFile::getInstance($model,'bannerimage');
  80. print_r($bannerUpload);
  81. if(!is_null($bannerUpload)) {
  82. $model->bannerimage = rand(0000,9999).'_'.$bannerUpload;
  83. } else {
  84. $model->bannerimage = "";
  85. }
  86. if($model->save(false)) {
  87. if(!is_null($bannerUpload)){
  88. $bannerUpload->saveAs('media/banners/'.str_replace(" ","-",$model->bannerimage));
  89. }
  90. } echo $model->bannerimage;die;*/
  91. $catImage = CUploadedFile::getInstances($models,'bannerimage');
  92. $appImage = CUploadedFile::getInstances($models,'appbannerimage');
  93. list($width,$height) = getimagesize($catImage[0]->tempName);
  94. list($width1,$height1) = getimagesize($appImage[0]->tempName);
  95. if($width == "1140" && $height =="325" && $width1 == "1024" && $height1 == "500")
  96. {
  97. if(!empty($catImage)) {
  98. $imageName = explode(".",$catImage[0]->name);
  99. $models->bannerimage = rand(000,9999).'-'.Myclass::productSlug($imageName[0]).'.'.$catImage[0]->extensionName;
  100. }
  101. if(!empty($catImage)) {
  102. $catImage[0]->saveAs('media/banners/'. $models->bannerimage);
  103. }
  104. $appImage = CUploadedFile::getInstances($models,'appbannerimage');
  105. if(!empty($appImage)) {
  106. $imageName = explode(".",$appImage[0]->name);
  107. $models->appbannerimage = rand(000,9999).'-'.Myclass::productSlug($imageName[0]).'.'.$appImage[0]->extensionName;
  108. }
  109. if(!empty($appImage)) {
  110. $appImage[0]->saveAs('media/banners/'. $models->appbannerimage);
  111. }
  112. if($models->save())
  113. $this->redirect(array('view','id'=>$models->id));
  114. }
  115. else
  116. {
  117. Yii::app()->user->setFlash('success','Please upload the image with the specified size');
  118. $this->redirect(array('create'));
  119. }
  120. }
  121. $this->render('create',array(
  122. 'model'=>$models,
  123. ));
  124. }
  125. /**
  126. * Updates a particular model.
  127. * If update is successful, the browser will be redirected to the 'view' page.
  128. * @param integer $id the ID of the model to be updated
  129. */
  130. public function actionUpdate($id)
  131. {
  132. $models=$this->loadModel($id);
  133. // Uncomment the following line if AJAX validation is needed
  134. // $this->performAjaxValidation($model);
  135. if(isset($_POST['Banners']))
  136. {
  137. $models->attributes=$_POST['Banners'];
  138. /*$bannerUpload = CUploadedFile::getInstance($model,'bannerimage');
  139. print_r($bannerUpload);
  140. if(!is_null($bannerUpload)) {
  141. $model->bannerimage = rand(0000,9999).'_'.$bannerUpload;
  142. } else {
  143. $model->bannerimage = "";
  144. }
  145. if($model->save(false)) {
  146. if(!is_null($bannerUpload)){
  147. $bannerUpload->saveAs('media/banners/'.str_replace(" ","-",$model->bannerimage));
  148. }
  149. } echo $model->bannerimage;die;*/
  150. $catImage = CUploadedFile::getInstances($models,'bannerimage');
  151. $appImage = CUploadedFile::getInstances($models,'appbannerimage');
  152. list($width,$height) = getimagesize($catImage[0]->tempName);
  153. list($width1,$height1) = getimagesize($appImage[0]->tempName);
  154. if(!empty($catImage))
  155. {
  156. if($width == "1140" && $height =="325")
  157. {
  158. if(!empty($catImage)) {
  159. $imageName = explode(".",$catImage[0]->name);
  160. $models->bannerimage = rand(000,9999).'-'.Myclass::productSlug($imageName[0]).'.'.$catImage[0]->extensionName;
  161. }
  162. if(!empty($catImage)) {
  163. $catImage[0]->saveAs('media/banners/'. $models->bannerimage);
  164. }
  165. }
  166. else
  167. {
  168. Yii::app()->user->setFlash('success','Please upload the image with the specified size');
  169. $this->redirect(array('update','id'=>$models->id));
  170. }
  171. }
  172. else if(!empty($appImage))
  173. {
  174. if($width1 == "1024" && $height1 == "500" || $width1 == "2048" && $height1 == "1000")
  175. {
  176. $appImage = CUploadedFile::getInstances($models,'appbannerimage');
  177. if(!empty($appImage)) {
  178. $imageName = explode(".",$appImage[0]->name);
  179. $models->appbannerimage = rand(000,9999).'-'.Myclass::productSlug($imageName[0]).'.'.$appImage[0]->extensionName;
  180. }
  181. if(!empty($appImage)) {
  182. $appImage[0]->saveAs('media/banners/'. $models->appbannerimage);
  183. }
  184. }
  185. else
  186. {
  187. Yii::app()->user->setFlash('success','Please upload the image with the specified size');
  188. $this->redirect(array('update','id'=>$models->id));
  189. }
  190. }
  191. if($models->save(false))
  192. $this->redirect(array('view','id'=>$models->id));
  193. }
  194. $this->render('update',array(
  195. 'model'=>$models,
  196. ));
  197. }
  198. /**
  199. * Deletes a particular model.
  200. * If deletion is successful, the browser will be redirected to the 'admin' page.
  201. * @param integer $id the ID of the model to be deleted
  202. */
  203. public function actionDelete($id)
  204. {
  205. $this->loadModel($id)->delete();
  206. // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
  207. if(!isset($_GET['ajax']))
  208. $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
  209. }
  210. /**
  211. * Lists all models.
  212. */
  213. public function actionIndex()
  214. {
  215. $siteSettings = Sitesettings::model()->find();
  216. $dataProvider=new CActiveDataProvider('Banners');
  217. $this->render('index',array(
  218. 'dataProvider'=>$dataProvider,
  219. 'sitesettings' => $sitesettings,
  220. ));
  221. }
  222. public function actionBannerenable()
  223. {
  224. $enablestatus = $_POST['enablestatus'];
  225. $sitesettings = Sitesettings::model()->findByPk(1);
  226. $sitesettings->bannerstatus = $enablestatus;
  227. $sitesettings->save(false);
  228. }
  229. /**
  230. * Manages all models.
  231. */
  232. public function actionAdmin()
  233. {
  234. $sitesettings = Sitesettings::model()->find();
  235. $model=new Banners('search');
  236. $model->unsetAttributes(); // clear any default values
  237. if(isset($_GET['Banners']))
  238. $model->attributes=$_GET['Banners'];
  239. $this->render('admin',array(
  240. 'model'=>$model,
  241. 'sitesettings' => $sitesettings,
  242. ));
  243. }
  244. public function actionCheckimage()
  245. {
  246. if ( 0 < $_FILES['file']['error'] ) {
  247. //echo 'Error: ' . $_FILES['file']['error'] . '<br>';
  248. }
  249. $ftmp = $_FILES['file']['tmp_name'];
  250. $oname = $_FILES['file']['name'];
  251. $fname = $_FILES['file']['name'];
  252. $fsize = $_FILES['file']['size'];
  253. $ftype = $_FILES['file']['type'];
  254. list($width,$height) = getimagesize($ftmp);
  255. if($width == "1140" && $height == "325")
  256. {
  257. echo "success";
  258. }
  259. else
  260. {
  261. echo "error";
  262. }
  263. }
  264. /**
  265. * Returns the data model based on the primary key given in the GET variable.
  266. * If the data model is not found, an HTTP exception will be raised.
  267. * @param integer $id the ID of the model to be loaded
  268. * @return Banners the loaded model
  269. * @throws CHttpException
  270. */
  271. public function loadModel($id)
  272. {
  273. $model=Banners::model()->findByPk($id);
  274. if($model===null)
  275. throw new CHttpException(404,'The requested page does not exist.');
  276. return $model;
  277. }
  278. /**
  279. * Performs the AJAX validation.
  280. * @param Banners $model the model to be validated
  281. */
  282. protected function performAjaxValidation($model)
  283. {
  284. if(isset($_POST['ajax']) && $_POST['ajax']==='banners-form')
  285. {
  286. echo CActiveForm::validate($model);
  287. Yii::app()->end();
  288. }
  289. }
  290. }