Skip to content

Instantly share code, notes, and snippets.

@joelmandell
Created November 2, 2015 21:06
Show Gist options
  • Save joelmandell/82a82588196dade90aa9 to your computer and use it in GitHub Desktop.
Save joelmandell/82a82588196dade90aa9 to your computer and use it in GitHub Desktop.
CodeIgniter - usermodel.php kodsnutt
<?php
class UserModel extends Model {
function UserModel()
{
parent::Model();
$this->load->library('auth');
$this->load->database();
$this->load->library('session');
}
function content()
{
$text="<h1>Användargränsnitt.</h1><br /><p>Välj modul att jobba med på höger sida.</p>";
return $text;
}
function form()
{
$text="<h1>Logga in</h1><br /><form method=\"post\" action=\"/".$this->get_url_prefix()."/login/\">
<p>Användarnamn:<br /><input type=\"text\" name=\"user\"/></p><p>Lösenord:<br /><input type=\"password\" name=\"pass\" /><br /><input type=\"submit\" value=\"Logga in\" /></p></form>";
return $text;
}
function not_active()
{
$text="<h1>Ej aktivt konto</h1><br /><p>Antingen har du inte bekräftat ett skapat konto, eller så har du blivit inaktiverad av sidans Administratör för olämpligt uppförande.</p>";
return $text;
}
function links()
{
$text="Hej <strong>".$this->auth->get_user_name()."</strong>!<br />Du tillhör grupperna:<br /><br />";
foreach($this->auth->get_group_names() as $group)
{
$text.="<p>&nbsp;".$group."</p>";
}
$text.="<br /><h3>Alternativ</h3><a href=\"/".$this->get_url_prefix()."/logout/\">Logga ut</a><br /><a href=\"/".$this->get_url_prefix()."/account_settings/\">Användarinställningar</a><br /><br /><h3>Hantera modul:</h3>";
return $text;
}
function get_url_prefix()
{
return str_replace("model","",strtolower(get_class($this)));
}
function list_modules()
{
//if($this->auth->get_session_groups()
$text="<form method=\"post\" action=\"/".$this->get_url_prefix()."/choose_module/\"><select id=\"Modul\" name=\"Modul\"><option value=\"Blog\">Blog</option><option value=\"News\">News</option></select><input type=\"submit\" value=\"Ok\" /></form>";
return $text;
}
function update_account_settings()
{
$text="";
$this->load->library('NameUi');
$query = $this->db->query("SHOW COLUMNS FROM web36829_portfolio.userInformation");
$columns = $query->result_array();
$allowFields[] = array(); //We want to have this. Cause we will just allow post changes for columns that exists.
unset($columns[0]); //Remove the first column. We dont need it!
unset($columns[1]); //And the second one as well...cause they are the id and the userId field.
foreach($columns as $column)
{
$allowFields[] = $column["Field"]; //Grab the field name.
}
if($this->auth->get_session_user_status()=="true")
{
$text.="<h2>Uppdaterade följande fält:</h2><br />";
$keys = array_keys($_POST); //Get the keys, or in more simplified words, the array field name. In this case the $_POST field names.
foreach($keys as $key)
{
if(in_array($key,$allowFields) && ($_POST[$key]!="")) //If the column actually is in the table, then we can allow changes to it. And just update if the value is not null.
{
$this->db->query("update userInformation set ".$key." = ".$this->db->escape($_POST[$key])."");
$text.="<p>".$this->nameui->convert($key).": ".$_POST[$key]."</p>";
}
}
}
return $text;
}
function account_settings()
{
if($this->auth->get_session_user_status()=="true")
{
$text="<h1>Användarinställningar</h1><br />";
$this->load->library('NameUi');
$this->load->helper(array('form', 'url'));
$account_settings_query="select "; //Holds sql query for settings.
/*First we need to get the columns from the userInformation table that hosts the user settings. And then select on userid basis.*/
$query_for_columns=$this->db->query("SHOW COLUMNS FROM web36829_portfolio.userInformation");
$columns = $query_for_columns->result_array();
$cols = array();
unset($columns[0]); //Remove the first column. We dont need it!
unset($columns[1]); //And the second one as well...cause they are the id and the userId field.
$i=0;
/*Continue to build the sqlquery for querying settings later on..*/
foreach($columns as $column)
{
$cols[] = $column["Field"]; //Get values from the field key..
//Format, select column_name,column-name etc.
$account_settings_query.=$column["Field"];
if($i!=sizeof($columns)-1) $account_settings_query.=",";
$i++;
}
//The final building of the query.
$account_settings_query.=" from userInformation where userId like ".$this->session->userdata('userid')."";
$attributes = array('id' => 'update_account_settings'); //Attributes for the form.
$text.=form_open("/../".$this->get_url_prefix()."/account_settings/update",$attributes); //Create the form
//Bring it ON. Query it baby!!
$query=$this->db->query($account_settings_query);
for($i=0;$query->num_rows()>$i;$i++)
{
foreach($cols as $col)
{
$text.="".$this->nameui->convert($col).":<br /><input name=\"".$col."\" type=\"text\" value=\"".$query->row()->$col."\" /><br />";
}
}
$text.="<input type=\"submit\" value=\"Uppdatera\" />";
$text.=form_close();
} else {
$text=$this->auth->no_auth();
}
return $text;
}
function activation_finished()
{
$text="<h2>Slutfört registreringen!</h2><p>Kontot är aktiverat, du kan logga in.</p>";
return $text;
}
function activation_already_done()
{
$text="<h2>Stop!</h2><p>Detta kontot är redan aktiverat!</p>";
return $text;
}
function activation_pending()
{
$text="<h2>Första steget slutfört</h2><p>Nu behöver du kolla din email för att bekräfta ditt konto!</p>";
return $text;
}
function register()
{
// throw the captcha-array to your view and load the view
$this->load->helper(array('form', 'url'));
$attributes = array('id' => '/user');
$text=form_open('/../user/reg_account',$attributes);
$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' => 15, // default: 18
'img_width' => '180', // default: 170
'img_height' => '60', // default: 60
'expiration' => 7200 // default: 7200
);
// get captcha-stuff
$aCaptcha=create_captcha($aCaptchaCfg);
$this->session->set_userdata(array('security_code' => strtolower($aCaptcha['word'])));
$text.="<div class=\"right_floating_info\"><div class=\"right_floating_box\"><h3>Eran email adress - viktigt!</h3><p>Efter registreringen skickas nämligen ett aktiverings-email!</p></div></div>
<br /><h2>Registrera</h2>
<p>Användarnamn:<br />
<input type=\"text\" name=\"user\" value=\"".set_value('user')."\" /><br /></p>
<p>Email:<br />
<input type=\"text\" name=\"email\" value=\"".set_value('email')."\"/><br /></p>
<p>Lösenord:<br />
<input type=\"password\" name=\"pass\" /><br /><br />".$aCaptcha["image"]."<br /><br /><input type=\"text\" name=\"captcha\" /><br /></p>
<p><input type=\"submit\" id=\"submit\" value=\"Skicka!\" /><br /></p>
";
$text.=form_close();
return $text;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment