2012-09-06 15:27:20 +02:00
< ? php
class ModelFolderFolder extends Model {
public function get_folders ( $search = '' , $page = 0 , $page_len = 0 , $sort = 'name' , $order = 0 ) {
$where_cond = '' ;
$_order = " " ;
$groups = array ();
$Q = array ();
$limit = " " ;
$from = ( int ) $page * ( int ) $page_len ;
$search = preg_replace ( " / \ s { 1,}/ " , " " , $search ) . '%' ;
if ( $search ){
$where_cond .= " WHERE `name` like ? " ;
array_push ( $Q , $search );
}
/* sort order */
if ( $order == 0 ) { $order = " ASC " ; }
else { $order = " DESC " ; }
$_order = " ORDER BY ` $sort ` $order " ;
if ( $page_len > 0 ) { $limit = " LIMIT " . ( int ) $from . " , " . ( int ) $page_len ; }
$query = $this -> db -> query ( " SELECT `id`, `name` FROM ` " . TABLE_FOLDER . " ` $where_cond $_order $limit " , $Q );
foreach ( $query -> rows as $q ) {
$groups [] = array (
'id' => $q [ 'id' ],
'foldername' => $q [ 'name' ]
);
}
return $groups ;
}
2015-09-03 14:41:52 +02:00
public function get_top_level_folders () {
$folders = array ();
$query = $this -> db -> query ( " SELECT `id`, `name` FROM ` " . TABLE_FOLDER . " ` WHERE parent_id=0 " );
foreach ( $query -> rows as $q ) {
$folders [ $q [ 'id' ]] = $q [ 'name' ];
}
return $folders ;
}
2012-09-06 15:27:20 +02:00
public function get_folders_for_user () {
2013-11-18 19:24:33 +01:00
$session = Registry :: get ( 'session' );
2020-12-21 21:52:16 +01:00
$folders = $session -> get ( " folders " );
2013-11-18 19:24:33 +01:00
2020-12-21 21:52:16 +01:00
$q = str_repeat ( " ?, " , count ( $folders ));
2012-09-06 15:27:20 +02:00
$q = preg_replace ( " / \ , $ / " , " " , $q );
2020-12-21 21:52:16 +01:00
$query = $this -> db -> query ( " SELECT `id`, `name` FROM ` " . TABLE_FOLDER . " ` WHERE id IN ( $q ) " , $folders );
2012-09-06 15:27:20 +02:00
2020-12-21 21:52:16 +01:00
if ( isset ( $query -> rows )) {
$arr = $query -> rows ;
if ( in_array ( 0 , $folders , TRUE )) {
array_unshift ( $arr , array ( 'id' => 0 , 'name' => '---' ));
}
return $arr ;
}
2012-09-06 15:27:20 +02:00
return array ();
}
2015-09-18 14:56:09 +02:00
private function is_your_folder ( $folder_id = 0 ) {
2013-11-18 19:24:33 +01:00
$session = Registry :: get ( 'session' );
2015-09-18 14:56:09 +02:00
if ( isAuditorUser () == 1 ) { return 1 ; }
2012-09-15 15:30:35 +02:00
2015-09-18 14:56:09 +02:00
$query = $this -> db -> query ( " SELECT f.id AS id FROM ` " . TABLE_FOLDER . " ` f, " . TABLE_FOLDER_USER . " ` fu WHERE f.id=fu.id AND fu.uid=? AND f.id=? " , array ( $session -> get ( " uid " ), $folder_id ));
2012-09-15 15:30:35 +02:00
if ( isset ( $query -> row [ 'id' ])) { return 1 ; }
return 0 ;
}
public function copy_message_to_folder_by_id ( $folder_id = 0 , $meta_id = 0 ) {
2015-09-18 14:56:09 +02:00
if ( ! $this -> is_your_folder ( $folder_id )) { return - 1 ; }
2012-09-15 15:30:35 +02:00
$query = $this -> db -> query ( " INSERT INTO " . TABLE_FOLDER_MESSAGE . " (folder_id, id) VALUES(?,?) " , array ( $folder_id , $meta_id ));
return $this -> db -> countAffected ();
}
2015-09-18 14:56:09 +02:00
public function get_folder_id_array_for_user ( $uid = 0 , $is_admin = 0 ) {
2012-09-06 15:27:20 +02:00
$arr = array ();
2020-12-21 21:52:16 +01:00
$add_folder0 = ( $is_admin == 0 );
2012-09-06 15:27:20 +02:00
2020-12-21 21:52:16 +01:00
$query = $this -> db -> query ( " SELECT id FROM ` " . TABLE_FOLDER_USER . " ` WHERE uid=? " , array ( $uid ));
if ( $is_admin == 2 && ( ! isset ( $query -> rows ) || count ( $query -> rows ) == 0 )) {
2015-09-18 14:56:09 +02:00
$query = $this -> db -> query ( " SELECT id FROM ` " . TABLE_FOLDER );
2020-12-21 21:52:16 +01:00
$add_folder0 = 1 ;
2015-09-18 14:56:09 +02:00
}
2012-09-06 15:27:20 +02:00
if ( isset ( $query -> rows )) {
foreach ( $query -> rows as $q ) {
array_push ( $arr , $q [ 'id' ]);
2012-09-11 14:55:23 +02:00
$this -> get_sub_folders ( $q [ 'id' ], $arr );
2012-09-06 15:27:20 +02:00
}
}
2020-12-21 21:52:16 +01:00
if ( $add_folder0 == 1 ) {
array_unshift ( $arr , 0 );
}
2012-09-06 15:27:20 +02:00
return $arr ;
}
2012-09-15 22:49:38 +02:00
public function get_all_folder_ids_hier ( $uid = 0 ) {
$arr = array ();
$a = array ();
$query = $this -> db -> query ( " SELECT " . TABLE_FOLDER_USER . " .id AS id, " . TABLE_FOLDER . " .name AS name FROM " . TABLE_FOLDER_USER . " , " . TABLE_FOLDER . " WHERE uid=? AND " . TABLE_FOLDER_USER . " .id= " . TABLE_FOLDER . " .id " , array ( $uid ));
if ( isset ( $query -> rows )) {
foreach ( $query -> rows as $q ) {
$a = array ( 'id' => $q [ 'id' ], 'name' => $q [ 'name' ], 'children' => array ());
$a [ 'children' ] = $this -> get_sub_folders_hier ( $q [ 'id' ]);
array_push ( $arr , $a );
}
}
return $arr ;
}
2012-09-11 14:55:23 +02:00
private function get_sub_folders ( $id = 0 , & $arr = array ()) {
2012-09-06 15:27:20 +02:00
$query = $this -> db -> query ( " SELECT id FROM ` " . TABLE_FOLDER . " ` WHERE parent_id=? " , array ( $id ));
if ( isset ( $query -> rows )) {
foreach ( $query -> rows as $q ) {
array_push ( $arr , $q [ 'id' ]);
2012-09-11 14:55:23 +02:00
$this -> get_sub_folders ( $q [ 'id' ], $arr );
2012-09-06 15:27:20 +02:00
}
}
}
2012-09-15 22:49:38 +02:00
private function get_sub_folders_hier ( $id = 0 ) {
$arr = array ();
$query = $this -> db -> query ( " SELECT id, name FROM ` " . TABLE_FOLDER . " ` WHERE parent_id=? " , array ( $id ));
if ( $query -> num_rows > 0 ) {
foreach ( $query -> rows as $q ) {
$a = array ( 'id' => $q [ 'id' ], 'name' => $q [ 'name' ], 'children' => array ());
$a [ 'children' ] = $this -> get_sub_folders_hier ( $q [ 'id' ]);
array_push ( $arr , $a );
}
}
return $arr ;
}
2012-09-06 15:27:20 +02:00
public function get_folders_by_string ( $s = '' ) {
if ( strlen ( $s ) < 2 ) { return array (); }
2012-09-11 15:12:58 +02:00
$query = $this -> db -> query ( " SELECT name FROM ` " . TABLE_FOLDER . " ` WHERE parent_id=0 AND name LIKE ? ORDER BY name ASC " , array ( $s . " % " ) );
2012-09-06 15:27:20 +02:00
if ( isset ( $query -> rows )) { return $query -> rows ; }
return array ();
}
public function get_folders_by_uid ( $uid = 0 ) {
$folders = '' ;
$query = $this -> db -> query ( " SELECT ` " . TABLE_FOLDER_USER . " `.id, name FROM ` " . TABLE_FOLDER_USER . " `, ` " . TABLE_FOLDER . " ` WHERE ` " . TABLE_FOLDER_USER . " `.id=` " . TABLE_FOLDER . " `.id AND uid=? " , array ( $uid ) );
if ( isset ( $query -> rows )) {
foreach ( $query -> rows as $q ) { $folders .= " \n " . $q [ 'name' ]; }
}
return preg_replace ( " /^ \n / " , " " , $folders );
}
2015-10-02 13:19:40 +02:00
public function get_folder_by_id ( $id = 0 ) {
$query = $this -> db -> query ( " SELECT * FROM ` " . TABLE_FOLDER . " ` WHERE id=? " , array ( $id ));
if ( isset ( $query -> row )) { return $query -> row ; }
return array ();
}
public function update_folder ( $data = array ()) {
$rc = 0 ;
if ( isset ( $data [ 'id' ]) && isset ( $data [ 'name' ])) {
$query = $this -> db -> query ( " UPDATE ` " . TABLE_FOLDER . " ` SET name=? WHERE id=? " , array ( $data [ 'name' ], $data [ 'id' ]));
$rc = $this -> db -> countAffected ();
}
return $rc ;
}
2015-09-18 14:56:09 +02:00
public function get_folder_id_by_id ( $id = 0 ) {
$query = $this -> db -> query ( " SELECT folder_id FROM ` " . TABLE_FOLDER_MESSAGE . " ` WHERE id=? " , array ( $id ));
if ( isset ( $query -> row )) { return $query -> row [ 'folder_id' ]; }
return 0 ;
}
public function update_message_folder ( $id = 0 , $folder_id = 0 ) {
$query = $this -> db -> query ( " UPDATE ` " . TABLE_FOLDER_MESSAGE . " ` SET folder_id=? WHERE id=? " , array ( $folder_id , $id ));
2020-12-21 22:35:09 +01:00
$query = $this -> sphx -> query ( " UPDATE " . SPHINX_MAIN_INDEX . " SET folder= $folder_id WHERE id= $id " );
2015-09-18 14:56:09 +02:00
}
public function add_folder ( $name = '' ) {
2012-09-15 15:30:35 +02:00
if ( $name == '' ) { return - 1 ; }
2013-11-18 19:24:33 +01:00
$session = Registry :: get ( 'session' );
2015-09-18 14:56:09 +02:00
$query = $this -> db -> query ( " INSERT INTO " . TABLE_FOLDER . " (name) VALUES(?) " , array ( $name ));
2013-03-31 14:50:21 +02:00
2015-09-18 14:56:09 +02:00
if ( isAuditorUser () == 0 ) {
$last_id = $this -> db -> getLastId ();
$query = $this -> db -> query ( " INSERT INTO " . TABLE_FOLDER_USER . " (id, uid) VALUES(?,?) " , array ( $last_id , $session -> get ( " uid " )));
2013-03-31 14:50:21 +02:00
2020-10-30 21:19:20 +01:00
$folders = $this -> get_folder_id_array_for_user ( $session -> get ( " uid " ), $session -> get ( " admin_user " ));
$session -> set ( " folders " , $folders );
2015-09-18 14:56:09 +02:00
}
2013-03-31 14:50:21 +02:00
2012-09-15 15:30:35 +02:00
return $this -> db -> countAffected ();
}
2015-09-18 14:56:09 +02:00
public function remove_folder ( $id = 0 ) {
if ( $id <= 0 ) { return - 1 ; }
2012-09-15 15:30:35 +02:00
2013-11-18 19:24:33 +01:00
$session = Registry :: get ( 'session' );
2015-09-18 14:56:09 +02:00
if ( $this -> is_your_folder ( $id ) == 1 ) {
$query = $this -> db -> query ( " DELETE FROM " . TABLE_FOLDER . " WHERE id=? " , array ( $id ));
$query = $this -> db -> query ( " DELETE FROM " . TABLE_FOLDER_USER . " WHERE id=? AND uid=? " , array ( $id , $session -> get ( " uid " )));
2020-10-30 21:19:20 +01:00
$folders = $this -> get_folder_id_array_for_user ( $session -> get ( " uid " ), $session -> get ( " admin_user " ));
$session -> set ( " folders " , $folders );
2015-09-18 14:56:09 +02:00
// shall we delete the existing message - folder id assignments from folder_message?
2012-09-15 15:30:35 +02:00
}
return 0 ;
}
2012-09-06 15:27:20 +02:00
}