PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Quản lý website bán mỹ phẩm PHP/administrator/components/com_virtuemart/helpers/vmtablexarray.php

https://gitlab.com/phamngsinh/baitaplon_sinhvien
PHP | 289 lines | 164 code | 53 blank | 72 comment | 36 complexity | 41cc22aeea9d39d93a127835d797170e MD5 | raw file
  1. <?php
  2. /**
  3. * Xref table abstract class to create tables specialised doing xref
  4. *
  5. * The pkey is the Where key in the load function,
  6. * the skey is the select key in the load function
  7. *
  8. *
  9. * @package VirtueMart
  10. * @subpackage Helpers
  11. * @author Max Milbers
  12. * @copyright Copyright (c) 2011 VirtueMart Team. All rights reserved.
  13. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
  14. * VirtueMart is free software. This version may have been modified pursuant
  15. * to the GNU General Public License, and as distributed it includes or
  16. * is derivative of works licensed under the GNU General Public License or
  17. * other free or open source software licenses.
  18. * See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.
  19. *
  20. * http://virtuemart.net
  21. */
  22. defined('_JEXEC') or die();
  23. if(!class_exists('VmTable'))require(JPATH_VM_ADMINISTRATOR.DS.'helpers'.DS.'vmtable.php');
  24. class VmTableXarray extends VmTable {
  25. /** @var int Primary key */
  26. protected $_autoOrdering = false;
  27. protected $_orderable = false;
  28. protected $_pvalue = '';
  29. // function setOrderable($key='ordering', $auto=true){
  30. // $this->_orderingKey = $key;
  31. // $this->_orderable = 1;
  32. // $this->_autoOrdering = $auto;
  33. // $this->$key = 0;
  34. // }
  35. function setSecondaryKey($key,$keyForm=0){
  36. $this->_skey = $key;
  37. $this->$key = array();
  38. $this->_skeyForm = empty($keyForm)? $key:$keyForm;
  39. }
  40. function setOrderableFormname($orderAbleFormName){
  41. $this->_okeyForm = $orderAbleFormName;
  42. }
  43. /**
  44. * swap the ordering of a record in the Xref tables
  45. * @param $direction , 1/-1 The increment to reorder by
  46. */
  47. function move($direction, $where='', $orderingkey=0) {
  48. if(empty($this->_skey) ) {
  49. vmError( 'No secondary keys defined in VmTableXarray '.$this->_tbl );
  50. return false;
  51. }
  52. $skeyId = JRequest::getInt($this->_skey, 0);
  53. // Initialize variables
  54. $db = JFactory::getDBO();
  55. $cid = JRequest::getVar( $this->_pkey , array(), 'post', 'array' );
  56. $order = JRequest::getVar( 'order', array(), 'post', 'array' );
  57. $query = 'SELECT `id` WHERE $this->_pkey = '.(int)$cid[0].' AND `virtuemart_category_id` = '.(int)$skeyId ;
  58. $this->_db->setQuery( $query );
  59. $id = $this->_db->loadResult();
  60. $keys = array_keys($order);
  61. // TODO next 2 lines not used ????
  62. if ($direction >0) $idToSwap = $order[$keys[array_search($id, $keys)]+1];
  63. else $idToSwap = $order[$keys[array_search($id, $keys)]-1];
  64. if (isset( $cid[0] )) {
  65. $query = 'UPDATE `'.$this->_tbl.'` '
  66. . ' SET `'.$this->_orderingKey.'` = `'.$this->_orderingKey.'` + '. $direction
  67. . ' WHERE `'.$this->_pkey.'` = ' . (int)$cid[0].
  68. ' AND `'.$this->_skey.'` = ' . (int)$skeyId
  69. ;
  70. $this->_db->setQuery( $query );
  71. if (!$this->_db->query())
  72. {
  73. $err = $this->_db->getErrorMsg();
  74. JError::raiseError( 500, get_class( $this ).':: move '. $err );
  75. }
  76. }
  77. }
  78. /**
  79. * Records in this table are arrays. Therefore we need to overload the load() function.
  80. * TODO, this function is giving back the array, not the table, it is not working like the other table, so we should change that
  81. * for the 2.2. at least.
  82. * @author Max Milbers
  83. * @param int $id
  84. */
  85. function load($oid=null,$overWriteLoadName=0,$andWhere=0,$tableJoins= array(),$joinKey = 0){
  86. if(empty($this->_skey) ) {
  87. vmError( 'No secondary keys defined in VmTableXarray '.$this->_tbl );
  88. return false;
  89. }
  90. if(empty($this->_db)) $this->_db = JFactory::getDBO();
  91. if($this->_orderable){
  92. $orderby = 'ORDER BY `'.$this->_orderingKey.'`';
  93. } else {
  94. $orderby = '';
  95. }
  96. $q = 'SELECT `'.$this->_skey.'` FROM `'.$this->_tbl.'` WHERE `'.$this->_pkey.'` = "'.(int)$oid.'" '.$orderby;
  97. $this->_db->setQuery($q);
  98. $result = $this->_db->loadResultArray();
  99. // vmdebug('my q ',$q,$result);
  100. $error = $this->_db->getErrorMsg();
  101. if(!empty($error)){
  102. vmError(get_class( $this ).':: load'.$error );
  103. return false;
  104. } else {
  105. if(empty($result)) return array();
  106. if(!is_array($result)) $result = array($result);
  107. return $result;
  108. }
  109. }
  110. /**
  111. * This binds the data to this kind of table. You can set the used name of the form with $this->skeyForm;
  112. *
  113. * @author Max Milbers
  114. * @param unknown_type $data
  115. */
  116. public function bind($data, $ignore = array()){
  117. if(!empty($data[$this->_pkeyForm])){
  118. $this->_pvalue = $data[$this->_pkeyForm];
  119. }
  120. if(!empty($data[$this->_skeyForm])){
  121. $this->_svalue = $data[$this->_skeyForm];
  122. }
  123. return true;
  124. }
  125. /**
  126. *
  127. * @author Max Milbers, George Kostopoulos
  128. * @see libraries/joomla/database/JTable#store($updateNulls)
  129. */
  130. public function store($updateNulls = false) {
  131. $returnCode = true;
  132. $this->setLoggableFieldsForStore();
  133. $db = JFactory::getDBO();
  134. $pkey = $this->_pkey;
  135. $skey = $this->_skey;
  136. $tblkey = $this->_tbl_key;
  137. // We select all database rows based on our _pkey
  138. $q = 'SELECT * FROM `'.$this->_tbl.'` WHERE `'.$this->_pkey.'` = "'. $this->_pvalue.'" ';
  139. $this->_db->setQuery($q);
  140. $objList = $this->_db->loadObjectList();
  141. // We convert the database object list that we got in a more friendly array
  142. $oldArray = null;
  143. if($objList) {
  144. foreach($objList as $obj){
  145. $oldArray[] = array($pkey=>$obj->$pkey, $skey=>$obj->$skey);
  146. }
  147. }
  148. // We make another database object list with the values that we want to insert into the database
  149. $newArray = array();
  150. if(!empty($this->_svalue)){
  151. if(!is_array($this->_svalue)) $this->_svalue = array($this->_svalue);
  152. foreach($this->_svalue as $value) $newArray[] = array($pkey=>$this->_pvalue, $skey=>$value);
  153. }
  154. // Inserts and Updates
  155. if(count($newArray)>0){
  156. $myOrdering = 1;
  157. foreach ($newArray as $newValue) {
  158. // We search in the existing (old) rows to find one of the new rows we want to insert
  159. $result = $this->array_msearch($oldArray, $newValue);
  160. // We start creating the row we will insert or update
  161. $obj = new stdClass;
  162. $obj->$pkey = $newValue[$pkey];
  163. $obj->$skey = $newValue[$skey];
  164. if($this->_autoOrdering){
  165. $oKey = $this->_orderingKey;
  166. $obj->$oKey = $myOrdering++;
  167. }
  168. // If the new row does not exist in the old rows, we will insert it
  169. if( $result === false ) {
  170. $returnCode = $this->_db->insertObject($this->_tbl, $obj, $pkey);
  171. }
  172. else {
  173. // If the new row exists in the old rows, we will update it
  174. $obj->$tblkey = $objList[$result]->$tblkey;
  175. $returnCode = $this->_db->updateObject($this->_tbl, $obj, $tblkey);
  176. }
  177. }
  178. }
  179. else {
  180. // There are zero new rows, so the user asked for all the rows to be deleted
  181. $q = 'DELETE FROM `'.$this->_tbl.'` WHERE `' . $pkey.'` = "'. $this->_pvalue .'" ';
  182. $this->_db->setQuery($q);
  183. if(!$this->_db->query()){
  184. $returnCode = false;
  185. vmError(get_class( $this ).':: store '.$this->_db->getErrorMsg());
  186. }
  187. }
  188. // Deletions
  189. if(!empty($oldArray)) {
  190. for ($i = 0; $i < count($oldArray); $i++) {
  191. $result = $this->array_msearch($newArray, $oldArray[$i]);
  192. // If no new row exists in the old rows, we will delete the old rows
  193. if( $result === false ) {
  194. // If the old row does not exist in the new rows, we will delete it
  195. $q = 'DELETE FROM `'.$this->_tbl.'` WHERE `' . $tblkey.'` = "'. $objList[$i]->$tblkey .'" ';
  196. $this->_db->setQuery($q);
  197. if(!$this->_db->Query()){
  198. $returnCode = false;
  199. vmError(get_class( $this ).':: store'.$this->_db->getErrorMsg());
  200. }
  201. }
  202. }
  203. }
  204. return $returnCode;
  205. }
  206. /**
  207. *
  208. * Searches in an array of arrays to find a specific array we want
  209. *
  210. * @author George Kostopoulos
  211. * @param source array of arrays that we will search
  212. * @param the target array we want to find
  213. */
  214. protected function array_msearch($parents, $searched) {
  215. if (empty($searched) || empty($parents)) {
  216. return false;
  217. }
  218. foreach ($parents as $key => $value) {
  219. $exists = true;
  220. foreach ($searched as $skey => $svalue) {
  221. $exists = ($exists && IsSet($parents[$key][$skey]) && $parents[$key][$skey] == $svalue);
  222. }
  223. if($exists){ return $key; }
  224. }
  225. return false;
  226. }
  227. function deleteRelation(){
  228. $q = 'DELETE FROM `'.$this->_tbl.'` WHERE `'.$this->_pkey.'` = "'. $this->_pvalue.'" ';
  229. $this->_db->setQuery($q);
  230. if(!$this->_db->Query()){
  231. vmError(get_class( $this ).':: store'.$this->_db->getErrorMsg(),'Couldnt delete relations');
  232. return false;
  233. }
  234. return true;
  235. }
  236. }