PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Pandamp/Modules/Dms/Catalog/Model/Row/Catalog.php

https://github.com/hukumonline/quart80
PHP | 297 lines | 232 code | 40 blank | 25 comment | 47 complexity | 421e6a493b9f4d8badee390e4e15c146 MD5 | raw file
  1. <?php
  2. class Pandamp_Modules_Dms_Catalog_Model_Row_Catalog extends Zend_Db_Table_Row_Abstract
  3. {
  4. protected function _insert()
  5. {
  6. if(empty($this->guid))
  7. {
  8. $guidMan = new Pandamp_Core_Guid();
  9. $this->guid = $guidMan->generateGuid();
  10. }
  11. if(!empty($this->shortTitle))
  12. {
  13. $sTitleLower = strtolower($this->shortTitle);
  14. $sTitleLower = preg_replace("/[^a-zA-Z0-9 s]/", "", $sTitleLower);
  15. $sTitleLower = str_replace(' ', '-', $sTitleLower);
  16. $this->shortTitle = $sTitleLower;
  17. }
  18. $today = date('Y-m-d H:i:s');
  19. if(empty($this->createdDate))
  20. $this->createdDate = $today;
  21. if(empty($this->modifiedDate) || $this->modifiedDate=='0000-00-00 00:00:00')
  22. $this->modifiedDate = $today;
  23. $this->deletedDate = '0000-00-00 00:00:00';
  24. if(empty($this->createdBy))
  25. {
  26. $auth = Zend_Auth::getInstance();
  27. if($auth->hasIdentity())
  28. {
  29. $this->createdBy = $auth->getIdentity()->username;
  30. }
  31. }
  32. if(empty($this->modifiedBy))
  33. $this->modifiedBy = $this->createdBy;
  34. if(empty($this->status))
  35. $this->status = 0;
  36. }
  37. protected function _postDelete()
  38. {
  39. $registry = Zend_Registry::getInstance();
  40. $config = $registry->get(Pandamp_Keys::REGISTRY_APP_OBJECT);
  41. $cdn = $config->getOption('cdn');
  42. //find related docs and delete them
  43. $tblRelatedItem = new Pandamp_Modules_Dms_Catalog_Model_RelatedItem();
  44. $rowsetRelatedDocs = $tblRelatedItem->fetchAll("relatedGuid='$this->guid' AND relateAs IN ('RELATED_FILE','RELATED_IMAGE')");
  45. if(count($rowsetRelatedDocs))
  46. {
  47. foreach ($rowsetRelatedDocs as $rowRelatedDoc)
  48. {
  49. $tblCatalog = new Pandamp_Modules_Dms_Catalog_Model_Catalog();
  50. $rowCatalog = $tblCatalog->find($rowRelatedDoc->itemGuid)->current();
  51. $rowCatalog->delete();
  52. }
  53. }
  54. if($this->profileGuid == 'kutu_doc')
  55. {
  56. //get parentGuid
  57. $tblRelatedItem = new Pandamp_Modules_Dms_Catalog_Model_RelatedItem();
  58. $rowsetRelatedItem = $tblRelatedItem->fetchAll("itemGuid='$this->guid' AND relateAs IN ('RELATED_FILE','RELATED_IMAGE')");
  59. if(count($rowsetRelatedItem))
  60. {
  61. foreach($rowsetRelatedItem as $rowRelatedItem)
  62. {
  63. //must delete the physical files
  64. $rowsetCatAtt = $this->findDependentRowsetCatalogAttribute();
  65. $systemname = $rowsetCatAtt->findByAttributeGuid('docSystemName')->value;
  66. $parentGuid = $rowRelatedItem->relatedGuid;
  67. //$sDir1 = ROOT_DIR.DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'files'.DIRECTORY_SEPARATOR.$systemname;
  68. //$sDir2 = ROOT_DIR.DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'files'.DIRECTORY_SEPARATOR.$parentGuid.DIRECTORY_SEPARATOR.$systemname;
  69. $sDir1 = $cdn['static']['dir']['files']."/".$systemname;
  70. $sDir2 = $cdn['static']['dir']['files']."/".$parentGuid."/".$systemname;
  71. $sDir1_Remote = $cdn['remote']['dir']['files']."/".$systemname;
  72. $sDir2_Remote = $cdn['remote']['dir']['files']."/".$parentGuid."/".$systemname;
  73. if(file_exists($sDir1))
  74. {
  75. // delete file
  76. unlink($sDir1);
  77. // check remote file
  78. if(file_exists($sDir1_Remote))
  79. {
  80. $this->remoteDeleteFile($sDir1_Remote);
  81. }
  82. }
  83. else
  84. {
  85. if(file_exists($sDir2))
  86. {
  87. //delete file
  88. unlink($sDir2);
  89. // check remote file
  90. if(file_exists($sDir2_Remote))
  91. {
  92. $this->remoteDeleteFile($sDir2_Remote);
  93. }
  94. }
  95. }
  96. $img1 = $cdn['static']['dir']['images']."/".$systemname;
  97. $img2 = $cdn['static']['dir']['images']."/".$parentGuid."/".$systemname;
  98. $img1_Remote = $cdn['remote']['dir']['images']."/".$systemname;
  99. $img2_Remote = $cdn['remote']['dir']['images']."/".$parentGuid."/".$systemname;
  100. if(file_exists($img1))
  101. {
  102. // delete file
  103. unlink($img1);
  104. // check remote file
  105. if(file_exists($img1_Remote))
  106. {
  107. $this->remoteDeleteFile($img1_Remote);
  108. }
  109. }
  110. else
  111. {
  112. if(file_exists($img2))
  113. {
  114. //delete file
  115. unlink($img2);
  116. // check remote file
  117. if(file_exists($img2_Remote))
  118. {
  119. $this->remoteDeleteFile($img2_Remote);
  120. }
  121. }
  122. }
  123. }
  124. }
  125. }
  126. //delete from table CatalogAttribute
  127. $tblCatalogAttribute = new Pandamp_Modules_Dms_Catalog_Model_CatalogAttribute();
  128. $tblCatalogAttribute->delete("catalogGuid='$this->guid'");
  129. //delete catalogGuid from table CatalogFolder
  130. $tblCatalogFolder = new Pandamp_Modules_Dms_Catalog_Model_CatalogFolder();
  131. $tblCatalogFolder->delete("catalogGuid='$this->guid'");
  132. //delete guid from table AssetSetting
  133. $tblAssetSetting = new Pandamp_Modules_Dms_Catalog_Model_AssetSetting();
  134. $tblAssetSetting->delete("guid='$this->guid'");
  135. //delete from table relatedItem
  136. $tblRelatedItem = new Pandamp_Modules_Dms_Catalog_Model_RelatedItem();
  137. $tblRelatedItem->delete("itemGuid='$this->guid'");
  138. $tblRelatedItem->delete("relatedGuid='$this->guid'");
  139. $indexingEngine = Pandamp_Search::manager();
  140. try {
  141. $hits = $indexingEngine->deleteCatalogFromIndex($this->guid);
  142. }
  143. catch (Exception $e)
  144. {
  145. }
  146. //delete physical catalog folder from uploads/files/[catalogGuid]
  147. //$sDir = ROOT_DIR.DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'files'.DIRECTORY_SEPARATOR.$this->guid;
  148. $sDir = $cdn['static']['dir']['files']."/".$this->guid;
  149. $sDir_Remote = $cdn['remote']['dir']['files']."/".$this->guid;
  150. try {
  151. if(is_dir($sDir)) {
  152. rmdir($sDir);
  153. // check remote is_dir
  154. if(is_dir($sDir_Remote))
  155. {
  156. $this->remoteRemoveDir($sDir_Remote);
  157. }
  158. }
  159. }
  160. catch (Exception $e)
  161. {
  162. }
  163. //$sDir = ROOT_DIR.DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'images';
  164. $sDir = $cdn['static']['dir']['images'];
  165. try {
  166. if (file_exists($sDir."/".$this->guid.".gif")) { unlink($sDir."/".$this->guid.".gif"); }
  167. if (file_exists($sDir."/tn_".$this->guid.".gif")) { unlink($sDir."/tn_".$this->guid.".gif"); }
  168. if (file_exists($sDir."/".$this->guid.".jpg")) { unlink($sDir."/".$this->guid.".jpg"); }
  169. if (file_exists($sDir."/tn_".$this->guid.".jpg")) { unlink($sDir."/tn_".$this->guid.".jpg"); }
  170. if (file_exists($sDir."/".$this->guid.".jpeg")) { unlink($sDir."/".$this->guid.".jpeg"); }
  171. if (file_exists($sDir."/tn_".$this->guid.".jpeg")) { unlink($sDir."/tn_".$this->guid.".jpeg"); }
  172. if (file_exists($sDir."/".$this->guid.".png")) { unlink($sDir."/".$this->guid.".png"); }
  173. if (file_exists($sDir."/tn_".$this->guid.".png")) { unlink($sDir."/tn_".$this->guid.".png"); }
  174. }
  175. catch (Exception $e)
  176. {
  177. }
  178. $sDir = $cdn['remote']['dir']['images'];
  179. try {
  180. if (file_exists($sDir."/".$this->guid.".gif")) { $this->remoteDeleteFile($sDir."/".$this->guid.".gif"); }
  181. if (file_exists($sDir."/tn_".$this->guid.".gif")) { $this->remoteDeleteFile($sDir."/tn_".$this->guid.".gif"); }
  182. if (file_exists($sDir."/".$this->guid.".jpg")) { $this->remoteDeleteFile($sDir."/".$this->guid.".jpg"); }
  183. if (file_exists($sDir."/tn_".$this->guid.".jpg")) { $this->remoteDeleteFile($sDir."/tn_".$this->guid.".jpg"); }
  184. if (file_exists($sDir."/".$this->guid.".jpeg")) { $this->remoteDeleteFile($sDir."/".$this->guid.".jpeg"); }
  185. if (file_exists($sDir."/tn_".$this->guid.".jpeg")) { $this->remoteDeleteFile($sDir."/tn_".$this->guid.".jpeg"); }
  186. if (file_exists($sDir."/".$this->guid.".png")) { $this->remoteDeleteFile($sDir."/".$this->guid.".png"); }
  187. if (file_exists($sDir."/tn_".$this->guid.".png")) { $this->remoteDeleteFile($sDir."/tn_".$this->guid.".png"); }
  188. }
  189. catch (Exception $e)
  190. {
  191. }
  192. }
  193. public function findDependentRowsetCatalogAttribute()
  194. {
  195. return $this->findDependentRowset('Pandamp_Modules_Dms_Catalog_Model_CatalogAttribute');
  196. }
  197. public function relateTo($relatedGuid, $as='RELATED_ITEM', $valRelation = 0)
  198. {
  199. $tblRelatedItem = new Pandamp_Modules_Dms_Catalog_Model_RelatedItem();
  200. if(empty($this->guid))
  201. throw new Zend_Exception('Can not relate to empty GUID');
  202. if(empty($relatedGuid))
  203. throw new Zend_Exception('Can not relate to empty related GUID');
  204. $rowsetRelatedItem = $tblRelatedItem->find($this->guid, $relatedGuid, $as);
  205. if(count($rowsetRelatedItem) > 0)
  206. {
  207. // if ($as == "RELATED_COMMENT") {
  208. $row = $rowsetRelatedItem->current();
  209. $row->valueIntRelation = $valRelation;
  210. // }
  211. }
  212. else
  213. {
  214. $row = $tblRelatedItem->createNew();
  215. $row->itemGuid = $this->guid;
  216. $row->relatedGuid = $relatedGuid;
  217. $row->relateAs = $as;
  218. $row->valueIntRelation = $valRelation;
  219. }
  220. $row->save();
  221. }
  222. private function remoteDeleteFile($remote_path)
  223. {
  224. $registry = Zend_Registry::getInstance();
  225. $config = $registry->get(Pandamp_Keys::REGISTRY_APP_OBJECT);
  226. $ftp = $config->getOption('ftp');
  227. $strServer = $ftp['remote']['server'];
  228. $strServerPort = $ftp['remote']['port'];
  229. $strServerUsername = $ftp['remote']['username'];
  230. $strServerPassword = $ftp['remote']['passwd'];
  231. //connect to server
  232. $resConnection = ssh2_connect($strServer, $strServerPort);
  233. if(ssh2_auth_password($resConnection, $strServerUsername, $strServerPassword))
  234. {
  235. $resSFTP = ssh2_sftp($resConnection);
  236. unlink("ssh2.sftp://{$resSFTP}".$remote_path);
  237. }
  238. }
  239. private function remoteRemoveDir($remote_path)
  240. {
  241. $registry = Zend_Registry::getInstance();
  242. $config = $registry->get(Pandamp_Keys::REGISTRY_APP_OBJECT);
  243. $ftp = $config->getOption('ftp');
  244. $strServer = $ftp['remote']['server'];
  245. $strServerPort = $ftp['remote']['port'];
  246. $strServerUsername = $ftp['remote']['username'];
  247. $strServerPassword = $ftp['remote']['passwd'];
  248. //connect to server
  249. $resConnection = ssh2_connect($strServer, $strServerPort);
  250. if(ssh2_auth_password($resConnection, $strServerUsername, $strServerPassword))
  251. {
  252. $resSFTP = ssh2_sftp($resConnection);
  253. rmdir("ssh2.sftp://{$resSFTP}".$remote_path);
  254. }
  255. }
  256. }
  257. ?>