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

Commit 817997f

Browse files
author
Charles Marion
committed
ENH: improved api module
1 parent 57b71e6 commit 817997f

File tree

12 files changed

+496
-665
lines changed

12 files changed

+496
-665
lines changed

core/controllers/DownloadController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function indexAction()
5858
foreach($itemIds as $itemId)
5959
{
6060
// check revision
61-
$tmp = explode(', ', $itemId);
61+
$tmp = explode(',', $itemId);
6262
if(empty($tmp[0]))
6363
{
6464
continue;

core/controllers/SearchController.php

Lines changed: 8 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class SearchController extends AppController
77
{
88
public $_models = array('ItemKeyword', 'Item', 'Folder', 'User', 'Community', 'Group');
99
public $_daos = array('ItemKeyword', 'Item', 'Folder', 'User', 'Community');
10-
public $_components = array('Sortdao', 'Date', 'Utility');
10+
public $_components = array('Sortdao', 'Date', 'Utility', 'Search');
1111

1212
/** Init Controller */
1313
function init()
@@ -38,20 +38,8 @@ public function indexAction()
3838
{
3939
$order = 'view';
4040
}
41-
// Get the items corresponding to the search
42-
$ItemsDao = $this->ItemKeyword->getItemsFromSearch($keyword, $this->userSession->Dao, 200, false, $order);
43-
44-
// Search for the folders
45-
$FoldersDao = $this->Folder->getFoldersFromSearch($keyword, $this->userSession->Dao, 15, false, $order);
46-
47-
// Search for the communities
48-
$CommunitiesDao = $this->Community->getCommunitiesFromSearch($keyword, $this->userSession->Dao, 15, false, $order);
49-
50-
// Search for the users
51-
$UsersDao = $this->User->getUsersFromSearch($keyword, $this->userSession->Dao, 15, false, $order);
52-
53-
$results = $this->_formatResults($order, $ItemsDao, $FoldersDao, $CommunitiesDao, $UsersDao);
5441

42+
$results = $this->Component->Search->searchAll($this->userSession->Dao, $keyword, $order);
5543
if(isset($ajax))
5644
{
5745
$this->_helper->layout->disableLayout();
@@ -60,90 +48,17 @@ public function indexAction()
6048
}
6149
else
6250
{
63-
$this->view->nitems = count($ItemsDao);
64-
$this->view->nfolders = count($FoldersDao);
65-
$this->view->ncommunities = count($CommunitiesDao);
66-
$this->view->nusers = count($UsersDao);
67-
$this->view->json['search']['results'] = $results;
51+
$this->view->nitems = $results['nitems'];
52+
$this->view->nfolders = $results['nfolders'];
53+
$this->view->ncommunities = $results['ncommunities'];
54+
$this->view->nusers = $results['nusers'];
55+
$this->view->json['search']['results'] = $results['results'];
6856
$this->view->json['search']['keyword'] = $keyword;
6957
$this->view->json['search']['noResults'] = $this->t('No result found.');
7058
$this->view->json['search']['moreResults'] = $this->t('Show more results.');
7159
}
7260
}//end indexAction
73-
74-
/**
75-
* Format search results
76-
* @param string $order
77-
* @param Array $items
78-
* @param Array $folders
79-
* @param Array $communities
80-
* @param Array $users
81-
* @return Array
82-
*/
83-
private function _formatResults($order, $items, $folders, $communities, $users)
84-
{
85-
foreach($users as $key => $user)
86-
{
87-
$users[$key]->name = $user->getLastname();
88-
$users[$key]->date = $user->getCreation();
89-
}
90-
foreach($communities as $key => $community)
91-
{
92-
$communities[$key]->date = $community->getCreation();
93-
}
94-
$results = array_merge($folders, $items, $communities, $users);
95-
96-
switch($order)
97-
{
98-
case 'name':
99-
$this->Component->Sortdao->field = 'name';
100-
$this->Component->Sortdao->order = 'asc';
101-
usort($results, array($this->Component->Sortdao, 'sortByName'));
102-
break;
103-
case 'date':
104-
$this->Component->Sortdao->field = 'date';
105-
$this->Component->Sortdao->order = 'asc';
106-
usort($results, array($this->Component->Sortdao, 'sortByDate'));
107-
break;
108-
case 'view':
109-
$this->Component->Sortdao->field = 'view';
110-
$this->Component->Sortdao->order = 'desc';
111-
usort($results, array($this->Component->Sortdao, 'sortByNumber'));
112-
break;
113-
default:
114-
throw new Zend_Exception('Error order parameter');
115-
break;
116-
}
117-
$resultsArray = array();
118-
foreach($results as $result)
119-
{
120-
$tmp = $result->toArray();
121-
if($result instanceof UserDao)
122-
{
123-
$tmp['resultType'] = 'user';
124-
$tmp['formattedDate'] = $this->Component->Date->formatDate($result->getCreation());
125-
}
126-
if($result instanceof ItemDao)
127-
{
128-
$tmp['resultType'] = 'item';
129-
$tmp['formattedDate'] = $this->Component->Date->formatDate($result->getDate());
130-
}
131-
if($result instanceof CommunityDao)
132-
{
133-
$tmp['resultType'] = 'community';
134-
$tmp['formattedDate'] = $this->Component->Date->formatDate($result->getCreation());
135-
}
136-
if($result instanceof FolderDao)
137-
{
138-
$tmp['resultType'] = 'folder';
139-
$tmp['formattedDate'] = $this->Component->Date->formatDate($result->getDate());
140-
}
141-
unset($tmp['password']);
142-
unset($tmp['email']);
143-
$resultsArray[] = $tmp;
144-
}
145-
return $resultsArray;
146-
}//formatResults
61+
14762

14863

14964
/** search live Action */

core/controllers/components/NotifyErrorComponent.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function fatalEror($logger, $mailer)
3838
$message .= "It seems you have just encountered an unknown issue.";
3939
$message .= "Our team has been notified and will deal with the problem as soon as possible.";
4040
header('content-type: text/plain');
41+
ob_clean();
4142
if($e['type'] == E_NOTICE)
4243
{
4344
$e['typeText'] = 'E_NOTICE';
@@ -101,6 +102,7 @@ public function fatalEror($logger, $mailer)
101102
return;
102103
}
103104
header('content-type: text/plain');
105+
ob_clean();
104106
echo $this->getFatalErrorMessage($e);
105107
}
106108
$logger->crit($this->getFatalErrorMessage($e));
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
/** Saerch */
3+
class SearchComponent extends AppComponent
4+
{
5+
/** search all the results */
6+
public function searchAll($userDao, $search, $order)
7+
{
8+
$modelLoad = new MIDAS_ModelLoader();
9+
$itemModel = $modelLoad->loadModel('ItemKeyword');
10+
$folderModel = $modelLoad->loadModel('Folder');
11+
$communityModel = $modelLoad->loadModel('Community');
12+
$userModel = $modelLoad->loadModel('User');
13+
14+
$ItemsDao = $itemModel->getItemsFromSearch($search, $userDao, 200, false, $order);
15+
16+
// Search for the folders
17+
$FoldersDao = $folderModel->getFoldersFromSearch($search, $userDao, 15, false, $order);
18+
19+
// Search for the communities
20+
$CommunitiesDao = $communityModel->getCommunitiesFromSearch($search, $userDao, 15, false, $order);
21+
22+
// Search for the users
23+
$UsersDao = $userModel->getUsersFromSearch($search, $userDao, 15, false, $order);
24+
25+
$return = array();
26+
27+
$return['nitems'] = count($ItemsDao);
28+
$return['nfolders'] = count($FoldersDao);
29+
$return['ncommunities'] = count($CommunitiesDao);
30+
$return['nusers'] = count($UsersDao);
31+
$return['results'] = $this->_formatResults($order, $ItemsDao, $FoldersDao, $CommunitiesDao, $UsersDao);
32+
33+
return $return;
34+
}
35+
36+
/**
37+
* Format search results
38+
* @param string $order
39+
* @param Array $items
40+
* @param Array $folders
41+
* @param Array $communities
42+
* @param Array $users
43+
* @return Array
44+
*/
45+
private function _formatResults($order, $items, $folders, $communities, $users)
46+
{
47+
foreach($users as $key => $user)
48+
{
49+
$users[$key]->name = $user->getLastname();
50+
$users[$key]->date = $user->getCreation();
51+
}
52+
foreach($communities as $key => $community)
53+
{
54+
$communities[$key]->date = $community->getCreation();
55+
}
56+
$results = array_merge($folders, $items, $communities, $users);
57+
58+
Zend_Loader::loadClass('SortdaoComponent', BASE_PATH . '/core/controllers/components');
59+
Zend_Loader::loadClass('DateComponent', BASE_PATH . '/core/controllers/components');
60+
61+
$sortdaoComponent = new SortdaoComponent();
62+
$dateComponent = new DateComponent();
63+
64+
switch($order)
65+
{
66+
case 'name':
67+
$sortdaoComponent->field = 'name';
68+
$sortdaoComponent->order = 'asc';
69+
usort($results, array($sortdaoComponent, 'sortByName'));
70+
break;
71+
case 'date':
72+
$sortdaoComponent->field = 'date';
73+
$sortdaoComponent->order = 'asc';
74+
usort($results, array($sortdaoComponent, 'sortByDate'));
75+
break;
76+
case 'view':
77+
$sortdaoComponent->field = 'view';
78+
$sortdaoComponent->order = 'desc';
79+
usort($results, array($sortdaoComponent, 'sortByNumber'));
80+
break;
81+
default:
82+
throw new Zend_Exception('Error order parameter');
83+
break;
84+
}
85+
$resultsArray = array();
86+
foreach($results as $result)
87+
{
88+
$tmp = $result->toArray();
89+
if($result instanceof UserDao)
90+
{
91+
$tmp['resultType'] = 'user';
92+
$tmp['formattedDate'] = $dateComponent->formatDate($result->getCreation());
93+
}
94+
if($result instanceof ItemDao)
95+
{
96+
$tmp['resultType'] = 'item';
97+
$tmp['formattedDate'] = $dateComponent->formatDate($result->getDate());
98+
}
99+
if($result instanceof CommunityDao)
100+
{
101+
$tmp['resultType'] = 'community';
102+
$tmp['formattedDate'] = $dateComponent->formatDate($result->getCreation());
103+
}
104+
if($result instanceof FolderDao)
105+
{
106+
$tmp['resultType'] = 'folder';
107+
$tmp['formattedDate'] = $dateComponent->formatDate($result->getDate());
108+
}
109+
unset($tmp['password']);
110+
unset($tmp['email']);
111+
$resultsArray[] = $tmp;
112+
}
113+
return $resultsArray;
114+
}//formatResults
115+
} // end class UploadComponent
116+
?>

core/database/upgrade/3.0.7.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
class Upgrade_3_0_7 extends MIDASUpgrade
43
{
54
public function preUpgrade()

core/models/base/ItemRevisionModelBase.php

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,33 +36,6 @@ public function save($dao)
3636
$uuModel = $modelLoad->loadModel('Uniqueidentifier');
3737
$uuModel->newUUID($dao);
3838
}
39-
40-
/** delete a revision*/
41-
function delete($revisiondao)
42-
{
43-
if(!$revisiondao instanceof ItemRevisionDao)
44-
{
45-
throw new Zend_Exception("Error param.");
46-
}
47-
$bitstreams = $revisiondao->getBitstreams();
48-
$this->ModelLoader = new MIDAS_ModelLoader();
49-
$bitstream_model = $this->ModelLoader->loadModel('Bitstream');
50-
foreach($bitstreams as $bitstream)
51-
{
52-
$bitstream_model->delete($bitstream);
53-
}
54-
55-
$modelLoad = new MIDAS_ModelLoader();
56-
$uuModel = $modelLoad->loadModel('Uniqueidentifier');
57-
$uudao = $uuModel->getIndentifier($revisiondao);
58-
if($uudao)
59-
{
60-
$uuModel->delete($uudao);
61-
}
62-
parent::delete($revisiondao);
63-
$revisiondao->saved = false;
64-
unset($revisiondao->itemrevision_id);
65-
}//end delete
66-
39+
6740

6841
} // end class ItemRevisionModelBase

