From 4febdda65f88f134706fa88d20126e1acd1188f6 Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Mon, 9 Feb 2015 23:24:51 -0600 Subject: [PATCH] Issue 91: Create feature to disable a repo --- indefero/src/IDF/Form/ProjectConf.php | 13 +++++-- indefero/src/IDF/Middleware/GoogleAds.php | 2 ++ .../IDF/Migrations/30SyntaxHighlightTheme.php | 2 +- .../src/IDF/Migrations/31DisableProject.php | 36 +++++++++++++++++++ indefero/src/IDF/Precondition.php | 4 +++ indefero/src/IDF/Project.php | 8 +++++ indefero/src/IDF/Views.php | 17 +++++++-- .../src/IDF/templates/idf/admin/summary.html | 6 ++++ pluf/src/Pluf/Middleware/Stats.php | 2 ++ 9 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 indefero/src/IDF/Migrations/31DisableProject.php diff --git a/indefero/src/IDF/Form/ProjectConf.php b/indefero/src/IDF/Form/ProjectConf.php index 4663c0a..c7e7ab8 100644 --- a/indefero/src/IDF/Form/ProjectConf.php +++ b/indefero/src/IDF/Form/ProjectConf.php @@ -86,6 +86,12 @@ class IDF_Form_ProjectConf extends Pluf_Form 'initial' => $this->project->enableads, 'widget' => 'Pluf_Form_Widget_CheckboxInput', )); + $this->fields['disabled'] = new Pluf_Form_Field_Boolean( + array('required' => false, + 'label' => __('Disable Project'), + 'initial' => $this->project->disabled, + 'widget' => 'Pluf_Form_Widget_CheckboxInput', + )); } else { $this->fields['enableads'] = new Pluf_Form_Field_Boolean( array('required' => false, @@ -94,6 +100,7 @@ class IDF_Form_ProjectConf extends Pluf_Form 'widget' => 'Pluf_Form_Widget_CheckboxInput', 'widget_attrs' => array('disabled' => 'disabled') )); + } $tags = $this->project->get_tags_list(); for ($i=1;$i<7;$i++) { @@ -228,8 +235,10 @@ class IDF_Form_ProjectConf extends Pluf_Form $this->project->description = $this->cleaned_data['description']; $this->project->batchAssoc('IDF_Tag', $tagids); $this->project->syntaxtheme = $this->cleaned_data["syntaxtheme"]; - if ($this->user->administrator) - $this->project->enableads = $this->cleaned_data['enableads']; + if ($this->user->administrator) { + $this->project->enableads = $this->cleaned_data['enableads']; + $this->project->disabled = $this->cleaned_data["disabled"]; + } $this->project->update(); $conf = $this->project->getConf(); diff --git a/indefero/src/IDF/Middleware/GoogleAds.php b/indefero/src/IDF/Middleware/GoogleAds.php index 918a744..7da182e 100644 --- a/indefero/src/IDF/Middleware/GoogleAds.php +++ b/indefero/src/IDF/Middleware/GoogleAds.php @@ -40,6 +40,8 @@ class IDF_Middleware_GoogleAds */ function process_response($request, $response) { + if (isset($response) && !isset($response->status_code)) + return $response; if (!Pluf::f('google_ads', false)) { return $response; } diff --git a/indefero/src/IDF/Migrations/30SyntaxHighlightTheme.php b/indefero/src/IDF/Migrations/30SyntaxHighlightTheme.php index 211ca0f..a4d4c18 100644 --- a/indefero/src/IDF/Migrations/30SyntaxHighlightTheme.php +++ b/indefero/src/IDF/Migrations/30SyntaxHighlightTheme.php @@ -18,7 +18,7 @@ function IDF_Migrations_30SyntaxHighlightTheme_up() } -function IDF_Migrations_28OTPKey_down() +function IDF_Migrations_30SyntaxHighlightTheme_down() { $table = Pluf::factory('IDF_Project')->getSqlTable(); diff --git a/indefero/src/IDF/Migrations/31DisableProject.php b/indefero/src/IDF/Migrations/31DisableProject.php new file mode 100644 index 0000000..dbdd9fc --- /dev/null +++ b/indefero/src/IDF/Migrations/31DisableProject.php @@ -0,0 +1,36 @@ +getSqlTable(); + + $sql = array(); + + $sql["MySQL"] = "ALTER TABLE " . $table . " ADD COLUMN `disabled` int(11) NULL AFTER `current_activity`;"; + + $db = Pluf::db(); + $engine = Pluf::f('db_engine'); + if (!isset($sql[$engine])) { + throw new Exception('SQLite complex migration not supported.'); + } + + $db->execute($sql[$engine]); + +} + +function IDF_Migrations_31DisableProject_down() +{ + $table = Pluf::factory('IDF_Project')->getSqlTable(); + + $sql = array(); + + $sql["MySQL"] = "ALTER TABLE " . $table . " DROP COLUMN `disabled`;"; + + $db = Pluf::db(); + $engine = Pluf::f('db_engine'); + if (!isset($sql[$engine])) { + throw new Exception('SQLite complex migration not supported.'); + } + + $db->execute($sql[$engine]); +} \ No newline at end of file diff --git a/indefero/src/IDF/Precondition.php b/indefero/src/IDF/Precondition.php index 163f457..4f4be8f 100644 --- a/indefero/src/IDF/Precondition.php +++ b/indefero/src/IDF/Precondition.php @@ -34,6 +34,10 @@ class IDF_Precondition */ static public function baseAccess($request) { + if ($request->user->administrator) + return true; + if ($request->project->disabled) + return false; if (!$request->project->private) { return true; } diff --git a/indefero/src/IDF/Project.php b/indefero/src/IDF/Project.php index 6f4845b..3e48e7a 100644 --- a/indefero/src/IDF/Project.php +++ b/indefero/src/IDF/Project.php @@ -117,6 +117,14 @@ class IDF_Project extends Pluf_Model 'default' => 1, ), + 'disabled' => + array( + 'type' => 'Pluf_DB_Field_Integer', + 'blank' => false, + 'verbose' => __('disabled'), + 'default' => 0, + ), + 'syntaxtheme' => array( 'type' => 'Pluf_DB_Field_Text', diff --git a/indefero/src/IDF/Views.php b/indefero/src/IDF/Views.php index 9bea911..1b7df7d 100644 --- a/indefero/src/IDF/Views.php +++ b/indefero/src/IDF/Views.php @@ -393,6 +393,15 @@ class IDF_Views // anonymous users can only see non-private projects $false = Pluf_DB_BooleanToDb(false, $db); + $true = Pluf_DB_BooleanToDb(true, $db); + + $dbpfx = $db->pfx; + $disabled_results = $db->select("SELECT id FROM ${dbpfx}idf_projects WHERE disabled = $true"); + $disabled_ids = []; + foreach($disabled_results as $id) { + $disabled_ids[] = $id['id']; + } + $sql_results = $db->select( 'SELECT id FROM '.$db->pfx.'idf_projects '. 'WHERE '.$db->qn('private').'='.$false @@ -400,9 +409,13 @@ class IDF_Views $ids = array(); foreach ($sql_results as $id) { - $ids[] = $id['id']; + if (!in_array($id['id'], $disabled_ids)) { + $ids[] = $id['id']; + } } + + // registered users may additionally see private projects with which // they're somehow affiliated if (!$user->isAnonymous()) { @@ -418,7 +431,7 @@ class IDF_Views $rows = Pluf::factory('Pluf_RowPermission')->getList(array('filter' => $permSql->gen())); if ($rows->count() > 0) { foreach ($rows as $row) { - if (in_array($row->model_id, $ids)) + if (in_array($row->model_id, $ids) || in_array($row->model_id, $disabled_ids)) continue; $ids[] = $row->model_id; } diff --git a/indefero/src/IDF/templates/idf/admin/summary.html b/indefero/src/IDF/templates/idf/admin/summary.html index ebb700b..90536da 100644 --- a/indefero/src/IDF/templates/idf/admin/summary.html +++ b/indefero/src/IDF/templates/idf/admin/summary.html @@ -77,6 +77,12 @@ {$form.f.enableads|unsafe} + + {$form.f.disabled.labelTag}: + {if $form.f.disabled.errors}{$form.f.disabled.fieldErrors}{/if} + {$form.f.disabled|unsafe} + + {$form.f.syntaxtheme.labelTag}: {if $form.f.syntaxtheme.errors}{$form.f.syntaxtheme.fieldErrors}{/if} diff --git a/pluf/src/Pluf/Middleware/Stats.php b/pluf/src/Pluf/Middleware/Stats.php index c32ad55..0703d15 100644 --- a/pluf/src/Pluf/Middleware/Stats.php +++ b/pluf/src/Pluf/Middleware/Stats.php @@ -42,6 +42,8 @@ class Pluf_Middleware_Stats */ function process_response($request, $response) { + if (isset($response) && !isset($response->status_code)) + return $response; if (!in_array($response->status_code, array(200, 201, 202, 203, 204, 205, 206, 404, 501))) { return $response;