1<?php
2
3/**
4 * Like ConfigManagerTwoLineCascadeConfig but with image support.
5 * An image pointed with value. I.e. the smileys config
6 */
7class ConfigManagerTwoLineRightImageConfigCascade extends ConfigManagerTwoLineLeftImageConfigCascade {
8
9    /**
10     * Constructor
11     *
12     * @param string $name
13     * @param string $imageFolder
14     * @param string $extension
15     */
16    public function __construct($name, $imageFolder, $extension) {
17        parent::__construct($name, $imageFolder, $extension);
18
19        $this->imageAlignment = 'right';
20    }
21    /**
22     * Returns path to image file
23     *
24     * @param string $configtype 'local' or 'default'
25     * @param string $key
26     * @return string
27     */
28    protected function getImagePath($configtype, $key) {
29        $configs = $this->readConfig();
30        $path = $this->imageFolder . $configs[$configtype][$key];
31
32        if (is_file(DOKU_INC . $path)) {
33            return $path;
34        }
35        return '';
36    }
37
38    /**
39     * Build path to file location
40     *
41     * @param string $key               key of entry
42     * @param string $value             value of entry
43     * @param string $upload_name       name of upload
44     * @param string $upload_extension  extension of upload
45     * @return string
46     */
47    protected function getImageFilename($key, $value, $upload_name, $upload_extension) {
48        $extension_position = strrpos($value, '.');
49        if($extension_position) {
50            $filename = substr($value, 0, $extension_position);
51        } else {
52            $filename = $value;
53        }
54        if(empty($filename)) {
55            $filename = $upload_name;
56        }
57
58        $filename = trim($filename);
59        if(substr($filename, 0, 6) !=  'local/') {
60            $filename = 'local/' . $filename;
61        }
62
63        if(empty(substr($filename,6))) {
64            return '';
65        }
66
67        return "$filename.$upload_extension";
68    }
69
70    /**
71     * Update image path
72     *
73     * @param string $key
74     * @param string $value
75     * @return bool success?
76     */
77    protected function updateValue($key, $value) {
78        $config = $this->readConfig();
79
80        $haschanges = false;
81
82        foreach($config['local'] as $confkey => $confvalue) {
83            if($confkey == $key && $confvalue != $value) {
84                $config['local'][$confkey] = $value;
85                $haschanges = true;
86            }
87        }
88
89        if($haschanges) {
90            $this->saveToFile($config['local']);
91        }
92        return true;
93    }
94
95
96    /**
97     * @return bool
98     */
99    public function deleteIcon() {
100        global $INPUT;
101
102        $key = $INPUT->str('key');
103        if ($key === '') {
104            header('Content-Type: text/plain');
105            echo $this->helper->getLang('upload_errNoConfigKeySend');
106            return false;
107        }
108
109        $configs = $this->readConfig();
110        if (isset($configs['default'][$key])) {
111            header('Content-Type: text/plain');
112            echo $this->helper->getLang('upload_errCannotOverwriteDefaultKey');
113            return false;
114        }
115
116        $path = $this->getImagePath('local', $key);
117        if (!@unlink(DOKU_INC . $path)) {
118            echo $this->helper->getLang('iconDelete_error');
119            return false;
120        }
121
122        return true;
123    }
124}
125