@@ -7,14 +7,14 @@ class SearchController extends AppController
7
7
{
8
8
public $ _models =array ('ItemKeyword ' ,'Item ' ,'Folder ' ,'User ' ,'Community ' );
9
9
public $ _daos =array ('ItemKeyword ' ,'Item ' ,'Folder ' ,'USer ' ,'Community ' );
10
- public $ _components =array ();
10
+ public $ _components =array (' Sortdao ' , ' Date ' );
11
11
12
12
/** Init Controller */
13
13
function init ()
14
14
{
15
15
$ this ->view ->activemenu = 'feed ' ; // set the active menu
16
16
17
- // if the number of parameters is more than 3 then it's the liveAction
17
+ // if the number of parameters is more than 3 then it's the liveAction or advanced search
18
18
if (count ($ this ->_getAllParams ()) == 3 )
19
19
{
20
20
$ actionName =Zend_Controller_Front::getInstance ()->getRequest ()->getActionName ();
@@ -32,29 +32,134 @@ public function indexAction()
32
32
$ keyword = $ this ->getRequest ()->getParam ('q ' );
33
33
$ this ->view ->json ['search ' ]['keyword ' ] = $ keyword ;
34
34
35
+ $ ajax =$ this ->_getParam ('ajax ' );
36
+ $ order =$ this ->_getParam ('order ' );
37
+ if (!isset ($ order ))
38
+ {
39
+ $ order ='view ' ;
40
+ }
35
41
// Get the items corresponding to the search
36
- $ ItemsDao = $ this ->ItemKeyword ->getItemsFromSearch ($ keyword ,$ this ->userSession ->Dao );
37
- $ this ->view ->items =$ ItemsDao ;
42
+ $ ItemsDao = $ this ->ItemKeyword ->getItemsFromSearch ($ keyword ,$ this ->userSession ->Dao ,200 ,false ,$ order );
38
43
39
44
// Search for the folders
40
- $ FoldersDao = $ this ->Folder ->getFoldersFromSearch ($ keyword ,$ this ->userSession ->Dao );
41
- $ this ->view ->folders =$ FoldersDao ;
45
+ $ FoldersDao = $ this ->Folder ->getFoldersFromSearch ($ keyword ,$ this ->userSession ->Dao ,15 ,false ,$ order );
42
46
43
47
// Search for the communities
44
- $ CommunitiesDao = $ this ->Community ->getCommunitiesFromSearch ($ keyword ,$ this ->userSession ->Dao );
45
- $ this ->view ->communities =$ CommunitiesDao ;
48
+ $ CommunitiesDao = $ this ->Community ->getCommunitiesFromSearch ($ keyword ,$ this ->userSession ->Dao ,15 ,false ,$ order );
46
49
47
50
// Search for the users
48
- $ UsersDao = $ this ->User ->getUsersFromSearch ($ keyword ,$ this ->userSession ->Dao );
49
- $ this ->view ->users =$ UsersDao ;
50
- }
51
+ $ UsersDao = $ this ->User ->getUsersFromSearch ($ keyword ,$ this ->userSession ->Dao ,15 ,false ,$ order );
52
+
53
+ $ results =$ this ->formatResults ($ order , $ ItemsDao , $ FoldersDao , $ CommunitiesDao , $ UsersDao );
54
+
55
+ if (isset ($ ajax ))
56
+ {
57
+ $ this ->_helper ->layout ->disableLayout ();
58
+ $ this ->_helper ->viewRenderer ->setNoRender ();
59
+ echo JsonComponent::encode ($ results );
60
+ }
61
+ else
62
+ {
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 ;
68
+ $ this ->view ->json ['search ' ]['keyword ' ] = $ keyword ;
69
+ $ this ->view ->json ['search ' ]['noResults ' ] = $ this ->t ('No result found. ' );
70
+ $ this ->view ->json ['search ' ]['moreResults ' ] = $ this ->t ('Show more results. ' );
71
+ }
72
+ }//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
147
+
148
+ /** advanced search Action */
149
+ public function advancedAction ()
150
+ {
151
+ $ this ->_helper ->layout ->disableLayout ();
152
+ }//advancedAction
51
153
52
154
/** search live Action */
53
155
public function liveAction ()
54
156
{
55
157
// This is necessary in order to avoid session lock and being able to run two
56
158
// ajax requests simultaneously
57
159
session_write_close ();
160
+
161
+ $ this ->_helper ->layout ->disableLayout ();
162
+ $ this ->_helper ->viewRenderer ->setNoRender ();
58
163
59
164
$ search = $ this ->getRequest ()->getParam ('term ' );
60
165
@@ -242,7 +347,6 @@ public function liveAction()
242
347
}
243
348
244
349
echo '] ' ;
245
- exit ();
246
350
}
247
351
248
352
} // end class
0 commit comments