Dotclear

source: plugins/filesAlias/inc/class.files.alias.php @ 1850

Revision 1850, 2.1 KB checked in by Osku, 14 years ago (diff)

filesAlias : initial commit

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ----------------------------------
3#
4# This file is part of filesAlias, a plugin for Dotclear 2.
5#
6# Copyright (c) 2009 Osku and contributors
7#
8# Licensed under the GPL version 2.0 license.
9# A copy of this license is available in LICENSE file or at
10# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
11#
12# -- END LICENSE BLOCK ------------------------------------
13
14class FilesAliases
15{
16     protected $core;
17     protected $aliases;
18     
19     public function __construct(&$core)
20     {
21          $this->core =& $core;
22     }
23     
24     public function getAliases()
25     {
26          if (is_array($this->aliases)) {
27               return $this->aliases;
28          }
29         
30          $this->aliases = array();
31          $sql =    'SELECT filesalias_url, filesalias_destination, filesalias_position '.
32                    'FROM '.$this->core->prefix.'filesalias '.
33                    "WHERE blog_id = '".$this->core->con->escape($this->core->blog->id)."' ".
34                    'ORDER BY filesalias_position ASC ';
35          $this->aliases = $this->core->con->select($sql)->rows();
36          return $this->aliases;
37     }
38     
39     public function updateAliases($aliases)
40     {
41          $this->core->con->begin();
42          try
43          {
44               $this->deleteAliases();
45               foreach ($aliases as $k => $v)
46               {
47                    if (!empty($v['filesalias_url']) && !empty($v['filesalias_destination']))
48                    {
49                         $this->createAlias($v['filesalias_url'],$v['filesalias_destination'],$k+1);
50                    }
51               }
52               
53               $this->core->con->commit();
54          }
55          catch (Exception $e)
56          {
57               $this->core->con->rollback();
58               throw $e;
59          }
60     }
61     
62     public function createAlias($url,$destination,$position)
63     {
64          if (!$url) {
65               throw new Exception(__('File URL is empty.'));
66          }
67         
68          if (!$destination) {
69               throw new Exception(__('File destination is empty.'));
70          }
71         
72          $cur = $this->core->con->openCursor($this->core->prefix.'filesalias');
73          $cur->blog_id = (string) $this->core->blog->id;
74          $cur->filesalias_url = (string) $url;
75          $cur->filesalias_destination = (string) $destination;
76          $cur->filesalias_position = abs((integer) $position);
77          $cur->insert();
78     }
79     
80     public function deleteAliases()
81     {
82          $this->core->con->execute(
83               'DELETE FROM '.$this->core->prefix.'filesalias '.
84               "WHERE blog_id = '".$this->core->con->escape($this->core->blog->id)."' "
85          );
86     }
87}
88?>
Note: See TracBrowser for help on using the repository browser.

Sites map