Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
Clipboard.php
Go to the documentation of this file.
1 <?php
2 import('Dataface/AuthenticationTool.php');
3 import('Dataface/IO.php');
4 define('Dataface_Clipboard_tablename', '_df_clipboard');
5 define('Dataface_Clipboard_lifetime', 1800);
6 define('Dataface_Clipboard_threshold', 20);
7 define('Dataface_Clipboard_clipboard_id_key', '_df_clipboard_id');
8 
14 
15  var $id;
16  var $errors;
17  var $warnings;
18  var $messages;
19 
21  $this->id = $id;
22  }
23 
28  function isInstalled(){
29  static $isInstalled = -1;
30  if ( $isInstalled == -1 ){
31 
33  $isInstalled = ( mysql_num_rows(mysql_query("show tables like '".Dataface_Clipboard_tablename."'", $app->db())) == 0 );
34  }
35  return $isInstalled;
36  }
37 
43  function install(){
46  mysql_query(
47  "CREATE TABLE `".Dataface_Clipboard_tablename."` (
48  `clipid` INT(11) auto_increment NOT NULL,
49  `clipperid` VARCHAR(32) NOT NULL,
50  `cut` TINYINT(1) DEFAULT 0,
51  `recordids` TEXT,
52  `lastmodified` datetime,
53  PRIMARY KEY (`clipid`),
54  UNIQUE (`clipperid`))", $app->db()) or trigger_error("Failed to create clipboard table: ".mysql_error($app->db()), E_USER_ERROR);
55  return true;
56  }
57  return false;
58 
59  }
60 
61 
66  function clean(){
68  mysql_query("delete from `".Dataface_Clipboard_tablename."` where UNIX_TIMESTAMP(`lastmodified`) > ".(time()-Dataface_Clipboard_lifetime),
69  $app->db()) or trigger_error("Failed to clean old data from the clipboard: ".mysql_error($app->db()), E_USER_ERROR);
70  }
71 
72 
80  function shotgunClean(){
81  if ( rand(0,100) > Dataface_Clipboard_threshold ){
83  }
84  }
85 
86 
94  public static function &getInstance(){
95  static $clipboard = 0;
96  if ( $clipboard == 0 ){
97  // we need to get an id for the clipboard
99  $username = $auth->getLoggedInUsername();
100  if ( isset($username) ) $id = $username;
101  else {
102  if ( @session_id() ){
103  if ( isset($_SESSION[Dataface_Clipboard_clipboard_id_key]) ){
105  } else {
106  $id = md5(rand(0,10000000));
108  }
109  } else {
110  $err= PEAR::raiseError("No clipboard is available because the user is not logged in and sessions are not enabled.");
111  return $err;
112  }
113  }
114 
115  $clipboard = new Dataface_Clipboard($id);
116 
117  }
118  return $clipboard;
119  }
120 
125  function empty(){
126  return (mysql_num_rows(mysql_query("select count(*) from `".Dataface_Clipboard_tablename."` where `clipperid`='".addslashes($this->id)."'")) == 0);
127  }
128 
129 
141  function copy($recordids){
142  $this->clearLogs();
145  $res = mysql_query(
146  "REPLACE INTO `".Dataface_Clipboard_tablename."`
147  (`clipperid`,`cut`,`recordids`,`lastmodified`)
148  VALUES
149  ('".addslashes($this->id)."',
150  0,'".addslashes(implode("\n",$recordids))."', NOW()
151  )", $app->db();
152  if ( !$res ){
153  return PEAR::raiseError(mysql_error($app->db()));
154  }
155  return true;
156 
157  }
158 
167  function cut($recordids){
168  $this->clearLogs();
171  $res = mysql_query(
172  "REPLACE INTO `".Dataface_Clipboard_tablename."`
173  (`clipperid`,`cut`,`recordids`,`lastmodified`)
174  VALUES
175  ('".addslashes($this->id)."',
176  1,'".addslashes(implode("\n",$recordids))."', NOW()
177  )", $app->db();
178  if ( !$res ){
179  return PEAR::raiseError(mysql_error($app->db()));
180  }
181  return true;
182  }
183 
196  function paste($destid, $relationship=null){
197  $this->clearLogs();
200  $res = mysql_query("SELECT * FROM `".Dataface_Clipboard_tablename."` where `clipperid`='".addslashes($this->id)."'", $app->db());
201  if ( mysql_num_rows($res)>0 ){
202  $row = mysql_fetch_assoc($res);
203  } else {
204  return PEAR::raiseError('The clipboard is empty.');
205  }
206 
207  $dest =& Dataface_IO::loadRecordById($destid);
208  // The destination record.
209 
210  if ( !isset( $dest ) ) return PEAR::raiseError('The destination "'.$destid.'" could not be found to paste the clipboard contents.');
211  if ( is_a($dest, 'Dataface_RelatedRecord') ){
212  // we don't paste into related records... so lets turn this into a Dataface_Record object
213  $destrecord =& $dest->toRecord();
214  unset($dest);
215  $dest =& $destrecord;
216  unset($destrecord);
217  }
218 
219  if ( !isset($relationship) ){
220  $rel =& $dest->_table->getChildrenRelationship();
221  if ( !isset($rel) ){
222  return PEAR::raiseError('No relationship was specified into which to paste the contents of the clipboard.');
223  } else {
224  $relationship = $rel->getName();
225  unset($rel);
226  }
227  }
228 
229  $io = new Dataface_IO($dest->_table->tablename);
230 
231  // $row should now contain the clipboard contents.
232  $recordids = explode("\n", $row['recordids']);
233  foreach ( $recordids as $recordid ){
234  $record =& Dataface_IO::loadRecordById($recordid);
235  if ( is_a($record, 'Dataface_Record')){
236  $io2 = new Dataface_IO($record->_table->tablename);
237  } else {
238  $io2 = new Dataface_IO($record->_record->_table);
239  }
240 
241  if ( isset($record) and !PEAR::isError($record) ){
242  // the record id was loaded successfully
243  $newrecord = new Dataface_RelatedRecord($dest, $relationship, $record->vals());
244  $io->addExistingRelatedRecord($newrecord);
245 
246  if ( $row['cut'] ){
247  // This was cut from the clipboard so we need to remove the original
248  $res = $io2->removeRelatedRecord($record, false);
249  if ( PEAR::isError($res) ){
250  if ( Dataface_Error::isWarning($res) ) $this->logWarning($res);
251  else $this->logError($res);
252  }
253  }
254  }
255 
256  unset($record);
257  unset($newrecord);
258  unset($io2);
259  }
260 
261 
262  }
263 
264 
265 
266  function logError($error){
267  $this->errors[] = $error;
268  }
269 
270  function logWarning($warning){
271  $this->warnings[] = $warning;
272  }
273 
274  function logMessage($message){
275  $this->messages[] = $message;
276  }
277 
278  function clearLogs(){
279  $this->errors = array();
280  $this->warnings = array();
281  $this->messages = array();
282  }
283 
284 }