1: <?php
2:
3: /**
4: * This file contains the generic file overview class.
5: *
6: * @package Core
7: * @subpackage GUI
8: * @author Mischa Holz
9: * @copyright four for business AG <www.4fb.de>
10: * @license http://www.contenido.org/license/LIZENZ.txt
11: * @link http://www.4fb.de
12: * @link http://www.contenido.org
13: */
14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: /**
18: * The class cGuiFileOverview is a cGuiPage displaying files.
19: * It is meant to be used but in the left bottom frame.
20: * As for now it is used to display HTML, CSS & JS files.
21: *
22: * <strong>Usage</strong>
23: * <code>
24: * // path to directory where files to display are located
25: * $path = $cfgClient[$client]['tpl']['path'];
26: * // basename of file to mark as selected
27: * $mark = stripslashes($_REQUEST['file']);
28: * // build page to display files
29: * $page = new cGuiFileOverview($path, $mark, 'html');
30: * // optionally set extension(s) to filter files
31: * $page->setFileExtension(array('html', 'tpl'));
32: * // render page
33: * $page->render();
34: * </code>
35: *
36: * <strong>Directory</strong>
37: * Only files in the directory defined by the given path are displayed.
38: *
39: * <strong>Extension</strong>
40: * If set via setFileExtension(array) only files of the given extensions
41: * are displayed. By default all extensions are considered.
42: *
43: * <strong>Order of files</strong>
44: * The files to be displayed are sorted alphabetically.
45: *
46: * <strong>Marking files</strong>
47: * When initializing the class the name of a file to mark can be given.
48: * This feature is totally optional.
49: *
50: * <strong>Additional file information</strong>
51: * When initializing the class the name of a file information type to
52: * display can be given.
53: * This feature is totally optional.
54: * @todo This feature does not work at the moment.
55: *
56: * <strong>Template</strong>
57: * This class is bound to the template generic_file_overview in a
58: * hardcoded manner (template,generic_file_overview.html).
59: * @todo This prevents this class to be used with other views.
60: *
61: * <strong>Editing a file</strong>
62: * When editing a file
63: * <ul>
64: * <li>the right top frame is opened with the URL:
65: * main.php
66: * ?area={AREA}
67: * &frame=3
68: * &file={FILENAME}
69: * &contenido=1
70: * <li>whereas the right bottom frame is opened with the URL:
71: * main.php
72: * ?area={AREA}
73: * &frame=4
74: * &action={ACTION}
75: * &file={FILENAME}
76: * &tmp_file={FILENAME}
77: * &contenido=1
78: * </ul>
79: * AREA & ACTION are filled with the current global values.
80: * FILENAME is the name of the current file.
81: * @todo Why the parameter contenido is set to 1 is to be clarified.
82: * IMHO it should be the current value of the global contenido variable.
83: * @todo Why the parameter file and tmp_file are both set is to be clarified.
84: *
85: * <strong>Deleting a file</strong>
86: * A delete icon is provided for each file.
87: * When deleting a file
88: * <ul>
89: * <li>the right bottom frame is opened with the URL:
90: * main.php
91: * ?area={AREA}
92: * &action={ACTION}
93: * &frame=4
94: * &delfile={FILENAME}
95: * </ul>
96: * @todo Why the parameter contenido is not set is to be clarified.
97: * @todo Why the URL is generated via JS is to be clarified.
98: *
99: * If the effective setting
100: * client/readonly is "true" or the current user has no privileges for
101: * the action $area . "_delete" of the current area this icon will be
102: * inactive though.
103: * @todo This prevents this class to be used in other areas.
104: *
105: * @package Core
106: * @subpackage GUI
107: */
108: class cGuiFileOverview extends cGuiPage {
109:
110: /**
111: * Path to the directory directory where files to display are located.
112: *
113: * @var string
114: */
115: protected $_directory;
116:
117: /**
118: * Basename of file that will be marked as selected.
119: *
120: * @var string
121: */
122: protected $_markedFile;
123:
124: /**
125: * Type of additional file information that should be displayed as
126: * description.
127: *
128: * @var string
129: */
130: protected $_fileInfoType;
131:
132: /**
133: * Selected file extension.
134: *
135: * @var string
136: */
137: protected $_fileExtension;
138:
139: /**
140: * Constructor to create an instance of this class.
141: *
142: * Initializes the class for the directory.
143: *
144: * @param string $dir
145: * path to the directory directory where files to display are
146: * located
147: * @param string $markedFile [optional]
148: * basename of file that will be marked as selected.
149: * @param string $fileInfoType [optional]
150: * type of additional file information that should be
151: * displayed as description
152: */
153: public function __construct($dir, $markedFile = '', $fileInfoType = '') {
154: parent::__construct('generic_file_overview');
155:
156: // assign properties
157: $this->_directory = $dir;
158: $this->_markedFile = $markedFile;
159: $this->_fileInfoType = $fileInfoType;
160: }
161:
162: /**
163: * Sets extension(s) to filter files that should be displayed.
164: *
165: * @param array|string $extension
166: * Name of extensions
167: */
168: public function setFileExtension($extension) {
169: if (cSecurity::isString($extension)) {
170: $extension = array($extension);
171: }
172: $this->_fileExtension = $extension;
173: }
174:
175: /**
176: * Renders the page
177: *
178: * @param cTemplate|null $template
179: * @param bool $return
180: *
181: * @throws cDbException
182: * @throws cException
183: * @throws cInvalidArgumentException
184: */
185: public function render($template = NULL, $return = false) {
186:
187: $cfg = cRegistry::getConfig();
188: $area = cRegistry::getArea();
189: $perm = cRegistry::getPerm();
190:
191: // create an array of all files in the directory
192: $files = array();
193: foreach (new DirectoryIterator($this->_directory) as $file) {
194: if ($file->isDir()) {
195: continue;
196: }
197: if (!empty($this->_fileExtension) && !in_array($file->getExtension(), $this->_fileExtension)) {
198: continue;
199: }
200: $files[] = $file->getBasename();
201: }
202:
203: // sort the files
204: sort($files);
205:
206: // assign variables for the JavaScript
207: $this->set('s', 'JS_AREA', $area);
208: $this->set('s', 'JS_ACTION_DELETE', $area . '_delete');
209:
210: // assign variables for every file
211: $fileInfos = new cApiFileInformationCollection();
212: foreach($files as $file) {
213: if($this->_fileInfoType != '') {
214: $fileInfo = $fileInfos->getFileInformation($file, $this->_fileInfoType);
215: $this->set('d', 'DESCRIPTION', conHtmlSpecialChars(cSecurity::escapeString($fileInfo['description'])));
216: } else {
217: $this->set('d', 'DESCRIPTION', '');
218: }
219: $this->set('d', 'AREA', $area);
220: $this->set('d', 'ACTION', $area . '_edit');
221: $this->set('d', 'FILENAME', $file);
222: if($file == $this->_markedFile) {
223: $this->set('d', 'MARKED', 'marked');
224: } else {
225: $this->set('d', 'MARKED', '');
226: }
227: if(getEffectiveSetting("client", "readonly", "false") == "true" || (!$perm->have_perm_area_action($area, $area . "_delete"))) {
228: $this->set('d', 'DELETE_IMAGE', $cfg['path']['images'] . 'delete_inact.gif');
229: } else {
230: $this->set('d', 'DELETE_IMAGE', $cfg['path']['images'] . 'delete.gif');
231: }
232:
233: $this->next();
234: }
235:
236: // call the render method of cGuiPage to display the webpage
237: parent::render();
238: }
239:
240: }
241: