<?php

class HomeController extends BaseController {

	/*
	|--------------------------------------------------------------------------
	| Default Home Controller
	|--------------------------------------------------------------------------
	|
	| You may wish to use controllers instead of, or in addition to, Closure
	| based routes. That's great! Here is an example controller method to
	| get you started. To route to this controller, just add the route:
	|
	|	Route::get('/', 'HomeController@showWelcome');
	|
	*/

	public function showWelcome()
	{
		try {
			require_once 'PHPWord-0.12.1/src/PhpWord/Autoloader.php';
			\PhpOffice\PhpWord\Autoloader::register();

			// Creating the new document...
			$phpWord = new \PhpOffice\PhpWord\PhpWord();

			$styleTable = array('borderColor'=>'006699',
              'borderSize'=>6,
              'cellMargin'=>50);
			$styleFirstRow = array('bgColor'=>'66BBFF');
			$phpWord->addTableStyle('myTable', $styleTable, $styleFirstRow);

			/* Note: any element you append to a document must reside inside of a Section. */

			// Adding an empty Section to the document...
			$section = $phpWord->addSection();
			// Adding Text element to the Section having font styled by default...
			$section->addText(
			    htmlspecialchars(
			        '"Learn from yesterday, live for today, hope for tomorrow. '
			            . 'The important thing is not to stop questioning." '
			            . '(Albert Einstein)'
			    )
			);

			$table = $section->addTable('myTable');
			$table->addRow(3);
			$table->addCell(1200)->addText("Col 1");
			$table->addCell(1200)->addText("Col 2");
			$table->addCell(1200)
			->addTable('myTable')
			->addRow(2)->addCell(1200)
			->addText("Col 1");

			/*
			 * Note: it's possible to customize font style of the Text element you add in three ways:
			 * - inline;
			 * - using named font style (new font style object will be implicitly created);
			 * - using explicitly created font style object.
			 */

			// Adding Text element with font customized inline...
			$section->addText(
			    htmlspecialchars(
			        '"Great achievement is usually born of great sacrifice, '
			            . 'and is never the result of selfishness." '
			            . '(Napoleon Hill)'
			    ),
			    array('name' => 'Tahoma', 'size' => 10)
			);


			// Adding Text element with font customized using named font style...
			$fontStyleName = 'oneUserDefinedStyle';
			$phpWord->addFontStyle(
			    $fontStyleName,
			    array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
			);
			$section->addText(
			    htmlspecialchars(
			        '"The greatest accomplishment is not in never falling, '
			            . 'but in rising again after you fall." '
			            . '(Vince Lombardi)'
			    ),
			    $fontStyleName
			);


			// Adding Text element with font customized using explicitly created font style object...
			$fontStyle = new \PhpOffice\PhpWord\Style\Font();
			$fontStyle->setBold(true);
			$fontStyle->setName('Tahoma');
			$fontStyle->setSize(13);
			$myTextElement = $section->addText(
			    htmlspecialchars('"Believe you can and you\'re halfway there." (Theodor Roosevelt)')
			);
			$myTextElement->setFontStyle($fontStyle);

			// Saving the document as OOXML file...
			$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
			

			$objWriter->save('helloWorld2.docx');

			return Response::download('helloWorld2.docx');
			
		}
		catch(Exception $e)
		{
		}
	}
}