Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit d815afb

Browse files
author
Charles Marion
committed
ENH: Added Item/ItemRevision/Bitstream Delete
1 parent c862fb5 commit d815afb

File tree

12 files changed

+198
-9
lines changed

12 files changed

+198
-9
lines changed

core/controllers/ItemController.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ function viewAction()
3838
{
3939
throw new Zend_Exception("Problem policies.");
4040
}
41+
42+
$this->view->isAdmin=$this->Item->policyCheck($itemDao,$this->userSession->Dao,MIDAS_POLICY_ADMIN);
43+
$this->view->isModerator=$this->Item->policyCheck($itemDao,$this->userSession->Dao,MIDAS_POLICY_WRITE);
44+
4145
$request = $this->getRequest();
4246
$cookieData = $request->getCookie('recentItems');
4347
$recentItems=array();
@@ -69,8 +73,35 @@ function viewAction()
6973
$itemDao->revisions=$itemDao->getRevisions();
7074
$itemDao->creation=$this->Component->Date->formatDate(strtotime($itemRevision->getDate()));
7175
$this->view->itemDao=$itemDao;
76+
77+
$this->view->json['item']=$itemDao->_toArray();
78+
$this->view->json['item']['message']['delete']=$this->t('Delete');
79+
$this->view->json['item']['message']['deleteMessage']=$this->t('Do you really want to delete this item? It cannot be undo.');
7280
}//end index
7381

82+
83+
/** Delete an item*/
84+
function deleteAction()
85+
{
86+
$this->_helper->layout->disableLayout();
87+
$this->_helper->viewRenderer->setNoRender();
88+
89+
$itemId=$this->_getParam("itemId");
90+
if(!isset($itemId) || (!is_numeric($itemId) && strlen($itemId)!=32)) // This is tricky! and for Cassandra for now
91+
{
92+
throw new Zend_Exception("itemId should be a number");
93+
}
94+
$itemDao=$this->Item->load($itemId);
95+
if($itemDao===false||!$this->Item->policyCheck($itemDao, $this->userSession->Dao,MIDAS_POLICY_ADMIN))
96+
{
97+
throw new Zend_Exception("This community doesn't exist or you don't have the permissions.");
98+
}
99+
100+
$this->Item->delete($itemDao);
101+
102+
$this->_redirect('/');
103+
}//end delete
104+
74105
}//end class
75106

76107
/* pour la récupérer

core/controllers/components/JsonComponent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private function _utf8_encode_array (&$array, $key) {
7171
if(is_array($array)) {
7272
array_walk ($array, array($this,'_utf8_encode_array'));
7373
} else {
74-
$array = utf8_encode($array);
74+
$array = @utf8_encode($array);
7575
}
7676
}
7777

core/models/base/BitstreamModelBase.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,22 @@ public function __construct()
2727
/** Abstract functions */
2828
abstract function getByChecksum($checksum);
2929

30+
/** delete a Bitstream*/
31+
function delete($bitstream)
32+
{
33+
if(!$bitstream instanceof BitstreamDao)
34+
{
35+
throw new Zend_Exception("Error param.");
36+
}
37+
$checksum=$bitstream->getChecksum();
38+
$path=$bitstream->getFullPath();
39+
parent::delete($bitstream);
40+
if($this->getByChecksum($checksum)==false)
41+
{
42+
unlink($path);
43+
}
44+
$bitstream->saved=false;
45+
unset($bitstream->bitstream_id);
46+
}
3047
} // end class BitstreamModelBase
3148
?>

core/models/base/ItemModelBase.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
class ItemModelBase extends AppModel
2+
abstract class ItemModelBase extends AppModel
33
{
44
public function __construct()
55
{
@@ -18,6 +18,8 @@ public function __construct()
1818
'folders' => array('type'=>MIDAS_MANY_TO_MANY, 'model'=>'Folder', 'table' => 'item2folder', 'parent_column'=> 'item_id', 'child_column' => 'folder_id'),
1919
'revisions' => array('type'=>MIDAS_ONE_TO_MANY, 'model'=>'ItemRevision', 'parent_column'=> 'item_id', 'child_column' => 'item_id'),
2020
'keywords' => array('type'=>MIDAS_MANY_TO_MANY, 'model'=>'ItemKeyword', 'table' => 'item2keyword', 'parent_column'=> 'item_id', 'child_column' => 'keyword_id'),
21+
'itempolicygroup' => array('type'=>MIDAS_ONE_TO_MANY, 'model' => 'Itempolicygroup', 'parent_column'=> 'item_id', 'child_column' => 'item_id'),
22+
'itempolicyuser' => array('type'=>MIDAS_ONE_TO_MANY, 'model' => 'Itempolicyuser', 'parent_column'=> 'item_id', 'child_column' => 'item_id'),
2123
);
2224
$this->initialize(); // required
2325
} // end __construct()

core/models/base/ItemRevisionModelBase.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,25 @@ public function __construct()
2424
$this->initialize(); // required
2525
} // end __construct()
2626

27-
27+
/** delete a revision*/
28+
function delete($revisiondao)
29+
{
30+
if(!$revisiondao instanceof ItemRevisionDao)
31+
{
32+
throw new Zend_Exception("Error param.");
33+
}
34+
$bitstreams=$revisiondao->getBitstreams();
35+
$this->ModelLoader = new MIDAS_ModelLoader();
36+
$bitstream_model=$this->ModelLoader->loadModel('Bitstream');
37+
foreach($bitstreams as $bitstream)
38+
{
39+
$bitstream_model->delete($bitstream);
40+
}
41+
parent::delete($revisiondao);
42+
$revisiondao->saved=false;
43+
unset($revisiondao->itemrevision_id);
44+
}//end delete
45+
2846

