summaryrefslogtreecommitdiff
path: root/app/Models/Share.php
blob: 86b1b9ed9a967d6f9bf15312422e7550e50bf1c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php

/**
 * Manage the sharing options in FreshRSS.
 */
class FreshRSS_Share {
	/**
	 * The list of available sharing options.
	 */
	private static $list_sharing = array();

	/**
	 * Register a new sharing option.
	 * @param $share_options is an array defining the share option.
	 */
	public static function register($share_options) {
		$type = $share_options['type'];

		if (isset(self::$list_sharing[$type])) {
			return;
		}

		$help_url = isset($share_options['help']) ? $share_options['help'] : '';
		self::$list_sharing[$type] = new FreshRSS_Share(
			$type, $share_options['url'], $share_options['transform'],
			$share_options['form'], $help_url
		);
	}

	/**
	 * Register sharing options in a file.
	 * @param $filename the name of the file to load.
	 */
	public static function load($filename) {
		$shares_from_file = @include($filename);
		if (!is_array($shares_from_file)) {
			$shares_from_file = array();
		}

		foreach ($shares_from_file as $share_type => $share_options) {
			$share_options['type'] = $share_type;
			self::register($share_options);
		}
	}

	/**
	 * Return the list of sharing options.
	 * @return an array of FreshRSS_Share objects.
	 */
	public static function enum() {
		return self::$list_sharing;
	}

	/**
	 * Return FreshRSS_Share object related to the given type.
	 * @param $type the share type, null if $type is not registered.
	 */
	public static function get($type) {
		if (!isset(self::$list_sharing[$type])) {
			return null;
		}

		return self::$list_sharing[$type];
	}

	/**
	 *
	 */
	private $type = '';
	private $name = '';
	private $url_transform = '';
	private $transform = array();
	private $form_type = 'simple';
	private $help_url = '';
	private $custom_name = null;
	private $base_url = null;
	private $title = null;
	private $link = null;

	/**
	 * Create a FreshRSS_Share object.
	 * @param $type is a unique string defining the kind of share option.
	 * @param $url_transform defines the url format to use in order to share.
	 * @param $transform is an array of transformations to apply on link and title.
	 * @param $form_type defines which form we have to use to complete. "simple"
	 *        is typically for a centralized service while "advanced" is for
	 *        decentralized ones.
	 * @param $help_url is an optional url to give help on this option.
	 */
	private function __construct($type, $url_transform, $transform,
	                             $form_type, $help_url = '') {
		$this->type = $type;
		$this->name = _t('gen.share.' . $type);
		$this->url_transform = $url_transform;
		$this->help_url = $help_url;

		if (!is_array($transform)) {
			$transform = array();
		}
		$this->transform = $transform;

		if (!in_array($form_type, array('simple', 'advanced'))) {
			$form_type = 'simple';
		}
		$this->form_type = $form_type;
	}

	/**
	 * Update a FreshRSS_Share object with information from an array.
	 * @param $options is a list of informations to update where keys should be
	 *        in this list: name, url, title, link.
	 */
	public function update($options) {
		$available_options = array(
			'name' => 'custom_name',
			'url' => 'base_url',
			'title' => 'title',
			'link' => 'link',
		);

		foreach ($options as $key => $value) {
			if (isset($available_options[$key])) {
				$this->{$available_options[$key]} = $value;
			}
		}
	}

	/**
	 * Return the current type of the share option.
	 */
	public function type() {
		return $this->type;
	}

	/**
	 * Return the current form type of the share option.
	 */
	public function formType() {
		return $this->form_type;
	}

	/**
	 * Return the current help url of the share option.
	 */
	public function help() {
		return $this->help_url;
	}

	/**
	 * Return the current name of the share option.
	 */
	public function name($real = false) {
		if ($real || is_null($this->custom_name) || empty($this->custom_name)) {
			return $this->name;
		} else {
			return $this->custom_name;
		}
	}

	/**
	 * Return the current base url of the share option.
	 */
	public function baseUrl() {
		return $this->base_url;
	}

	/**
	 * Return the current url by merging url_transform and base_url.
	 */
	public function url() {
		$matches = array(
			'~URL~',
			'~TITLE~',
			'~LINK~',
		);
		$replaces = array(
			$this->base_url,
			$this->title(),
			$this->link(),
		);
		return str_replace($matches, $replaces, $this->url_transform);
	}

	/**
	 * Return the title.
	 * @param $raw true if we should get the title without transformations.
	 */
	public function title($raw = false) {
		if ($raw) {
			return $this->title;
		}

		return $this->transform($this->title, $this->getTransform('title'));
	}

	/**
	 * Return the link.
	 * @param $raw true if we should get the link without transformations.
	 */
	public function link($raw = false) {
		if ($raw) {
			return $this->link;
		}

		return $this->transform($this->link, $this->getTransform('link'));
	}

	/**
	 * Transform a data with the given functions.
	 * @param $data the data to transform.
	 * @param $tranform an array containing a list of functions to apply.
	 * @return the transformed data.
	 */
	private static function transform($data, $transform) {
		if (!is_array($transform) || empty($transform)) {
			return $data;
		}

		foreach ($transform as $action) {
			$data = call_user_func($action, $data);
		}

		return $data;
	}

	/**
	 * Get the list of transformations for the given attribute.
	 * @param $attr the attribute of which we want the transformations.
	 * @return an array containing a list of transformations to apply.
	 */
	private function getTransform($attr) {
		if (array_key_exists($attr, $this->transform)) {
			return $this->transform[$attr];
		}

		return $this->transform;
	}
}