|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/* |
|
3
|
|
|
You may not change or alter any portion of this comment or credits |
|
4
|
|
|
of supporting developers from this source code or any supporting source code |
|
5
|
|
|
which is considered copyrighted (c) material of the original comment or credit authors. |
|
6
|
|
|
|
|
7
|
|
|
This program is distributed in the hope that it will be useful, |
|
8
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
9
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* oledrion |
|
14
|
|
|
* |
|
15
|
|
|
* @copyright {@link http://xoops.org/ XOOPS Project} |
|
16
|
|
|
* @license {@link http://www.fsf.org/copyleft/gpl.html GNU public license} |
|
17
|
|
|
* @author Hervé Thouzard (http://www.herve-thouzard.com/) |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Gestion des fichies attachés aux produits |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
require __DIR__ . '/classheader.php'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Class Oledrion_files |
|
28
|
|
|
*/ |
|
29
|
|
|
class Oledrion_files extends Oledrion_Object |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* constructor |
|
33
|
|
|
* |
|
34
|
|
|
* normally, this is called from child classes only |
|
35
|
|
|
* |
|
36
|
|
|
* @access public |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct() |
|
39
|
|
|
{ |
|
40
|
|
|
$this->initVar('file_id', XOBJ_DTYPE_INT, null, false); |
|
41
|
|
|
$this->initVar('file_product_id', XOBJ_DTYPE_INT, null, false); |
|
42
|
|
|
$this->initVar('file_filename', XOBJ_DTYPE_TXTBOX, null, false); |
|
43
|
|
|
$this->initVar('file_description', XOBJ_DTYPE_TXTBOX, null, false); |
|
44
|
|
|
$this->initVar('file_mimetype', XOBJ_DTYPE_TXTBOX, null, false); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Supprime un fichier |
|
49
|
|
|
*/ |
|
50
|
|
View Code Duplication |
public function deleteAttachedFile() |
|
|
|
|
|
|
51
|
|
|
{ |
|
52
|
|
|
if (!defined('OLEDRION_ATTACHED_FILES_PATH')) { |
|
53
|
|
|
include OLEDRION_PATH . 'config.php'; |
|
54
|
|
|
} |
|
55
|
|
|
@unlink(OLEDRION_ATTACHED_FILES_PATH . '/' . $this->getVar('file_filename')); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Indique si le fichier courant est un fichier MP3 |
|
60
|
|
|
* @return boolean |
|
61
|
|
|
*/ |
|
62
|
|
|
public function isMP3() |
|
63
|
|
|
{ |
|
64
|
|
|
return strtolower($this->getVar('file_mimetype')) === 'audio/mpeg' ? true : false; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Indique si le fichier attaché existe physiquement sur le site |
|
69
|
|
|
* @return boolean |
|
70
|
|
|
*/ |
|
71
|
|
View Code Duplication |
public function fileExists() |
|
|
|
|
|
|
72
|
|
|
{ |
|
73
|
|
|
if (!defined('OLEDRION_ATTACHED_FILES_PATH')) { |
|
74
|
|
|
include OLEDRION_PATH . 'config.php'; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return file_exists(OLEDRION_ATTACHED_FILES_PATH . '/' . $this->getVar('file_filename')); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Retourne l'url pour accéder au fichier |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
|
View Code Duplication |
public function getURL() |
|
|
|
|
|
|
85
|
|
|
{ |
|
86
|
|
|
if (!defined('OLEDRION_ATTACHED_FILES_URL')) { |
|
87
|
|
|
include OLEDRION_PATH . 'config.php'; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return OLEDRION_ATTACHED_FILES_URL . '/' . $this->getVar('file_filename'); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Retourne le chemin physique pour accéder au fichier |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
View Code Duplication |
public function getPath() |
|
|
|
|
|
|
98
|
|
|
{ |
|
99
|
|
|
if (!defined('OLEDRION_ATTACHED_FILES_URL')) { |
|
100
|
|
|
include OLEDRION_PATH . 'config.php'; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return OLEDRION_ATTACHED_FILES_PATH . '/' . $this->getVar('file_filename'); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param string $format |
|
108
|
|
|
* @return array |
|
109
|
|
|
*/ |
|
110
|
|
|
public function toArray($format = 's') |
|
111
|
|
|
{ |
|
112
|
|
|
$ret = parent::toArray($format); |
|
113
|
|
|
$ret['file_is_mp3'] = $this->isMP3(); |
|
114
|
|
|
$ret['file_download_url'] = $this->getURL(); |
|
115
|
|
|
|
|
116
|
|
|
return $ret; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Class OledrionOledrion_filesHandler |
|
122
|
|
|
*/ |
|
123
|
|
|
class OledrionOledrion_filesHandler extends Oledrion_XoopsPersistableObjectHandler |
|
|
|
|
|
|
124
|
|
|
{ |
|
125
|
|
|
/** |
|
126
|
|
|
* OledrionOledrion_filesHandler constructor. |
|
127
|
|
|
* @param XoopsDatabase|null $db |
|
128
|
|
|
*/ |
|
129
|
|
|
public function __construct(XoopsDatabase $db) |
|
130
|
|
|
{ // Table Classe Id Libellé |
|
131
|
|
|
parent::__construct($db, 'oledrion_files', 'oledrion_files', 'file_id', 'file_filename'); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Supprime un fichier (son fichier joint ET l'enregistrement dans la base de données) |
|
136
|
|
|
* |
|
137
|
|
|
* @param oledrion_files $file |
|
138
|
|
|
* @return boolean Le résultat de la suppression |
|
139
|
|
|
*/ |
|
140
|
|
|
public function deleteAttachedFile(Oledrion_files $file) |
|
141
|
|
|
{ |
|
142
|
|
|
if ($file->fileExists()) { |
|
143
|
|
|
$file->deleteAttachedFile(); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return $this->delete($file, true); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Retourne les fichiers attachés à un produit |
|
151
|
|
|
* |
|
152
|
|
|
* @param integer $file_product_id L'Id du produit |
|
153
|
|
|
* @param integer $start Position de départ |
|
154
|
|
|
* @param integer $limit Nombre maxi de produits à retourner |
|
155
|
|
|
* @return array tableau d'objets de type oledrion_files |
|
156
|
|
|
*/ |
|
157
|
|
|
public function getProductFiles($file_product_id, $start = 0, $limit = 0) |
|
158
|
|
|
{ |
|
159
|
|
|
$criteria = new Criteria('file_product_id', $file_product_id, '='); |
|
160
|
|
|
$criteria->setStart($start); |
|
161
|
|
|
$criteria->setLimit($limit); |
|
162
|
|
|
|
|
163
|
|
|
return $this->getObjects($criteria); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Retourne le nombre de fichiers attachés à un produit qui sont des MP3 |
|
168
|
|
|
* |
|
169
|
|
|
* @param integer $file_product_id L'Id du produit |
|
170
|
|
|
* @return integer le nombre de fichiers MP3 |
|
171
|
|
|
*/ |
|
172
|
|
|
public function getProductMP3Count($file_product_id) |
|
173
|
|
|
{ |
|
174
|
|
|
$criteria = new CriteriaCompo(); |
|
175
|
|
|
$criteria->add(new Criteria('file_product_id', $file_product_id, '=')); |
|
176
|
|
|
$criteria->add(new Criteria('file_mimetype', 'audio/mpeg', '=')); |
|
177
|
|
|
|
|
178
|
|
|
return $this->getCount($criteria); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Retourne le nombre de fichiers attachés à un produit |
|
183
|
|
|
* |
|
184
|
|
|
* @param integer $file_product_id L'Id du produit |
|
185
|
|
|
* @return integer le nombre de fichiers |
|
186
|
|
|
*/ |
|
187
|
|
|
public function getProductFilesCount($file_product_id) |
|
188
|
|
|
{ |
|
189
|
|
|
$criteria = new Criteria('file_product_id', $file_product_id, '='); |
|
190
|
|
|
|
|
191
|
|
|
return $this->getCount($criteria); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Supprime les fichiers attachés à un produit |
|
196
|
|
|
* |
|
197
|
|
|
* @param integer $file_product_id L'Id du produit |
|
198
|
|
|
* @return void |
|
199
|
|
|
*/ |
|
200
|
|
|
public function deleteProductFiles($file_product_id) |
|
201
|
|
|
{ |
|
202
|
|
|
$files = array(); |
|
|
|
|
|
|
203
|
|
|
$criteria = new Criteria('file_product_id', $file_product_id, '='); |
|
204
|
|
|
$files = $this->getObjects($criteria); |
|
205
|
|
|
if (count($files) > 0) { |
|
206
|
|
|
foreach ($files as $file) { |
|
207
|
|
|
$file->deleteAttachedFile(); |
|
208
|
|
|
$this->delete($file, true); |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.