[ Index ] |
|
Code source de eGroupWare 1.2.106-2 |
1 <?php 2 /***************************************************************************\ 3 * eGroupWare - File Manager * 4 * http://www.egroupware.org * 5 * Written by: * 6 * - Vinicius Cubas Brand <viniciuscb@users.sourceforge.net> * 7 * sponsored by Thyamad - http://www.thyamad.com * 8 * ------------------------------------------------------------------------- * 9 * Description: Custom Field class handler for VFS (SQL implementation v2) * 10 * ------------------------------------------------------------------------- * 11 * This program is free software; you can redistribute it and/or modify it * 12 * under the terms of the GNU General Public License as published by the * 13 * Free Software Foundation; either version 2 of the License, or (at your * 14 * option) any later version. * 15 \***************************************************************************/ 16 17 class vfs_customfields 18 { 19 var $db; 20 21 function vfs_customfields() 22 { 23 $this->db = clone($GLOBALS['phpgw']->db); 24 25 } 26 27 # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- # 28 # Functions to associate customfields to a file # 29 # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- # 30 31 32 //$file_id: the id of the file 33 function & get_fields_by_fileid($file_id) 34 { 35 if (!is_numeric($file_id) || !(((int)$file_id)==$file_id)) 36 return false; 37 38 $sql = "SELECT cf.customfield_name as name, 39 cfd.data as data 40 FROM phpgw_vfs2_customfields as cf, 41 phpgw_vfs2_customfields_data as cfd 42 WHERE cf.customfield_id = cfd.customfield_id 43 AND cf.customfield_active = 'Y' 44 AND cfd.file_id = $file_id"; 45 46 $this->db->query($sql,__LINE__,__FILE__); 47 48 while($this->db->next_record()) 49 { 50 $result[$this->db->Record['name']] = $this->db->Record['data']; 51 } 52 53 return $result; 54 55 } 56 57 //$data = array(file_id => array('field1'=> 'value', ....), 58 // file_id2 => array(...) ... ) 59 // Store will do insert or update, depending if the field exists or 60 // not. 61 function store_fields($data) 62 { 63 if (!is_array($data)) 64 return false; 65 66 $fields = $this->get_customfields('customfield_name'); 67 68 foreach($data as $file_id => $file_data) 69 { 70 foreach($file_data as $name => $value) 71 { 72 //Column type does not exists 73 if (!array_key_exists($name,$fields)) 74 { 75 //TODO ERROR HANDLING 76 continue; 77 } 78 79 $this->db->insert('phpgw_vfs2_customfields_data', 80 array('data' => $value), 81 array('file_id'=>$file_id,'customfield_id'=>$fields[$name]['customfield_id']), 82 __LINE__,__FILE__); 83 84 } 85 } 86 return true; 87 } 88 89 //$data = array('file_id' => array('field1','field2',...), 90 // 'fileid2' => ... ) 91 function delete_fields($data) 92 { 93 94 $fields = $this->get_customfields('customfield_name'); 95 96 foreach($data as $file_id => $file_fields) 97 { 98 foreach($file_fields as $column_name) 99 { 100 if (!array_key_exists($column_name,$fields)) 101 { 102 //TODO ERROR HANDLING 103 continue; 104 } 105 106 $this->db->delete('phpgw_vfs2_customfields_data', 107 array('file_id' => $file_id,'customfield_id'=>$fields[$column_name]['customfield_id']),__LINE__,__FILE__); 108 } 109 } 110 } 111 112 /* Search the files that have $keyword in any field, or in the fields 113 specified in the array fields 114 @param $fields array('field1','fields2',...)*/ 115 function search_files($keyword,$fields=null) 116 { 117 118 $where = ''; 119 120 if ($fields) 121 { 122 $customfields = $this->get_customfields('customfield_name'); 123 124 foreach ($fields as $field) 125 { 126 if ($customfields[$field]) 127 { 128 $cf_ids[] = 'customfield_id='.$customfields[$field]['customfield_id']; 129 } 130 } 131 132 if ($cf_ids) 133 { 134 $where = implode(' OR ',$cf_ids); 135 136 $where = 'AND ('.$where.')'; 137 } 138 139 } 140 141 142 143 $sql = "SELECT file_id 144 FROM phpgw_vfs2_customfields_data 145 WHERE data LIKE '%$keyword%' 146 $where"; 147 148 $this->db->query($sql,__LINE__,__FILE__); 149 150 $res = array(); 151 while($this->db->next_record()) 152 { 153 $res[] = $this->db->Record['file_id']; 154 } 155 return $res; 156 } 157 158 # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- # 159 # Functions to manage custom field types # 160 # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- # 161 162 //Gets all custom field types, indexed by $indexedby. 163 //Example: array( 'customfieldname1' => array('customfield_id'=>x,'customfield_description'=>y,'type'=>z...)...) 164 //@param bool $activeonly If true, brings only active fields 165 function get_customfields($indexedby='customfield_name',$activeonly=true) 166 { 167 $where=($activeonly)?array('customfield_active'=>'Y'):array(); 168 169 $this->db->select('phpgw_vfs2_customfields','*',$where, 170 __LINE__,__FILE__); 171 172 $result = array(); 173 while($this->db->next_record()) 174 { 175 $result[] = $this->db->Record; 176 } 177 178 if (!is_array($result[0]) || !array_key_exists($indexedby,$result[0])) 179 { 180 $indexedby = 'customfield_name'; 181 } 182 183 $result2 = array(); 184 foreach($result as $key => $val) 185 { 186 $result2[$val[$indexedby]] = $val; 187 } 188 189 return $result2; 190 } 191 192 //samething that $this->get_customfields, but returns an array in the 193 //format array('name' => 'description', 'name2'=>'desc2',...) 194 function get_attributes($activeonly=true) 195 { 196 $where=($activeonly)?array('customfield_active'=>'Y'):array(); 197 198 $this->db->select('phpgw_vfs2_customfields','*',$where, 199 __LINE__,__FILE__); 200 201 while($this->db->next_record()) 202 { 203 $result[] = $this->db->Record; 204 } 205 206 $result2 = array(); 207 foreach($result as $key => $val) 208 { 209 $result2[$val['customfield_name']] = $val['customfield_description']; 210 } 211 212 return $result2; 213 } 214 215 216 217 //Add a new type of custom field 218 //$type can be of the same possible types for egw 219 function add_customfield($name,$description,$type,$precision='',$active='N') 220 { 221 $active = strtoupper($active); 222 223 $res = $this->db->insert('phpgw_vfs2_customfields',array( 224 'customfield_name' => $name, 225 'customfield_description' => $description, 226 'customfield_type' => $type, 227 'customfield_precision' => $precision, 228 'customfield_active' => $active ), 229 __LINE__,__FILE__); 230 231 if ($res) 232 { 233 return true; 234 } 235 return false; 236 } 237 238 //Update a customfield type (customfield_name, customfield_description, type, precision, active) 239 function update_customfield($customfield_id,$data) 240 { 241 if (!is_numeric($customfield_id) || !(((int)$customfield_id)==$customfield_id)) 242 { 243 return false; 244 } 245 246 if ($data['customfield_active']) 247 { 248 $data['customfield_active'] = strtoupper($data['customfield_active']); 249 } 250 251 if ($this->db->update('phpgw_vfs2_customfields',$data,array('customfield_id'=>$customfield_id),__LINE__,__FILE__)) 252 { 253 return true; 254 } 255 return false; 256 } 257 258 //generally better inactivate a field than remove. 259 function remove_customfield($customfield_id) 260 { 261 $res = $this->db->delete('phpgw_vfs2_customfields',array('customfield_id'=>$customfield_id),__LINE__,__FILE__); 262 263 if ($res) 264 { 265 $res2 = $this->db->delete('phpgw_vfs2_customfields_data',array('customfield_id'=>$customfield_id),__LINE__,__FILE__); 266 267 if ($res2) 268 { 269 return true; 270 } 271 } 272 return false; 273 } 274 275 } 276 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 17:20:01 2007 | par Balluche grâce à PHPXref 0.7 |