Created
November 2, 2015 21:07
-
-
Save joelmandell/0357efee2442c7981def to your computer and use it in GitHub Desktop.
CodeIgniter - blogmodel.php kodsnutt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class BlogModel extends Model { | |
var $permission_table; | |
function BlogModel() | |
{ | |
parent::Model(); | |
$this->load->database(); | |
$this->load->library('auth'); | |
$this->permission_table="blog_permissions"; | |
$this->load->library('session'); | |
} | |
function get_url_prefix() | |
{ | |
return str_replace("model","",strtolower(get_class($this))); | |
} | |
function list_latest_items($limit=0,$page=0) | |
{ | |
$text=""; | |
$total_items=0; | |
$count_items_query=$this->db->query("SELECT * FROM blog_items"); | |
$pages=0; | |
$page=$page*$limit; | |
foreach ($count_items_query->result() as $row) | |
{ | |
$total_items++; | |
} | |
if($page==0) | |
{ | |
if($limit==0) //If argument limit is zero then there is no limited query. | |
{ | |
$query = $this->db->query("SELECT * FROM blog_items ORDER BY date"); | |
} else { | |
$query = $this->db->query("SELECT * FROM blog_items ORDER BY date DESC LIMIT ".$this->db->escape($limit)." "); | |
$pages=(int) $total_items/$limit; | |
} | |
} else { | |
if($limit==0) //If argument limit is zero then there is no limited query. | |
{ | |
$query = $this->db->query("SELECT * FROM blog_items ORDER BY date"); | |
} else { | |
$query = $this->db->query("SELECT * FROM blog_items ORDER BY date DESC LIMIT ".$this->db->escape($limit).",".$page." "); | |
$pages=$total_items/$limit; | |
} | |
} | |
foreach ($query->result() as $row) | |
{ | |
$comments = $this->db->query("SELECT * FROM blog_comments WHERE blogid LIKE '$row->id'")->num_rows(); | |
$text.="<h2>".$row->title."</h2>"; | |
$text.="<em>".$row->date."</em>"; | |
$text.="<br /><br /><p><strong>".$row->preamble."</strong></p><a href=\"./show_item/".$row->id."\">Läs vidare (<span title=\"Antalet kommentarer.\"> $comments </span>)</a><br /><br /><hr /><br />"; | |
} | |
if($pages!=0) | |
{ | |
$text.="<br /><br />"; | |
for($i=1;$i<$pages;$i++) | |
{ | |
//$text.="<a href=\"/blog/page/$i/\">$i</a> "; | |
} | |
} | |
//$text.="SELECT * FROM blog_items ORDER BY date DESC LIMIT ".$this->db->escape($limit).",".$page." "; | |
return $text; | |
} | |
function create_edit_item_links() | |
{ | |
$text="<h1>Blogg</h1><br />"; | |
$text.="<p><a href=\"".get_class($this)."/create\">Skapa nytt inlägg</a></p><br />"; | |
$query = $this->db->query("SELECT * FROM blog_items ORDER BY date"); | |
foreach ($query->result() as $row) | |
{ | |
$text.="".$row->title."<br /><a href=\"".get_class($this)."/edit/".$row->id."\">Redigera</a> | <a href=\"".get_class($this)."/delete/".$row->id."\">Ta bort</a> | <a href=\"".get_class($this)."/move/".$row->id."\">Flytta</a> | <a href=\"".get_class($this)."/comments/".$row->id."\">Moderera kommentarer</a><br /><br />"; | |
} | |
return $text; | |
} | |
function from_twitter($q) | |
{ | |
$text=""; | |
$xml_file="http://search.twitter.com/search.rss?q=".$q.""; | |
$xml = simplexml_load_file($xml_file,'SimpleXMLElement', LIBXML_NOCDATA); | |
$result = $xml->xpath("/rss/channel/item"); | |
$i=0; | |
$count=count($result); | |
for($a=0;$a<$count;$a++) | |
{ | |
foreach($result[$a] as $key => $attribute) { | |
if($i!=10) | |
{ | |
//print_r($attribute); | |
if($key=="description") | |
{ | |
$text.="<br /><div class=\"comment\"><div class=\"space\"><p><strong>Från twitter <span class=\"u\"></span></strong>:</p><p>".$attribute."</p></div></div>"; | |
$i++; | |
} | |
} | |
} | |
} | |
return $text; | |
} | |
function microblog() //For the moment static hack...needs to make it depend on options from admin ui. | |
{ | |
$text="<h3>Twitter</h3><hr /><br />"; | |
$xml_file="http://twitter.com/statuses/user_timeline/50093268.rss"; | |
$xml = simplexml_load_file($xml_file,'SimpleXMLElement', LIBXML_NOCDATA); | |
$result["content"] = $xml->xpath("/rss/channel/item/title"); | |
$i=0; | |
foreach($result as $key => $attribute) { | |
foreach($attribute as $element) { | |
if($i!=10) | |
{ | |
$text.="<p>".$element."</p><br />"; | |
$i++; | |
} | |
} | |
} | |
return $text; | |
} | |
function generate_links_to_blog($limit) | |
{ | |
$text="<h3>Bloggen</h3><hr /><br />"; | |
$groups=$this->auth->get_session_groups(); | |
// $permissions=$this->auth->get_session_permissions(); | |
$query = $this->db->query("SELECT * FROM blog_items ORDER BY date DESC LIMIT ".$this->db->escape($limit).""); | |
if($query->num_rows() > 0) | |
{ | |
foreach ($query->result() as $row) | |
{ | |
$comments = $this->db->query("SELECT * FROM blog_comments WHERE blogid LIKE '$row->id'")->num_rows(); | |
$text.="<p><a href=\"/blog/show_item/".$row->id."\">".$row->title."(<span title=\"Antalet kommentarer.\"> $comments </span>)</a></p>"; | |
} | |
} else { | |
} | |
return $text; | |
} | |
function title($id=0) | |
{ | |
$text=""; | |
$query = $this->db->query("SELECT title FROM blog_items WHERE id LIKE ".$this->db->escape($id).""); | |
$text.=strip_tags($query->row()->title); | |
return $text; | |
} | |
function description() | |
{ | |
$text="Joel Mandells personliga sida, portfolio, projekt samt blogg om öppen källkod, programmering och annat i livet."; | |
return $text; | |
} | |
function preamble($id=0) | |
{ | |
$text=""; | |
$query = $this->db->query("SELECT preamble FROM blog_items WHERE id LIKE ".$this->db->escape($id).""); | |
$text.=$query->row()->preamble; | |
return $text; | |
} | |
function body($id=0) | |
{ | |
$text=""; | |
$query = $this->db->query("SELECT body FROM blog_items WHERE id LIKE ".$this->db->escape($id).""); | |
$text.=$query->row()->body; | |
return $text; | |
} | |
function list_comments($id=0) | |
{ | |
$query = $this->db->query("SELECT * FROM blog_comments WHERE blogid LIKE ".$this->db->escape($id)." ORDER BY date"); | |
$text=""; | |
if($query->num_rows() > 0) | |
{ | |
$text.="<br /><hr /><br /><h2>Kommentarer</h2>"; | |
foreach ($query->result() as $row) | |
{ | |
$text.="<br /><div class=\"comment\"><div class=\"space\"><p><strong>".$row->publisher." skrev <span class=\"u\">".$row->date."</span></strong>:</p>"; | |
$text.="<p>".$row->text."</p></div></div>"; | |
} | |
$text.=$this->from_twitter($this->title($id)); | |
} else { | |
$twitter=$this->from_twitter($this->title($id)); | |
if($twitter!="") | |
{ | |
$text.=$twitter; | |
} else { | |
$text .="<br /><hr /><br /><h2>Kommentarer</h2><br /><p>Hitills inga kommentarer, bli först med att kommentera!</p>"; | |
} | |
} | |
return $text; | |
} | |
function write_comment($id) | |
{ | |
$this->load->helper('captcha'); | |
// create the captcha-config | |
$aCaptchaCfg = array( | |
//'word' => 'myrandomword', //default: random() | |
'length' => 6, //default: 5 | |
'img_path' => 'captcha/', //no default ! | |
'img_url' => '../../../captcha/', // no default! | |
'font_path' => '/Libraries/sys1.7.1/fonts/', // default: ./system/fonts/ | |
'fonts' => array('texb.ttf'), // default: texb.ttf | |
'font_size' => 10, // default: 18 | |
'img_width' => '180', // default: 170 | |
'img_height' => '30', // default: 60 | |
'expiration' => 7200 // default: 7200 | |
); | |
// get captcha-stuff | |
$aCaptcha=create_captcha($aCaptchaCfg); | |
$this->session->set_userdata(array('security_code' => md5($aCaptcha['word']))); | |
$text=""; | |
$text.="<br /><br /><form method=\"post\" action=\"../create_comment/$id\"><fieldset><legend> Skriv kommentar </legend>"; | |
$text.="<div class=\"space\">"; | |
$text.="<p>Namn, nick: <br/><input type=\"text\" name=\"nick\" /><textarea class=\"ilu\" rows=\"13\" cols=\"42\" name=\"msg\">Skriv meddelande här..</textarea><br /></p>"; | |
$text.="<p><br />".$aCaptcha['image']."<br />Säkerhets-kod<br /><input type=\"text\" name=\"cap\" /></p>"; | |
$text.="<p><br /><input type=\"submit\" value=\"Publicera!\" /></p></div></fieldset>"; | |
$text.="</form>"; | |
return $text; | |
} | |
function list_item($id) | |
{ | |
$text=""; | |
$query = $this->db->query("SELECT * FROM blog_items WHERE id LIKE ".$this->db->escape($id).""); | |
foreach ($query->result() as $row) | |
{ | |
$text.="<h2>".$row->title."</h2>"; | |
$text.="<em>".$row->date."</em>"; | |
$text.="<br /><br />Dela med dig av detta inlägget till: <a href=\"http://www.facebook.com/share.php?u=http://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]."&t=".$row->title."\">Facebook</a> eller "; | |
$text.="<a href=\"http://twitter.com/home?status=http://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]." - ".$row->title."\">Twitter</a>."; | |
$text.="<br /><br />".$row->preamble.""; | |
$text.="<br />".$row->body."<br />"; | |
} | |
return $text; | |
} | |
function admin_functions() | |
{ | |
$text="<a href=\"./edit_item_form\">Ändra blogg-post</a>"; | |
return $text; | |
} | |
function create_form_editor() | |
{ | |
$text="<script type=\"text/javascript\" src=\"/ckeditor/ckeditor.js\"></script>"; | |
return $text; | |
} | |
function create_form($id) | |
{ | |
$function_name="create"; //This variable is crucial in all model calls that we are using. | |
(int) $data=0; //Initialize this with zero. | |
/*If there is a permission that is set to true for the group(s) that the current user belongs to then | |
the $data | |
*/ | |
$data=$this->session->userdata($this->permission_table."@".$function_name); | |
if($data!=0) | |
{ | |
$text="<h1>Skapa post:</h1><br /><form method=\"post\" action=\"/../../blog/create_item/\"><p>Rubrik :<input type=\"\" name=\"title\" value=\"\"/></p><br />"; | |
$text.="<label for=\"preamble\"> | |
Brödtext:</label> | |
<textarea cols=\"30\" id=\"preamble\" name=\"preamble\" rows=\"10\"></textarea> | |
<script type=\"text/javascript\"> | |
//<![CDATA[ | |
CKEDITOR.replace( 'preamble', | |
{ | |
/* | |
* Style sheet for the contents | |
*/ | |
contentsCss : '/assets/output_xhtml.css', | |
/* | |
* Core styles. | |
*/ | |
coreStyles_bold : { element : 'span', attributes : {'class': 'Bold'} }, | |
coreStyles_italic : { element : 'span', attributes : {'class': 'Italic'}}, | |
coreStyles_underline : { element : 'span', attributes : {'class': 'Underline'}}, | |
coreStyles_strike : { element : 'span', attributes : {'class': 'StrikeThrough'}, overrides : 'strike' }, | |
coreStyles_subscript : { element : 'span', attributes : {'class': 'Subscript'}, overrides : 'sub' }, | |
coreStyles_superscript : { element : 'span', attributes : {'class': 'Superscript'}, overrides : 'sup' }, | |
/* | |
* Font face | |
*/ | |
// List of fonts available in the toolbar combo. Each font definition is | |
// separated by a semi-colon (;). We are using class names here, so each font | |
// is defined by {Combo Label}/{Class Name}. | |
font_names : 'Comic Sans MS/FontComic;Courier New/FontCourier;Times New Roman/FontTimes/Trebuchet MS/Tahoma', | |
// Define the way font elements will be applied to the document. The \"span\" | |
// element will be used. When a font is selected, the font name defined in the | |
// above list is passed to this definition with the name \"Font\", being it | |
// injected in the \"class\" attribute. | |
// We must also instruct the editor to replace span elements that are used to | |
// set the font (Overrides). | |
font_style : | |
{ | |
element : 'span', | |
attributes : { 'class' : '#(family)' }, | |
overrides : [ { element : 'span', attributes : { 'class' : /^Font(?:Comic|Courier|Times)$/ } } ] | |
}, | |
/* | |
* Font sizes. | |
*/ | |
fontSize_sizes : 'Smaller/FontSmaller;Larger/FontLarger;8pt/FontSmall;14pt/FontBig;Double Size/FontDouble', | |
fontSize_style : | |
{ | |
element : 'span', | |
attributes : { 'class' : '#(size)' }, | |
overrides : [ { element : 'span', attributes : { 'class' : /^Font(?:Smaller|Larger|Small|Big|Double)$/ } } ] | |
} , | |
/* | |
* Font colors. | |
*/ | |
colorButton_enableMore : false, | |
colorButton_colors : 'FontColor1/FF9900,FontColor2/0066CC,FontColor3/F00', | |
colorButton_foreStyle : | |
{ | |
element : 'span', | |
attributes : { 'class' : '#(color)' }, | |
overrides : [ { element : 'span', attributes : { 'class' : /^FontColor(?:1|2|3)$/ } } ] | |
}, | |
colorButton_backStyle : | |
{ | |
element : 'span', | |
attributes : { 'class' : '#(color)BG' }, | |
overrides : [ { element : 'span', attributes : { 'class' : /^FontColor(?:1|2|3)BG$/ } } ] | |
}, | |
/* | |
* Indentation. | |
*/ | |
indentClasses : ['Indent1', 'Indent2', 'Indent3'], | |
/* | |
* Paragraph justification. | |
*/ | |
justifyClasses : [ 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyFull' ], | |
/* | |
* Styles combo. | |
*/ | |
stylesSet : | |
[ | |
{ name : 'Strong Emphasis', element : 'strong' }, | |
{ name : 'Emphasis', element : 'em' }, | |
{ name : 'Computer Code', element : 'code' }, | |
{ name : 'Keyboard Phrase', element : 'kbd' }, | |
{ name : 'Sample Text', element : 'samp' }, | |
{ name : 'Variable', element : 'var' }, | |
{ name : 'Deleted Text', element : 'del' }, | |
{ name : 'Inserted Text', element : 'ins' }, | |
{ name : 'Cited Work', element : 'cite' }, | |
{ name : 'Inline Quotation', element : 'q' } | |
] | |
}); | |
//]]> | |
</script> | |
<br />"; | |
$text.="<label for=\"body\"> | |
Sprödtext:</label> | |
<textarea cols=\"70\" id=\"body\" name=\"body\" rows=\"10\"></textarea> | |
<script type=\"text/javascript\"> | |
//<![CDATA[ | |
CKEDITOR.replace( 'body', | |
{ | |
/* | |
* Style sheet for the contents | |
*/ | |
contentsCss : '/assets/output_xhtml.css', | |
/* | |
* Core styles. | |
*/ | |
coreStyles_bold : { element : 'span', attributes : {'class': 'Bold'} }, | |
coreStyles_italic : { element : 'span', attributes : {'class': 'Italic'}}, | |
coreStyles_underline : { element : 'span', attributes : {'class': 'Underline'}}, | |
coreStyles_strike : { element : 'span', attributes : {'class': 'StrikeThrough'}, overrides : 'strike' }, | |
coreStyles_subscript : { element : 'span', attributes : {'class': 'Subscript'}, overrides : 'sub' }, | |
coreStyles_superscript : { element : 'span', attributes : {'class': 'Superscript'}, overrides : 'sup' }, | |
/* | |
* Font face | |
*/ | |
// List of fonts available in the toolbar combo. Each font definition is | |
// separated by a semi-colon (;). We are using class names here, so each font | |
// is defined by {Combo Label}/{Class Name}. | |
font_names : 'Comic Sans MS/FontComic;Courier New/FontCourier;Times New Roman/FontTimes/Trebuchet MS/Tahoma', | |
// Define the way font elements will be applied to the document. The \"span\" | |
// element will be used. When a font is selected, the font name defined in the | |
// above list is passed to this definition with the name \"Font\", being it | |
// injected in the \"class\" attribute. | |
// We must also instruct the editor to replace span elements that are used to | |
// set the font (Overrides). | |
font_style : | |
{ | |
element : 'span', | |
attributes : { 'class' : '#(family)' }, | |
overrides : [ { element : 'span', attributes : { 'class' : /^Font(?:Comic|Courier|Times)$/ } } ] | |
}, | |
/* | |
* Font sizes. | |
*/ | |
fontSize_sizes : 'Smaller/FontSmaller;Larger/FontLarger;8pt/FontSmall;14pt/FontBig;Double Size/FontDouble', | |
fontSize_style : | |
{ | |
element : 'span', | |
attributes : { 'class' : '#(size)' }, | |
overrides : [ { element : 'span', attributes : { 'class' : /^Font(?:Smaller|Larger|Small|Big|Double)$/ } } ] | |
} , | |
/* | |
* Font colors. | |
*/ | |
colorButton_enableMore : false, | |
colorButton_colors : 'FontColor1/FF9900,FontColor2/0066CC,FontColor3/F00', | |
colorButton_foreStyle : | |
{ | |
element : 'span', | |
attributes : { 'class' : '#(color)' }, | |
overrides : [ { element : 'span', attributes : { 'class' : /^FontColor(?:1|2|3)$/ } } ] | |
}, | |
colorButton_backStyle : | |
{ | |
element : 'span', | |
attributes : { 'class' : '#(color)BG' }, | |
overrides : [ { element : 'span', attributes : { 'class' : /^FontColor(?:1|2|3)BG$/ } } ] | |
}, | |
/* | |
* Indentation. | |
*/ | |
indentClasses : ['Indent1', 'Indent2', 'Indent3'], | |
/* | |
* Paragraph justification. | |
*/ | |
justifyClasses : [ 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyFull' ], | |
/* | |
* Styles combo. | |
*/ | |
stylesSet : | |
[ | |
{ name : 'Strong Emphasis', element : 'strong' }, | |
{ name : 'Emphasis', element : 'em' }, | |
{ name : 'Computer Code', element : 'code' }, | |
{ name : 'Keyboard Phrase', element : 'kbd' }, | |
{ name : 'Sample Text', element : 'samp' }, | |
{ name : 'Variable', element : 'var' }, | |
{ name : 'Deleted Text', element : 'del' }, | |
{ name : 'Inserted Text', element : 'ins' }, | |
{ name : 'Cited Work', element : 'cite' }, | |
{ name : 'Inline Quotation', element : 'q' } | |
] | |
}); | |
//]]> | |
</script> | |
"; | |
$text.="<br /><input type=\"submit\" value=\"Skapa!\" /></form>"; | |
//$text="<h1>Skapa post:</h1><br /><form method=\"post\" action=\"/../../blog/create_item/\"><p>Rubrik :<input type=\"\" name=\"title\" value=\"\"/></p> | |
//<br /><p>Brödtext:</p><textarea name=\"preamble\" cols=\"70\" rows=\"8\"></textarea><br /><br /><p>Sprödtext:</p><textarea name=\"body\" cols=\"70\" rows=\"8\"></textarea><br /><br /><input type=\"submit\" value=\"Skapa!\" /></form>"; | |
} else { | |
$text="Du har inte tillåtelse att skapa en blogg artikel."; | |
} | |
return $text; | |
} | |
function edit_form($id) | |
{ | |
$function_name="edit"; //This variable is crucial in all model calls that we are using. | |
(int) $data=0; //Initialize this with zero. | |
/*If there is a permission that is set to true for the group(s) that the current user belongs to then | |
the $data | |
*/ | |
$data=$this->session->userdata($this->permission_table."@".$function_name); | |
if($data!=0) | |
{ | |
$query = $this->db->query("SELECT * FROM blog_items WHERE id LIKE ".$this->db->escape($id).""); | |
$text="<h1>Ändra post:</h1><br /><form method=\"post\" action=\"\"><p>Rubrik :<input type=\"\" name=\"title\" value=\"".$query->row()->title."\"/></p> | |
<br /><p>Brödtext:</p><textarea name=\"preamble\" cols=\"70\" rows=\"8\">".$query->row()->preamble."</textarea><br /><br /><p>Sprödtext:</p><textarea name=\"body\" cols=\"70\" rows=\"8\">".$query->row()->body."</textarea></form>"; | |
} else { | |
$text="Du har inte tillåtelse att använda editeringsfunktionen för blogg artiklar"; | |
} | |
return $text; | |
} | |
function delete_form($id) | |
{ | |
$function_name="delete"; //This variable is crucial in all model calls that we are using. | |
(int) $data=0; //Initialize this with zero. | |
$data=$this->session->userdata($this->permission_table."@".$function_name); | |
if($data!=0) | |
{ //Granted permission | |
$text="Du får ta bort!"; | |
} else { | |
//Dont have permission. | |
$text="Du har inte tillåtelse att använda raderings-funktionen för blogg artiklar"; | |
} | |
return $text; | |
} | |
function move_form($id) | |
{ | |
$function_name="move"; //This variable is crucial in all model calls that we are using. | |
$data=0; | |
$data=$this->session->userdata($this->permission_table."@".$function_name); | |
if($data!=0) | |
{ //Granted permission | |
} else { | |
//Dont have permission. | |
$text="Du har inte tillåtelse att byta ordning på blogg inläggen."; | |
} | |
return $text; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment