Skip to content

Commit

Permalink
Bulk delete implemented for users to manage attachments.
Browse files Browse the repository at this point in the history
  • Loading branch information
reecefowell committed Feb 25, 2012
1 parent 2ed518e commit 33be997
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 30 deletions.
18 changes: 15 additions & 3 deletions Component/FileResolver.php
Expand Up @@ -138,15 +138,27 @@ public function getFileData()
$this->resolveType();
return $this->fileData;
}




/**
*
*
* credit to <biolanor at googlemail dot com>, example code used linked below.
* @link http://php.net/manual/en/function.filesize.php
*/
public function calcFileSize($bytes)
{
// array of SI units.
$fs = array('b', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB');

//$size = number_format($bytes/pow(1024, $index=floor(log($bytes, 1024))), ($index >= 1) ? 2 : 0) . ' ' . $fs[$index];

// get kb value
$bpow = floor(log($bytes, 1024));
$size = round($bytes / pow(1024, $bpow), 1) . $fs[$bpow];

// use kb value to work out size
$size = round($bytes / pow(1024, $bpow), ($bpow >= 1) ? 2 : 0) . $fs[$bpow];

return $size;
}
Expand Down Expand Up @@ -174,7 +186,7 @@ public function getFileThumbnailData()
imagecopyresized($tmp, $imgResource, 0, 0, 0, 0, $nx, $ny - 15, $cx, $cy);

// add a filesize stamp to bottom of thumbnail
$fileSize = $this->calcFileSize($this->fileSize); //'15kb';
$fileSize = $this->calcFileSize($this->fileSize);
$textWidth = imagefontwidth(4)*strlen($fileSize);
$tx = 2;//ceil($textWidth/2);
$ty = 45;
Expand Down
55 changes: 46 additions & 9 deletions Controller/ManageController.php
Expand Up @@ -50,7 +50,7 @@ public function indexAction($page, $user_id)

$crumb_trail = $this->container->get('crumb_trail')
->add($this->container->get('translator')->trans('crumbs.attachments_index', array(), 'CCDNComponentAttachmentBundle'),
$this->container->get('router')->generate('cc_attachment_home_for_user', array('user_id' => $user_id)), "home");
$this->container->get('router')->generate('cc_attachment_index_for_user', array('user_id' => $user_id)), "home");
}
} else {
if ( ! $this->container->get('security.context')->isGranted('ROLE_USER'))
Expand All @@ -61,7 +61,7 @@ public function indexAction($page, $user_id)

$crumb_trail = $this->container->get('crumb_trail')
->add($this->container->get('translator')->trans('crumbs.attachment_index', array(), 'CCDNComponentAttachmentBundle'),
$this->container->get('router')->generate('cc_attachment_home'), "home");
$this->container->get('router')->generate('cc_attachment_index'), "home");
}
}

Expand Down Expand Up @@ -114,14 +114,14 @@ public function uploadAction()
$this->container->get('session')->setFlash('notice',
$this->container->get('translator')->trans('flash.attachment.upload.success', array('%file_name%' => $form->getData()->getAttachmentOriginal()), 'CCDNComponentAttachmentBundle'));

return new RedirectResponse($this->container->get('router')->generate('cc_attachment_home'));
return new RedirectResponse($this->container->get('router')->generate('cc_attachment_index'));
}
else
{
// setup crumb trail.
$crumb_trail = $this->container->get('crumb_trail')
->add($this->container->get('translator')->trans('crumbs.attachment_index', array(), 'CCDNComponentAttachmentBundle'),
$this->container->get('router')->generate('cc_attachment_home'), "home")
$this->container->get('router')->generate('cc_attachment_index'), "home")
->add($this->container->get('translator')->trans('crumbs.attachment_upload', array(), 'CCDNComponentAttachmentBundle'),
$this->container->get('router')->generate('cc_attachment_upload'), "publish");

