PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/controllers/RepositoryController.php

https://bitbucket.org/felagund18/bugitor
PHP | 172 lines | 86 code | 18 blank | 68 comment | 15 complexity | e68cd8a845c14d9af99e0edc80e52a01 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, AGPL-1.0
  1. <?php
  2. /*
  3. * This file is part of
  4. * ____ _ __
  5. * / __ )__ ______ _(_) /_____ _____
  6. * / __ / / / / __ `/ / __/ __ \/ ___/
  7. * / /_/ / /_/ / /_/ / / /_/ /_/ / /
  8. * /_____/\__,_/\__, /_/\__/\____/_/
  9. * /____/
  10. * A Yii powered issue tracker
  11. * http://bitbucket.org/jacmoe/bugitor/
  12. *
  13. * Copyright (C) 2009 - 2012 Bugitor Team
  14. *
  15. * Permission is hereby granted, free of charge, to any person
  16. * obtaining a copy of this software and associated documentation files
  17. * (the "Software"), to deal in the Software without restriction,
  18. * including without limitation the rights to use, copy, modify, merge,
  19. * publish, distribute, sublicense, and/or sell copies of the Software,
  20. * and to permit persons to whom the Software is furnished to do so,
  21. * subject to the following conditions:
  22. * The above copyright notice and this permission notice shall be included
  23. * in all copies or substantial portions of the Software.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  28. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  29. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
  30. * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  31. * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. */
  33. ?>
  34. <?php
  35. class RepositoryController extends Controller
  36. {
  37. /**
  38. * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
  39. * using two-column layout. See 'protected/views/layouts/column2.php'.
  40. */
  41. //public $layout='//layouts/column2';
  42. /**
  43. * @return array action filters
  44. */
  45. public function filters()
  46. {
  47. return array(
  48. 'rights', // perform access control for CRUD operations
  49. );
  50. }
  51. /**
  52. * Creates a new model.
  53. * If creation is successful, the browser will be redirected to the 'view' page.
  54. */
  55. public function actionCreate($identifier)
  56. {
  57. $_GET['projectname'] = Project::getProjectNameFromIdentifier($identifier);
  58. $model=new Repository;
  59. // Uncomment the following line if AJAX validation is needed
  60. // $this->performAjaxValidation($model);
  61. if(isset($_POST['Repository']))
  62. {
  63. $model->attributes=$_POST['Repository'];
  64. $model->identifier = preg_replace( '/\s*/m', '', strtolower($model->name));
  65. $directory = Yii::app()->file->set('repositories', true);
  66. $model->local_path = $directory->getRealPath() . DIRECTORY_SEPARATOR . $model->identifier;
  67. if($model->save()){
  68. $this->redirect(array('project/settings','identifier'=>$identifier, 'tab' => 'repositories'));
  69. } else {
  70. if (PHP_OS === 'WINNT') {
  71. $commandString = 'start /b rmdir /S /Q "'.$model->local_path.'"';
  72. } else {
  73. $commandString = 'rm -rf "'.$model->local_path.'"';
  74. }
  75. pclose(popen($commandString, 'r'));
  76. }
  77. }
  78. $this->render('create',array(
  79. 'model'=>$model,
  80. ));
  81. }
  82. /**
  83. * Updates a particular model.
  84. * If update is successful, the browser will be redirected to the 'view' page.
  85. * @param integer $id the ID of the model to be updated
  86. */
  87. public function actionUpdate($id, $identifier)
  88. {
  89. $_GET['projectname'] = Project::getProjectNameFromIdentifier($identifier);
  90. $model=$this->loadModel($id);
  91. // Uncomment the following line if AJAX validation is needed
  92. // $this->performAjaxValidation($model);
  93. if(isset($_POST['Repository']))
  94. {
  95. $model->attributes=$_POST['Repository'];
  96. if($model->save())
  97. $this->redirect(array('project/settings','identifier'=>$identifier, 'tab' => 'repositories'));
  98. }
  99. $this->render('update',array(
  100. 'model'=>$model,
  101. ));
  102. }
  103. /**
  104. * Deletes a particular model.
  105. * If deletion is successful, the browser will be redirected to the 'index' page.
  106. * @param integer $id the ID of the model to be deleted
  107. */
  108. public function actionDelete($id)
  109. {
  110. if(Yii::app()->request->isPostRequest)
  111. {
  112. // we only allow deletion via POST request
  113. $model = $this->loadModel($id);
  114. if($model->delete())
  115. {
  116. if (PHP_OS === 'WINNT') {
  117. $commandString = 'start /b rmdir /S /Q "'.$model->local_path.'"';
  118. } else {
  119. $commandString = 'rm -rf "'.$model->local_path.'"';
  120. }
  121. pclose(popen($commandString, 'r'));
  122. }
  123. // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
  124. if(!isset($_GET['ajax']))
  125. $this->redirect(array('project/settings','identifier'=>$_GET['identifier'], 'tab' => 'repositories'));
  126. }
  127. else
  128. throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
  129. }
  130. /**
  131. * Returns the data model based on the primary key given in the GET variable.
  132. * If the data model is not found, an HTTP exception will be raised.
  133. * @param integer the ID of the model to be loaded
  134. */
  135. public function loadModel($id)
  136. {
  137. $model=Repository::model()->findByPk((int)$id);
  138. if($model===null)
  139. throw new CHttpException(404,'The requested page does not exist.');
  140. return $model;
  141. }
  142. /**
  143. * Performs the AJAX validation.
  144. * @param CModel the model to be validated
  145. */
  146. protected function performAjaxValidation($model)
  147. {
  148. if(isset($_POST['ajax']) && $_POST['ajax']==='repository-form')
  149. {
  150. echo CActiveForm::validate($model);
  151. Yii::app()->end();
  152. }
  153. }
  154. }