· 6 years ago · Jan 04, 2020, 12:36 PM
1Codeigniter web application blueprints.pdf
2
3application/
4├── controllers/
5│ ├── discussions.php
6│ ├── comments.php
7│ ├── admin.php
8├── models/
9│ ├── comments_model.php
10│ ├── discussions_model.php
11│ ├── admin_model.php
12├── views/discussions/
13│ ├── view.php
14│ ├── new.php
15├── views/comments/
16│ ├── view.php
17├── views/admin/
18│ ├── login.php
19│ ├── dashboard.php
20├── views/nav/
21│ ├── top_nav.php
22├── views/common/
23│ ├── login_header.php
24├── language/english/
25│ ├── en_admin_lang.php
26bootstrap/
27├── css/
28├── signin.css
29
30*Change ' to ` if mysql errors.
31
32
331)
34
35
36
37CREATE DATABASE 'discuss_forum';
38
39USE `discuss_forum';
40
41DROP TABLE IF EXISTS 'ci_sessions';
42
43CREATE TABLE 'ci_sessions' (
44'session_id' varchar(40) COLLATE utf8_bin NOT NULL DEFAULT '0',
45'ip_address' varchar(16) COLLATE utf8_bin NOT NULL DEFAULT '0',
46'user_agent' varchar(120) COLLATE utf8_bin DEFAULT NULL,
47'last_activity' int(10) unsigned NOT NULL DEFAULT '0',
48'user_data' text COLLATE utf8_bin NOT NULL,
49PRIMARY KEY ('session_id'),
50KEY 'last_activity_idx' ('last_activity')
51) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
52
53DROP TABLE IF EXISTS 'comments';
54
55CREATE TABLE 'comments' (
56'cm_id' int(11) NOT NULL AUTO_INCREMENT,
57'ds_id' int(11) NOT NULL,
58'cm_body' text NOT NULL,
59'cm_created_at' timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
60'usr_id' int(11) NOT NULL,
61'cm_is_active' int(1) NOT NULL,
62PRIMARY KEY ('cm_id')
63) ENGINE=InnoDB DEFAULT CHARSET=utf8;
64
65DROP TABLE IF EXISTS 'discussions';
66
67CREATE TABLE 'discussions' (
68'ds_id' int(11) NOT NULL AUTO_INCREMENT,
69'usr_id' int(11) NOT NULL,
70'ds_title' varchar(255) NOT NULL,
71'ds_body' text NOT NULL,
72'ds_created_at' timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
73'ds_is_active' int(1) NOT NULL,
74PRIMARY KEY ('ds_id')
75) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
76
77DROP TABLE IF EXISTS 'users';
78
79CREATE TABLE 'users' (
80'usr_id' int(11) NOT NULL AUTO_INCREMENT,
81'usr_name' varchar(25) NOT NULL,
82'usr_hash' varchar(255) NOT NULL,
83'usr_email' varchar(125) NOT NULL,
84'usr_created_at' timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
85'usr_is_active' int(1) NOT NULL,
86'usr_level' int(1) NOT NULL,
87PRIMARY KEY ('usr_id')
88) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
89
902)In config.php
91Change following lines:
92 Find the following line:
93$config['encryption_key'] = '';
94Change it to the following:
95$config['encryption_key'] = 'a-random-string-of-alphanumcharacters';
96
97Find the following lines:
98$config['sess_cookie_name'] = 'ci_session';
99$config['sess_expiration'] = 7200;
100$config['sess_expire_on_close'] = FALSE;
101$config['sess_encrypt_cookie'] = FALSE;
102$config['sess_use_database'] = FALSE;
103$config['sess_table_name'] = 'ci_sessions';
104$config['sess_match_ip'] = FALSE;
105$config['sess_match_useragent'] = TRUE;
106$config['sess_time_to_update'] = 300;
107Change them to the following:
108$config['sess_cookie_name'] = 'ci_session';
109$config['sess_expiration'] = 7200;
110$config['sess_expire_on_close'] = TRUE;
111$config['sess_encrypt_cookie'] = TRUE;
112$config['sess_use_database'] = TRUE;
113$config['sess_table_name'] = 'ci_sessions';
114$config['sess_match_ip'] = TRUE;
115$config['sess_match_useragent'] = TRUE;
116$config['sess_time_to_update'] = 300;
117
1183)Change routes.php
119$route['default_controller'] = "discussions";
120
1214)models/discussions_model.php
122
123<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
124class Discussions_model extends CI_Model {
125 function __construct() {
126 parent::__construct();
127 }
128 function fetch_discussions($filter = null, $direction = null) {
129 $query = "SELECT * FROM `discussions`, `users`
130 WHERE `discussions`.`usr_id` = `users`.`usr_id`
131 AND `discussions`.`ds_is_active` != '0' ";
132 if ($filter != null) {
133 if ($filter == 'age') {
134 $filter = 'ds_created_at';
135 switch ($direction) {
136 case 'ASC':
137 $dir = 'ASC';
138 break;
139 case 'DESC':
140 $dir = 'DESC';
141 break;
142 default:
143 $dir = 'ASC';
144 }
145 }
146 } else {
147 $dir = 'ASC';
148 }
149
150 $query .= "ORDER BY `ds_created_at` " . $dir;
151 $result = $this->db->query($query, array($dir));
152 if ($result) {
153 return $result;
154 } else {
155 return false;
156 }
157 }
158 function fetch_discussion($ds_id) {
159 $query = "SELECT * FROM `discussions`, `users` WHERE `ds_id` = ?
160 AND `discussions`.`usr_id` = `users`.`usr_id`";
161 return $result = $this->db->query($query, array($ds_id));
162 }
163 function create($data) {
164 // Look and see if the email address already exists in the users
165 // table, if it does return the primary key, if not create them
166 // a user account and return the primary key.
167 $usr_email = $data['usr_email'];
168 $query = "SELECT * FROM `users` WHERE `usr_email` = ? ";
169 $result = $this->db->query($query,array($usr_email));
170 if ($result->num_rows() > 0) {
171 foreach ($result->result() as $rows) {
172 $data['usr_id'] = $rows->usr_id;
173 }
174 } else {
175 $password = random_string('alnum', 16);
176 $hash = $this->encrypt->sha1($password);
177 $user_data = array('usr_email' => $data['usr_email'],
178 'usr_name' => $data['usr_name'],
179 'usr_is_active' => '1',
180 'usr_level' => '1',
181 'usr_hash' => $hash);
182 if ($this->db->insert('users',$user_data)) {
183 $data['usr_id'] = $this->db->insert_id();
184 // Send email with password???
185 }
186 }
187 $discussion_data = array('ds_title' => $data['ds_title'],
188 'ds_body' => $data['ds_body'],
189 'usr_id' => $data['usr_id'],
190 'ds_is_active' => '1');
191 if ($this->db->insert('discussions',$discussion_data) ) {
192 return $this->db->insert_id();
193 } else {
194 return false;
195 }
196 }
197 function flag($ds_id) {
198 $this->db->where('ds_id', $ds_id);
199 if ($this->db->update('discussions', array('ds_is_active' => '0'))) {
200 return true;
201 } else {
202 return false;
203 }
204 }
205}
206
207
2085)model/comments_model.php
209
210<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
211class Comments_model extends CI_Model {
212 function __construct() {
213 parent::__construct();
214 }
215 function fetch_comments($ds_id) {
216 $query = "SELECT * FROM `comments`, `discussions`, `users`
217 WHERE `discussions`.`ds_id` = ?
218 AND `comments`.`ds_id` = `discussions`.`ds_id`
219 AND `comments`.`usr_id` = `users`.`usr_id`
220 AND `comments`.`cm_is_active` = '1'
221 ORDER BY `comments`.`cm_created_at` DESC " ;
222 $result = $this->db->query($query, array($ds_id));
223 if ($result) {
224 return $result;
225 } else {
226 return false;
227 }
228 }
229 function new_comment($data) {
230 // Look and see if the email address already exists in the users
231 // table, if it does return the primary key, if not create them
232 // a user account and return the primary key.
233 $usr_email = $data['usr_email'];
234 $query = "SELECT * FROM `users` WHERE `usr_email` = ? ";
235 $result = $this->db->query($query,array($usr_email));
236 if ($result->num_rows() > 0) {
237 foreach ($result->result() as $rows) {
238 $data['usr_id'] = $rows->usr_id;
239 }
240 } else {
241 $password = random_string('alnum', 16);
242 $hash = $this->encrypt->sha1($password);
243 $user_data = array('usr_email' => $data['usr_email'],
244 'usr_name' => $data['usr_name'],
245 'usr_is_active' => '1',
246 'usr_level' => '1',
247 'usr_hash' => $hash);
248 if ($this->db->insert('users',$user_data)) {
249 $data['usr_id'] = $this->db->insert_id();
250 }
251 }
252 $comment_data = array('cm_body' => $data['cm_body'],
253 'ds_id' => $data['ds_id'],
254 'cm_is_active' => '1',
255 'usr_id' => $data['usr_id']);
256 if ($this->db->insert('comments',$comment_data) ) {
257 return $this->db->insert_id();
258 } else {
259 return false;
260 }
261 }
262 function flag($cm_id) {
263 $query = "UPDATE `comments`
264 SET `cm_is_active` = '0'
265 WHERE `cm_id` = ? ";
266 if ($this->db->query($query,array($cm_id))) {
267 return true;
268 } else {
269 return false;
270 }
271 }
272}
273
274
2756)model/admin.php
276
277<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
278class Admin_model extends CI_Model {
279 function __construct() {
280 parent::__construct();
281 }
282 function dashboard_fetch_comments() {
283 $query = "SELECT * FROM `comments`, `users`
284 WHERE `comments`.`usr_id` = `users`.`usr_id`
285 AND `cm_is_active` = '0' ";
286 $result = $this->db->query($query);
287 if ($result) {
288 return $result;
289 } else {
290 return false;
291 }
292 }
293 function dashboard_fetch_discussions() {
294 $query = "SELECT * FROM `discussions`, `users`
295 WHERE `discussions`.`usr_id` = `users`.`usr_id`
296 AND `ds_is_active` = '0' ";
297 $result = $this->db->query($query);
298 if ($result) {
299 return $result;
300 } else {
301 return false;
302 }
303 }
304 function does_user_exist($email) {
305 $this->db->where('usr_email', $email);
306 $query = $this->db->get('users');
307 return $query;
308 }
309 function update_comments($is_active, $id) {
310 if ($is_active == 1) {
311 $query = "UPDATE `comments` SET `cm_is_active` = ? WHERE `cm_id` = ? " ;
312 if ($this->db->query($query,array($is_active,$id))) {
313 return true;
314 } else {
315 return false;
316 }
317 } else {
318 $query = "DELETE FROM `comments` WHERE `cm_id` = ? " ;
319 if ($this->db->query($query,array($id))) {
320 return true;
321 } else {
322 return false;
323 }
324 }
325 }
326 function update_discussions($is_active, $id) {
327 if ($is_active == 1) {
328 $query = "UPDATE `discussions` SET `ds_is_active` = ? WHERE `ds_id` = ? " ;
329 if ($this->db->query($query, array($is_active,$id))) {
330 return true;
331 } else {
332 return false;
333 }
334 } else {
335 $query = "DELETE FROM `discussions` WHERE `ds_id` = ? " ;
336 if ($this->db->query($query,array($id))) {
337 $query = "DELETE FROM `comments` WHERE `ds_id` = ? " ;
338 if ($this->db->query($query,array($id))) {
339 return true;
340 }
341 } else {
342 return false;
343 }
344 }
345 }
346}
347
348Creating views
349• discussions/view.php: This displays all active discussions
350• discussions/new.php: This displays a form to the user, allowing them to
351create a discussion
352• comments/view.php: This displays all active comments within a discussion
353• nav/top_nav.php: This contains the top navigation links
354• admin/login.php: This displays a login form for the user; don't forget to add
355the signin.css script, which you can ind later in this chapter
356
357-admin/dashborad.php
358
359• common/login_header.php: The views/admin/login.php view requires
360different resources from the rest of the application, which is supported by
361this header
362
3637)discussions/view.php
364SORT: <?php echo anchor('discussions/index/sort/age/' . (($dir == 'ASC') ? 'DESC' : 'ASC'),'Newest '
365 . (($dir == 'ASC') ? 'DESC' : 'ASC'));?>
366
367 <table class="table table-hover">
368 <thead>
369 <tr>
370 <th><?php echo $this->lang->line('discussions_title') ; ?></th>
371 </tr>
372 </thead>
373 <tbody>
374
375 <?php foreach ($query->result() as $result) : ?>
376 <tr>
377 <td>
378 <?php echo anchor('comments/index/'.$result->ds_id,$result->ds_title) . ' '
379 . $this->lang->line('comments_created_by') . $result->usr_name; ?>
380
381 <?php Ty echo anchor('discussions/flag/'.$result->ds_id,
382 $this->lang->line('discussion_flag')) ; ?>
383 <br />
384 <?php echo $result->ds_body ; ?>
385 </td>
386 </tr>
387 <?php endforeach ; ?>
388
389 </tbody>
390 </table>
391
392
3938)comments/view.php
394
395<!-- Discussion - initial comment -->
396<?php foreach ($discussion_query->result() as $discussion_result) : ?>
397 <h2>
398 <?php echo $discussion_result->ds_title; ?><br />
399 <small><?php echo $this->lang->line('comments_created_by') . $discussion_result->usr_name . $this->lang->line('comments_created_at') . $discussion_result->ds_created_at; ?></small>
400 </h2>
401 <p class="lead"><?php echo $discussion_result->ds_body; ?></p>
402<?php endforeach ; ?>
403
404<!-- Comment - list of comments -->
405<?php foreach ($comment_query->result() as $comment_result) : ?>
406 <li class="media">
407 <a class="pull-left" href="#">
408 <img class="media-object" src="<?php echo base_url() ; ?>img/profile.svg" />
409 </a>
410 <div class="media-body">
411 <h4 class="media-heading"><?php echo $comment_result->usr_name . anchor('comments/flag/'.$comment_result->ds_id . '/' . $comment_result->cm_id,$this->lang->line('comments_flag')) ; ?></h4>
412 <?php echo $comment_result->cm_body ; ?>
413 </div>
414 </li>
415<?php endforeach ; ?>
416
417<!-- Form - begin form section -->
418<br /><br />
419<p class="lead"><?php echo $this->lang->line('comments_form_instruction');?></p>
420
421<?php echo validation_errors(); ?>
422<?php echo form_open('comments/index','role="form"') ; ?>
423 <div class="form-group col-md-5">
424 <label for="comment_name"><?php echo $this->lang->line('comments_comment_name');?></label>
425 <input type="text" name="comment_name" class="form-control" id="comment_name" value="<?php echo set_value('comment_name'); ?>">
426 </div>
427 <div class="form-group col-md-5">
428 <label for="comment_email"><?php echo $this->lang->line('comments_comment_email');?></label>
429 <input type="email" name="comment_email" class="form-control" id="comment_email" value="<?php echo set_value('comment_email'); ?>">
430 </div>
431 <div class="form-group col-md-10">
432 <label for="comment_body"><?php echo $this->lang->line('comments_comment_body');?></label>
433 <textarea class="form-control" rows="3" name="comment_body" id="comment_body"><?php echo set_value('comment_body'); ?></textarea>
434 </div>
435 <div class="form-group col-md-11">
436 <button type="submit" class="btn btn-success"><?php echo $this->lang->line('common_form_elements_go');?></button>
437 </div>
438 <?php echo form_hidden('ds_id',$ds_id) ; ?>
439<?php echo form_close() ; ?>
440
441
4429) discussions/new.php
443<!-- Form - begin form section -->
444<br /><br />
445<p class="lead"><?php echo $this->lang->line('discussion_form_instruction');?></p>
446
447<?php echo validation_errors(); ?>
448<?php echo form_open('discussions/create','role="form"') ; ?>
449 <div class="form-group col-md-5">
450 <label for="usr_name"><?php echo $this->lang->line('discussion_usr_name');?></label>
451 <input type="text" name="usr_name" class="form-control" id="usr_name" value="<?php echo set_value('usr_name'); ?>">
452 </div>
453 <div class="form-group col-md-5">
454 <label for="usr_email"><?php echo $this->lang->line('discussion_usr_email');?></label>
455 <input type="email" name="usr_email" class="form-control" id="usr_email" value="<?php echo set_value('usr_email'); ?>">
456 </div>
457 <div class="form-group col-md-10">
458 <label for="ds_title"><?php echo $this->lang->line('discussion_ds_title');?></label>
459 <input type="text" name="ds_title" class="form-control" id="ds_title" value="<?php echo set_value('ds_title'); ?>">
460 </div>
461 <div class="form-group col-md-10">
462 <label for="ds_body"><?php echo $this->lang->line('discussion_ds_body');?></label>
463 <textarea class="form-control" rows="3" name="ds_body" id="ds_body"><?php echo set_value('ds_body'); ?></textarea>
464 </div>
465 <div class="form-group col-md-11">
466 <button type="submit" class="btn btn-success"><?php echo $this->lang->line('common_form_elements_go');?></button>
467 </div>
468<?php echo form_close() ; ?>
469
47010)nav/top_nav.php
471
472<!-- Fixed navbar -->
473 <div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
474 <div class="container">
475 <div class="navbar-header">
476 <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
477 <span class="sr-only">Toggle navigation</span>
478 <span class="icon-bar"></span>
479 <span class="icon-bar"></span>
480 <span class="icon-bar"></span>
481 </button>
482 <a class="navbar-brand" href="#"><?php echo $this->lang->line('system_system_name'); ?></a>
483 </div>
484 <div class="navbar-collapse collapse">
485 <ul class="nav navbar-nav">
486 <li <?php if ($this->uri->segment(1) == '') {echo 'class="active"';} ; ?>><?php echo anchor('/', $this->lang->line('top_nav_view_discussions')) ; ?></li>
487 <li <?php if ($this->uri->segment(1) == 'discussions') {echo 'class="active"';} ; ?>><?php echo anchor('discussions/create', $this->lang->line('top_nav_new_discussion')) ; ?></li>
488 </ul>
489
490 <ul class="nav navbar-nav navbar-right">
491 <li><?php echo anchor('admin/login', $this->lang->line('top_nav_login')) ; ?></li>
492 </ul>
493 </div><!--/.nav-collapse -->
494 </div>
495 </div>
496
497 <div class="container theme-showcase" role="main">
498
49911)admin/login.php
500
501<?php if (isset($login_fail)) : ?>
502 <div class="alert alert-danger"><?php echo $this->lang->line('admin_login_error') ; ?></div>
503<?php endif ; ?>
504<?php echo validation_errors(); ?>
505
506<div class="container">
507 <?php echo form_open('admin/login', 'class="form-signin" role="form"') ; ?>
508 <h2 class="form-signin-heading"><?php echo $this->lang->line('admin_login_header') ; ?></h2>
509 <input type="email" name="usr_email" class="form-control" placeholder="<?php echo $this->lang->line('admin_login_email') ; ?>" required autofocus>
510 <input type="password" name="usr_password" class="form-control" placeholder="<?php echo $this->lang->line('admin_login_password') ; ?>" required>
511 <button class="btn btn-lg btn-primary btn-block" type="submit"><?php echo $this->lang->line('admin_login_signin') ; ?></button>
512 <?php echo form_close() ; ?>
513</div>
514
51512)common/login_header.php
516
517<!DOCTYPE html>
518<html lang="en">
519 <head>
520 <meta charset="utf-8">
521 <meta http-equiv="X-UA-Compatible" content="IE=edge">
522 <meta name="viewport" content="width=device-width, initial-scale=1">
523 <meta name="description" content="">
524 <meta name="author" content="">
525 <link rel="shortcut icon" href="<?php echo base_url('bootstrap/ico/favicon.ico'); ?>">
526
527 <title><?php echo $this->lang->line('system_system_name'); ?></title>
528
529 <!-- Bootstrap core CSS -->
530 <link href="<?php echo base_url('bootstrap/css/bootstrap.min.css'); ?>" rel="stylesheet">
531 <!-- Bootstrap theme -->
532 <link href="<?php echo base_url('bootstrap/css/bootstrap-theme.min.css'); ?>" rel="stylesheet">
533
534 <!-- Custom styles for this template -->
535 <link href="<?php echo base_url('bootstrap/css/signin.css');?>" rel="stylesheet">
536
537
538 <!-- Just for debugging purposes. Don't actually copy this line! -->
539 <!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
540
541 <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
542 <!--[if lt IE 9]>
543 <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
544 <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
545 <![endif]-->
546 </head>
547
548 <body>
549
55013)admin/dashborad.php
551
552
553
554 <h1 id="tables" class="page-header">Dashboard</h1>
555
556<table class="table">
557 <thead>
558 <tr>
559 <th>#</th>
560 <th>Name</th>
561 <th>Email</th>
562 <td>Actions</td>
563 </tr>
564 </thead>
565 <tbody>
566 <?php if ($discussion_query->num_rows() > 0) : ?>
567 <?php foreach ($discussion_query->result() as $row) : ?>
568 <tr>
569 <td><?php echo $row->ds_id ; ?></td>
570 <td><?php echo $row->usr_name ; ?></td>
571 <td><?php echo $row->usr_email ; ?></td>
572 <td><?php echo anchor('admin/update_item/ds/allow/'.
573 $row->ds_id,$this->lang->line('admin_dash_allow')) .
574 ' ' . anchor('admin/update_item/ds/disallow/'.
575 $row->ds_id,$this->lang->line('admin_dash_disallow')) ; ?>
576 </td>
577 </tr>
578 <tr>
579 <td colspan="3"><?php echo $row->ds_title; ?></td>
580 <td></td>
581 </tr>
582 <tr>
583 <td colspan="3"><?php echo $row->ds_body; ?></td>
584 <td></td>
585 </tr>
586 <?php endforeach ; ?>
587 <?php else : ?>
588 <tr>
589 <td colspan="4">No naughty forums here, horay!</td>
590 </tr>
591 <?php endif; ?>
592 </tbody>
593</table>
594
595<table class="table">
596 <thead>
597 <tr>
598 <th>#</th>
599 <th>Name</th>
600 <th>Email</th>
601 <td>Actions</td>
602 </tr>
603 </thead>
604 <tbody>
605 <?php if ($comment_query->num_rows() > 0) : ?>
606 <?php foreach ($comment_query->result() as $row) : ?>
607 <tr>
608 <td><?php echo $row->cm_id ; ?></td>
609 <td><?php echo $row->usr_name ; ?></td>
610 <td><?php echo $row->usr_email ; ?></td>
611 <td><?php echo anchor('admin/update_item/cm/allow/'.
612 $row->cm_id,$this->lang->line('admin_dash_allow')) .
613 ' ' . anchor('admin/update_item/cm/disallow/'.
614 $row->cm_id,$this->lang->line('admin_dash_disallow')) ; ?>
615 </td>
616 </tr>
617 <tr>
618 <td colspan="3"><?php echo $row->cm_body; ?></td>
619 <td></td>
620 </tr>
621 <?php endforeach ; ?>
622 <?php else : ?>
623 <tr>
624 <td colspan="4">No naughty comments here, horay!</td>
625 </tr>
626 <?php endif; ?>
627 </tbody>
628</table>
629
630
63114)add Css
632
633body {
634padding-top: 40px;
635padding-bottom: 40px;
636background-color: #eee;
637}
638.form-signin {
639max-width: 330px;
640padding: 15px;
641
642margin: 0 auto;
643}
644.form-signin .form-signin-heading,
645.form-signin .checkbox {
646margin-bottom: 10px;
647}
648.form-signin .checkbox {
649font-weight: normal;
650}
651.form-signin .form-control {
652position: relative;
653height: auto;
654-webkit-box-sizing: border-box;
655-moz-box-sizing: border-box;
656box-sizing: border-box;
657padding: 10px;
658font-size: 16px;
659}
660.form-signin .form-control:focus {
661z-index: 2;
662}
663.form-signin input[type="email"] {
664margin-bottom: -1px;
665border-bottom-right-radius: 0;
666border-bottom-left-radius: 0;
667}
668.form-signin input[type="password"] {
669margin-bottom: 10px;
670border-top-left-radius: 0;
671border-top-right-radius: 0;
672}
673
674• discussions.php: This fetches discussions from the discussions table
675in the database and allows the user to create a new discussion
676• comments.php: This fetches comments from the comments table in the
677database and allows the user to join a discussion by adding a comment
678to a discussion forum
679• admin.php: This contains basic admin functions, login functionalities,
680and moderation options
681
68215)controller/discussions.php
683
684<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
685class Discussions extends MY_Controller {
686 function __construct() {
687 parent::__construct();
688 $this->load->helper('string');
689 $this->load->library('encrypt');
690 $this->load->model('Discussions_model');
691 $this->load->library('form_validation');
692 $this->form_validation->set_error_delimiters('<div class="alert alert-danger">', '</div>');
693 }
694 public function index() {
695 if ($this->uri->segment(3)) {
696 $filter = $this->uri->segment(4);
697 $direction = $this->uri->segment(5);
698 $page_data['dir'] = $this->uri->segment(5);
699 } else {
700 $filter = null;
701 $direction = null;
702 $page_data['dir'] = 'ASC';
703 }
704 $page_data['query'] = $this->Discussions_model->fetch_discussions($filter,$direction);
705 $this->load->view('common/header');
706 $this->load->view('nav/top_nav');
707 $this->load->view('discussions/view', $page_data);
708 $this->load->view('common/footer');
709 }
710 public function create() {
711 $this->form_validation->set_rules('usr_name', $this->lang->line('discussion_usr_name'), 'required|min_length[1]|max_length[255]');
712 $this->form_validation->set_rules('usr_email', $this->lang->line('discussion_usr_email'), 'required|min_length[1]|max_length[255]');
713 $this->form_validation->set_rules('ds_title', $this->lang->line('discussion_ds_title'), 'required|min_length[1]|max_length[255]');
714 $this->form_validation->set_rules('ds_body', $this->lang->line('discussion_ds_body'), 'required|min_length[1]|max_length[5000]');
715 if ($this->form_validation->run() == FALSE) {
716 $this->load->view('common/header');
717 $this->load->view('nav/top_nav');
718 $this->load->view('discussions/new');
719 $this->load->view('common/footer');
720 } else {
721 $data = array('usr_name' => $this->input->post('usr_name'),
722 'usr_email' => $this->input->post('usr_email'),
723 'ds_title' => $this->input->post('ds_title'),
724 'ds_body' => $this->input->post('ds_body')
725 );
726 if ($ds_id = $this->Discussions_model->create($data)) {
727 redirect('comments/index/'.$ds_id);
728 } else {
729 // error
730 // load view and flash sess error
731 }
732 }
733 }
734 public function flag() {
735 $ds_id = $this->uri->segment(3);
736 if ($this->Discussions_model->flag($ds_id)) {
737 redirect('discussions/');
738 } else {
739 // error
740 // load view and flash sess error
741 }
742 }
743}
744/* End of file discussions.php */
745/* Location: ./application/controllers/discussions.php */
746
74716)controller/comments.php
748
749<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
750class Comments extends MY_Controller {
751 function __construct() {
752 parent::__construct();
753 $this->load->helper('string');
754 $this->load->library('form_validation');
755 $this->load->model('Discussions_model');
756 $this->load->model('Comments_model');
757 $this->form_validation->set_error_delimiters('<div class="alert alert-danger">', '</div>');
758 }
759 public function index() {
760 if ($this->input->post()) {
761 $ds_id = $this->input->post('ds_id');
762 } else {
763 $ds_id = $this->uri->segment(3);
764 }
765 $page_data['discussion_query'] = $this->Discussions_model->fetch_discussion($ds_id);
766 $page_data['comment_query'] = $this->Comments_model->fetch_comments($ds_id);
767 $page_data['ds_id'] = $ds_id;
768 $this->form_validation->set_rules('ds_id', $this->lang->line('comments_comment_hidden_id'), 'required|min_length[1]|max_length[11]');
769 $this->form_validation->set_rules('comment_name', $this->lang->line('comments_comment_name'), 'required|min_length[1]|max_length[25]');
770 $this->form_validation->set_rules('comment_email', $this->lang->line('comments_comment_email'), 'required|min_length[1]|max_length[255]');
771 $this->form_validation->set_rules('comment_body', $this->lang->line('comments_comment_body'), 'required|min_length[1]|max_length[5000]');
772 if ($this->form_validation->run() == FALSE) {
773 $this->load->view('common/header');
774 $this->load->view('nav/top_nav');
775 $this->load->view('comments/view', $page_data);
776 $this->load->view('common/footer');
777 } else {
778 $data = array('cm_body' => $this->input->post('comment_body'),
779 'usr_email' => $this->input->post('comment_email'),
780 'usr_name' => $this->input->post('comment_name'),
781 'ds_id' => $this->input->post('ds_id')
782 );
783 if ($this->Comments_model->new_comment($data)) {
784 redirect('comments/index/'.$ds_id);
785 } else {
786 // error
787 // load view and flash sess error
788 }
789 }
790 }
791 public function flag() {
792 $cm_id = $this->uri->segment(4);
793 if ($this->Comments_model->flag($cm_id)) {
794 redirect('comments/index/'.$this->uri->segment(3));
795 } else {
796 // error
797 // load view and flash sess error
798 }
799 }
800}
801/* End of file comments.php */
802/* Location: ./application/controllers/comments.php */
803
804
80517)controller/admin.php
806
807<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
808class Admin extends MY_Controller {
809 function __construct() {
810 parent::__construct();
811 $this->load->helper('string');
812 $this->load->library('form_validation');
813 $this->load->model('Admin_model');
814 $this->form_validation->set_error_delimiters('<div class="alert alert-danger">', '</div>');
815 }
816 public function index() {
817 if ($this->session->userdata('logged_in') == FALSE) {
818 redirect('admin/login');
819 }
820 redirect('admin/dashboard');
821 }
822 public function login() {
823 $this->form_validation->set_rules('usr_email', $this->lang->line('admin_login_email'), 'required|min_length[1]|max_length[125]');
824 $this->form_validation->set_rules('usr_password', $this->lang->line('admin_login_password'), 'required|min_length[1]|max_length[25]');
825 if ($this->form_validation->run() == FALSE) {
826 $this->load->view('common/login_header');
827 $this->load->view('nav/top_nav');
828 $this->load->view('admin/login');
829 $this->load->view('common/footer');
830 } else {
831 $usr_email = $this->input->post('usr_email');
832 $usr_password = $this->input->post('usr_password');
833 $query = $this->Admin_model->does_user_exist($usr_email);
834 if ($query->num_rows() == 1) { // One matching row found
835 foreach ($query->result() as $row) {
836 // Call Encrypt library
837 $this->load->library('encrypt');
838 // Generate hash from a their password
839 $hash = $this->encrypt->sha1($usr_password);
840 // Compare the generated hash with that in the database
841 if ($hash != $row->usr_hash) {
842 // Didn't match so send back to login
843 $page_data['login_fail'] = true;
844 $this->load->view('common/login_header');
845 $this->load->view('nav/top_nav');
846 $this->load->view('admin/login',$page_data);
847 $this->load->view('common/footer');
848 } else {
849 $data = array(
850 'usr_id' => $row->usr_id,
851 'usr_email' => $row->usr_email,
852 'logged_in' => TRUE
853 );
854 // Save data to session
855 $this->session->set_userdata($data);
856 redirect('admin/dashboard');
857 }
858 }
859 }
860 }
861 }
862 public function dashboard() {
863 if ($this->session->userdata('logged_in') == FALSE) {
864 redirect('admin/login');
865 }
866 $page_data['comment_query'] = $this->Admin_model->dashboard_fetch_comments();
867 $page_data['discussion_query'] = $this->Admin_model->dashboard_fetch_discussions();
868 $this->load->view('common/header');
869 $this->load->view('nav/top_nav');
870 $this->load->view('admin/dashboard',$page_data);
871 $this->load->view('common/footer');
872 }
873 public function update_item() {
874 if ($this->session->userdata('logged_in') == FALSE) {
875 redirect('admin/login');
876 }
877 if ($this->uri->segment(4) == 'allow') {
878 $is_active = 1;
879 } else {
880 $is_active = 0;
881 }
882 if ($this->uri->segment(3) == 'ds') {
883 $result = $this->Admin_model->update_discussions($is_active, $this->uri->segment(5));
884 } else {
885 $result = $this->Admin_model->update_comments($is_active, $this->uri->segment(5));
886 }
887 redirect('admin');
888 }
889}
890/* End of file admin.php */
891/* Location: ./application/controllers/admin.php */
892
89318)language/english/en_admin_lang.php
894
895<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
896// General
897$lang['system_system_name'] = "Forum";
898// Top Nav
899$lang['top_nav_view_discussions'] = "Home";
900$lang['top_nav_new_discussion'] = "New Discussion";
901$lang['top_nav_login'] = "Login";
902// Discussions
903$lang['discussions_title'] = "Discussions";
904$lang['discussions_num_comments'] = 'Comments';
905// Comments
906$lang['comments_form_instruction'] = "Join in, add your comment below.";
907$lang['comments_flag'] = ' [Flag]';
908$lang['comments_created_by'] = 'Created by ';
909$lang['comments_created_at'] = ' at ';
910$lang['comments_comment_name'] = 'Your name';
911$lang['comments_comment_email'] = 'Your email';
912$lang['comments_comment_body'] = 'Comment';
913// Discussions
914$lang['discussion_form_instruction'] = "Create your own discussion, fill in the form below";
915$lang['discussion_flag'] = ' [Flag]';
916$lang['discussion_usr_name'] = 'Your name';
917$lang['discussion_usr_email'] = 'Your email';
918$lang['discussion_ds_title'] = 'Discussion title';
919$lang['discussion_ds_body'] = 'Your question, point etc';
920// Admin - login
921$lang['admin_login_header'] = "Please sign in";
922$lang['admin_login_email'] = "Email";
923$lang['admin_login_password'] = "Password";
924$lang['admin_login_signin'] = "Signin...";
925$lang['admin_login_error'] = "Whoops! Somethig went wrong - have another go!";
926$lang['admin_dash_allow'] = "Allow";
927$lang['admin_dash_disallow'] = "Disallow";
928// Common form elements
929$lang['common_form_elements_next'] = "Next...";
930$lang['common_form_elements_save'] = "Save...";
931$lang['common_form_elements_cancel'] = "Cancel";
932$lang['common_form_elements_go'] = "Go...";
933$lang['common_form_elements_go_back'] = "Go back";
934$lang['common_form_elements_submission_error'] = "There were errors with the form:";
935$lang['common_form_elements_success_notifty'] = "Success:";
936$lang['common_form_elements_error_notifty'] = "Error:";
937$lang['common_form_elements_actions'] = "Actions";
938$lang['common_form_elements_action_edit'] = "Edit";
939$lang['common_form_elements_action_delete'] = "Delete";
940$lang['common_form_elements_active'] = "Active";
941$lang['common_form_elements_inactive'] = "Inactive";
942$lang['common_form_elements_seccessful_change'] = "Your changes have been saved";
943$lang['common_form_elements_seccessful_delete'] = "The item has been deleted";
944$lang['common_form_elements_yes'] = "Yes";
945$lang['common_form_elements_no'] = "No";
946$lang['common_form_elements_to'] = "to";
947$lang['common_form_elements_from'] = "from";
948$lang['common_form_elements_history'] = "History";