Expand All @@ -135,13 +135,50 @@ public function uploadAction()



public function deleteAction($attachment_id)
/**
*
* @access public
* @return RedirectResponse
*/
public function bulkAction()
{

if ( ! $this->container->get('security.context')->isGranted('ROLE_USER'))
{
throw new AccessDeniedException('You do not have access to this section.');
}

// get all the message id's
$objectIds = array();
$ids = $_POST;
foreach ($ids as $objectKey => $objectId)
{
if (substr($objectKey, 0, 18) == 'check_multiselect_')
{
$objectIds[] = substr($objectKey, 18, (strlen($objectKey) - 18));
}
}

if (count($objectIds) < 1)
{
return new RedirectResponse($this->container->get('router')->generate('cc_attachment_index'));
}

$user = $this->container->get('security.context')->getToken()->getUser();

$attachments = $this->container->get('attachment.repository')->findTheseAttachmentsByUserId($objectIds, $user->getId());

if (isset($_POST['submit_delete']))
{
$this->container->get('attachment.manager')->bulkDelete($attachments)->flushNow();
}

// $this->container->get('attachment.manager')->updateAllFolderCachesForUser($user);

return new RedirectResponse($this->container->get('router')->generate('cc_attachment_index'));
}



/**
*
* @access protected
Expand Down
53 changes: 50 additions & 3 deletions Entity/Attachment.php
Expand Up @@ -65,11 +65,14 @@ class Attachment

/**
* @ORM\ManyToOne(targetEntity="CCDNUser\UserBundle\Entity\User", cascade={"persist"})
* @ORM\JoinColumn(name="created_by_user_id", referencedColumnName="id", onDelete="SET NULL")
* @ORM\JoinColumn(name="owned_by_user_id", referencedColumnName="id", onDelete="SET NULL")
*/
protected $created_by;

protected $owned_by;

/**
* @ORM\Column(type="text", length=255)
*/
protected $file_size;


/**
Expand Down Expand Up @@ -237,4 +240,48 @@ public function getAttachmentOriginal()
{
return $this->attachment_original;
}

/**
* Set file_size
*
* @param text $fileSize
* @return Attachment
*/
public function setFileSize($fileSize)
{
$this->file_size = $fileSize;
return $this;
}

/**
* Get file_size
*
* @return text
*/
public function getFileSize()
{
return $this->file_size;
}

/**
* Set owned_by
*
* @param CCDNUser\UserBundle\Entity\User $ownedBy
* @return Attachment
*/
public function setOwnedBy(\CCDNUser\UserBundle\Entity\User $ownedBy = null)
{
$this->owned_by = $ownedBy;
return $this;
}

/**
* Get owned_by
*
* @return CCDNUser\UserBundle\Entity\User
*/
public function getOwnedBy()
{
return $this->owned_by;
}
}
21 changes: 21 additions & 0 deletions Entity/Manager/AttachmentManager.php
Expand Up @@ -39,4 +39,25 @@ public function insert($attachment)
return $this;
}


/**
*
* @link http://www.php.net/manual/en/function.unlink.php
*/
public function bulkDelete($attachments)
{

foreach ($attachments as $key => $attachment)
{
if (@unlink($attachment->getAttachment()))
{
$this->remove($attachment);
}
}

return $this;
}



}
8 changes: 7 additions & 1 deletion Form/Handler/AttachmentInsertFormHandler.php
Expand Up @@ -134,7 +134,7 @@ public function process()

// set the file properties in the db
$formData->setCreatedDate(new \DateTime());
$formData->setCreatedBy($this->options['user']);
$formData->setOwnedBy($this->options['user']);

