next up previous contents
Next: 5.4 ผลกระทบของการปรับปรุงใช้งาน MVC ในเซอร์วิซอฟต์ Up: 5. การใช้งานแบบแผนซอฟต์แวร์ในการปรับปรุงพัฒนาเซอร์วิซอฟต์ Previous: 5.2 แบบแผน Model View   Contents

5.3 การปรับการเขียนโปรแกรมเซอร์วิซอฟต์มาอยู่ในรูปแบบ MVC

ในการปรับปรุงเซอร์วิซอฟต์ ให้มาอยู่ในรูปแบบของ MVC นั้น ทางบริษัทได้คัดเลือก Codeigniter 5.1 ที่เป็นชุดของเครื่องมือสำหรับการสร้างโปรแกรมประยุกต์บนเว็ป เพื่อสามารถลดเวลาในการพัฒนาซอฟต์แวร์ที่ต้องทำจากเริ่มแรก

Codeigniter ประกอบด้วยไลบราสี่สำหรับการทำงานทั้งไปของโปรแกรมประยุกต์บนเว็ป รวมถึงอินเตอร์เฟส และโครงสร้างวิธีการเข้าใช้ไลบราลี่ดังกล่าว ซึ่งอยู่บนพื้นฐานของแบบแผน MVC Codeigniter มีความสามารถหลักดังนี้ • Model-View-Controller Based System • PHP 4 Compatible • Extremely Light Weight • Full Featured database classes with support for several platforms. • Active Record Database Support • Form and Data Validation • Security and XSS Filtering • Session Management • Email Sending Class. Supports Attachments, HTML/Text email, multiple protocols (sendmail, SMTP, and Mail) and more. • Image Manipulation Library (cropping, resizing, rotating, etc.). Supports GD, ImageMagick, and NetPBM • File Uploading Class • FTP Class • Localization • Pagination • Data Encryption • Benchmarking • Full Page Caching • Error Logging • Application Profiling • Scaffolding • Calendaring Class • User Agent Class • Zip Encoding Class • Template Engine Class • Trackback Class • XML-RPC Library • Unit Testing Class • Search-engine Friendly URLs • Flexible URI Routing

จากการศึกษา Codeigniter ในโครงการนี้ได้ทำการปรับจากโครงสร้างเดิมออกเป็น MVC อย่างเต็มรูปแบบโดยรูป [*] แสดงโครงสร้างไดเร็กตอรี่ก่อนการปรับปรุง และ รูป [*] แสดงโครงสร้างไดเร็กตอรี่หลังการปรับปรุง ที่มีการแยกส่วนต่างๆ ออกจากกันตาม Model View และ Controller ตามลำดับ

Figure 5.6: โครงสร้างไดเร็กตอรี่ของเซอร์วิซอฟต์ก่อนการปรับปรุง
\includegraphics[width=3in]{fig/OrgServiSoftDir}

Figure 5.7: โครงสร้างไดเร็กตอรี่ของเซอร์วิซอฟต แบบ Model View Controller โดยได้แสดงรายละเอียดของไฟล์ในส่วน View ของเซอร์วิซอฟต์ที่ปรับปรุง
\includegraphics[width=3in]{fig/MVCServiSoftDir}

เพื่อให้เห็นภาพของการปรับปรุง ได้นำส่วนของซอฟตฺ์แวร์ของการแสดงข้อมูลลูกค้า เพื่อเป็นตัวอย่างทั้งในส่วน Model View และ Controller

แสดงไฟล์ customer_info_individual_view.php ที่เป็นส่วนของการนำเสนอ ผ่านส่วนของ View ในการดูข้อมูล Customer

<?php 
$this->config->load('servisoft_ui');
$ui = $this->config->item('ui');
?>
<!-- ==================== Page Information =================== -->
<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">	
	<tr > 
		<td align="center">			
			<table width="100%" border="0" cellspacing="1" cellpadding="0" 
align="center" bgcolor="#CCCCCC">		
				<tr > 
					<td align="center">			
						<table width="100%" border="0" cellspacing="0" cellpadding="0" 
bgcolor="#EAEAEA">
							<tr > 
								<td align="center">
									<table width="100%" border="0" cellspacing="1"
 cellpadding="2" class="black12_bold" bgcolor="#FFFFFF">
										<tr height="20">
											<td bgcolor="#CCCCCC"><?=$ui['party_code'];?>:</td>
											<td bgcolor="#EAEAEA" colspan="4" class="black12_normal">
&nbsp;<?= $rs_info['code']?></td>
											<td bgcolor="#EAEAEA" colspan="1" rowspan="4" 
align="center"><?= $rs_info['picture']?></td>
										</tr>
										<tr height="20">
											<td bgcolor="#CCCCCC"><?=$ui['party_type'];?>:</td>
											<td bgcolor="#EAEAEA" colspan="4" class="black12_normal">
&nbsp;<?= $rs_info['customertype_name']?></td>
										</tr>
										<tr height="20">
											<td bgcolor="#CCCCCC" width="140"><?=$ui['prefix'];?>
 (<?=$ui['TH'];?>):</td>
											<td bgcolor="#EAEAEA" width="130" 
class="black12_normal">&nbsp;<?=$rs_info['prefix_name_th']?></td>
											<td bgcolor="#CCCCCC"	width="95">
<?=$ui['party_name'];?>:</td>
											<td bgcolor="#EAEAEA"	width="135" class="black12_normal">
&nbsp;<?=$rs_info['name_th']?></td>
											<td bgcolor="#CCCCCC"	width="95"><?=$ui['party_lastname'];
?>:</td>
											<td bgcolor="#EAEAEA"	width="115" class="black12_normal">
&nbsp;<?=$rs_info['lastname_th']?></td>
										</tr>
										<tr height="20">
											<td bgcolor="#CCCCCC"><?=$ui['email'];?> 1:</td>
											<td bgcolor="#EAEAEA" colspan="2" class="black12_normal">
&nbsp;<?=$rs_info['email'];?></td>
											<td bgcolor="#CCCCCC"><?=$ui['email'];?> 2:</td>
											<td bgcolor="#EAEAEA" colspan="2" class="black12_normal">
&nbsp;<?=$rs_info['email2'];?></td>
										</tr>
                                          .
                                          .
                                          .
										</tr>
										<tr>
											<td bgcolor="#CCCCCC"><?=$ui['party_phone_contact'];?>
:</td>
											<td bgcolor="#EAEAEA" colspan="5" class="black12_bold">
												<input type="checkbox" name="view[homeflag]" value="" 
<?php if($rs_info['homeflag']=='t'){echo "checked";} ?> disabled />
												&nbsp;<?=$ui['home_phone'];?>&nbsp;
												<input type="checkbox" name="view[mobileflag]" value="" 
<?php if($rs_info['mobileflag']=='t'){echo "checked";} ?> disabled />
												&nbsp;<?=$ui['mobile_phone'];?>&nbsp;
												<input type="checkbox" name="view[officeflag]" value="" 
<?php if($rs_info['officeflag']=='t'){echo "checked";} ?> disabled />
												&nbsp;<?=$ui['office_phone'];?>
											</td>
										</tr>
										<tr
											<td bgcolor="#CCCCCC"><?=$ui['party_time_contact'];?>
:</td>
											<td bgcolor="#EAEAEA" colspan="5" class="black12_bold">
												<input type="checkbox" name="view[morningflag]" 
value="" <?php if($rs_info['morningflag']=='t'){echo "checked";} ?> disabled />
												&nbsp;<?=$ui['party_time_morning'];?>&nbsp;
<span class="black12_normal"><?php if($rs_info['morningflag']=='t')
{echo $rs_info['morningnote'];} ?></span>
												<input type="checkbox" name="view[noonflag]" value=""
<?php if($rs_info['noonflag']=='t'){echo "checked";} ?> disabled />
												&nbsp;<?=$ui['party_time_afternoon'];?>&nbsp;
<span class="black12_normal"><?php if($rs_info['noonflag']=='t')
{echo $rs_info['noonnote'];} ?></span>
				</tr>
			</table>
		</td>
	</tr>
</table>