2947
} // end class ItemRevisionModelBase
3048
?>

core/models/pdo/BitstreamModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function save($dao)
1919
}//end Save
2020

2121
/** Get bitstream by checksum */
22-
function getByChecksum($checksum)
22+
function getByChecksum($checksum)
2323
{
2424
$row = $this->database->fetchRow($this->database->select()->where('checksum = ?', $checksum));
2525
$dao= $this->initDao(ucfirst($this->_name),$row);

core/models/pdo/ItemModel.php

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,64 @@
77
*/
88
class ItemModel extends ItemModelBase
99
{
10-
10+
/** Delete an item */
11+
function delete($itemdao)
12+
{
13+
if(!$itemdao instanceof ItemDao)
14+
{
15+
throw new Zend_Exception("Error param.");
16+
}
17+
18+
$deleteType=array(
19+
MIDAS_FEED_CREATE_ITEM,MIDAS_FEED_CREATE_LINK_ITEM
20+
);
21+
$sql=$this->database->select()
22+
->setIntegrityCheck(false)
23+
->from(array('p' => 'feed'))
24+
->where('ressource = ?', $itemdao->getKey());
25+
26+
$rowset=$this->database->fetchAll($sql);
27+
$this->ModelLoader = new MIDAS_ModelLoader();
28+
$feed_model=$this->ModelLoader->loadModel('Feed');
29+
$revision_model=$this->ModelLoader->loadModel('ItemRevision');
30+
foreach($rowset as $row)
31+
{
32+
$feed=$this->initDao('Feed', $row);
33+
if(in_array($feed->getType(), $deleteType))
34+
{
35+
$feed_model->delete($feed);
36+
}
37+
}
38+
$revisions=$itemdao->getRevisions();
39+
foreach($revisions as $revision)
40+
{
41+
$revision_model->delete($revision);
42+
}
43+
44+
$keywords=$itemdao->getKeywords();
45+
foreach($keywords as $keyword)
46+
{
47+
$this->removeKeyword($itemdao,$keyword);
48+
}
49+
50+
$policy_group_model=$this->ModelLoader->loadModel('Itempolicygroup');
51+
$policiesGroup=$itemdao->getItempolicygroup();
52+
foreach($policiesGroup as $policy)
53+
{
54+
$policy_group_model->delete($policy);
55+
}
56+
57+
$policy_user_model=$this->ModelLoader->loadModel('Itempolicyuser');
58+
$policiesUser=$itemdao->getItempolicyuser();
59+
foreach($policiesUser as $policy)
60+
{
61+
$policy_user_model->delete($policy);
62+
}
63+
parent::delete($itemdao);
64+
unset($itemdao->item_id);
65+
$itemdao->saved=false;
66+
}//end delete
67+
1168
/** check if the policy is valid*/
1269
function policyCheck($itemdao,$userDao=null,$policy=0)
1370
{
@@ -195,6 +252,20 @@ function addKeyword($itemdao,$keyworddao)
195252
}
196253
$this->database->link('keywords',$itemdao,$keyworddao);
197254
} // end addKeyword
198-
255+
256+
/** Remove a keyword to an item
257+
* @return void*/
258+
function removeKeyword($itemdao,$keyworddao)
259+
{
260+
if(!$itemdao instanceof ItemDao)
261+
{
262+
throw new Zend_Exception("First argument should be an item");
263+
}
264+
if(!$keyworddao instanceof ItemKeywordDao)
265+
{
266+
throw new Zend_Exception("Second argument should be a keyword");
267+
}
268+
$this->database->removeLink('keywords',$itemdao,$keyworddao);
269+
} // end addKeyword
199270
} // end class
200271
?>

core/models/pdo/ItemRevisionModel.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
*/
88
class ItemRevisionModel extends ItemRevisionModelBase
99
{
10+
11+
12+
1013
/** Returns the latest revision of a model */
1114
function getLatestRevision($itemdao)
1215
{

core/views/item/view.phtml

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
<link type="text/css" rel="stylesheet" href="<?php echo $this->webroot?>/public/css/item/item.view.css" />
33
<link type="text/css" rel="stylesheet" href="<?php echo $this->webroot?>/public/css/common/common.browser.css" />
44

5+
<?php
6+
$this->headScript()->appendFile($this->webroot . '/public/js/item/item.view.js');
7+
?>
58
<div class="viewMain">
69
<div class="genericThumbnail">
710
<?php
@@ -43,11 +46,27 @@
4346
<ul>
4447
<?php
4548
echo
46-
"
47-
<li>
49+
"<li>
4850
<a href='{$this->webroot}/download/?items={$this->itemDao->getKey()}'>{$this->t('Download')}</a>
4951
</li>
5052
";
53+
if($this->isModerator)
54+
{
55+
echo
56+
"<li>
57+
<a >{$this->t('Copy')}</a>
58+
</li>
59+
";
60+
}
61+
if($this->isAdmin)
62+
{
63+
echo
64+
"
65+
<li>
66+
<a href='javascript:;' id='itemDeleteLink'>{$this->t('Delete')}</a>
67+
</li>
68+
";
69+
}
5170
?>
5271
</ul>
5372
</div>

library/MIDAS/models/MIDASDatabasePdo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ public function save($dataarray)
293293
public function delete($dao)
294294
{
295295
$instance=ucfirst($this->_name)."Dao";
296-
if(get_class($dao) != $instance)
296+
if(strtolower(get_class($dao)) != strtolower($instance))
297297
{
298298
throw new Zend_Exception("Should be an object ($instance). It was: ".get_class($dao) );
299299
}

0 commit comments

Comments
 (0)