![]() |
Xataface 2.0
Xataface Application Framework
|
00001 <?php 00002 import('Dataface/AuthenticationTool.php'); 00003 import('Dataface/IO.php'); 00004 define('Dataface_Clipboard_tablename', '_df_clipboard'); 00005 define('Dataface_Clipboard_lifetime', 1800); 00006 define('Dataface_Clipboard_threshold', 20); 00007 define('Dataface_Clipboard_clipboard_id_key', '_df_clipboard_id'); 00008 00013 class Dataface_Clipboard { 00014 00015 var $id; 00016 var $errors; 00017 var $warnings; 00018 var $messages; 00019 00020 function Dataface_Clipboard($id){ 00021 $this->id = $id; 00022 } 00023 00028 function isInstalled(){ 00029 static $isInstalled = -1; 00030 if ( $isInstalled == -1 ){ 00031 00032 $app =& Dataface_Application::getInstance(); 00033 $isInstalled = ( mysql_num_rows(mysql_query("show tables like '".Dataface_Clipboard_tablename."'", $app->db())) == 0 ); 00034 } 00035 return $isInstalled; 00036 } 00037 00043 function install(){ 00044 if ( !Dataface_Clipboard::isInstalled() ){ 00045 $app =& Dataface_Application::getInstance(); 00046 mysql_query( 00047 "CREATE TABLE `".Dataface_Clipboard_tablename."` ( 00048 `clipid` INT(11) auto_increment NOT NULL, 00049 `clipperid` VARCHAR(32) NOT NULL, 00050 `cut` TINYINT(1) DEFAULT 0, 00051 `recordids` TEXT, 00052 `lastmodified` datetime, 00053 PRIMARY KEY (`clipid`), 00054 UNIQUE (`clipperid`))", $app->db()) or trigger_error("Failed to create clipboard table: ".mysql_error($app->db()), E_USER_ERROR); 00055 return true; 00056 } 00057 return false; 00058 00059 } 00060 00061 00066 function clean(){ 00067 $app =& Dataface_Application::getInstance(); 00068 mysql_query("delete from `".Dataface_Clipboard_tablename."` where UNIX_TIMESTAMP(`lastmodified`) > ".(time()-Dataface_Clipboard_lifetime), 00069 $app->db()) or trigger_error("Failed to clean old data from the clipboard: ".mysql_error($app->db()), E_USER_ERROR); 00070 } 00071 00072 00080 function shotgunClean(){ 00081 if ( rand(0,100) > Dataface_Clipboard_threshold ){ 00082 Dataface_Clipboard::clean(); 00083 } 00084 } 00085 00086 00094 public static function &getInstance(){ 00095 static $clipboard = 0; 00096 if ( $clipboard == 0 ){ 00097 // we need to get an id for the clipboard 00098 $auth =& Dataface_AuthenticationTool::getInstance(); 00099 $username = $auth->getLoggedInUsername(); 00100 if ( isset($username) ) $id = $username; 00101 else { 00102 if ( @session_id() ){ 00103 if ( isset($_SESSION[Dataface_Clipboard_clipboard_id_key]) ){ 00104 $id = $_SESSION[Dataface_Clipboard_clipboard_id_key]; 00105 } else { 00106 $id = md5(rand(0,10000000)); 00107 $_SESSION[Dataface_Clipboard_clipboard_id_key] = $id; 00108 } 00109 } else { 00110 $err= PEAR::raiseError("No clipboard is available because the user is not logged in and sessions are not enabled."); 00111 return $err; 00112 } 00113 } 00114 00115 $clipboard = new Dataface_Clipboard($id); 00116 00117 } 00118 return $clipboard; 00119 } 00120 00125 function empty(){ 00126 return (mysql_num_rows(mysql_query("select count(*) from `".Dataface_Clipboard_tablename."` where `clipperid`='".addslashes($this->id)."'")) == 0); 00127 } 00128 00129 00141 function copy($recordids){ 00142 $this->clearLogs(); 00143 $app =& Dataface_Application::getInstance(); 00144 Dataface_Clipboard::shotgunClean(); 00145 $res = mysql_query( 00146 "REPLACE INTO `".Dataface_Clipboard_tablename."` 00147 (`clipperid`,`cut`,`recordids`,`lastmodified`) 00148 VALUES 00149 ('".addslashes($this->id)."', 00150 0,'".addslashes(implode("\n",$recordids))."', NOW() 00151 )", $app->db(); 00152 if ( !$res ){ 00153 return PEAR::raiseError(mysql_error($app->db())); 00154 } 00155 return true; 00156 00157 } 00158 00167 function cut($recordids){ 00168 $this->clearLogs(); 00169 $app =& Dataface_Application::getInstance(); 00170 Dataface_Clipboard::shotgunClean(); 00171 $res = mysql_query( 00172 "REPLACE INTO `".Dataface_Clipboard_tablename."` 00173 (`clipperid`,`cut`,`recordids`,`lastmodified`) 00174 VALUES 00175 ('".addslashes($this->id)."', 00176 1,'".addslashes(implode("\n",$recordids))."', NOW() 00177 )", $app->db(); 00178 if ( !$res ){ 00179 return PEAR::raiseError(mysql_error($app->db())); 00180 } 00181 return true; 00182 } 00183 00196 function paste($destid, $relationship=null){ 00197 $this->clearLogs(); 00198 $app =& Dataface_Application::getInstance(); 00199 Dataface_Clipboard::shotgunClean(); 00200 $res = mysql_query("SELECT * FROM `".Dataface_Clipboard_tablename."` where `clipperid`='".addslashes($this->id)."'", $app->db()); 00201 if ( mysql_num_rows($res)>0 ){ 00202 $row = mysql_fetch_assoc($res); 00203 } else { 00204 return PEAR::raiseError('The clipboard is empty.'); 00205 } 00206 00207 $dest =& Dataface_IO::loadRecordById($destid); 00208 // The destination record. 00209 00210 if ( !isset( $dest ) ) return PEAR::raiseError('The destination "'.$destid.'" could not be found to paste the clipboard contents.'); 00211 if ( is_a($dest, 'Dataface_RelatedRecord') ){ 00212 // we don't paste into related records... so lets turn this into a Dataface_Record object 00213 $destrecord =& $dest->toRecord(); 00214 unset($dest); 00215 $dest =& $destrecord; 00216 unset($destrecord); 00217 } 00218 00219 if ( !isset($relationship) ){ 00220 $rel =& $dest->_table->getChildrenRelationship(); 00221 if ( !isset($rel) ){ 00222 return PEAR::raiseError('No relationship was specified into which to paste the contents of the clipboard.'); 00223 } else { 00224 $relationship = $rel->getName(); 00225 unset($rel); 00226 } 00227 } 00228 00229 $io = new Dataface_IO($dest->_table->tablename); 00230 00231 // $row should now contain the clipboard contents. 00232 $recordids = explode("\n", $row['recordids']); 00233 foreach ( $recordids as $recordid ){ 00234 $record =& Dataface_IO::loadRecordById($recordid); 00235 if ( is_a($record, 'Dataface_Record')){ 00236 $io2 = new Dataface_IO($record->_table->tablename); 00237 } else { 00238 $io2 = new Dataface_IO($record->_record->_table); 00239 } 00240 00241 if ( isset($record) and !PEAR::isError($record) ){ 00242 // the record id was loaded successfully 00243 $newrecord = new Dataface_RelatedRecord($dest, $relationship, $record->vals()); 00244 $io->addExistingRelatedRecord($newrecord); 00245 00246 if ( $row['cut'] ){ 00247 // This was cut from the clipboard so we need to remove the original 00248 $res = $io2->removeRelatedRecord($record, false); 00249 if ( PEAR::isError($res) ){ 00250 if ( Dataface_Error::isWarning($res) ) $this->logWarning($res); 00251 else $this->logError($res); 00252 } 00253 } 00254 } 00255 00256 unset($record); 00257 unset($newrecord); 00258 unset($io2); 00259 } 00260 00261 00262 } 00263 00264 00265 00266 function logError($error){ 00267 $this->errors[] = $error; 00268 } 00269 00270 function logWarning($warning){ 00271 $this->warnings[] = $warning; 00272 } 00273 00274 function logMessage($message){ 00275 $this->messages[] = $message; 00276 } 00277 00278 function clearLogs(){ 00279 $this->errors = array(); 00280 $this->warnings = array(); 00281 $this->messages = array(); 00282 } 00283 00284 }