PageRenderTime 59ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/functions/tlAttachment.class.php

https://github.com/viglesiasce/testlink
PHP | 342 lines | 158 code | 43 blank | 141 comment | 10 complexity | 65bca24be865f121fa9f84c04dd6600e MD5 | raw file
  1. <?php
  2. /**
  3. * TestLink Open Source Project - http://testlink.sourceforge.net/
  4. * This script is distributed under the GNU General Public License 2 or later.
  5. *
  6. * @package TestLink
  7. * @author Francisco Mancardi
  8. * @copyright 2007-2009, TestLink community
  9. * @version CVS: $Id: tlAttachment.class.php,v 1.2 2009/12/28 08:53:37 franciscom Exp $
  10. * @link http://www.teamst.org/index.php
  11. *
  12. */
  13. /** parenthal class */
  14. require_once( 'object.class.php' );
  15. /**
  16. * An attachment helper class used to manage the storage of the attachment's meta information
  17. * Attachments contents are handled by the repository
  18. * @package TestLink
  19. */
  20. class tlAttachment extends tlDBObject
  21. {
  22. /**
  23. * @var integer error code for invalid title length
  24. */
  25. const E_TITLELENGTH = -1;
  26. /**
  27. * @var int $fkid the foreign key id (attachments.fk_id)
  28. */
  29. protected $fkID;
  30. /**
  31. * @var string $fktableName the tablename to which the $id refers to (attachments.fk_table)
  32. */
  33. protected $fkTableName;
  34. /**
  35. * @var string the filename the attachment is stored to
  36. */
  37. protected $fName;
  38. /**
  39. * @var string the title used for the attachment
  40. */
  41. protected $title;
  42. /**
  43. * @var string $fType the mime-type of the file
  44. */
  45. protected $fType;
  46. /**
  47. * @var int $fSize the filesize (uncompressed)
  48. */
  49. protected $fSize;
  50. /**
  51. * @var string $destFPath the path to file within the repository
  52. */
  53. protected $destFPath;
  54. /**
  55. * @var string $fContents the contents of the file
  56. */
  57. protected $fContents;
  58. /**
  59. *
  60. * @var int the compression type used for the attachment
  61. * @see TL_REPOSITORY_COMPRESSIONTYPE_NONE
  62. * @see TL_REPOSITORY_COMPRESSIONTYPE_GZIP
  63. * */
  64. protected $compressionType;
  65. /**
  66. * @var string a description for the attachment
  67. */
  68. protected $description;
  69. /**
  70. * @var timestamp the timestampe when the attachment was added
  71. */
  72. protected $dateAdded;
  73. /**
  74. * @var string the path to the repository
  75. */
  76. protected $repositoryPath;
  77. /**
  78. * @var unknown_type
  79. */
  80. protected $attachmentCfg;
  81. /* Cleanup function to set the object to an initial state
  82. * @param $options options to control the initialization
  83. */
  84. protected function _clean($options = self::TLOBJ_O_SEARCH_BY_ID)
  85. {
  86. $this->fkID = NULL;
  87. $this->fkTableName = NULL;
  88. $this->fName = NULL;
  89. $this->title = NULL;
  90. $this->fType = NULL;
  91. $this->fSize = NULL;
  92. $this->destFPath = NULL;
  93. $this->fContents = NULL;
  94. $this->description = NULL;
  95. $this->dateAdded = NULL;
  96. if (!($options & self::TLOBJ_O_SEARCH_BY_ID))
  97. {
  98. $this->dbID = null;
  99. }
  100. }
  101. /**
  102. * Class constructor
  103. *
  104. * @param $dbID integer the database identifier of the attachment
  105. */
  106. function __construct($dbID = null)
  107. {
  108. parent::__construct();
  109. $this->compressionType = tlAttachmentRepository::getCompression();
  110. $this->repositoryPath = tlAttachmentRepository::getPathToRepository();
  111. $this->attachmentCfg = config_get('attachments');
  112. $this->_clean();
  113. $this->dbID = $dbID;
  114. }
  115. /*
  116. * Class destructor, cleans the object
  117. */
  118. function __destruct()
  119. {
  120. parent::__destruct();
  121. $this->_clean();
  122. }
  123. /*
  124. * Initializes the attachment object
  125. *
  126. * @param object $db [ref] the db-object
  127. * @param int $fkid the foreign key id (attachments.fk_id)
  128. * @param string $fktableName the tablename to which the $id refers to (attachments.fk_table)
  129. * @param string $fName the filename
  130. * @param string $destFPath the file path
  131. * @param string $fContents the contents of the file
  132. * @param string $fType the mime-type of the file
  133. * @param int $fSize the filesize (uncompressed)
  134. * @param string $title the title used for the attachment
  135. *
  136. * @return integer returns tl::OK
  137. */
  138. public function create($fkid,$fkTableName,$fName,$destFPath,$fContents,$fType,$fSize,$title)
  139. {
  140. $this->_clean();
  141. $title = trim($title);
  142. $config = $this->attachmentCfg;
  143. if($title == "")
  144. {
  145. switch($config->action_on_save_empty_title)
  146. {
  147. case 'use_filename':
  148. $title = $fName;
  149. break;
  150. default:
  151. break;
  152. }
  153. }
  154. if(!$config->allow_empty_title && $title == "")
  155. {
  156. return self::E_TITLELENGTH;
  157. }
  158. $this->fkID = $fkid;
  159. $this->fkTableName = trim($fkTableName);
  160. $this->fType = trim($fType);
  161. $this->fSize = $fSize;
  162. $this->fName = $fName;
  163. $this->destFPath = trim($destFPath);
  164. $this->fContents = $fContents;
  165. //for FS-repository, the path to the repository itself is cut off, so the path is
  166. // relative to the repository itself
  167. $this->destFPath = str_replace($this->repositoryPath.DIRECTORY_SEPARATOR,"",$destFPath);
  168. $this->title = trim($title);
  169. return tl::OK;
  170. }
  171. /* Read the attachment information from the database, for filesystem repository this doesn't read
  172. * the contents of the attachments
  173. *
  174. * @param $db [ref] the database connection
  175. * @param $options integer null or TLOBJ_O_SEARCH_BY_ID
  176. *
  177. * @return integer returns tl::OK on success, tl::ERROR else
  178. */
  179. public function readFromDB(&$db,$options = self::TLOBJ_O_SEARCH_BY_ID)
  180. {
  181. $this->_clean($options);
  182. $query = "SELECT id,title,description,file_name,file_type,file_size,date_added,".
  183. "compression_type,file_path,fk_id,fk_table FROM {$this->tables['attachments']} ";
  184. $clauses = null;
  185. if ($options & self::TLOBJ_O_SEARCH_BY_ID)
  186. $clauses[] = "id = {$this->dbID}";
  187. if ($clauses)
  188. $query .= " WHERE " . implode(" AND ",$clauses);
  189. $info = $db->fetchFirstRow($query);
  190. if ($info)
  191. {
  192. $this->fkID = $info['fk_id'];
  193. $this->fkTableName = $info['fk_table'];
  194. $this->fName = $info['file_name'];
  195. $this->destFPath = $info['file_path'];
  196. $this->fType = $info['file_type'];
  197. $this->fSize = $info['file_size'];
  198. $this->dbID = $info['id'];
  199. $this->description = $info['description'];
  200. $this->dateAdded = $info['date_added'];
  201. $this->title = $info['title'];
  202. $this->compressionType = $info['compression_type'];
  203. }
  204. return $info ? tl::OK : tl::ERROR;
  205. }
  206. /**
  207. * Returns the attachment meta information in a legacy way
  208. *
  209. * @return array array with the attachment information
  210. */
  211. public function getInfo()
  212. {
  213. return array("id" => $this->dbID,"title" => $this->title,
  214. "description" => $this->description,
  215. "file_name" => $this->fName, "file_type" => $this->fType,
  216. "file_size" => $this->fSize,
  217. "date_added" => $this->dateAdded,
  218. "compression_type" => $this->compressionType,
  219. "file_path" => $this->destFPath,
  220. "fk_id" => $this->fkID,"fk_table" => $this->fkTableName,
  221. );
  222. }
  223. /*
  224. * Writes the attachment into the database, for database repositories also the contents
  225. * of the attachments are written
  226. *
  227. * @return integer returns tl::OK on success, tl::ERROR else
  228. */
  229. public function writeToDB(&$db)
  230. {
  231. $tableName = $db->prepare_string($this->fkTableName);
  232. $fName = $db->prepare_string($this->fName);
  233. $title = $db->prepare_string($this->title);
  234. $fType = $db->prepare_string($this->fType);
  235. $destFPath = is_null($this->destFPath) ? 'NULL' : "'".$db->prepare_string($this->destFPath)."'";
  236. //for FS-repository the contents are null
  237. $fContents = is_null($this->fContents) ? 'NULL' : "'".$db->prepare_string($this->fContents)."'";
  238. $query = "INSERT INTO {$this->tables['attachments']}
  239. (fk_id,fk_table,file_name,file_path,file_size,file_type, date_added,content,compression_type,title)
  240. VALUES ({$this->fkID},'{$tableName}','{$fName}',{$destFPath},{$this->fSize},'{$this->fType}'," . $db->db_now() .
  241. ",$fContents,{$this->compressionType},'{$title}')";
  242. $result = $db->exec_query($query);
  243. if ($result)
  244. {
  245. $this->dbID = $db->insert_id();
  246. }
  247. return $result ? tl::OK : tl::ERROR;
  248. }
  249. /*
  250. * Deletes an attachment from the db, for databse repositories also the contents are deleted
  251. *
  252. * @return integer return tl::OK on success, tl::ERROR else
  253. */
  254. public function deleteFromDB(&$db)
  255. {
  256. $query = "DELETE FROM {$this->tables['attachments']} WHERE id = {$this->dbID}";
  257. $result = $db->exec_query($query);
  258. return $result ? tl::OK : tl::ERROR;
  259. }
  260. /**
  261. * Creates an attachment by a given database identifier
  262. *
  263. * @param $db [ref] the database connection
  264. * @param $id the database identifier of the attachment
  265. * @param $detailLevel the detailLevel
  266. * @return tlAttachment the created attachment or null on failure
  267. */
  268. static public function getByID(&$db,$id,$detailLevel = self::TLOBJ_O_GET_DETAIL_FULL)
  269. {
  270. return tlDBObject::createObjectFromDB($db,$id,__CLASS__,tlAttachment::TLOBJ_O_SEARCH_BY_ID,$detailLevel);
  271. }
  272. /**
  273. * Creates some attachments by given database identifiers
  274. *
  275. * @param $db [ref] the database connection
  276. * @param $id the database identifier of the attachment
  277. * @param $detailLevel the detailLevel
  278. * @return array returns an array of tlAttachment (the created attachments) or null on failure
  279. */
  280. static public function getByIDs(&$db,$ids,$detailLevel = self::TLOBJ_O_GET_DETAIL_FULL)
  281. {
  282. return self::handleNotImplementedMethod(__FUNCTION__);
  283. }
  284. /**
  285. * Currently not implemented
  286. *
  287. * @param $db [ref] the database connection
  288. * @param $whereClause string and addtional where clause
  289. * @param $column string the name of column which holds the id
  290. * @param $orderBy string an additional ORDER BY clause
  291. * @param $detailLevel the detailLevel
  292. * @return unknown_type
  293. */
  294. static public function getAll(&$db,$whereClause = null,$column = null,$orderBy = null,$detailLevel = self::TLOBJ_O_GET_DETAIL_FULL)
  295. {
  296. return self::handleNotImplementedMethod(__FUNCTION__);
  297. }
  298. };
  299. ?>