Location: PHPKode > projects > CMS Admin > www/modules/managecontent.php
<?php
$module = new ManageContent();
$mod = 'managecontent';
require_once dirname(__FILE__).'/../classes/user.php';
require_once dirname(__FILE__).'/../classes/course.php';
require_once dirname(__FILE__).'/../classes/content.php';
require_once dirname(__FILE__).'/../classes/contentmanager.php';
require_once dirname(__FILE__).'/../layout/layout.php';
require_once dirname(__FILE__).'/../include/login.php';


class ManageContent
{
	private $course;

	function main(User $user, Permissions $perm, $course)
	{
		$this->course = $course;
		$c = new Course($course);

		if($user->userLevel <= $perm->permissions['modifyContent'] && $c->isUserInCourse($user->userID, $course) ||
			$user->userLevel == ADMIN) {
			// send the file first. can't let any other headers get by.
			if(isset($_GET['get'])) { //XXX:checkpermission
				$cm = new ContentManager($this->course);
				$c = new Content($_GET['get']);
				$cm->getFile($c);
			} else {
				Layout::header('Manage Content');
				Layout::sidebar($user, $this->course);
				echo '<div id="main">';

				if(isset($_GET['action'])) {
					switch($_GET['action']) {
					case 'add':
						$this->addContent();
						break;
					case 'modify':
						$this->modifyContent();
						break;
					case 'delete':
						$this->deleteContent();
						break;
					}
				} else if(isset($_GET['add'])) {
					$this->addContentForm();
				} else if(isset($_GET['contentID'])) {
					$this->modifyContentForm();
				} else {
					$this->menu();
				}

				echo "</div>\n";
				Layout::footer();
			}

		} else {
			echo 'insufficient privledges';
		}
	}

	public function addContentForm()
	{
		$this->printForm(new Content(0), 'add');
	}

	public function modifyContentForm()
	{
		$c = new Content($_GET['contentID']);

		echo '<div id="linklist" class="linklist"><h5><a href="managecontent.php?c='.$c->courseID.'&amp;action=delete&amp;contentID=' . $c->ID . '">Delete Content</a></h5></div>'."\n";
		$action = 'modify&amp;contentID=' . $c->ID;
		$this->printForm($c, $action);
	}

	public function addContent()
	{
		$a = new ContentManager($this->course);

		$c = new Content(0);
		$c->courseID = $this->course;
		$c->description = htmlspecialchars($_POST['description']);
		$c->visible = htmlspecialchars($_POST['visible']);
		$c->datePosted = date('Y-m-d');

		echo '<div id="message" class="message">';

		if($a->add($c)) {
			echo '<h5>Content successfully added.</h5></div>';
			$this->menu();
		} else {
			echo "<ul>\n";
			foreach($a->error as $error)
				echo "<li>$error</li>\n";
			echo "</ul>\n";
			echo '</div>';

			$this->printForm($c, 'add');
		}
	}

	public function modifyContent()
	{

		$content = new Content($_GET['contentID']);
		$cm = new ContentManager($this->course);

		$content->description = $_POST['description'];
		$content->visible = ($_POST['visible'] == 'on') ? 1 : 0;

		echo '<div id="message" class="message">';

		if($cm->updateFile($content))
		{
            echo '<h5>Content successfully updated.</h5></div>';
            $this->menu();
        } else {

            echo "<ul>\n";
            foreach($a->error as $error)
                echo "<li>$error</li>\n";
            echo "</ul>\n";
            echo '</div>';

            $this->printForm($content, $action);
        }  
	}


	public function deleteContent()
	{
		// more error checking would be good, but not critical since the delete would just silently fail
		$a = new ContentManager($this->course);
		$a->removeContent($_GET['contentID']);
		echo '<div id="message"><h5>Content successfully removed.</h5></div>';
		$this->menu();
	}

	public function menu()
	{
		$a = new ContentManager($this->course);

		echo '<div id="linklist" class="linklist"><h5><a href="managecontent.php?c='.$this->course.'&amp;add">Add Content</a></h5></div>';
		echo '<div id="linklist" class="linklist">';
		echo '<h5>Content</h5>';


		echo '<ul>';

		$data = $a->listContent();
		for ($i=0; $i<$data->numRows(); ++$i)
		{
			$rowData = $data->fetchRow();
			echo '<li><a href="managecontent.php?c='. $this->course.'&amp;contentID=' . $rowData['ContentID'] .'">';
			echo $rowData['ContentID'] .' '. $rowData['FileName'] .' - '. $rowData['Description'] .' - '. $rowData['DatePosted'] .'</a></li>';
		}
		echo '</ul>';
		echo '</div>';
	}

	private function printForm(Content $c, $action)
	{
		$title = ($action == 'add') ? 'Add Content' : 'Modify Content';
		$curfile = ($action == 'add') ? '': '<li><label>Current File</label><a href="managecontent.php?c='.$c->courseID.'&amp;get='.$c->ID.'">'.$c->filename.'</a></li>';
		$newfile = ($action == 'add') ? 'File' : 'New File';
		$selected_visible = ($c->visible) ? 'checked="checked"' : '';

		echo <<< END
<div id="linklist" class="linklist"><h5>$title</h5>
<form enctype="multipart/form-data" method="post" action="managecontent.php?c=$this->course&amp;action=$action">
<ul>$curfile
<li><label>$newfile</label><input type="file" name="filename" /></li>
<li><label>Description</label><input type="text" name="description" value="$c->description"/></li>
<li><label>Visible</label><input type="checkbox" name="visible" $selected_visible/></li>
<li><label>&nbsp;</label><input type="submit" value="$title" /></li>
</ul>
</form>
</div>
END;
	}

}
?>
Return current item: CMS Admin