PageRenderTime 41ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/application/models/project_file_revisions/ProjectFileRevision.class.php

https://github.com/cj/Project-Pier
PHP | 463 lines | 198 code | 55 blank | 210 comment | 38 complexity | 2244c9fc2503cd5f7028b8ee5f6c793b MD5 | raw file
  1. <?php
  2. /**
  3. * ProjectFileRevision class
  4. * Generated on Tue, 04 Jul 2006 06:46:08 +0200 by DataObject generation tool
  5. *
  6. * @http://www.projectpier.org/
  7. */
  8. class ProjectFileRevision extends BaseProjectFileRevision {
  9. /**
  10. * Message comments are searchable
  11. *
  12. * @var boolean
  13. */
  14. protected $is_searchable = true;
  15. /**
  16. * Array of searchable columns
  17. *
  18. * @var array
  19. */
  20. protected $searchable_columns = array('comment', 'filecontent');
  21. /**
  22. * Parent file object
  23. *
  24. * @var ProjectFile
  25. */
  26. private $file;
  27. /**
  28. * Cached file type object
  29. *
  30. * @var FileType
  31. */
  32. private $file_type;
  33. /**
  34. * Construct file revision object
  35. *
  36. * @param void
  37. * @return ProjectFileRevision
  38. */
  39. function __construct() {
  40. $this->addProtectedAttribute('file_id', 'file_type_id', 'system_filename', 'thumb_filename', 'revision_number', 'type_string', 'filesize');
  41. parent::__construct();
  42. } // __construct
  43. /**
  44. * Return parent file object
  45. *
  46. * @param void
  47. * @return ProjectFile
  48. */
  49. function getFile() {
  50. if (is_null($this->file)) {
  51. $this->file = ProjectFiles::findById($this->getFileId());
  52. } // if
  53. return $this->file;
  54. } // getFile
  55. /**
  56. * Return parent project
  57. *
  58. * @param void
  59. * @return Project
  60. */
  61. function getProject() {
  62. if (is_null($this->project)) {
  63. $file = $this->getFile();
  64. if ($file instanceof ProjectFile) {
  65. $this->project = $file->getProject();
  66. }
  67. } // if
  68. return $this->project;
  69. } // getProject
  70. /**
  71. * Return project ID
  72. *
  73. * @param void
  74. * @return integer
  75. */
  76. function getProjectId() {
  77. $project = $this->getProject();
  78. return $project instanceof Project ? $project->getId() : null;
  79. } // getProjectId
  80. /**
  81. * Return file type object
  82. *
  83. * @param void
  84. * @return FileType
  85. */
  86. function getFileType() {
  87. if (is_null($this->file_type)) {
  88. $this->file_type = FileTypes::findById($this->getFileTypeId());
  89. } // if
  90. return $this->file_type;
  91. } // getFileType
  92. /**
  93. * Return content of this file
  94. *
  95. * @param void
  96. * @return string
  97. */
  98. function getFileContent() {
  99. return FileRepository::getFileContent($this->getRepositoryId());
  100. } // getFileContent
  101. // ---------------------------------------------------
  102. // Utils
  103. // ---------------------------------------------------
  104. /**
  105. * This function will return content of specific searchable column. It uses inherited
  106. * behaviour for all columns except for `filecontent`. In case of this column function
  107. * will return file content if file type is marked as searchable (text documents, office
  108. * documents etc).
  109. *
  110. * @param string $column_name Column name
  111. * @return string
  112. */
  113. function getSearchableColumnContent($column_name) {
  114. if ($column_name == 'filecontent') {
  115. // Unknown type or type not searchable
  116. $file_type = $this->getFileType();
  117. if (!($file_type instanceof FileType) || !$file_type->getIsSearchable()) {
  118. return null;
  119. } // if
  120. $content = $this->getFileContent();
  121. if (strlen($content) <= MAX_SEARCHABLE_FILE_SIZE) {
  122. return $content;
  123. }
  124. } else {
  125. return parent::getSearchableColumnContent($column_name);
  126. } // if
  127. } // getSearchableColumnContent
  128. /**
  129. * Create image thumbnail. This function will return true on success, false otherwise
  130. *
  131. * @param void
  132. * @return boolean
  133. */
  134. protected function createThumb() {
  135. do {
  136. $source_file = CACHE_DIR . '/' . sha1(uniqid(rand(), true));
  137. } while (is_file($source_file));
  138. if (!file_put_contents($source_file, $this->getFileContent()) || !is_readable($source_file)) {
  139. return false;
  140. } // if
  141. do {
  142. $temp_file = CACHE_DIR . '/' . sha1(uniqid(rand(), true));
  143. } while (is_file($temp_file));
  144. try {
  145. Env::useLibrary('simplegd');
  146. $image = new SimpleGdImage($source_file);
  147. $thumb = $image->scale(100, 100, SimpleGdImage::BOUNDARY_DECREASE_ONLY, false);
  148. $thumb->saveAs($temp_file, IMAGETYPE_PNG);
  149. $public_filename = PublicFiles::addFile($temp_file, 'png');
  150. if ($public_filename) {
  151. $this->setThumbFilename($public_filename);
  152. $this->save();
  153. } // if
  154. $result = true;
  155. } catch(Exception $e) {
  156. $result = false;
  157. } // try
  158. @unlink($source_file);
  159. @unlink($temp_file);
  160. return $result;
  161. } // createThumb
  162. // ---------------------------------------------------
  163. // URLs
  164. // ---------------------------------------------------
  165. /**
  166. * Return revision details URL
  167. *
  168. * @param void
  169. * @return string
  170. */
  171. function getDetailsUrl() {
  172. $file = $this->getFile();
  173. return $file instanceof ProjectFile ? $file->getDetailsUrl() . '#revision' . $this->getId() : null;
  174. } // getDetailsUrl
  175. /**
  176. * Show download URL
  177. *
  178. * @param void
  179. * @return string
  180. */
  181. function getDownloadUrl() {
  182. return get_url('files', 'download_revision', array('id' => $this->getId(), 'active_project' => $this->getProjectId()));
  183. } // getDownloadUrl
  184. /**
  185. * Return edit revision URL
  186. *
  187. * @param void
  188. * @return string
  189. */
  190. function getEditUrl() {
  191. return get_url('files', 'edit_file_revision', array('id' => $this->getId(), 'active_project' => $this->getProjectId()));
  192. } // getEditUrl
  193. /**
  194. * Return delete revision URL
  195. *
  196. * @param void
  197. * @return string
  198. */
  199. function getDeleteUrl() {
  200. return get_url('files', 'delete_file_revision', array('id' => $this->getId(), 'active_project' => $this->getProjectId()));
  201. } // getDeleteUrl
  202. /**
  203. * Return thumb URL
  204. *
  205. * @param void
  206. * @return string
  207. */
  208. function getThumbUrl() {
  209. if ($this->getThumbFilename() == '') {
  210. $this->createThumb();
  211. } // if
  212. if (trim($this->getThumbFilename())) {
  213. return PublicFiles::getFileUrl($this->getThumbFilename());
  214. } else {
  215. return '';
  216. } // if
  217. } // getThumbUrl
  218. /**
  219. * Return URL of file type icon. If we are working with image file type this function
  220. * will return thumb URL if it success in creating it
  221. *
  222. * @param void
  223. * @return string
  224. */
  225. function getTypeIconUrl() {
  226. $file_type = $this->getFileType();
  227. if ($file_type instanceof FileType) {
  228. if ($file_type->getIsImage()) {
  229. $thumb_url = $this->getThumbUrl();
  230. if (trim($thumb_url)) {
  231. return $thumb_url; // we have the thumb!
  232. } // if
  233. } // if
  234. } // if
  235. $icon_file = $file_type instanceof FileType ? $file_type->getIcon() : 'unknown.png';
  236. return get_image_url("filetypes/$icon_file");
  237. } // getTypeIconUrl
  238. // ---------------------------------------------------
  239. // Permissions
  240. // ---------------------------------------------------
  241. /**
  242. * Check CAN_MANAGE_DOCUMENS permission
  243. *
  244. * @access public
  245. * @param User $user
  246. * @return boolean
  247. */
  248. function canManage(User $user) {
  249. if (!$user->isProjectUser($this->getProject())) {
  250. return false;
  251. }
  252. return $user->getProjectPermission($this->getProject(), ProjectUsers::CAN_MANAGE_FILES);
  253. } // canManage
  254. /**
  255. * Empty implementation of abstract method. Message determins if user have view access
  256. *
  257. * @param void
  258. * @return boolean
  259. */
  260. function canView(User $user) {
  261. //if (!$user->isProjectUser($this->getProject())) return false;
  262. if ($this->isPrivate() && !$user->isMemberOfOwnerCompany()) {
  263. return false;
  264. }
  265. return true;
  266. } // canView
  267. /**
  268. * Returns true if user can download this file
  269. *
  270. * @param User $user
  271. * @return boolean
  272. */
  273. function canDownload(User $user) {
  274. return $this->canView($user);
  275. } // canDownload
  276. /**
  277. * Empty implementation of abstract methods. Messages determine does user have
  278. * permissions to add comment
  279. *
  280. * @param void
  281. * @return null
  282. */
  283. function canAdd(User $user, Project $project) {
  284. return $user->isAdministrator() || ProjectFile::canUpload($user, $project);
  285. } // canAdd
  286. /**
  287. * Check if specific user can edit this file
  288. *
  289. * @access public
  290. * @param User $user
  291. * @return boolean
  292. */
  293. function canEdit(User $user) {
  294. if (!$this->canManage(logged_user())) {
  295. return false; // user don't have access to this project or can't manage files
  296. }
  297. if ($user->isAdministrator()) {
  298. return true; // give access to admin
  299. }
  300. return false;
  301. } // canEdit
  302. /**
  303. * Check if specific user can delete this comment
  304. *
  305. * @access public
  306. * @param User $user
  307. * @return boolean
  308. */
  309. function canDelete(User $user) {
  310. if (!$user->isProjectUser($this->getProject())) {
  311. return false;
  312. } // if
  313. $file = $this->getFile();
  314. if (!($file instanceof ProjectFile)) {
  315. return false;
  316. } // if
  317. if ($file->countRevisions() == 1) {
  318. return false; // this is the only file revision! it can't be deleted!
  319. } // if
  320. if ($user->isAdministrator()) {
  321. return true;
  322. } // if
  323. return false;
  324. } // canDelete
  325. // ---------------------------------------------------
  326. // System
  327. // ---------------------------------------------------
  328. /**
  329. * Validate before save. This one is used to keep the data in sync. Users
  330. * can't create revisions directly...
  331. *
  332. * @param array $errors
  333. * @return null
  334. */
  335. function validate(&$errors) {
  336. if (!$this->validatePresenceOf('file_id')) {
  337. $errors[] = lang('file revision file_id required');
  338. } // if
  339. if (!$this->validatePresenceOf('repository_id')) {
  340. $errors[] = lang('file revision filename required');
  341. } // if
  342. if (!$this->validatePresenceOf('type_string')) {
  343. $errors[] = lang('file revision type_string required');
  344. } // if
  345. } // validate
  346. /**
  347. * Delete from DB and from the disk
  348. *
  349. * @param void
  350. * @return boolean
  351. */
  352. function delete() {
  353. FileRepository::deleteFile($this->getRepositoryId());
  354. $this->deleteThumb(false);
  355. return parent::delete();
  356. } // delete
  357. /**
  358. * Delete thumb
  359. *
  360. * @param boolean $save
  361. * @return boolean
  362. */
  363. function deleteThumb($save = true) {
  364. $thumb_filename = $this->getThumbFilename();
  365. if ($thumb_filename) {
  366. $this->setThumbFilename('');
  367. PublicFiles::deleteFile($this->getThumbFilename());
  368. } // if
  369. if ($save) {
  370. return $this->save();
  371. } // if
  372. return true;
  373. } // deleteThumb
  374. // ---------------------------------------------------
  375. // ApplicationDataObject implementation
  376. // ---------------------------------------------------
  377. /**
  378. * Return object name
  379. *
  380. * @access public
  381. * @param void
  382. * @return string
  383. */
  384. function getObjectName() {
  385. $file = $this->getFile();
  386. return $file instanceof ProjectFile ? $file->getObjectName() . ' revision #' . $this->getRevisionNumber() : 'Unknown file revision';
  387. } // getObjectName
  388. /**
  389. * Return object type name
  390. *
  391. * @param void
  392. * @return string
  393. */
  394. function getObjectTypeName() {
  395. return lang('file revision');
  396. } // getObjectTypeName
  397. /**
  398. * Return object URl
  399. *
  400. * @access public
  401. * @param void
  402. * @return string
  403. */
  404. function getObjectUrl() {
  405. return $this->getDetailsurl();
  406. } // getObjectUrl
  407. } // ProjectFileRevision
  408. ?>