ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilChatroomSmilies.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 
14 {
15 
21  private static function _setupDatabase()
22  {
23  global $ilDB;
24 
25  $fields = array(
26  'smiley_id' => array(
27  'type' => 'integer',
28  'length' => 4,
29  ),
30  'smiley_keywords' => array(
31  'type' => 'text',
32  'length' => 100,
33  ),
34  'smiley_path' => array(
35  'type' => 'text',
36  'length' => 200,
37  )
38  );
39 
40  //$ilDB->dropTable( "chatroom_smilies" );
41  //$ilDB->dropTable( "chatroom_smilies_seq" );
42 
43  $ilDB->createTable( 'chatroom_smilies', $fields );
44  $ilDB->addPrimaryKey( 'chatroom_smilies', array('smiley_id') );
45  $ilDB->createSequence( 'chatroom_smilies' );
46  }
47 
53  private static function _insertDefaultValues()
54  {
55  global $ilDB;
56 
57  $values = array(
58  array("icon_smile.gif", ":)\n:-)\n:smile:"),
59  array("icon_wink.gif", ";)\n;-)\n:wink:"),
60  array("icon_laugh.gif", ":D\n:-D\n:laugh:\n:grin:\n:biggrin:"),
61  array("icon_sad.gif", ":(\n:-(\n:sad:"),
62  array("icon_shocked.gif", ":o\n:-o\n:shocked:"),
63  array("icon_tongue.gif", ":p\n:-p\n:tongue:"),
64  array("icon_cool.gif", ":cool:"),
65  array("icon_eek.gif", ":eek:"),
66  array("icon_angry.gif", ":||\n:-||\n:angry:"),
67  array("icon_flush.gif", ":flush:"),
68  array("icon_idea.gif", ":idea:"),
69  array("icon_thumbup.gif", ":thumbup:"),
70  array("icon_thumbdown.gif", ":thumbdown:"),
71  );
72 
73  $stmt = $ilDB->prepare("
74  INSERT INTO chatroom_smilies (smiley_id, smiley_keywords, smiley_path)
75  VALUES (?, ?, ?)",
76  array( "integer", "text", "text" )
77  );
78 
79  foreach( $values as $val )
80  {
81  $row = array(
82  $ilDB->nextID( "chatroom_smilies" ),
83  $val[1],
84  $val[0]
85  );
86  $stmt->execute( $row );
87  }
88  }
89 
93  private static function _setupFolder()
94  {
95  $path = ilUtil::getWebspaceDir() . '/chatroom/smilies';
96 
97  if( !is_dir( $path ) )
98  {
99  mkdir( $path, 0755, true );
100  }
101  }
102 
108  public static function _getSmileyDir()
109  {
110  return ilUtil::getWebspaceDir() . '/chatroom/smilies';
111  }
112 
116  public static function _initial()
117  {
121  }
122 
131  public static function _checkSetup()
132  {
133  global $lng;
134 
136 
137  if( !is_dir( $path ) )
138  {
139  ilUtil::sendInfo( $lng->txt( 'chatroom_smilies_dir_not_exists' ) );
141 
142  if( !is_dir( $path ) )
143  {
144  ilUtil::sendFailure( $lng->txt( 'chatroom_smilies_dir_not_available' ) );
145  return false;
146  }
147  else
148  {
149  $smilies = array(
150  "icon_smile.gif",
151  "icon_wink.gif",
152  "icon_laugh.gif",
153  "icon_sad.gif",
154  "icon_shocked.gif",
155  "icon_tongue.gif",
156  "icon_cool.gif",
157  "icon_eek.gif",
158  "icon_angry.gif",
159  "icon_flush.gif",
160  "icon_idea.gif",
161  "icon_thumbup.gif",
162  "icon_thumbdown.gif",
163  );
164 
165  foreach( $smilies as $smiley )
166  {
167  copy( "templates/default/images/emoticons/$smiley", $path . "/$smiley" );
168  }
169 
171  ilUtil::sendSuccess( $lng->txt( 'chatroom_smilies_initialized' ) );
172  }
173  }
174 
175  if( !is_writable( $path ) )
176  {
177  ilUtil::sendInfo( $lng->txt( 'chatroom_smilies_dir_not_writable' ) );
178  }
179 
180  return true;
181  }
182 
188  public static function _getSmilies()
189  {
190  global $ilDB;
191 
192  $res = $ilDB->query(
193  "SELECT smiley_id, smiley_keywords, smiley_path
194  FROM chatroom_smilies"
195  );
196  $result = array();
197 
198  for( $i = 0; $i < $res->numRows(); $i++ )
199  {
200  $tmp = $res->fetchRow();
201  $result[] = array(
202  "smiley_id" => $tmp[0],
203  "smiley_keywords" => $tmp[1],
204  "smiley_path" => $tmp[2],
205  "smiley_fullpath" => ilUtil::getWebspaceDir() . '/chatroom/smilies/' . $tmp[2]
206  );
207  }
208  return $result;
209  }
210 
219  public static function _getSmiley($a_id)
220  {
221  global $ilDB;
222 
223  $res = $ilDB->queryF( "
224  SELECT smiley_id, smiley_keywords, smiley_path
225  FROM chatroom_smilies
226  WHERE smiley_id = %s ",
227  array('integer'), array($a_id)
228  );
229 
230  if( $res->numRows() )
231  {
232  $tmp = $res->fetchRow();
233  $result = array(
234  "smiley_id" => $tmp[0],
235  "smiley_keywords" => $tmp[1],
236  "smiley_path" => $tmp[2],
237  "smiley_fullpath" => ilUtil::getWebspaceDir() . '/chatroom/smilies/' . $tmp[2]
238  );
239  return $result;
240  }
241  throw new Exception( 'smiley with id $a_id not found' );
242  }
243 
251  public static function _getSmiliesById($ids = array())
252  {
253  global $ilDB;
254 
255  if( !count( $ids ) )
256  return;
257 
258  $sql = "SELECT smiley_id, smiley_keywords, smiley_path FROM chatroom_smilies WHERE ";
259 
260  $sql_parts = array();
261 
262  foreach( $ids as $id )
263  {
264  $sql_parts[] .= "smiley_id = " . $ilDB->quote( $id, "integer" );
265  }
266 
267  $sql .= join( " OR ", $sql_parts );
268  $res = $ilDB->query( $sql );
269  $result = array();
270 
271  for( $i = 0; $i < $res->numRows(); $i++ )
272  {
273  $tmp = $res->fetchRow();
274  $result[] = array(
275  "smiley_id" => $tmp[0],
276  "smiley_keywords" => $tmp[1],
277  "smiley_path" => $tmp[2],
278  "smiley_fullpath" => ilUtil::getWebspaceDir() . '/chatroom/smilies/' . $tmp[2]
279  );
280  }
281  return $result;
282  }
283 
290  public static function _deleteMultipleSmilies($ids = array())
291  {
292  global $ilDB;
293 
294  $smilies = self::_getSmiliesById( $ids );
295 
296  if( count( $smilies ) <= 0 )
297  return;
298 
299  $sql_parts = array();
300 
301  foreach( $smilies as $s )
302  {
303  unlink( $s["smiley_fullpath"] );
304  $sql_parts[] = "smiley_id = " . $ilDB->quote( $s["smiley_id"], 'integer' );
305  }
306 
307  $ilDB->manipulate( "DELETE FROM chatroom_smilies WHERE " . join( " OR ", $sql_parts ) );
308  }
309 
317  public static function _updateSmiley($data)
318  {
319  global $ilDB;
320 
321  $ilDB->manipulateF(
322  "UPDATE chatroom_smilies
323  SET smiley_keywords = %s
324  WHERE
325  smiley_id = %s",
326  array('text', 'integer'),
327  array($data["smiley_keywords"], $data["smiley_id"])
328  );
329 
330  if( $data["smiley_path"] )
331  {
332  $sm = self::_getSmiley( $data["smiley_id"] );
333  unlink( $sm["smiley_fullpath"] );
334  $ilDB->manipulateF(
335  "UPDATE chatroom_smilies
336  SET smiley_path = %s
337  WHERE
338  smiley_id = %s",
339  array('text', 'integer'),
340  array($data["smiley_path"], $data["smiley_id"])
341  );
342  }
343  }
344 
350  public static function _getSmiliesBasePath()
351  {
352  return ilUtil::getWebspaceDir() . '/chatroom/smilies/';
353  }
354 
361  public static function _deleteSmiley($a_id)
362  {
363  global $ilDB;
364 
365  try
366  {
367  $smiley = self::_getSmiley( $a_id );
368  $path = ilUtil::getWebspaceDir() . '/chatroom/smilies/' . $smiley["smiley_path"];
369 
370  if( is_file( $path ) )
371  unlink( $path );
372 
373  $ilDB->manipulateF(
374  "DELETE FROM chatroom_smilies
375  WHERE
376  smiley_id = %s",
377  array('integer'),
378  array($a_id)
379  );
380  }
381  catch(Exception $e)
382  {
383 
384  }
385  }
386 
394  public static function _storeSmiley($keywords, $path)
395  {
396  global $ilDB;
397 
398  $stmt = $ilDB->prepare( "
399  INSERT INTO chatroom_smilies (smiley_id, smiley_keywords, smiley_path)
400  VALUES (?, ?, ?)",
401  array(
402  "integer", "text", "text"
403  )
404  );
405  $row = array(
406  $ilDB->nextID( "chatroom_smilies" ),
407  $keywords,
408  $path
409  );
410  $stmt->execute( $row );
411  }
412 
419  public static function _prepareKeywords($words)
420  {
421  $keywordscheck = true;
422 
423  // check keywords
424  $keywords_unchecked = explode( "\n", $words );
425  if( count( $keywords_unchecked ) <= 0 )
426  $keywordscheck = false;
427 
428  if( $keywordscheck )
429  {
430  $keywords = array();
431 
432  foreach( $keywords_unchecked as $word )
433  {
434  if( trim( $word ) )
435  $keywords[] = trim( $word );
436  }
437  }
438 
439  if( $keywordscheck && count( $keywords ) <= 0 )
440  $keywordscheck = false;
441 
442  if( $keywordscheck )
443  return $keywords;
444  else
445  return array();
446  }
447 
448 
449  /*public static function _parseString($str)
450  {
451  global $ilDB;
452 
453  $q = $ilDB->query(
454  "SELECT smiley_keywords, smiley_path
455  FROM chatroom_smilies"
456  );
457 
458  $ar_search = array();
459  $ar_replace = array();
460 
461  $ostr = "";
462 
463  for( $i = 0; $i < $q->numRows(); $i++ )
464  {
465  $row = $q->fetchRow();
466  $keywords = explode( "\n", $row[0] );
467 
468  for( $x = 0; $x < count( $keywords ); $x++ )
469  {
470  $ar_search[] = $keywords[$x];
471 
472  $tpl = new ilTemplate(
473  "tpl.chatroom_smiley_line.html", true, true, "Modules/Chatroom"
474  );
475  $tpl->setVariable(
476  "SMILEY_PATH",
477  ilUtil::getHtmlPath( self::_getSmiliesBasePath() . $row[1] )
478  );
479  $tpl->setVariable( "SMILEY_ALT", urlencode( $keywords[$x] ) );
480  $tpl->parseCurrentBlock();
481 
482  $ar_replace[] = $tpl->get();
483  }
484  }
485 
486  $str = str_replace( $ar_search, $ar_replace, $str );
487 
488  return $str;
489  }*/
490 
491 }
492 
493 ?>