aboutsummaryrefslogtreecommitdiff
path: root/admin/blocks/functions.php
blob: 84c20b57022ced60b23e9e746b5f05ae2c046a2e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
/**
 * Bayonet Content Management System
 * Copyright (C) 2008  Joseph Hunkeler & Evan O'Connell
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */ 

function ListBlocks()
{
  global $db;
  $result = $db->Query("SELECT * FROM `bayonet_blocks` ORDER BY `position`, `active` DESC, `weight` ASC");
  $blocks = $db->Fetch($result);
  
  
  echo "<table style=\"text-align:left;\" width=\"75%\">
  			<tr><th colspan=\"6\" style=\"text-align:center;\">Existing Blocks</th></tr>";
  echo "<tr><th>Weight</th><th>Position</th><th>Name</th><th>Active</th><th></th><th></th></tr>";
  foreach($blocks as $block)
  {
    echo "<tr><td>{$block['weight']}</td><td>";
    	if($block['position'])
    		echo "Right";
   		else
   			echo "Left";
	echo "</td><td>{$block['title']}</td><td>{$block['active']}</td><td><a href=\"?load=admin&op=blocks&edit={$block['block_id']}\">Edit</a></td><td><a href=\"?load=admin&op=blocks&delete={$block['block_id']}\">Delete</a></td></tr>";
  }
  echo "</table>";
}

function NewBlock()
{
  global $db;
  if(isset($_POST['processed']))
  {
    //Secure our data to prevent injection attacks.
    $weight = addslashes($_POST['weight']);
    $dir_name = addslashes($_POST['dir_name']);
    $position = addslashes($_POST['position']);
    $active = addslashes($_POST['active']);
    $title = addslashes($_POST['title']);
    
    if(!is_int($weight) || empty($dir_name) || empty($title) || !is_int($position))
    {
      echo "You must fill everything out before proceeding.";
      return;
    }
    //Update the database with the new data.
    $db->Query("INSERT INTO `bayonet_blocks` SET `weight` = '$weight', `dir_name` = '$dir_name', `title` = '$title', `position` = '$position', `active` = '$active'");
    //die, because we have completed what we wanted to do.
    echo "New block, '$dir_name', at position '$weight' added.\n";
    return;
  }
    
  ?>
  You are currently adding a new block<br /><br />
  <form action="<?php $_SERVER['PHP_SELF']?>" method="post">
  <table align="center">
  <tr><th>Title</th><td><input type="text" name="title" value="<?php echo $block['title'] ?>" /></td></tr>
  <tr><th>Weight</th><td><input type="text" name="weight" value="<?php echo $block['weight'] ?>" /></td></tr>
  <tr><th>Position</th><td><?php GetPosition(); ?></td></tr>
  <tr><th>Directory Name</th><td><input type="text" name="dir_name" value="<?php echo $block['dir_name'] ?>" /></td>
  <tr><th>Active</th><td>
  <select name="active">
    <option value="1">Yes</option>
    <option value="0">No</option>
  </select></td>
  <tr><th colspan="2"><input type="submit" name="processed" value="Submit" /><?php echo LinkInternal('<input type="button" value="Cancel" />', "?op=blocks"); ?></th></tr>
  </table>
  </form>
  <?php
}

function GetActive($block_id, &$active)
{ 
  $options = array(1 => 'Yes',0 => 'No');
  foreach($options as $option => $value)
  {
    $selected = NULL;
    if($active == $option)
    {
      $selected = "selected";
    }
    echo "<option " . $selected . " value=\"". $option ."\">" . $value . "</option>\n";  
  }
  

}

function EditBlock($block_id)
{
  global $db;
  if(isset($_POST['processed']))
  {
    //Secure our data to prevent injection attacks.
    $weight = (int) addslashes($_POST['weight']);
    $dir_name = addslashes($_POST['dir_name']);
    $position = (int) addslashes($_POST['position']);
    $active = addslashes($_POST['active']);
    $title = addslashes($_POST['title']);
    
    if(!is_int($weight) || empty($dir_name) || empty($title) || !is_int($position))
    {
      echo "You must fill everything out before proceeding.";
      return;
    }
    
    //Update the database with the new data.
    $db->Query("UPDATE bayonet_blocks SET title = '$title', weight = '$weight', dir_name = '$dir_name', position = '$position', active = '$active' WHERE block_id = '$block_id'");
    //$isActive = $active ? "IS" : "IS NOT";
    echo "Block, '$dir_name', at position '$weight'(order) has been edited.\n";
    PageRedirect(3, "?op=blocks");
    //die, because we have completed what we wanted to do.
    return;
  }
  
  //Grab the page from the database according to the $page_id passed to the function.
  $result = $db->Query("SELECT weight,dir_name,position,active,title FROM bayonet_blocks WHERE block_id = '$block_id'");
  $block = $db->FetchRow($result);
 
  ?>
  You are currently editing the '<?php echo $block['title']; ?>' block<br /><br />
  <form action="<?php $_SERVER['PHP_SELF']?>" method="post">
  <table align="center">
  <tr><th>Title</th><td><input type="text" name="title" value="<?php echo $block['title'] ?>" /></td></tr>
  <tr><th>Weight</th><td><input type="text" name="weight" value="<?php echo $block['weight'] ?>" /></td></tr>
  <tr><th>Position</th><td><?php GetPosition($block['position']); ?></td></tr>
  <tr><th>Directory Name</th><td><input type="text" name="dir_name" value="<?php echo $block['dir_name'] ?>" /></td>
  <tr><th>Active</th><td>
    <select name="active">
      <?php GetActive($block_id, $block['active']) ?>  
    </select>
  </td>
  
  <tr><th colspan="2"><input type="submit" name="processed" value="Submit" /><?php echo LinkInternal('<input type="button" value="Cancel" />', "?op=blocks"); ?></th></tr>
  </table>
  </form>
  <?php
}

function DeleteBlock($block_id)
{
  global $db;
  
  $result = $db->Query("SELECT dir_name FROM bayonet_blocks WHERE block_id = '$block_id'");
  $block = $db->Fetch($result);
  
  if(isset($_POST['proceed']))
  {
    echo "Block '{$block['dir_name']}', was deleted.";
    $db->Query("DELETE FROM bayonet_blocks WHERE block_id = '$block_id' LIMIT 1");
    return;
  }
  if(isset($_POST['cancel']))
  {
    echo "User cancelled deletion of page: '{$block['dir_name']}'";
    return;
  }
  
  ?>
  <form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
  <table align="center">
  <th>Are you SURE you want to delete the block titled: '<?php echo $block['dir_name']?>'?</th>
  <tr><th><button name="proceed">Yes</button>&nbsp;&nbsp;&nbsp;<button name="cancel">No</button></th></tr>
  </table>
  </form>
  <?php  
}

/**
 * GetPosition($value)
 * -prints a drop down menu for positions defaulting the right pos.
 */
function GetPosition($value = 0){

	echo "<select name=\"position\">";
	if(!$value){
		echo "<option value=\"0\" selected>Left</option>";
		echo "<option value=\"1\">Right</option>";		
	}else{
		echo "<option value=\"0\">Left</option>";
		echo "<option value=\"1\" selected>Right</option>";
	}
	echo "</select>";
}

?>