| 1 | <?php |
| 2 | /* |
| 3 | * Copyright (C) 2003-2004 Polytechnique.org |
| 4 | * http://opensource.polytechnique.org/ |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | |
| 22 | require_once 'Barrel.php'; |
| 23 | require_once 'Barrel/Options.php'; |
| 24 | |
| 25 | |
| 26 | /** This class provides a viewer/editor for Diogenes's barrels |
| 27 | */ |
| 28 | class Diogenes_Barrel_Editor |
| 29 | { |
| 30 | /** Is this barrel editor read only (i.e. a list) ? */ |
| 31 | var $readonly = 0; |
| 32 | |
| 33 | |
| 34 | /** Adds a barrel to the list of barrels we display. |
| 35 | * |
| 36 | * @param $page |
| 37 | * @param $barrel |
| 38 | */ |
| 39 | function addBarrel(&$page, $barrel) |
| 40 | { |
| 41 | global $page; |
| 42 | |
| 43 | $actions = array(); |
| 44 | if (!$this->readonly) { |
| 45 | array_push($actions, array(__("edit"), "?action=edit&target={$barrel['alias']}")); |
| 46 | } |
| 47 | array_push($actions, array(__("admin"), $page->urlBarrel($barrel['alias'],$barrel['vhost'],"admin/"))); |
| 48 | |
| 49 | if (!$this->readonly) { |
| 50 | $flags = new flagset($barrel['flags']); |
| 51 | if ($flags->hasFlag('plug')) { |
| 52 | array_push($actions, array(__("plugins"), "plugins?plug_barrel={$barrel['alias']}")); |
| 53 | } |
| 54 | array_push($actions, array(__("delete"), "javascript:del('{$barrel['alias']}');")); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | $page->append('sites',array( |
| 59 | 'alias' => $barrel['alias'], |
| 60 | 'title' => array( $barrel['title'], $page->urlBarrel($barrel['alias'],$barrel['vhost']) ), |
| 61 | 'description' => $barrel['description'], |
| 62 | 'actions' => $actions ) |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | /** Run the Barrel editor. |
| 68 | * |
| 69 | * @param $page |
| 70 | * @param $outputvar |
| 71 | */ |
| 72 | function run(&$page, $outputvar = '') |
| 73 | { |
| 74 | global $globals; |
| 75 | |
| 76 | $page->assign('post',$page->script_self()); |
| 77 | |
| 78 | $action = isset($_REQUEST["action"]) ? $_REQUEST["action"] : "list"; |
| 79 | switch($action) { |
| 80 | case "create": |
| 81 | if ($this->readonly) die("Sorry, this barrel editor is read-only."); |
| 82 | if (isset($_REQUEST["target"])) { |
| 83 | Diogenes_Barrel::create($_REQUEST["target"], $page); |
| 84 | } |
| 85 | $action="list"; |
| 86 | break; |
| 87 | case "update": |
| 88 | if ($this->readonly) die("Sorry, this barrel editor is read-only."); |
| 89 | if (isset($_REQUEST["target"])) { |
| 90 | // build flags |
| 91 | $flags = isset($_REQUEST['flags']) ? implode(",",array_values($_REQUEST['flags'])) : ""; |
| 92 | $globals->db->query("update diogenes_site set vhost='{$_REQUEST['vhost']}',flags='$flags' where alias='{$_REQUEST['target']}'"); |
| 93 | $opts = new Diogenes_Barrel_Options($_REQUEST['target']); |
| 94 | $opts->updateOption('description', $_REQUEST['description']); |
| 95 | $opts->updateOption('title', $_REQUEST['title']); |
| 96 | $opts->updateOption('keywords', $_REQUEST['keywords']); |
| 97 | } |
| 98 | $action="list"; |
| 99 | break; |
| 100 | case "edit": |
| 101 | if ($this->readonly) die("Sorry, this barrel editor is read-only."); |
| 102 | if (isset($_REQUEST["target"])) { |
| 103 | $res = $globals->db->query("select alias,vhost,flags from diogenes_site where alias='{$_REQUEST['target']}'"); |
| 104 | if (!list($target,$vhost,$flags) = mysql_fetch_row($res)) { |
| 105 | $page->info("Could not find specified site!"); |
| 106 | $action = "list"; |
| 107 | break; |
| 108 | } |
| 109 | mysql_free_result($res); |
| 110 | $page->assign('target',$target); |
| 111 | $page->assign('v_vhost',$vhost); |
| 112 | $flags = new flagset($flags); |
| 113 | $flag_opts = array('tpl' => __("custom templates"), 'plug' => __("plugins")); |
| 114 | $page->assign('v_flag_opts', $flag_opts); |
| 115 | foreach($flag_opts as $key=>$val) { |
| 116 | if ($flags->hasFlag($key)) |
| 117 | $page->append('v_flags', $key); |
| 118 | } |
| 119 | $opts = new Diogenes_Barrel_Options($target); |
| 120 | $page->assign('v_desc',$opts->description); |
| 121 | $page->assign('v_title',$opts->title); |
| 122 | $page->assign('v_keywords',$opts->keywords); |
| 123 | |
| 124 | } |
| 125 | break; |
| 126 | case "delete": |
| 127 | if ($this->readonly) die("Sorry, this barrel editor is read-only."); |
| 128 | if (isset($_REQUEST["target"])) { |
| 129 | Diogenes_Barrel::destroy($_REQUEST["target"], $page); |
| 130 | } |
| 131 | $action="list"; |
| 132 | break; |
| 133 | default: |
| 134 | $action="list"; |
| 135 | } |
| 136 | |
| 137 | if ($action == "list") { |
| 138 | $res = $globals->db->query("select s.alias,s.vhost,s.flags,o.name,o.value from diogenes_site as s left join diogenes_option as o on s.alias=o.barrel order by s.alias"); |
| 139 | $barrel = array('alias' => ''); |
| 140 | |
| 141 | while (list($s_alias,$s_vhost,$s_flags,$o_name,$o_value) = mysql_fetch_row($res)) { |
| 142 | if ($s_alias != $barrel['alias']) { |
| 143 | if ($barrel['alias']) $this->addBarrel($page, $barrel); |
| 144 | $barrel = array('alias' => $s_alias,'vhost' => $s_vhost, 'flags' => $s_flags, 'title' => '','description' => ''); |
| 145 | } |
| 146 | $barrel[$o_name] = $o_value; |
| 147 | } |
| 148 | mysql_free_result($res); |
| 149 | if ($barrel['alias']) $this->addBarrel($page, $barrel); |
| 150 | } // $action == "list" |
| 151 | |
| 152 | // values |
| 153 | $page->assign('action',$action); |
| 154 | $page->assign('readonly', $this->readonly); |
| 155 | |
| 156 | |
| 157 | // translations |
| 158 | $page->assign('msg_alias',__("alias")); |
| 159 | $page->assign('msg_title',__("site")); |
| 160 | $page->assign('msg_desc',__("description")); |
| 161 | $page->assign('msg_keywords',__("keywords")); |
| 162 | $page->assign('msg_create',__("Create a new site")); |
| 163 | $page->assign('msg_create_note',__("The site alias may only contain letters, numbers and underscores.")); |
| 164 | $page->assign('msg_submit',__("Submit")); |
| 165 | $page->assign('msg_no_barrels', __("no barrels available")); |
| 166 | $page->assign('msg_vhost',__("vhost (see note)")); |
| 167 | $page->assign('msg_vhost_note', __("<b>vhost note</b>: If you intend to serve this site from a virtualhost, you should enter the name of the virtualhost in this field.")); |
| 168 | $page->assign('msg_flags',__("flags")); |
| 169 | |
| 170 | // if requested, assign the content to be displayed |
| 171 | if (!empty($outputvar)) { |
| 172 | $page->assign($outputvar, $page->fetch('barrel-editor.tpl')); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | ?> |