core/models/pdo/FolderModel.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,80 @@ public function getSizeFiltered($folders, $userDao = null, $policy = 0)
170170
return $folders;
171171
}
172172

173+
174+
175+
/** Get the folder tree */
176+
function getAllChildren($folder, $userDao)
177+
{
178+
$isAdmin = false;
179+
if($userDao == null)
180+
{
181+
$userId = -1;
182+
}
183+
else if(!$userDao instanceof UserDao)
184+
{
185+
throw new Zend_Exception("Should be an user.");
186+
}
187+
else
188+
{
189+
$userId = $userDao->getUserId();
190+
if($userDao->isAdmin())
191+
{
192+
$isAdmin = true;
193+
}
194+
}
195+
196+
if(!$folder instanceof FolderDao)
197+
{
198+
throw new Zend_Exception("Should be a folder" );
199+
}
200+
$subqueryUser = $this->database->select()
201+
->setIntegrityCheck(false)
202+
->from(array('f' => 'folder'));
203+
if(!$isAdmin)
204+
{
205+
$subqueryUser ->join(array('fpu' => 'folderpolicyuser'), '
206+
f.folder_id = fpu.folder_id AND '.$this->database->getDB()->quoteInto('fpu.policy >= ?', $policy).'
207+
AND '.$this->database->getDB()->quoteInto('user_id = ? ', $userId).' ', array());
208+
}
209+
$subqueryUser ->where('left_indice > ?', $folder->getLeftIndice())
210+
->where('right_indice < ?', $folder->getRightIndice());
211+
212+
$subqueryGroup = $this->database->select()
213+
->setIntegrityCheck(false)
214+
->from(array('f' => 'folder'));
215+
if(!$isAdmin)
216+
{
217+
$subqueryGroup ->join(array('fpg' => 'folderpolicygroup'), '
218+
f.folder_id = fpg.folder_id AND '.$this->database->getDB()->quoteInto('fpg.policy >= ?', $policy).'
219+
AND ( '.$this->database->getDB()->quoteInto('group_id = ? ', MIDAS_GROUP_ANONYMOUS_KEY).' OR
220+
group_id IN (' .new Zend_Db_Expr(
221+
$this->database->select()
222+
->setIntegrityCheck(false)
223+
->from(array('u2g' => 'user2group'),
224+
array('group_id'))
225+
->where('u2g.user_id = ?', $userId)
226+
) .'))', array());
227+
}
228+
229+
$subqueryGroup ->where('left_indice > ?', $folder->getLeftIndice())
230+
->where('right_indice < ?', $folder->getRightIndice());
231+
232+
$subSqlFolders = $this->database->select()
233+
->union(array($subqueryUser, $subqueryGroup));
234+
235+
236+
$rowset = $this->database->fetchAll($subSqlFolders);
237+
238+
$folders = array();
239+
240+
foreach($rowset as $row)
241+
{
242+
$folders[] = $this->initDao('Folder', $row);
243+
}
244+
245+
return $folders;
246+
}
173247

174248
/** Custom delete function */
175249
function delete($folder, $recursive = false)

0 commit comments

Comments
 (0)