generic blocks, now with database storage
This commit is contained in:
		
							parent
							
								
									1bfe2d3f34
								
							
						
					
					
						commit
						a69d80d950
					
				| @ -29,53 +29,81 @@ | ||||
|  */ | ||||
| 
 | ||||
| class Blocks extends Extension { | ||||
| 	public function onPageRequest(PageRequestEvent $event) { | ||||
| 		global $config, $page; | ||||
| 		$all = $config->get_string("blocks_text"); | ||||
| 		$blocks = explode("----", $all); | ||||
| 		foreach($blocks as $block) { | ||||
| 			$title = ""; | ||||
| 			$text = ""; | ||||
| 			$area = "left"; | ||||
| 			$pri = 50; | ||||
| 			$pages = "*"; | ||||
| 
 | ||||
| 			$lines = explode("\n", $block); | ||||
| 			foreach($lines as $line) { | ||||
| 				if(strpos($line, ":")) { | ||||
| 					$parts = explode(":", $line, 2); | ||||
| 					$parts[0] = trim($parts[0]); | ||||
| 					$parts[1] = trim($parts[1]); | ||||
| 					if($parts[0] == "Title") { | ||||
| 						$title = $parts[1]; | ||||
| 						continue; | ||||
| 					} | ||||
| 					if($parts[0] == "Area") { | ||||
| 						$area = $parts[1]; | ||||
| 						continue; | ||||
| 					} | ||||
| 					if($parts[0] == "Priority") { | ||||
| 						$pri = (int)$parts[1]; | ||||
| 						continue; | ||||
| 					} | ||||
| 					if($parts[0] == "Pages") { | ||||
| 						$pages = $parts[1]; | ||||
| 						continue; | ||||
| 					} | ||||
| 				} | ||||
| 				$text = $text . "\n" . $line; | ||||
| 			} | ||||
| 			if(fnmatch($pages, implode("/", $event->args))) { | ||||
| 				$page->add_block(new Block($title, $text, $area, $pri)); | ||||
| 			} | ||||
| 	public function onInitExt(InitExtEvent $event) { | ||||
| 		global $config, $database; | ||||
| 		if($config->get_int("ext_blocks_version") < 1) { | ||||
| 			$database->create_table("blocks", " | ||||
| 				id SCORE_AIPK, | ||||
| 				pages VARCHAR(128) NOT NULL, | ||||
| 				title VARCHAR(128) NOT NULL, | ||||
| 				area VARCHAR(16) NOT NULL, | ||||
| 				priority INTEGER NOT NULL, | ||||
| 				content TEXT NOT NULL, | ||||
| 				INDEX (pages) | ||||
| 			");
 | ||||
| 			$config->set_int("ext_blocks_version", 1); | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	public function onSetupBuilding(SetupBuildingEvent $event) { | ||||
| 		$sb = new SetupBlock("Blocks"); | ||||
| 		$sb->add_label("See <a href='".make_link("ext_doc/blocks")."'>the docs</a> for formatting"); | ||||
| 		$sb->add_longtext_option("blocks_text"); | ||||
| 		$event->panel->add_block($sb); | ||||
| 	public function onUserBlockBuilding(UserBlockBuildingEvent $event) { | ||||
| 		global $user; | ||||
| 		if($user->can("manage_blocks")) { | ||||
| 			$event->add_link("Blocks Editor", make_link("blocks/list")); | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	public function onPageRequest(PageRequestEvent $event) { | ||||
| 		global $config, $database, $page, $user; | ||||
| 
 | ||||
| 		$blocks = $database->get_all("SELECT * FROM blocks"); | ||||
| 		foreach($blocks as $block) { | ||||
| 			if(fnmatch($block['pages'], implode("/", $event->args))) { | ||||
| 				$page->add_block(new Block($block['title'], $block['content'], $block['area'], $block['priority'])); | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 		if($event->page_matches("blocks") && $user->can("manage_blocks")) { | ||||
| 			if($event->get_arg(0) == "add") { | ||||
| 				if($user->check_auth_token()) { | ||||
| 					$database->execute(" | ||||
| 						INSERT INTO blocks (pages, title, area, priority, content) | ||||
| 						VALUES (?, ?, ?, ?, ?) | ||||
| 					", array($_POST['pages'], $_POST['title'], $_POST['area'], (int)$_POST['priority'], $_POST['content']));
 | ||||
| 					$page->set_mode("redirect"); | ||||
| 					$page->set_redirect(make_link("blocks/list")); | ||||
| 				} | ||||
| 			} | ||||
| 			if($event->get_arg(0) == "update") { | ||||
| 				if($user->check_auth_token()) { | ||||
| 					if(!empty($_POST['delete'])) { | ||||
| 						$database->execute(" | ||||
| 							DELETE FROM blocks | ||||
| 							WHERE id=? | ||||
| 						", array($_POST['id']));
 | ||||
| 					} | ||||
| 					else { | ||||
| 						$database->execute(" | ||||
| 							UPDATE blocks SET pages=?, title=?, area=?, priority=?, content=? | ||||
| 							WHERE id=? | ||||
| 						", array($_POST['pages'], $_POST['title'], $_POST['area'], (int)$_POST['priority'], $_POST['content'], $_POST['id']));
 | ||||
| 					} | ||||
| 					$page->set_mode("redirect"); | ||||
| 					$page->set_redirect(make_link("blocks/list")); | ||||
| 				} | ||||
| 			} | ||||
| 			else if($event->get_arg(0) == "remove") { | ||||
| 				if($user->check_auth_token()) { | ||||
| 					$database->execute("DELETE FROM blocks WHERE id=:id", array("id" => $_POST['id'])); | ||||
| 					log_info("alias_editor", "Deleted Block #".$_POST['id']); | ||||
| 
 | ||||
| 					$page->set_mode("redirect"); | ||||
| 					$page->set_redirect(make_link("blocks/list")); | ||||
| 				} | ||||
| 			} | ||||
| 			else if($event->get_arg(0) == "list") { | ||||
| 				$this->theme->display_blocks($database->get_all("SELECT * FROM blocks")); | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| ?>
 | ||||
|  | ||||
| @ -1,30 +1,9 @@ | ||||
| <?php | ||||
| class BlocksTest extends SCoreWebTestCase { | ||||
| 	function testNews() { | ||||
| 	function testBlocks() { | ||||
| 		$this->log_in_as_admin(); | ||||
| 
 | ||||
| 		$this->get_page("setup"); | ||||
| 		$this->set_field("_config_blocks_text", " | ||||
| Title: some text | ||||
| Area: main | ||||
| Priority: 100 | ||||
| Pages: * | ||||
| 
 | ||||
| waffles | ||||
| ");
 | ||||
| 		$this->click("Save Settings"); | ||||
| 
 | ||||
| 		$this->get_page("post/list"); | ||||
| 		$this->assert_text("some text"); | ||||
| 		$this->assert_text("waffles"); | ||||
| 
 | ||||
| 		$this->get_page("setup"); | ||||
| 		$this->set_field("_config_blocks_text", ""); | ||||
| 		$this->click("Save Settings"); | ||||
| 
 | ||||
| 		$this->get_page("post/list"); | ||||
| 		$this->assert_no_text("some text"); | ||||
| 		$this->assert_no_text("waffles"); | ||||
| 		$this->get_page("blocks/list"); | ||||
| 
 | ||||
| 		$this->log_out(); | ||||
| 	} | ||||
|  | ||||
							
								
								
									
										43
									
								
								contrib/blocks/theme.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								contrib/blocks/theme.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,43 @@ | ||||
| <?php | ||||
| class BlocksTheme extends Themelet { | ||||
| 	public function display_blocks($blocks) { | ||||
| 		global $page, $user; | ||||
| 
 | ||||
| 		$html = "<table class='form' style='width: 100%;'>"; | ||||
| 		foreach($blocks as $block) { | ||||
| 			$html .= make_form(make_link("blocks/update")); | ||||
| 			$html .= "<input type='hidden' name='id' value='".html_escape($block['id'])."'>"; | ||||
| 			$html .= "<tr>"; | ||||
| 			$html .= "<th>Title</th><td><input type='text' name='title' value='".html_escape($block['title'])."'></td>"; | ||||
| 			$html .= "<th>Area</th><td><input type='text' name='area' value='".html_escape($block['area'])."'></td>"; | ||||
| 			$html .= "<th>Priority</th><td><input type='text' name='priority' value='".html_escape($block['priority'])."'></td>"; | ||||
| 			$html .= "<th>Pages</th><td><input type='text' name='pages' value='".html_escape($block['pages'])."'></td>"; | ||||
| 			$html .= "<th>Delete</th><td><input type='checkbox' name='delete'></td>"; | ||||
| 			$html .= "<td><input type='submit' value='Save'></td>"; | ||||
| 			$html .= "</tr>"; | ||||
| 			$html .= "<tr>"; | ||||
| 			$html .= "<td colspan='11'><textarea rows='5' name='content'>".html_escape($block['content'])."</textarea></td>"; | ||||
| 			$html .= "</tr>\n"; | ||||
| 			$html .= "</form>\n"; | ||||
| 		} | ||||
| 		$html .= make_form(make_link("blocks/add")); | ||||
| 			$html .= "<tr>"; | ||||
| 			$html .= "<th>Title</th><td><input type='text' name='title' value=''></td>"; | ||||
| 			$html .= "<th>Area</th><td><select name='area'><option>left<option>main</select></td>"; | ||||
| 			$html .= "<th>Priority</th><td><input type='text' name='priority' value='50'></td>"; | ||||
| 			$html .= "<th>Pages</th><td><input type='text' name='pages' value='post/list/*'></td>"; | ||||
| 			$html .= "<td><input type='submit' value='Add'></td>"; | ||||
| 			$html .= "</tr>"; | ||||
| 			$html .= "<tr>"; | ||||
| 			$html .= "<td colspan='11'><textarea rows='5' name='content'></textarea></td>"; | ||||
| 			$html .= "</tr>\n"; | ||||
| 		$html .= "</form>"; | ||||
| 		$html .= "</table>"; | ||||
| 
 | ||||
| 		$page->set_title("Blocks"); | ||||
| 		$page->set_heading("Blocks"); | ||||
| 		$page->add_block(new NavBlock()); | ||||
| 		$page->add_block(new Block("Block Editor", $html)); | ||||
| 	} | ||||
| } | ||||
| ?>
 | ||||
| @ -71,6 +71,8 @@ new UserClass("base", null, array( | ||||
| 	"edit_wiki_page" => False, | ||||
| 	"delete_wiki_page" => False, | ||||
| 
 | ||||
| 	"manage_blocks" => False, | ||||
| 
 | ||||
| 	"protected" => False,          # only admins can modify protected users (stops a moderator changing an admin's password)
 | ||||
| )); | ||||
| 
 | ||||
| @ -111,6 +113,7 @@ new UserClass("admin", "base", array( | ||||
| 	"view_image_report" => True, | ||||
| 	"edit_wiki_page" => True, | ||||
| 	"delete_wiki_page" => True, | ||||
| 	"manage_blocks" => True, | ||||
| 	"protected" => True, | ||||
| )); | ||||
| 
 | ||||
|  | ||||
| @ -6,6 +6,7 @@ TD>INPUT[type="submit"] {width: 100%;} | ||||
| TD>INPUT[type="text"] {width: 100%;} | ||||
| TD>INPUT[type="password"] {width: 100%;} | ||||
| TD>SELECT {width: 100%;} | ||||
| TD>TEXTAREA {width: 100%;} | ||||
| 
 | ||||
| TABLE.form {width: 300px;} | ||||
| TABLE.form TD, TABLE.form TH {vertical-align: middle;} | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user