Last active
June 13, 2018 23:35
-
-
Save sanPuerquitoProgramador/c2d2d18b72202e307c8dfdb5eed433da to your computer and use it in GitHub Desktop.
Library for CI; The common taks for development
This file contains 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 defined('BASEPATH') OR exit('No direct script access allowed'); | |
Class Romina{ | |
public function __construct() | |
{ | |
date_default_timezone_set('America/Mexico_City'); | |
setlocale(LC_TIME, 'es_ES'); | |
$this->load->library('session'); | |
$this->load->library('user_agent'); | |
$this->load->database(); | |
} | |
public function msg($body, $toWhere=true, $se=false) | |
{ | |
$msg = '<div class="msg2usr">'.$body.'</div>'; | |
$this->session->set_flashdata('msg2usr', $msg); | |
if($toWhere==true) { | |
redirect($toWhere); | |
} | |
} | |
public function logThis($log,$userExterno=false) | |
{ | |
if($userExterno!=false) { | |
$usuario_id = $userExterno; | |
} elseif(!$this->session->usuario->id) { | |
return false; | |
} else { | |
$usuario_id = $this->session->usuario->id; | |
} | |
$table = $this->tableLogRomina(); | |
if($table) { | |
$data = $this->prepareLog(); | |
$data['usuario_id'] = $usuario_id; | |
$data['log'] = $log; | |
$this->timeStamp($table); | |
return $this->db->insert($table,$data); | |
} else { | |
return false; | |
} | |
} | |
public function fecha($fecha) | |
{ | |
return strftime('%d de %B de %Y', strtotime($fecha)); | |
} | |
public function hora($fecha) | |
{ | |
return strftime('%H:%M', strtotime($fecha)); | |
} | |
public function fechahora($fecha) | |
{ | |
return $fecha!='' ? strftime('%d de %B de %Y a las %H:%M', strtotime($fecha)) : 'No disponible'; | |
} | |
public function guardian($admin=false, $modulo='No reportado') | |
{ | |
if(!$this->session->usuario) { | |
redirect(site_url()); | |
} | |
if($admin!=false) { | |
if($this->session->usuario->rol=='admin') { | |
return true; | |
} else { | |
session_unset($this->session->usuario); | |
$this->romina->msg('Intento malicioso. Su sesión ha sido cerrada y el administrador ha sido notificado', site_url(), false); | |
} | |
} | |
} | |
public function storage($file,$name) | |
{ | |
if(isset($file[$name]['tmp_name'])&&$file[$name]['tmp_name']!='') { | |
$monthYear = date('mY'); | |
$folderUsuario='/uploads/storage/'.$monthYear.'/'.$this->session->usuario->id.'/'; | |
if(!is_dir('.'.$folderUsuario)) { | |
mkdir('.'.$folderUsuario,0777,true); | |
} | |
$config['upload_path'] = '.'.$folderUsuario; | |
$config['max_width'] = 0; | |
$config['max_height'] = 0; | |
$config['file_ext_tolower'] = true; | |
$config['file_name'] = uniqid(); | |
$this->load->library('upload', $config); | |
if ( ! $this->upload->do_upload($name)) { | |
$errorUpload = array('error' => $this->upload->display_errors()); | |
echo json_encode($errorUpload); | |
return false; | |
} else { | |
$archivo = array('upload_data' => $this->upload->data()); | |
$data['path_to_file'] = $folderUsuario.$archivo['upload_data']['file_name']; | |
$data['original_name'] = $archivo['upload_data']['orig_name']; | |
return $data; | |
} | |
} else { | |
return false; | |
} | |
} | |
public function unStorage($simplePath) | |
{ | |
unlink('.'.$simplePath); | |
} | |
public function slugify($string, $replace = array(), $delimiter = '-') | |
{ | |
if (!extension_loaded('iconv')) { | |
throw new Exception('iconv module not loaded'); | |
} | |
$oldLocale = setlocale(LC_ALL, '0'); | |
setlocale(LC_ALL, 'en_US.UTF-8'); | |
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $string); | |
if (!empty($replace)) { | |
$clean = str_replace((array) $replace, ' ', $clean); | |
} | |
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean); | |
$clean = strtolower($clean); | |
$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean); | |
$clean = trim($clean, $delimiter); | |
setlocale(LC_ALL, $oldLocale); | |
return $clean; | |
} | |
private function tableLogRomina() | |
{ | |
$table = 'romina_log'; | |
$fields = array( | |
'created_at' => array( | |
'type' => 'DATETIME', | |
), | |
'usuario_id' => array( | |
'type' =>'INT', | |
'constraint' => '5', | |
'unsigned' => true, | |
), | |
'log' => array( | |
'type' => 'VARCHAR', | |
'constraint' => 250, | |
), | |
'ip' => array( | |
'type' => 'VARCHAR', | |
'constraint' => 20, | |
), | |
'browser' => array( | |
'type' => 'VARCHAR', | |
'constraint' => 250, | |
), | |
'so' => array( | |
'type' => 'VARCHAR', | |
'constraint' => 250, | |
), | |
); | |
return $this->coct($table, $fields); | |
} | |
private function coct($table, $fields) | |
{ | |
if (!$this->db->table_exists($table)) { | |
$this->load->dbforge(); | |
$this->dbforge->add_field($fields); | |
$this->dbforge->add_field('id'); | |
if($this->dbforge->create_table($table)) { | |
return $table; | |
} else { | |
return false; | |
} | |
} else { | |
return $table; | |
} | |
} | |
private function prepareLog() | |
{ | |
return array( | |
'ip' => $this->input->ip_address(), | |
'browser' => $this->user_agent(), | |
'so' => $this->agent->platform(), | |
); | |
} | |
private function user_agent() | |
{ | |
if ($this->agent->is_browser()) { | |
$agent = $this->agent->browser().' - '.$this->agent->version(); | |
} | |
elseif ($this->agent->is_robot()) { | |
$agent = $this->agent->robot(); | |
} | |
elseif ($this->agent->is_mobile()) { | |
$agent = $this->agent->mobile(); | |
} | |
else { | |
$agent = 'Agente de usuario no identificado'; | |
} | |
return $agent; | |
} | |
private function timeStamp($table,$accion=true) | |
{ | |
if($accion!=false) { | |
if($this->db->field_exists('created_at', $table)) { | |
$this->db->set('created_at', 'NOW()', false); | |
} | |
} | |
if($this->db->field_exists('updated_at', $table)) { | |
$this->db->set('updated_at', 'NOW()', false); | |
} | |
} | |
public function __get($var) | |
{ | |
return get_instance()->$var; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment