fork download
  1. <?php
  2. /**
  3.  * Core Function of Villa Portal
  4.  */
  5. Class VillaPortal {
  6.  
  7. /**
  8.   * URL to back to the last page, usefull for redirect header
  9.   * @var string
  10.   */
  11. public $linkBack;
  12.  
  13. /**
  14.   * Url Admin, usefull to set redirect
  15.   * <br />eg. /wp-admin/admin.php?page=vp-property
  16.   * @var string
  17.   */
  18. public $urlAdmin;
  19.  
  20. /**
  21.   * Property ID
  22.   * @var array|bool Property ID
  23.   */
  24. public $propertyIdentifier;
  25.  
  26. /**
  27.   * Array of Configuration
  28.   * @var array
  29.   */
  30. public $config;
  31.  
  32. /**
  33.   * Array of Plugin PATH
  34.   * @var array
  35.   */
  36. public $path;
  37.  
  38. /**
  39.   * array of link page
  40.   * @var string
  41.   */
  42. public $linkpage;
  43.  
  44. // Construct
  45. function VillaPortal($path, $linkpage) {
  46. // Configuration
  47. $query = 'SELECT * FROM `vm_sys_config`';
  48. $result = mysql_query($query);
  49.  
  50. while ( $row = mysql_fetch_assoc($result) ) {
  51. $this->config = array( $row['config_name'] => $row['config_value'] );
  52. }
  53.  
  54. // Path
  55. $this->path = $path;
  56. $this->path['dir_tpl'] = $this->path['dir'].'templates/'.$this->config['tpl_dir'].'/';
  57. $this->path['dir_inc'] = $this->path['dir'].'includes/';
  58. $this->path['url_tpl'] = $this->path['url'].'templates/'.$this->config['tpl_dir'].'/';
  59. $this->path['url_ico'] = $this->path['url'].'icon/';
  60.  
  61. // linkpage
  62. $this->linkpage = $linkpage;
  63.  
  64. // Load Extention
  65. $this->loadExtention('DBAPI');
  66. }
  67.  
  68. function loadExtention($extName, $arrConstruct=null){
  69. $construct = ($arrConstruct)? implode(', ', $arrConstruct) : $arrConstruct;
  70.  
  71. // Database API
  72. switch ($extName){
  73. case 'DBAPI' :
  74. if(!require $this->path['dir_inc'].'inc.db.php')
  75. return false;
  76. $this->db = new DBAPI;
  77. return true;
  78. break;
  79. case 'Property':
  80. if(!require $this->path['dir_inc'].'inc.property.php')
  81. return false;
  82. $this->property = new Property($construct);
  83. $this->propertyIdentifier =& $this->property->identifier;
  84. return true;
  85. break;
  86. case 'Room':
  87. if(!require_once $this->path['dir_inc'].'inc.room.php')
  88. return false;
  89. $this->room = new Room($construct);
  90. $this->propertyIdentifier =& $this->property->identifier;
  91. return true;
  92. break;
  93. default:
  94. return false;
  95. break;
  96. }
  97. }
  98.  
  99. function listLocation() {
  100. $resultCountry = mysql_query("
  101. SELECT *
  102. FROM `vm_sys_location`
  103. WHERE `loc_parent` = '0'
  104. ");
  105.  
  106. if (mysql_num_rows($resultCountry) != 0 ) {
  107. echo '<ul class="list-location">';
  108. while ( $row = mysql_fetch_assoc($resultCountry) ) {
  109. echo '<li>';
  110. echo '<span>'.$row['loc_name'].'
  111. <a href="?page=vp-config-country&mode=addlocation&do=add&parent='.$row['loc_id'].'">
  112. <img src="'.$this->path['url_ico'].'action_add.png" title="Add Child" />
  113. </a>
  114. <a href="?page=vp-config-country&mode=addlocation&do=edit&id='.$row['loc_id'].'">
  115. <img src="'.$this->path['url_ico'].'file.png" title="Edit" />
  116. </a>
  117. <a href="?page=vp-config-country&mode=deletelocation&id='.$row['loc_id'].'">
  118. <img src="'.$this->path['url_ico'].'action_delete.png" title="Delete" />
  119. </a>
  120. </span>';
  121.  
  122. $resultArea = mysql_query('
  123. SELECT *
  124. FROM `vm_sys_location`
  125. WHERE `loc_parent` = \''.$row['loc_id'].'\'
  126. ');
  127. if ( mysql_num_rows($resultArea) != 0) {
  128. echo '<ul>';
  129. while ( $rowArea = mysql_fetch_assoc($resultArea) ) {
  130. echo '<li>';
  131. echo '<span>'.$rowArea['loc_name'].'
  132. <a href="?page=vp-config-country&mode=addlocation&do=add&parent='.$rowArea['loc_id'].'">
  133. <img src="'.$this->path['url_ico'].'action_add.png" title="Add Child" />
  134. </a>
  135. <a href="?page=vp-config-country&mode=addlocation&do=edit&id='.$rowArea['loc_id'].'">
  136. <img src="'.$this->path['url_ico'].'file.png" title="Edit" />
  137. </a>
  138. <a href="?page=vp-config-country&mode=deletelocation&id='.$rowArea['loc_id'].'">
  139. <img src="'.$this->path['url_ico'].'action_delete.png" title="Delete" />
  140. </a>
  141. </span>';
  142.  
  143. $resultCity = mysql_query('
  144. SELECT *
  145. FROM `vm_sys_location`
  146. WHERE `loc_parent` = \''.$rowArea['loc_id'].'\'
  147. ');
  148. if ( mysql_num_rows($resultCity) != 0 ) {
  149. echo '<ul>';
  150. while ( $rowCity = mysql_fetch_assoc($resultCity) ) {
  151. echo '<li>'.$rowCity['loc_name'].'
  152. <a href="?page=vp-config-country&mode=addlocation&do=edit&id='.$rowCity['loc_id'].'">
  153. <img src="'.$this->path['url_ico'].'file.png" title="Edit" />
  154. </a>
  155. <a href="?page=vp-config-country&mode=deletelocation&id='.$rowCity['loc_id'].'">
  156. <img src="'.$this->path['url_ico'].'action_delete.png" title="Delete" />
  157. </a>
  158. </li>';
  159. }
  160. echo '</ul>';
  161. }
  162.  
  163. echo '</li>';
  164. }
  165.  
  166. echo '</ul>';
  167. }
  168. echo '</li>';
  169. }
  170. echo '</ul>';
  171. }
  172. }
  173.  
  174. function saveLocation($callback){
  175. $data['loc_id'] = $_POST['thisId'];
  176. $data['loc_parent'] = $_POST['parentId'];
  177. $data['loctype_id'] = $_POST['thisTypeId'];
  178. $data['loc_name'] = $_POST['thisName'];
  179.  
  180. if ($data['loc_id'] == '') { // mode Add
  181. $data['loc_id'] = 'null';
  182. $this->db->insert('vm_sys_location', $data);
  183. } else { // mode edit
  184. $this->db->update('vm_sys_location', $data, 'loc_id='.$data['loc_id']);
  185. }
  186.  
  187. header('Location: '.$callback);
  188. }
  189.  
  190. function deleteLocation($id, $callback) {
  191. $data['loc_id'] = $_GET['id'];
  192. $this->db->delete('vm_sys_location', "loc_id=$id");
  193.  
  194. header('Location: '.$callback);
  195. }
  196.  
  197. function formAddLocation($target) {
  198. if ($_GET['do'] == 'add') {
  199. $parent = ( isset($_GET['parent']) )? $_GET['parent'] : 0;
  200. $parentId = $parent;
  201. $thisId = '';
  202. $thisName = '';
  203. if ($parent==0){
  204. $thisTypeId = 1;
  205. } else {
  206. $thisTypeId = $this->getLocType($parent)+1;
  207. }
  208. } else if ($_GET['do'] == 'edit') {
  209. $id = $_GET['id'];
  210. $parentId = $this->getLocation($id, 'loc_parent');
  211. $thisTypeId = $this->getLocation($id, 'loctype_id');
  212. $thisId = $this->getLocation($id, 'loc_id');
  213. $thisName = $this->getLocation($id, 'loc_name');
  214. }
  215.  
  216. $search = array(
  217. 'target' => $target,
  218. 'thisId' => $thisId,
  219. 'parentId' => $parentId,
  220. 'thisTypeId' => $thisTypeId,
  221. 'thisName' => $thisName
  222. );
  223.  
  224. $return = $this->parseChunk('form-addlocation.php', $search, '[+', '+]'); //
  225. print $return;
  226.  
  227. }
  228.  
  229. function getLocation ($loc_id, $field) {
  230. $result = $this->dbResult('vm_sys_location', $field, "loc_id=$loc_id");
  231. while ($row = mysql_fetch_assoc($result)) {
  232. $return = $row[$field];
  233. }
  234.  
  235. return $return;
  236. }
  237.  
  238. function getLocType ($loc_id) {
  239. $result = $this->dbResult('vm_sys_location', '*', "loc_id=$loc_id");
  240. while ($row = mysql_fetch_assoc($result)) {
  241. $return = $row['loctype_id'];
  242. }
  243.  
  244. return (int)$return;
  245. }
  246.  
  247. function getListLocation($tpl, $parent=0) {
  248. $result = $this->dbResult('vm_sys_location', '*', "loc_parent=$parent");
  249.  
  250. if ( mysql_num_rows($result) != 0 ) {
  251. while ( $row = mysql_fetch_assoc($result) ) {
  252. $return .= $this->parseChunk($tpl, array('id' => $row['loc_id'],
  253. 'name' => $row['loc_name']) );
  254. }
  255. }
  256.  
  257. return $return;
  258. }
  259.  
  260. function dbResult ($table, $select, $condition='', $limit='', $orderBy='') {
  261. $table = "FROM $table ";
  262. $select = "SELECT $select ";
  263. $condition = ($condition != '')? "WHERE $condition " : $condition;
  264. $limit = ($limit != '')? "LIMIT $limit " : $limit;
  265. $orderBy = ($orderBy != '')? "ORDER BY $orderBy " : $orderBy;
  266.  
  267. $query = $select.$table.$condition.$orderBy.$limit;
  268. return mysql_query($query);
  269. }
  270.  
  271. /**
  272.   * Show List Property
  273.   * @param string $tpl Template
  274.   * @param string $tplWrap Template Wrap
  275.   * @param int $limit Limit Show, DEFAULT: NULL
  276.   * @return string
  277.   */
  278. function propertyList($tpl, $tplWrap, $limit=null) {
  279. $fetch = $this->db->fetch('vm_property');
  280.  
  281. foreach ($fetch as $key=>$value) {
  282. $fetch[$key] = $this->addPropertyKey($value);
  283. }
  284.  
  285. foreach ($fetch as $value) {
  286. $data .= $this->parseChunk($tpl, $value);
  287. }
  288. $property['list'] = $data;
  289. $property['urlAdd'] = $this->linkpage['property'].'&show=propertyedit';
  290. $template = $this->parseChunk($tplWrap, $property);
  291. return $template;
  292. }
  293.  
  294. function propertyEdit($tpl) {
  295. if($this->propertyIdentifier){
  296. $fetch = $this->property->data;
  297. }else{
  298. $fieldName = $this->db->getTableFields('vm_property');
  299. while ($row = mysql_fetch_assoc($fieldName) ){
  300. $fetch[ $row['Field'] ] = '';
  301. }
  302. }
  303.  
  304. // Aditional Data
  305. $fetch['listCountry'] = $this->getFormLocation(1, 'country', 'country');
  306. $fetch['listProvince'] = $this->getFormLocation(3, 'province', 'province');
  307. $fetch['listCity'] = $this->getFormLocation(4, 'city', 'city');
  308. $fetch['formAction'] = '?page=vp-property&show=action&action=update&id='.$this->propertyIdentifier;
  309.  
  310. $return = $this->parseChunk($tpl, $fetch);
  311. return $return;
  312. }
  313.  
  314. function getFormLocation($typeLocation, $className, $selectName){
  315. $fetch = $this->db->fetch('vm_sys_location', '*', 'loctype_id='.$typeLocation);
  316. if ( count($fetch) ){
  317. $return = '<select class="'.$className.'" name="'.$selectName.'">';
  318. $return .= '<option value="">------</option>';
  319. foreach($fetch as $value){
  320. if ( $value['loc_id'] == $this->property->getLocationIdByType($typeLocation) ){
  321. $return .= '<option value="'.$value['loc_id'].'" selected="selected">'.$value['loc_name'].'</option>';
  322. }else{
  323. $return .= '<option value="'.$value['loc_id'].'">'.$value['loc_name'].'</option>';
  324. }
  325. }
  326. $return .= '</select>';
  327. }
  328. return $return;
  329. }
  330.  
  331. function addPropertyKey($data) {
  332. $id = $data['id'];
  333. $data['link'] = $this->linkpage['property'].'&show=property'."&id=$id";
  334. $data['actionEdit'] = $this->linkpage['property'].'&show=propertyedit'."&id=$id";
  335. $data['actionDelete'] = $this->linkpage['property'].'&show=action'.'&action=delete'."&id=$id";
  336. $data['actionViewRoom'] = $this->linkpage['property'].'&show=property'."&id=$id";
  337. $data['action'] = $this->getIcon(
  338. 'icon.php',
  339. array('viewroom','edit','delete'),
  340. array($data['actionViewRoom'],$data['actionEdit'], $data['actionDelete'])
  341. );
  342. return $data;
  343. }
  344.  
  345.  
  346.  
  347. function getIcon($tpl, $arrIcon, $arrLinkAction) {
  348. $path = $this->path['url_ico'];
  349. $listIcon = array(
  350. 'delete' => $path.'action_delete.png',
  351. 'edit' => $path.'file.png',
  352. 'viewroom' => $path.'application.png',
  353. 'setPrice' => $path.'reply.png'
  354. );
  355. foreach ($arrIcon as $key=>$value) {
  356. $data['name'] = $value;
  357. $data['image'] = $listIcon[$value];
  358. $data['link'] = $arrLinkAction[$key];
  359. $return .= $this->parseChunk($tpl, $data);
  360. }
  361.  
  362. return $return;
  363. }
  364.  
  365. /*------- Chunk Template System ----------*/
  366. function parseChunk($chunkName, $chunkArr, $prefix= "[+", $suffix= "+]") {
  367. if ( !is_array($chunkArr) ) {
  368. return false;
  369. }
  370. $chunk = $this->getChunk($chunkName);
  371. foreach ($chunkArr as $key=>$value) {
  372. $chunk = str_replace($prefix.$key.$suffix, $value, $chunk);
  373. }
  374. return $chunk;
  375. }
  376.  
  377. function getChunk($chunkName) {
  378. $read = $this->path['dir_tpl'].$chunkName;
  379.  
  380. if ( file_exists($read) ) {
  381. $content = file_get_contents($read);
  382. } else {
  383. return false;
  384. }
  385.  
  386. return $content;
  387. }
  388.  
  389. /**
  390.   * Print Document Parser
  391.   * @param string $tpl Template Name
  392.   * @param array $arrData Data to be sent
  393.   */
  394. function parser($tpl, $arrData) {
  395. print $this->parseChunk($tpl, $arrData);
  396. }
  397.  
  398. function registerCSS(){
  399. // Register CSS
  400. wp_register_style('villa-portal-style', $this->path['url_tpl'].'style.css', null, '0.1' );
  401. wp_enqueue_style('villa-portal-style');
  402. }
  403.  
  404. function registerScript(){
  405. // Tyny MCE
  406. wp_register_script('tinymce', $this->path['url'].'extention/tiny_mce/jquery.tinymce.js', null, '0.1' );
  407. wp_enqueue_script('tinymce');
  408.  
  409. wp_register_script('villa-portal-script', $this->path['url'].'site.js', null, '0.1' );
  410. wp_enqueue_script('villa-portal-script');
  411. }
  412.  
  413. function getProperty($tpl, $propertyId=null) {
  414. $propertyId = ($propertyId)? $propertyId : $this->propertyIdentifier;
  415.  
  416. $return = $this->parseChunk($tpl, $this->property->data);
  417. return $return;
  418. }
  419.  
  420. function propertyUpdate($data) {
  421. if( $this->db->update('vm_property', $data, 'id='.$this->propertyIdentifier) ){
  422. return true;
  423. }else{
  424. return false;
  425. }
  426. }
  427.  
  428. function propertyInsert($data) {
  429. $this->db->insert('vm_property', $data);
  430. }
  431.  
  432. /**
  433.   * Delete Property
  434.   * @return bool Return true on success
  435.   */
  436. function propertyDelete() {
  437. if ($this->propertyIdentifier) {
  438. $this->db->delete('vm_property', 'id='.$this->propertyIdentifier);
  439. return true;
  440. }
  441. }
  442.  
  443. function showRoom($tpl, $tplWrap, $limit=null) {
  444. if ($this->propertyIdentifier) {
  445. $return = '<h2>List Room</h2>';
  446. $return .= '<a href="'.$this->urlAdmin.'&show=roomaction&roomaction=create&id='.$this->propertyIdentifier.'" class="button">Create Room</a>';
  447. $data['listRoom'] = '';
  448.  
  449. $result = $this->db->select('vm_room', '*', 'property_id='.$this->propertyIdentifier);
  450. if ( mysql_num_rows($result) ) {
  451. while ( $row = mysql_fetch_assoc($result) ){
  452. $row['url'] = $this->urlAdmin.'&show=roomaction&roomaction=create&id='.$this->propertyIdentifier.'&roomid='.$row['id'];
  453. $row['price'] = $this->getRoomPrice($row['id']);
  454. $row['action'] = $this->getRoomAction( $row['id'], "<td>%s</td>", $row['url'] );
  455. $data['listRoom'] .= $this->parseChunk($tpl, $row);
  456. }
  457. $return .= $this->parseChunk($tplWrap, $data);
  458. }
  459.  
  460. print($return);
  461. }
  462. }
  463.  
  464. function getRoomAction($id, $wrap, $linkBase){
  465. $data['actionEdit'] = $linkBase; //
  466. $data['actionDelete'] = str_replace('&roomaction=create', '&roomaction=dodelete', $linkBase);
  467. $data['actionSetPrice'] = str_replace(
  468. array('&show=roomaction', '&roomaction=create'),
  469. array('&show=roomprice', ''),
  470. $linkBase
  471. );
  472. $action = $this->getIcon(
  473. 'icon.php',
  474. array('edit','delete','setPrice'),
  475. array($data['actionEdit'],$data['actionDelete'], $data['actionSetPrice'])
  476. );
  477. return $action;
  478. }
  479.  
  480. function getRoomPrice($roomId){
  481.  
  482. }
  483.  
  484. function room_delete($roomId) {
  485. $this->db->delete('vm_room', 'id='.$roomId);
  486. }
  487.  
  488. function listRate($roomId=null, $yearMonth=null ) {
  489. $roomId = ($roomId)? $roomId : $this->room->identifier;
  490. $yearMonth = ($yearMonth)? $yearMonth : $_SESSION['rateDate'];
  491. $yearMonth = ($yearMonth)? $yearMonth : date('Y-m');
  492.  
  493. $rate = $this->room->generateRate($yearMonth);
  494. foreach ($rate as $value) {
  495. $data['listRate'] .= $this->parseChunk('roomrate.php', $value);
  496. }
  497.  
  498. $data['link-property'] = $this->urlAdmin.'&show=property&id='.$this->propertyIdentifier;
  499. $data['link-formaction'] = $this->urlAdmin.'&show=roomprice&roomprice=update&id='.$this->propertyIdentifier.'&roomid='.$this->room->identifier;
  500. $data['listSelectMonth'] = $this->genSelectMonth();
  501. $data['listSelectYear'] = $this->genSelectYear();
  502.  
  503. $return = $this->parseChunk('roomrate-wrap.php', $data);
  504. return $return;
  505. }
  506.  
  507. function genSelectMonth() {
  508. $monthArry = array(
  509. '01' => 'January',
  510. '02' => 'February',
  511. '03' => 'March',
  512. '04' => 'April',
  513. '05' => 'May',
  514. '06' => 'June',
  515. '07' => 'July',
  516. '08' => 'August',
  517. '09' => 'September',
  518. '10' => 'October',
  519. '11' => 'November',
  520. '12' => 'December'
  521. );
  522. $monthNow = Date('m');
  523. foreach ($monthArry as $key=>$value) {
  524. $selected = ($key == $monthNow)? 'selected="selected"' : '';
  525. $return .= '<option value="'.$key.'" '.$selected.'>'.$value.'</option>';
  526. }
  527. return $return;
  528. }
  529.  
  530. function genSelectYear() {
  531. $year = (int)Date('Y');
  532. for ($i=0; $i < 4; $i++) {
  533. $yearIncrement = $year + $i;
  534. $return .= '<option value="'.$yearIncrement.'">'.$yearIncrement.'</option>';
  535. }
  536. return $return;
  537. }
  538.  
  539. function configListFacility() {
  540. $i = 1;
  541. $result = $this->db->select('vm_facility');
  542. if( mysql_num_rows($result) ) {
  543. $data = $this->db->fetch('vm_facility');
  544. foreach($data as $key=>$value) {
  545. $data[$key]['actionDelete'] = '<a href="/wp-admin/admin.php?page=vp-config-facility&action=delete&id='.$data[$key]['id'].'">
  546. <img src="'.$this->path['url_ico'].'action_delete.png" /></a>';
  547. }
  548. $i++;
  549. }
  550. foreach ($data as $key=>$value) {
  551. $parser['listFacility'] .= $this->parseChunk('config-facility.php', $value);
  552. }
  553.  
  554. $return = $this->parseChunk('config-facility-wrap.php', $parser);
  555. print($return);
  556. }
  557.  
  558. function showFacility(){
  559. if ( $_POST['action'] == 'facility' ) {
  560. unset($_POST['action']);
  561. $this->propertyFacilityUpdate($_POST);
  562. }
  563.  
  564. $data['title'] = 'Facility';
  565. $data['propertyId'] = $this->propertyIdentifier;
  566. $data['listFacility'] = $this->genListFacility();
  567. $return = $this->parseChunk('facility-wrap.php', $data);
  568. print $return;
  569. }
  570.  
  571. function genListFacility() {
  572. $arrFacility = $this->db->fetch('vm_facility');
  573. $arrPropertyFacility = explode(',', $this->db->result('vm_property', 'facility', 'id='.$this->propertyIdentifier));
  574. foreach ($arrFacility as $facility) {
  575. foreach ($arrPropertyFacility as $propertyFacility) { // Check if the Facility ID is included in this property facility
  576. if ($facility['id'] == $propertyFacility) {
  577. $facility['checked'] = 'checked="checked"';
  578. break;
  579. }else{
  580. $facility['checked'] = '';
  581. }
  582. }
  583. $return .= $this->parseChunk('facility.php', $facility);
  584. }
  585. return $return;
  586. }
  587.  
  588. function propertyFacilityUpdate($data) {
  589. $data = implode(',', $data['facility']);
  590. $update['facility'] = $data;
  591. $this->db->update('vm_property', $update, 'id='.$this->propertyIdentifier, $debug=false);
  592. }
  593. }
  594. ?>
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout

Standard output is empty