示例#1
0
 /**
  * Upload files and attach them to an issue or a comment.
  *
  * @param   array  $files      Array containing the uploaded files.
  * @param   int    $issueId    ID of the issue/comment where to attach the files.
  * @param   int    $commentId  One of 'issue' or 'comment', indicating if the files should be attached to an issue or a comment.
  *
  * @return  boolean   True on success, false otherwise.
  */
 public function upload($files, $issueId, $commentId = null)
 {
     if (!$issueId || !is_array($files)) {
         return false;
     }
     jimport('joomla.filesystem.file');
     if ($commentId) {
         $type = 'comment';
         $id = $commentId;
     } else {
         $type = 'issue';
         $id = $issueId;
     }
     foreach ($files as $file) {
         $rand = MonitorHelper::genRandHash();
         $pathParts = array($type, $id, $rand . '-' . $file[0]['name']);
         $path = JPath::clean(implode(DIRECTORY_SEPARATOR, $pathParts));
         $values = array('issue_id' => $issueId, 'comment_id' => $commentId, 'path' => $path, 'name' => $file[0]['name']);
         if (!JFile::upload($file[0]['tmp_name'], $this->pathPrefix . $path)) {
             JFactory::getApplication()->enqueueMessage(JText::sprintf('COM_MONITOR_ATTACHMENT_UPLOAD_FAILED', $file[0]['name']));
             return false;
         }
         $query = $this->db->getQuery(true);
         $query->insert('#__monitor_attachments')->set(MonitorHelper::sqlValues($values, $query));
         $this->db->setQuery($query);
         if ($this->db->execute() === false) {
             return false;
         }
     }
     return true;
 }