แสดงไฟล์ customer_model.php ที่เป็นส่วนของการนำเสนอ ผ่านส่วนของ Model ในการดูข้อมูล Customer

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Customer_model extends Model {
	var $last_num_rows;
	function Customer_model(){
		parent::model();
		$this->load->database();
		$this->load->library('datechange');
    }
                             .
                             .
                             .
	function get_oldpic($id, $column_name){
		$rs = $this->db->query("SELECT $column_name FROM 
customer WHERE id='$id'");
		$row = $rs->row_array();
		return $row[$column_name];
	}

	/**
	 *	Get customer info from search page.
	 *
	 * @return Object.
	 */
	 function get_search_customerinfo($condition, $offsetPage, $pageSize, 
$orderBy){
		 $orderBy = isset($orderBy) ? $orderBy : "id ASC";
		 $query = "SELECT 
		 					cust.id, cust.code, cust.name_th, cust.lastname_th,
cust.name_en, cust.lastname_en
							, (SELECT name_th FROM prefix WHERE
 id=cust.prefix_en) as prefix
							, COALESCE(cust.name_th,'')||' '||
COALESCE(cust.lastname_th,'') as fullname_th
							, COALESCE(cust.name_en,'')||' '||
COALESCE(cust.lastname_en,'') as fullname_en
							, cust.phone, cust.mobile, cust.fax, 
cust.office, cust.note,
							(SELECT name_th FROM customertype 
WHERE id=cust.customertype) as customer_type,
							(SELECT name_th FROM customerstatus 
WHERE id=cust.customerstatus) as customer_status,
							(SELECT name_th FROM prefix WHERE id=cust.prefix_en) as prefix, 
							cust.name_th as name, cust.lastname_th as lastname
						FROM 
							customer as cust
						WHERE
							$condition
						ORDER BY
							$orderBy
						OFFSET
							$offsetPage
						LIMIT
							$pageSize";
		$rs = $this->db->query($query);
//echo "customerInfo[get_search_customerInfo] : $query<hr>";
		return $rs;
	 }
                             .
                             .
                             .
?>

แสดงไฟล์ customer_info.php ที่เป็นส่วนของการนำเสนอ ผ่านส่วนของ Controller ในการดูข้อมูล Customer

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

 class Customer_info extends Controller {
	function Customer_info()
    {
        parent::Controller();
        $this->load->library('servisoft');
        $this->load->library('showfile');
        $this->load->library('searchfooter');
		$this->load->library('searchbycriteria');
		$this->load->library('Popcalendar');
		$this->load->library('datename');
		$this->load->library('search_items_customer');
        $this->load->helper('url');
		$this->load->helper('file');
        $this->load->helper('form');
        $this->load->model('customer/customer_model', 'custModel');
		$this->load->library('datechange');

		include(str_replace('index.php','share/config_system.php',
$_SERVER['SCRIPT_FILENAME']));
		$this->file_path = $file_path;
    }

	function Index()
	{
		show_error('Session Expired, <br>Please login again <br>' . 
anchor('login/welcome', 'Click Here'));
	}
                             .
                             .
                             .
	/*
	*	For view customer data.
	*/
	function view($cusId='') {

		/*******************************************************
		*	viewItem[] structure :
		*	+ viewItem[...]						เก็บตัวแปรทั่วไปที่รับค่ามาจาก post และตัวแปรอื่นๆ
		*	+ viewItem['information'][...]	เก็บตัวแปรของ customer Info
		*	+ viewItem['address'][...]			เก็บตัวแปรของ customer address
		*
		*******************************************************/

		//**************** Call function get Bar&Sub Menu. *********************//
		$content = $this->servisoft->get_content_new();
		$content['page_title'] = "View Customer System.";

		//***************** Get Data. *********************//

		$viewSubPage['customerId'] = $viewItem['customerId'] = 
$this->input->post('customerId') ? $this
->input->post('customerId') : $cusId;
		$viewItem['pageAction'] = $this->input->post('pageAction');
		$offsetPage = $this->input
->post("offsetPage")?$this->input->post("offsetPage"):"0";
		$pageSize = "10";
		$viewItem['barMenu'] = $content['menu'];
		$viewItem['subMenu'] = $content['submenuName'];
		$viewItem['sidePosition'] = $content['submenu'];

		//************* Get Item information,Address and contact *************//

		$viewItem['rs_info'] = $this->custModel
->get_customer_info($viewItem['customerId'])
->row_array();
		$viewItem['rs_customer_type'] = $this
->custModel->get_init_data('customertype');
		$viewItem['rs_customer_status'] = $this
->custModel->get_init_data('customerstatus');
		$viewItem['rs_customer_priority'] = $this
->custModel->get_init_data('customerpriority');

		//*********************** Get Picture ****************************//

		$pics_file = "customer/".$viewItem['rs_info']['pics'];
		$viewItem['rs_info']['picture'] = $this
->showfile->go_showfile($pics_file, '1', '', 
"view[photoCust]", '100', '100');
		$viewItem['information'] = $viewItem['address'] = "";
		$viewItem['information'] = $this
->get_update_customerInfo($viewItem['customerId'], 
$viewItem['information']);
		$viewItem['address'] = $this
->get_update_customerAddr($viewItem['customerId'],
 $viewItem['address'], $offsetPage, 3);
		$viewItem['footer'] = $this
->searchfooter->search_footer('BUT-NO-LINE', 
'nextOffsetAddr', $viewItem['address']['max_row'], 3, $offsetPage);
		$viewItem['contact'] = $this
->custModel->get_cuscontact($viewItem['customerId'],
 $offsetPage, 3);
		$viewItem['contact_footer'] = $this
->searchfooter->search_footer('BUT-NO-LINE', 
'nextOffsetContact', $this->custModel->last_num_rows, 3, $offsetPage);
		$viewItem['customer_createby'] = $content['emp_id'];
		$viewItem['customer_updateby'] = $content['emp_id'];
                             .
                             .
                             .
 ?>


next up previous contents
Next: 5.4 ผลกระทบของการปรับปรุงใช้งาน MVC ในเซอร์วิซอฟต์ Up: 5. การใช้งานแบบแผนซอฟต์แวร์ในการปรับปรุงพัฒนาเซอร์วิซอฟต์ Previous: 5.2 แบบแผน Model View   Contents
2008-2009 Copyright Vara Varavithya. All rights reserved, DEPT of EE, KMUTNB, vara@computer.org