$formData->setAttachmentOriginal($fileName);
$formData->setAttachmentHashed($fileNameHashed);
Expand All @@ -144,6 +144,12 @@ public function process()
{
$this->form['attachment']->getData()->move($dir, $fileNameHashed);
$formData->setAttachment($dir . $fileNameHashed);

//$resolver = $this->container->get('attachment.file.resolver');
//$resolver->setFileName($fileRecord->getAttachmentOriginal());
//$fileSize = $resolver->calcFileSize($fileRecord->getAttachment());

$formData->setFileSize(filesize($fileSize));

$this->onSuccess($this->form->getData());

Expand Down
24 changes: 22 additions & 2 deletions Repository/AttachmentRepository.php
Expand Up @@ -37,8 +37,8 @@ public function findForUserById($user_id)
$query = $this->getEntityManager()
->createQuery('
SELECT a, u FROM CCDNComponentAttachmentBundle:Attachment a
LEFT JOIN a.created_by u
WHERE a.created_by = :user_id
LEFT JOIN a.owned_by u
WHERE a.owned_by = :user_id
GROUP BY a.id
ORDER BY a.created_date DESC')
->setParameter('user_id', $user_id);
Expand All @@ -51,4 +51,24 @@ public function findForUserById($user_id)
}


public function findTheseAttachmentsByUserId($objectIds, $userId)
{
$qb = $this->getEntityManager()->createQueryBuilder();
$query = $qb->add('select', 'a')
->from('CCDNComponentAttachmentBundle:Attachment', 'a')
->where($qb->expr()->andx(
$qb->expr()->eq('a.owned_by', '?1'),
$qb->expr()->in('a.id', '?2')))
->setParameters(array('1' => $userId, '2' => array_values($objectIds)))
->getQuery();

try {
return $query->getResult();
} catch (\Doctrine\ORM\NoResultException $e) {
return null;
}
}



}
18 changes: 11 additions & 7 deletions Resources/config/routing.yml
@@ -1,10 +1,10 @@
cc_attachment_home:
cc_attachment_index:
pattern: /{_locale}/attachments
defaults: { _controller: CCDNComponentAttachmentBundle:Manage:index, _locale: en, page: 0, user_id: 0 }
requirements:
_locale: en

cc_attachment_home_paginated:
cc_attachment_index_paginated:
pattern: /{_locale}/attachments/page/{page}
defaults: { _controller: CCDNComponentAttachmentBundle:Manage:index, _locale: en, page: 0, user_id: 0 }
requirements:
Expand Down Expand Up @@ -41,8 +41,12 @@ cc_attachment_download:
requirements:
_locale: en

cc_attachment_delete:
pattern: /{_locale}/attachment/{attachment_id}/delete
defaults: { _controller: CCDNComponentAttachmentBundle:Manage:delete, _locale: en }
requirements:
_locale: en
#cc_attachment_delete:
# pattern: /{_locale}/attachment/{attachment_id}/delete
# defaults: { _controller: CCDNComponentAttachmentBundle:Manage:delete, _locale: en }
# requirements:
# _locale: en

cc_attachment_action_bulk:
pattern: /{_locale}/attachment/action/checked
defaults: { _controller: CCDNComponentAttachmentBundle:Manage:bulk, _locale: en }
4 changes: 3 additions & 1 deletion Resources/translations/CCDNComponentAttachmentBundle.en.yml
Expand Up @@ -4,6 +4,7 @@ title.attachment.index: 'Attachments.'
crumbs.attachment_index: 'Attachments'
crumbs.attachment_upload: 'Upload Attachment'

sidebar.header.attachment: 'Attachments'

form.label.attachment.upload.file: 'File: '
form.label.attachment.upload.description: 'Description: '
Expand All @@ -14,7 +15,8 @@ attachment.table.head.description: 'Description'
attachment.table.head.uploaded: 'Uploaded On'
attachment.table.empty: 'There are no attachments uploaded.'

link.upload: 'Upload'
link.download: 'Download'
link.remove: 'Delete'
link.delete: 'Delete'

flash.attachment.upload.success: 'File "%file_name%" uploaded successfully.'

0 comments on commit 33be997

Please sign in to comment.