Advertisement
Guest User

Untitled

a guest
Jun 15th, 2017
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.61 KB | None | 0 0
  1. <?php
  2.  
  3. class importController extends Controller {
  4.  
  5.     function __construct() {
  6.        
  7.         parent::__construct();
  8.        
  9.         if(!@$_COOKIE['wanting']) {
  10.             echo "...";
  11.             exit();
  12.         }
  13.     }
  14.    
  15.     /*
  16.      * Creates a theoretical situation in which, three users, work on 6 projects simultaneosly.
  17.      * * Users work 8 hours a day, 5 days a week
  18.      * * Proyects are 'Proyectos', 'Plataforma', 'Filtros', 'Gamelandia', 'Relatividad', 'Portatiles'
  19.      * * Users are 'Pedro Worcel', 'José Martinez' and 'Albert Einstein'
  20.      *  * Pedro worcel spends most of his time working at project 'Proyectos', but also works at 'Plataforma', 'Gamelandia', and 'Filtros'
  21.      *  * José Martinez works at 'Gamelandia', 'plataforma' and 'portatiles'.
  22.      *  * Albert Einstein works at 'Relatividad' and 'Gamelandia'
  23.     */
  24.    
  25.     function generate_dummy() {
  26.        
  27.         /*
  28.          * PROJECTS
  29.         */
  30.         $projects = array(
  31.             'proyectos',
  32.             'plataforma',
  33.             'gamelandia',
  34.             'filtros',
  35.             'relatividad',
  36.             'portatiles'
  37.         );
  38.        
  39.         $this->load->model('projects');
  40.         foreach($projects as $project) {
  41.             $this->projects->name = $project;
  42.             $this->projects->description = generate_string(80);
  43.             $this->projects->date_creation = generate_date("-3 months", '-0 months');
  44.             $this->projects->SaveNew();
  45.             $project_ids[$project] = $this->projects->projectsId;
  46.         }
  47.                
  48.         /*
  49.          * USERS
  50.         */
  51.         $this->load->model('users');
  52.         $this->users->username = "PedroWorcel";
  53.         $this->users->password = $this->auth->HashPassword('password');
  54.         $this->users->name = "Pedro";
  55.         $this->users->last_name = "Worcel";
  56.         $this->users->role = 1;
  57.         $this->users->date_registration = NOW();
  58.         $this->users->SaveNew();
  59.         $users['pedro_worcel'] = $this->users->usersId;
  60.        
  61.         $this->users->username = "JoseMartinez";
  62.         $this->users->password = $this->auth->HashPassword('password');
  63.         $this->users->name = "Jose";
  64.         $this->users->last_name = "Martinez";
  65.         $this->users->role = 1;
  66.         $this->users->date_registration = NOW();
  67.         $this->users->SaveNew();
  68.         $users['jose_martinez'] = $this->users->usersId;
  69.        
  70.         $this->users->username = "AlbertEnstein";
  71.         $this->users->password = $this->auth->HashPassword('password');
  72.         $this->users->name = "Albert";
  73.         $this->users->last_name = "Einstein";
  74.         $this->users->role = 1;
  75.         $this->users->date_registration = NOW();
  76.         $this->users->SaveNew();
  77.         $users['albert_einstein'] = $this->users->usersId;
  78.        
  79.         /*
  80.          * USERS PROJECTS
  81.         */
  82.         $users_projects['proyectos'] = array(
  83.             $users['pedro_worcel'],
  84.         );
  85.        
  86.         $users_projects['plataforma'] = array(
  87.             $users['pedro_worcel'],
  88.             $users['jose_martinez']
  89.         );
  90.        
  91.         $users_projects['filtros'] = array(
  92.             $users['pedro_worcel']
  93.         );
  94.        
  95.         $users_projects['gamelandia'] = array(
  96.             $users['pedro_worcel'],
  97.             $users['jose_martinez'],
  98.             $users['albert_einstein']
  99.         );
  100.        
  101.         $users_projects['relatividad'] = array(
  102.             $users['albert_einstein']
  103.         );
  104.        
  105.         $users_projects['portatiles'] = array(
  106.             $users['jose_martinez']
  107.         );
  108.        
  109.         $this->load->model('users_projects');
  110.         foreach($users_projects as $project_name => $project_users) {
  111.             $project_id = $project_ids[$project_name];
  112.             foreach($project_users as $project_user) {
  113.                 $this->users_projects->usersId = $project_user;
  114.                 $this->users_projects->projectsId = $project_id;
  115.                 $this->users_projects->SaveNew();
  116.             }
  117.         }
  118.        
  119.        
  120.         /*
  121.          * DIVISIONS
  122.         */
  123.        
  124.         $this->load->model('divisions');
  125.         $divisions = array(
  126.             'contenido',
  127.             'ciencia',
  128.             'juegos'
  129.         );
  130.        
  131.         foreach($divisions as $division) {
  132.             $this->divisions->name = $division;
  133.             $this->divisions->detail = generate_string();
  134.             $this->divisions->date_creation = NOW();
  135.             $this->divisions->SaveNew();
  136.             $division_ids[$division] = $this->divisions->divisionsId;
  137.         }
  138.        
  139.         /*
  140.          * PROJECTS DIVISIONS
  141.         */
  142.         $projects_divisions = array(
  143.             'contenido' => array(
  144.                 'plataforma',
  145.                 'filtros',
  146.                 'portatiles'
  147.             ),
  148.             'ciencia' => array(
  149.                 'relatividad',
  150.                 'proyectos'
  151.             ),
  152.             'juegos' => array(
  153.                 'gamelandia'
  154.             )
  155.         );
  156.        
  157.         $this->load->model('projects_divisions');
  158.         foreach($projects_divisions as $division => $projects) {
  159.             $division_id = $division_ids[$division];
  160.             foreach($projects as $project) {
  161.                 $this->projects_divisions->divisionsId = $division_id;
  162.                 $this->projects_divisions->projectsId = $project_ids[$project];
  163.                 $this->projects_divisions->SaveNew();
  164.             }
  165.         }
  166.        
  167.         /*
  168.          * USERS PRICES
  169.         */
  170.        
  171.         $users_prices = array(
  172.             $users["pedro_worcel"] => array(
  173.                 '2010-01-01' => '2700',
  174.                 '2010-08-01' => '3100'
  175.             ),
  176.             $users['jose_martinez'] => array(
  177.                 '2010-01-01' => '2200',
  178.                 '2010-08-01' => '2700'
  179.             ),
  180.             $users['albert_einstein'] => array(
  181.                 '2010-01-01' => '3200',
  182.                 '2010-08-01' => '4000'
  183.             ),
  184.         );
  185.        
  186.         $this->load->model("users_prices");
  187.         foreach($users_prices as $usersId => $users_price) {
  188.             foreach($users_price as $date => $price) {
  189.                 $this->users_prices->usersId = $usersId;
  190.                 $this->users_prices->price = $price;
  191.                 $this->users_prices->date_updated = $date;
  192.                 $this->users_prices->SaveNew();
  193.             }
  194.         }
  195.        
  196.         /*
  197.          * REPORTS
  198.         */
  199.        
  200.         /*tendency*/
  201.         function get_tendency($users, $project_ids) {
  202.             foreach($users as $user) {
  203.                
  204.                 global $controller;
  205.                
  206.                 $fcv_where = array(
  207.                     array(
  208.                         'usersId',
  209.                         '=',
  210.                         $user
  211.                     )
  212.                 );
  213.                 $user_projects = $controller->users_projects->GetList($fcv_where);
  214.                 $workedHours = 0;
  215.                 $x = 1;
  216.                 foreach($user_projects as $user_project) {
  217.                     if($workedHours > 8) break;
  218.                     if($user == $users['pedro_worcel']) {
  219.                         if($user_project->projectsId == $project_ids['proyectos']) {
  220.                             $rand = mt_rand(4,8-$workedHours);
  221.                             $workedHours += $rand;
  222.                             $tendency[$user][$project_ids['proyectos']] = $rand;
  223.                         } elseif($user_project->projectsId == $project_ids['plataforma']) {
  224.                             $rand = mt_rand(0,8-$workedHours);
  225.                             $workedHours += $rand;
  226.                             $tendency[$user][$project_ids['plataforma']] = $rand;
  227.                         } elseif($user_project->projectsId == $project_ids['filtros']) {
  228.                             $rand = mt_rand(0,8-$workedHours);
  229.                             $workedHours += $rand;
  230.                             $tendency[$user][$project_ids['filtros']] = $rand;
  231.                         } elseif($user_project->projectsId == $project_ids['gamelandia']) {
  232.                             $rand = mt_rand(0,8-$workedHours);
  233.                             $workedHours += $rand;
  234.                             $tendency[$user][$project_ids['gamelandia']] = $rand;
  235.                         }
  236.                     } elseif($user == $users['jose_martinez']) {
  237.                         if($user_project->projectsId == $project_ids['plataforma']) {
  238.                             $rand = mt_rand(4,8-$workedHours);
  239.                             $workedHours += $rand;
  240.                             $tendency[$user][$project_ids['plataforma']] = $rand;
  241.                         } elseif($user_project->projectsId == $project_ids['gamelandia']) {
  242.                             $rand = mt_rand(0, 8-$workedHours);
  243.                             $workedHours += $rand;
  244.                             $tendency[$user][$project_ids['gamelandia']] = $rand;
  245.                         } elseif($user_project->projectsId == $project_ids['portatiles']) {
  246.                             $rand = mt_rand(0, 8-$workedHours);
  247.                             $workedHours += $rand;
  248.                             $tendency[$user][$project_ids['portatiles']] = $rand;
  249.                         }
  250.                     } elseif($user == $users['albert_einstein']) {
  251.                         if( $user_project->projectsId == $project_ids['gamelandia']) {
  252.                             $rand = mt_rand(0, 8-$workedHours);
  253.                             $workedHours += $rand;
  254.                             $tendency[$user][$project_ids['gamelandia']] = $rand;
  255.                         } elseif($user_project->projectsId == $project_ids['relatividad'] ) {
  256.                             $rand = mt_rand(0, 8-$workedHours);
  257.                             $workedHours += $rand;
  258.                             $tendency[$user][$project_ids['relatividad']] = $rand;
  259.                         }
  260.                     }
  261.                    
  262.                     $x++;
  263.                     if($x == count($user_projects) && $workedHours < 8) {
  264.                         @$tendency[$user][$project_ids[array_search($user_projects[mt_rand(0, count($user_projects) - 1)]->projectsId, $project_ids)]] += 8 - $workedHours;
  265.                     }
  266.                    
  267.                 }
  268.             }
  269.             return $tendency;
  270.         }
  271.        
  272.         $months = array(
  273.             'january' => range(1,31),
  274.             'february' => range(1,28),
  275.             'march' => range(1,31),
  276.             'april' => range(1,30),
  277.             'may' => range(1,31),
  278.             'june' => range(1,30),
  279.             'july' => range(1,31),
  280.             'august' => range(1,31),
  281.             'september' => range(1,date("j"))
  282.         );
  283.         $a = -1;
  284.         $this->load->model('reports');
  285.         foreach($months as $month => $days) {
  286.             foreach($days as $day) {
  287.                 $date = date("Y-m-d H:i:s", strtotime("$month $day 2010 16:00"));
  288.                 if($a == 0) {
  289.                     $isWorkingDay = true;
  290.                 } elseif($a == 5) {
  291.                     $isWorkingDay = false;
  292.                 } elseif($a == 6) {
  293.                     $a = -1;
  294.                 }
  295.                
  296.                 if(@$isWorkingDay) {
  297.                    
  298.                     $projects_worked = get_tendency($users, $project_ids);
  299.                     foreach($projects_worked as $user_id => $project) {
  300.                         foreach($project as $project_id => $worked_hours) {
  301.                             $this->reports->projectsId = $project_id;
  302.                             $this->reports->usersId = $user_id;
  303.                             $this->reports->date_creation = $date;
  304.                             $this->reports->hours_invested = $worked_hours;
  305.                             $this->reports->detail = generate_string(mt_rand(0, 120));
  306.                             $this->reports->SaveNew();
  307.                         }
  308.                     }
  309.                    
  310.                 }              
  311.                
  312.                 $a++;              
  313.             }      
  314.         }      
  315.    
  316.     }
  317.    
  318.     function import_csv() {
  319.        
  320.         error_reporting(E_ALL);
  321.  
  322.         $csv_handle = fopen(SITE_PATH . "/csv/reportes_diarios.csv", "r");
  323.        
  324.         /*
  325.          * first parse.
  326.          * Creates and stores
  327.          *      divisions
  328.          *      projects
  329.          *      projects_divisions
  330.          *      users
  331.          *      users_projects
  332.         */
  333.         $a = 1;
  334.         while(false !== ($line = fgetcsv($csv_handle))) {
  335.        
  336.             if($a > 2) {
  337.                 $date_creation = trim($line[0]);
  338.                
  339.                 $user = trim($line[1]);
  340.                 $exploded_user = explode(" ", $user);
  341.                 $name = $this->standarize($exploded_user[0]);
  342.                 $last_name = $this->standarize(@$exploded_user[1]);
  343.                
  344.                 $division = $this->standarize($line[2]);
  345.                 $project = $this->standarize($line[3]);
  346.                 $duracion_tarea = trim($line[4]);
  347.                 $detalle = trim($line[5]);
  348.                
  349.                 /* divisions */
  350.                 if(null === ($divisions[$division] = $this->db->get_var("SELECT divisionsId FROM divisions WHERE name = '$division'"))) {
  351.                    
  352.                     $insertArray = array(
  353.                         'name' => $division
  354.                     );
  355.                    
  356.                     $this->db->insert('divisions', $insertArray);
  357.                    
  358.                     $divisions[$division] = mysql_insert_id();
  359.                    
  360.                 }
  361.  
  362.                 /* projects */
  363.                 if(null === ($projects[$project] = $this->db->get_var("SELECT * FROM projects WHERE name = '$project'"))) {
  364.                
  365.                     $insertArray = array(
  366.                         'name' => $project
  367.                     );
  368.                    
  369.                     $this->db->insert('projects', $insertArray);
  370.                    
  371.                     $projects[$project] = mysql_insert_id();
  372.                    
  373.                 }
  374.                
  375.                 /* projects_divisions */
  376.                 if(null === ($this->db->get_var("SELECT projects_divisionsId
  377.                                                                  FROM projects_divisions
  378.                                                                  WHERE projectsId = {$projects[$project]}
  379.                                                                  AND divisionsId = {$divisions[$division]}"))) {
  380.                    
  381.                     $insertArray = array(
  382.                         'projectsid' => $projects[$project],
  383.                         'divisionsid' => $divisions[$division]
  384.                     );
  385.                    
  386.                     $this->db->insert('projects_divisions', $insertArray);
  387.                
  388.                 }
  389.                
  390.                 /* users */
  391.                 if(null === ($users[$user] = $this->db->get_var("SELECT usersId FROM users WHERE name = '$name' AND last_name = '$last_name'"))) {
  392.                
  393.                     $password = "password";
  394.                     $hashed = $this->auth->HashPassword($password);
  395.                    
  396.                     $insertArray = array(
  397.                         'username' => $name,
  398.                         'password' => $hashed,
  399.                         'name' => $name,
  400.                         'last_name' => $last_name,
  401.                         'email' => 'invalid_email@hotmail.com',
  402.                         'role' => 1,
  403.                         'date_registration' => NOW()
  404.                     );
  405.                    
  406.                     $this->db->insert('users', $insertArray);
  407.                    
  408.                     $users[$user] = mysql_insert_id();
  409.                    
  410.                 }
  411.                
  412.                 /* users_projects */
  413.                 if(null == $this->db->get_var("SELECT users_projectsId FROM users_projects WHERE usersId = {$users[$user]} AND projectsId = {$projects[$project]}")) {
  414.                    
  415.                     $insertArray = array(
  416.                         'usersId' => $users[$user],
  417.                         'projectsId' => $projects[$project]
  418.                     );
  419.                    
  420.                     $this->db->insert("users_projects", $insertArray);
  421.                    
  422.                 }
  423.                
  424.                
  425.             } else {
  426.                 $a++;
  427.             }
  428.            
  429.         }
  430.        
  431.         rewind($csv_handle);
  432.                
  433.         /*
  434.          * second parse
  435.          * Creates reports
  436.         */
  437.         $a = 1;
  438.         while(false !== ($line = fgetcsv($csv_handle))) {
  439.        
  440.             if($a > 2) {
  441.                
  442.                 $date_creation = trim($line[0]);
  443.                 $date_exploded = explode("/", $date_creation);
  444.                 $new_date = $date_exploded[1] . "/" . $date_exploded[0] . "/" . $date_exploded[2];
  445.                 $new_date = date_mysql(strtotime($new_date));
  446.                
  447.                 $user = trim($line[1]);
  448.                 $exploded_user = explode(" ", $user);
  449.                 $name = $this->standarize($exploded_user[0]);
  450.                 $last_name = $this->standarize($exploded_user[1]);
  451.                
  452.                 $division = $this->standarize($line[2]);
  453.                 $project = $this->standarize($line[3]);
  454.                
  455.                 $detail = trim($line[5]);
  456.                
  457.                 $duracion_tarea = explode(":", trim($line[4]));
  458.                
  459.                 $hours_invested = $duracion_tarea[0];
  460.                 if($duracion_tarea[1] != "00") {
  461.                     $converted_minutes = $duracion_tarea[1] / 60;
  462.                     $hours_invested += $converted_minutes;
  463.                 }
  464.                
  465.                 $hours_invested = round($hours_invested, 2);
  466.  
  467.                 $insertArray = array(
  468.                     'projectsid' => $projects[$project],
  469.                     'usersid' => $users[$user],
  470.                     'date_creation' => $new_date,
  471.                     'hours_invested' => $hours_invested,
  472.                     'detail' => $detail
  473.                 );
  474.                
  475.                 $this->db->insert('reports', $insertArray);
  476.                
  477.  
  478.             } else {
  479.                 $a++;
  480.             }
  481.            
  482.         }
  483.        
  484.         if(!@$errors) {
  485.             echo "Este mensaje debería de indicar que todo marchó bien. Pero en realidad no indica nada.";
  486.         }
  487.        
  488.     }
  489.    
  490.     function prettify_names() {
  491.    
  492.         $this->load->model("divisions");
  493.         $divisions = $this->divisions->GetList();
  494.         foreach($divisions as $division) {
  495.             $division->name = str_replace("-", " ", ucfirst($division->name));
  496.             $division->Save();
  497.         }
  498.        
  499.         $this->load->model("projects");
  500.         $projects = $this->projects->GetList();
  501.         foreach($projects as $project) {
  502.             $project->name = preg_replace_callback("-", " ", ucfirst($project->name));
  503.             $project->Save();
  504.         }
  505.        
  506.         $this->load->model("users");
  507.         $users = $this->users->GetList();
  508.         foreach($users as $user) {
  509.             $user->name = ucfirst($user->name);
  510.             $user->last_name = ucfirst($user->last_name);
  511.             $user->Save();
  512.         }
  513.        
  514.         echo "All good.";
  515.        
  516.     }
  517.    
  518.     function standarize($text, $encoding = 'utf-8') {
  519.    
  520.         global $encodign;
  521.         if($encodign) {
  522.             $encoding = $encodign;
  523.         }
  524.        
  525.         $text = preg_replace ( '~[^\\pL\d]+~u', '-', $text );
  526.        
  527.         $text = trim ( $text, '-' );
  528.        
  529.         if (function_exists ( 'iconv' )) {
  530.             $text = iconv ( $encoding, 'us-ascii//TRANSLIT', $text );
  531.         }
  532.        
  533.         $text = strtolower ( $text );
  534.    
  535.         // remove unwanted characters
  536.         $text = preg_replace ( '~[^-\w]+~', '', $text );
  537.        
  538.         if(empty($text)) {
  539.             return false;  
  540.         }
  541.        
  542.         return $text;
  543.     }
  544.    
  545. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement