Advertisement
Guest User

Untitled

a guest
May 26th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.95 KB | None | 0 0
  1. <?php
  2. namespace Adianti\Base;
  3.  
  4. use Exception;
  5. use Adianti\Core\AdiantiCoreTranslator;
  6. use TTransaction;
  7. use TMessage;
  8.  
  9. /**
  10.  * File Save Trait
  11.  *
  12.  * @version    5.0
  13.  * @package    base
  14.  * @author     Nataniel Rabaioli
  15.  * @author     Pablo Dall'Oglio
  16.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  17.  * @license    http://www.adianti.com.br/framework-license
  18.  */
  19. use Intervention\Image\ImageManagerStatic as Image;
  20. trait AdiantiFileSaveTrait
  21. {
  22.     /**
  23.      * Save file
  24.      * @param $object      Active Record
  25.      * @param $data        Form data
  26.      * @param $input_name  Input field name
  27.      * @param $target_path Target file path
  28.      */
  29.     public function saveFile($object, $data, $input_name, $target_path)
  30.     {
  31.         $dados_file = json_decode(urldecode($data->$input_name));
  32.        
  33.         if (isset($dados_file->fileName))
  34.         {
  35.             $pk = $object->getPrimaryKey();
  36.            
  37.            
  38.             $target_path.= '/' . $object->$pk;
  39.             $source_file = $dados_file->fileName;
  40.             //Minhas Modificações ##############################
  41.  
  42.             $arquivo = pathinfo($dados_file->fileName);
  43.             $extensao = Array('jpg','png','jpeg','gif','bmp');
  44.             $array_sem_imagens = Array('contents/recursos/semfoto.jpg','contents/recursos/cartao_vazio.jpg');
  45.             $class = get_class($object);
  46.             try
  47.             {
  48.                 TTransaction::open('base'); // open a transaction
  49.                 $objeto = new $class($object->$pk);
  50.                 TTransaction::close();
  51.             }
  52.             catch (Exception $e)
  53.             {
  54.                 new TMessage('error', $e->getMessage());
  55.                 TTransaction::rollback(); // undo all pending operations
  56.             }
  57.            
  58.             if (in_array($arquivo['extension'], $extensao))
  59.             {
  60.                 if($dados_file->fileName)
  61.                 {
  62.                         $img = Image::make($dados_file->fileName)->resize(200, 200)->insert('contents/recursos/watermark.png', 'bottom-right')->save();
  63.                         $target_file = strpos($img, $target_path) === FALSE ? $target_path . '/' .uniqid(). '.jpg' : $img;
  64.                         $target_file = str_replace('tmp/', '', $target_file);
  65.                 }
  66.             }
  67.             else
  68.             {
  69.                 $target_file = strpos($dados_file->fileName, $target_path) === FALSE ? $target_path . '/' . uniqid() . '.' . $arquivo['extension'] : $dados_file->fileName;
  70.                 $target_file = str_replace('tmp/', '', $target_file);
  71.             }
  72.             //Fim #################################################
  73.  
  74.            
  75.             $obj_store = new $class;
  76.             $obj_store->$pk = $object->$pk;
  77.             $obj_store->$input_name = $target_file;
  78.            
  79.             var_dump($input_name);
  80.            
  81.             $delFile = null;
  82.            
  83.             if (!empty($dados_file->delFile))
  84.             {
  85.                 $obj_store->$input_name = '';
  86.                 $dados_file->fileName = '';
  87.                
  88.                 if (is_file(urldecode($dados_file->delFile)))
  89.                 {
  90.                     $delFile = urldecode($dados_file->delFile);
  91.                    
  92.                     if (file_exists($delFile))
  93.                     {
  94.                         unlink($delFile);
  95.                     }
  96.                 }
  97.             }
  98.    
  99.             if (!empty($dados_file->newFile))
  100.             {
  101.                 if (file_exists($source_file))
  102.                 {
  103.                     if (!file_exists($target_path))
  104.                     {
  105.                         if (!mkdir($target_path, 0777, true))
  106.                         {
  107.                             throw new Exception(AdiantiCoreTranslator::translate('Permission denied') . ': '. $target_path);
  108.                         }
  109.                     }
  110.                    
  111.                     // if the user uploaded a source file
  112.                     if (file_exists($target_path))
  113.                     {
  114.                         // move to the target directory
  115.                         if (! rename($source_file, $target_file))
  116.                         {
  117.                             throw new Exception(AdiantiCoreTranslator::translate('Error while copying file to ^1', $target_file));
  118.                         }
  119.                        
  120.                         $obj_store->$input_name = $target_file;
  121.  
  122.                     }
  123.                 }
  124.             }
  125.             elseif ($dados_file->fileName != $delFile)
  126.             {
  127.                 $obj_store->$input_name = $dados_file->fileName;
  128.             }
  129.            
  130.             $obj_store->store();
  131.            
  132.             if ($obj_store->$input_name)
  133.             {
  134.                 $dados_file->fileName = $obj_store->$input_name;
  135.                 $data->$input_name = urlencode(json_encode($dados_file));
  136.             }
  137.             else
  138.             {
  139.                 $data->$input_name = '';
  140.             }
  141.         }
  142.     }
  143.    
  144.     /**
  145.      * Save files
  146.      * @param $object      Active Record
  147.      * @param $data        Form data
  148.      * @param $input_name  Input field name
  149.      * @param $target_path Target file path
  150.      * @param $model_files Files Active Record
  151.      * @param $file_field  File field in model_files
  152.      * @param $foreign_key Foreign key to $object
  153.      */
  154.     public function saveFiles($object, $data, $input_name, $target_path, $model_files, $file_field, $foreign_key)
  155.     {
  156.         $pk = $object->getPrimaryKey();
  157.        
  158.         $delFiles   = [];
  159.         $files_form = [];
  160.         $target_path.= '/' . $object->$pk;
  161.        
  162.         if (isset($data->$input_name) AND $data->$input_name)
  163.         {
  164.             foreach ($data->$input_name as $key => $info_file)
  165.             {            
  166.                 $dados_file = json_decode(urldecode($info_file));
  167.                
  168.                 if (!empty($dados_file->fileName))
  169.                 {
  170.                     $source_file = $dados_file->fileName;
  171.                     $target_file = $target_path . '/' . $dados_file->fileName;
  172.                     $target_file = str_replace('tmp/', '', $target_file);
  173.                    
  174.                     $file_form = [];
  175.                     $file_form['delFile']  = false;
  176.                     $file_form['idFile']   = (isset($dados_file->idFile) AND $dados_file->idFile) ? $dados_file->idFile : null;
  177.                     $file_form['fileName'] = $dados_file->fileName;
  178.                    
  179.                     if (!empty($dados_file->delFile))
  180.                     {
  181.                         $file_form['delFile'] = true;
  182.                        
  183.                         if (!empty($dados_file->idFile))
  184.                         {
  185.                             $file = $model_files::find($dados_file->idFile);
  186.                            
  187.                             if ($file)
  188.                             {
  189.                                 if ($file->$file_field AND is_file($file->$file_field))
  190.                                 {
  191.                                     unlink( $file->$file_field );
  192.                                 }
  193.                                 $file->delete();
  194.                             }
  195.                         }
  196.                     }
  197.                    
  198.                     if (!empty($dados_file->newFile))
  199.                     {
  200.                         if (file_exists($source_file))
  201.                         {
  202.                             if (!file_exists($target_path))
  203.                             {
  204.                                 if (!mkdir($target_path, 0777, true))
  205.                                 {    
  206.                                     throw new Exception(AdiantiCoreTranslator::translate('Permission denied') . ': '. $target_path);
  207.                                 }
  208.                             }
  209.                        
  210.                             // if the user uploaded a source file
  211.                             if (file_exists($target_path))
  212.                             {
  213.                                 // move to the target directory
  214.                                 if (! rename($source_file, $target_file))
  215.                                 {
  216.                                     throw new Exception(AdiantiCoreTranslator::translate('Error while copying file to ^1', $target_file));
  217.                                 }
  218.                                
  219.                                 $model_file = new $model_files;
  220.                                 $model_file->$file_field = $target_file;
  221.                                 $model_file->$foreign_key = $object->$pk;
  222.                                
  223.                                 $model_file->store();
  224.                                
  225.                                 $pk_detail = $model_file->getPrimaryKey();
  226.                                 $file_form['idFile'] = $model_file->$pk_detail;
  227.                                 $file_form['fileName'] = $target_file;
  228.                             }
  229.                         }
  230.                     }
  231.                    
  232.                     if ($file_form and !$file_form['delFile'])
  233.                     {
  234.                         $files_form[] = $file_form;
  235.                     }
  236.                 }
  237.             }
  238.            
  239.             $data->$input_name = $files_form;
  240.         }
  241.     }
  242.    
  243.     /**
  244.      * Save files comma separated
  245.      * @param $object      Active Record
  246.      * @param $data        Form data
  247.      * @param $input_name  Input field name
  248.      * @param $target_path Target file path
  249.      */
  250.     public function saveFilesByComma($object, $data, $input_name, $target_path)
  251.     {
  252.         $save_files = [];
  253.         $delFiles   = [];
  254.         $files_form = [];
  255.        
  256.         $pk = $object->getPrimaryKey();
  257.         $target_path.= '/' . $object->$pk;
  258.        
  259.         if (isset($data->$input_name) AND $data->$input_name)
  260.         {
  261.             foreach ($data->$input_name as $key => $info_file)
  262.             {            
  263.                 $dados_file = json_decode(urldecode($info_file));
  264.                
  265.                 $source_file = $dados_file->fileName;
  266.                 $target_file = $target_path . '/' . $dados_file->fileName;
  267.                 $target_file = str_replace('tmp/', '', $target_file);
  268.                
  269.                 $save_file = $dados_file->fileName;
  270.                
  271.                 $file_form = [];
  272.                 $file_form['delFile']  = false;
  273.                 $file_form['idFile']   = (isset($dados_file->idFile) AND $dados_file->idFile) ? $dados_file->idFile : null;
  274.                 $file_form['fileName'] = $dados_file->fileName;
  275.                
  276.                 if (!empty($dados_file->delFile))
  277.                 {
  278.                     $file_form['delFile'] = true;
  279.                     $save_file = null;
  280.                    
  281.                     if (file_exists( urldecode($dados_file->delFile) ))
  282.                     {
  283.                         unlink( urldecode($dados_file->delFile) );
  284.                     }
  285.                 }
  286.                
  287.                 if (!empty($dados_file->newFile))
  288.                 {
  289.                     if (file_exists($source_file))
  290.                     {
  291.                         if (!file_exists($target_path))
  292.                         {
  293.                             if (!mkdir($target_path, 0777, true))
  294.                             {    
  295.                                 throw new Exception(AdiantiCoreTranslator::translate('Permission denied') . ': '. $target_path);
  296.                             }
  297.                         }
  298.                    
  299.                         // if the user uploaded a source file
  300.                         if (file_exists($target_path))
  301.                         {
  302.                             // move to the target directory
  303.                             if (! rename($source_file, $target_file))
  304.                             {
  305.                                 throw new Exception(AdiantiCoreTranslator::translate('Error while copying file to ^1', $target_file));
  306.                             }
  307.                            
  308.                             $file_form['idFile'] = $target_file;
  309.                             $file_form['fileName'] = $target_file;
  310.                            
  311.                             $save_file = $target_file;
  312.                         }
  313.                     }
  314.                 }
  315.                
  316.                 if ($save_file)
  317.                 {
  318.                     $save_files[] = $save_file;
  319.                 }
  320.                
  321.                 if ($file_form and !$file_form['delFile'])
  322.                 {
  323.                     $files_form[] = $file_form;
  324.                 }                
  325.             }
  326.            
  327.             $class = get_class($object);
  328.             $obj_store = new $class;
  329.             $obj_store->$pk = $object->$pk;
  330.             $obj_store->$input_name = implode(',', $save_files);
  331.             $obj_store->store();
  332.            
  333.             $data->$input_name = $files_form;
  334.         }
  335.     }
  336. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement