2011-02-01 3 views
0

現在、自分のコントローラーで使用している画像処理コードがモデルに適していると私は気付いていますが、どこから始めるべきかわかりません。コードイグナイターでモデル化するコントローラをリファクタリングする

私は、画像をアップロードするファイルの名前を変更し、教義を使用してデータベースに格納処理するコントローラを持っている:

<?php 
class Addimage extends Controller 
{ 

    function index() 
    { 


     $vars['content_view'] = 'uploadimage'; 
     $this->load->view('template', $vars);   

    } 

    public function do_upload() 
    { 
     $this->load->library('form_validation'); 
     if($this->_submit_validate() == FALSE) 
     { 
/*THIS CODE BLOCK IS DUPLICATED FROM MY HOME PAGE CONTROLLER - this is one of the reasons I want to refactor.*/ 
      $vars['recentimages'] = Doctrine_Query::create() 
     ->select('photo_path') 
     ->from('Gif g') 
     ->orderBy('g.created_at DESC') 
     ->limit(12) 
     ->execute(); 



     $vars['title'] = 'Home'; 
     $vars['content_view'] = 'welcome_message'; 


     $this->load->view('template_front', $vars); 

     } 
     else 
     {   

      $basedir = $this->config->item('server_root') . $this->config->item('upload_dir'); 

      //If the directory doesn't already exist, create it. 
      if (!is_dir($basedir)) 
      { 
       mkdir($basedir, 0777); 
      }   



      $config = array(
       'allowed_types' => "gif",   
       'upload_path' => $basedir, 
       'remove_spaces' => true 
      ); 
      $this->load->library('upload', $config); 
      if(!$this->upload->do_upload()) 
      { 
       $data['error'] = 'There was a problem with the upload'; 
      } 
      else 
      { 

       $image_data = $this->upload->data();     
       $fileName = $image_data['file_name']; 
       $title = $this->input->post('title'); 

       //Rename File based on how many of that letter 
       //are already in the database 
       $imageCount = Doctrine_Query::create() 
        ->select('COUNT(i.id) as num_images') 
        ->from('Gif i')     
        ->execute(); 

       $imageCount = $imageCount[0]->num_images++; 
       //Rename file based on title and number of images in db. 
       $newFileName = preg_replace('/[^a-zA-Z0-9\s]/', '', $title) . '_' . $imageCount . $image_data['file_ext']; 
       rename($basedir . $fileName, $basedir . $newFileName); 


       $gif = new Gif(); 
       $gif->photo_path = $newFileName; 
       $gif->title = $title; 
       if(Current_User::user()) 
       { 
        $gif->User = Current_User::user();    
       } 
       else 
       {     
        $gif->User = Doctrine::getTable('User')->findOneById($this->config->item('anonuid')); 
       } 
       $gif->save(); 
      } 



      redirect('/', 'location'); 
     } 
    } 

    private function _submit_validate() 
    { 
     $this->form_validation->set_rules('title', 'Title', 'required'); 
     return $this->form_validation->run(); 
    } 

} 

私はモデルでこれのほとんどを持ってできるようにしたいと思い、私ので、私のuploadimage.phpビューはアップロードフォームだけなので、どのページにもドロップすることができるビューのテンプレートシステムを使用しています。また、私はDoctrineモデルを使った経験があります。事前

答えて

0

で任意の助け

おかげで私は自分のプロジェクトに非常によく似た問題を持っていた:コントローラーでの重複を。私はあなたのケースでは、そのロジックの一部をモデルに移動することが理にかなっていると考えています。なぜなら、実際にはコントローラの中にあるのが理にかなっているからです。

レンダリングビューは間違いなくコントローラ内にある必要があります。私はトランザクション部分をモデルに移します:SQL、ファイル処理、画像操作です。

この場合、コントローラのロジックとモデルのロジックが絡み合っているため、重複はありますが、それ以外の方法はありません。

関連する問題