Separating the controller and the view

Hi there,

I am using CodeIgnitor and PHPWord for an application that outputs a few reports.

Everything is working well but I realise that I am not strictly following the MVC pattern as when I am using PHPWord to output a report, the content of the word document is being added in the controller when it should be the view.

Here is the code for the function outputting the word report. It is within the reports controller fiel: reports.php

So if I understand correctly all the calls to the addText() function should be made from the view file, is that correct? Any tips on how to move these calls into a "view"?

function print_inspection_word() {
                $data['buildingdetails'] = $this->report_model->getBuildingDetails($this->uri->segment(3),$this->uri->segment(4),$this->uri->segment(5),$this->uri->segment(6));
                $data['buildinghazards'] = $this->report_model->getBuildingHazards($this->uri->segment(5));
                $companyName = "";
    foreach($data['buildingdetails'] as $detail){
     $companyName = $detail->company;

                $data['company_types'] = $this->group_model->getCompany_type();
                $data['countries'] = $this->group_model->getCountries();
                $data['hazard_levels'] = $this->hazard_model->getHazardLevels();
                $data['load_header'] = false;
    $data['load_footer'] = false;
    $data['main_content'] = 'print_inspection';
    $data['message'] = "";
    $data['message_type'] = "";
    $data['page_title'] = "Building Inspection Report";
    $data['body_class'] = "reports";

    //our docx will have 'lanscape' paper orientation
    $section = $this->word->createSection(array('orientation'=>'portrait'));
    // Add text elements
    // Define table style arrays
    //$styleTable = array('borderSize'=>0, 'borderColor'=>'000000', 'cellMargin'=>80);
    $styleTable = array();
    //$styleFirstRow = array('borderBottomSize'=>18, 'borderBottomColor'=>'0000FF', 'bgColor'=>'66BBFF');
    // Define cell style arrays
    $styleCell = array('valign'=>'center');
    $styleCellBTLR = array('valign'=>'center', 'textDirection'=>PHPWord_Style_Cell::TEXT_DIR_BTLR);

    // Define font style for first row
    $fontStyle = array('bold'=>true, 'align'=>'center');
    $frontPageFontStyle = array('bold'=>false, 'size'=>18, 'align'=>'center');
    $headingFontStyle = array('bold'=>true, 'size'=>18, 'align'=>'center');
    $subHeadingFontStyle = array('bold'=>true, 'underline'=> PHPWord_Style_Font::UNDERLINE_SINGLE, 'align'=>'center');
    // Add table style
    $this->word->addTableStyle('myOwnTableStyle', $styleTable);
    foreach($data['buildingdetails'] as $detail){
     $companyType = '';
     foreach($data['company_types'] as $type) {
                        if($type->id == $detail->company_type) {
                            $companyType =  $type->company_type;
     $countryName = '';
     foreach($data['countries'] as $country) {
                        if($country->id == $detail->country) {
                            $countryName =  $country->name;
     $constructionDate = date( 'F', mktime(0, 0, 0, $detail->month_contructed) )." ".$detail->year_constructed;
     $inspectionResult = '';
     if($detail->status == 1) {
                         $inspectionResult =  "Pass";
     else {
                         $inspectionResult =  "Fail";
        $section->addText($detail->buildingname, $headingFontStyle);
     $section->addText("Company Details",$subHeadingFontStyle);
     $section->addText('Company/Owner Name: ', $fontStyle);
     $section->addText('Company Type: ', $fontStyle);
     $section->addText('Address: ', $fontStyle);
     if($detail->address_line_2 != ""){
     $section->addText('Suburb: ', $fontStyle);
     $section->addText('City: ', $fontStyle);
     $section->addText('Country: ', $fontStyle);
     $section->addText('Phone: ', $fontStyle);
     $section->addText('E Mail: ', $fontStyle);
$filename='report.docx'; //save our document as this file name
    header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document'); //mime type
    header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
    header('Cache-Control: max-age=0'); //no cache
    $objWriter = PHPWord_IOFactory::createWriter($this->word, 'Word2007');


Views are typically used for presenting HTML to the user. I don't think most people would consider a downloaded file, a "view". Also, the HTML views are usually separated so a designer can work just on the html and the developer can work on the code.

There's nothing really wrong with how you're doing it. But, if you are doing this often, in multiple places, it might be better to create a library so all of your PHPWord code is self contained in a single file instead of spread across several controllers/methods. Much easier for maintenance if the PHPWord class ever gets upgraded and things change.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.