bors-core /classes/bors/admin/engine.php

Language PHP Lines 530
MD5 Hash cb3b3464b327105176064701272f741f Estimated Cost $10,079 (why?)
Repository https://bitbucket.org/Balancer/bors-core View Raw File View Project SPDX
  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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
<?php

class bors_admin_engine extends bors_object
{
	function object() { return $this->id(); }

	function real_object()
	{
		$obj = $this->object();
		if($obj->class_name() != $obj->extends_class_name())
			$obj = object_property($this->object(), 'real_object', $this->object());

		return $obj;
	}

	function admin_object()
	{
		$obj = $this->object();
		return object_property($this->object(), 'admin_object', $this->object());
	}

	function url() { return $this->url_ex($this->page()); }

	function url_ex($page)
	{
		$url = $this->admin_url();

		if($page && $page != 1)
			$url .= "$page/";

		return $url;
	}

	function admin_url()
	{
		$obj = $this->real_object();

		if($url = $obj->get('admin_url'))
			return $url;

		if($url = $obj->get('edit_url'))
			return $url;

		if($obj->storage())
			return '/_bors/admin/edit-smart/?object='.$obj->internal_uri_ascii();

		return NULL;
	}

	function edit_url()
	{
		$obj = $this->real_object();

		if($url = $obj->get('edit_url'))
			return $url;

		if($url = $obj->get('admin_url'))
			return $url;

		if($obj->storage())
			return '/_bors/admin/edit-smart/?object='.$obj->internal_uri_ascii();

		return NULL;
	}

	function delete_url()
	{
		$obj = $this->admin_object();

		if(!$obj)
			return NULL;

		if(method_exists($obj, 'delete_url'))
			return $obj->delete_url();

		$ref = urlencode($this->parent_delete_url());

		//TODO: придумать лучший вариант определения. Отказаться от has_smart_field.
		if(method_exists($obj, 'fields') && $obj->has_smart_field('is_deleted'))
			return '/_bors/admin/mark/delete/?object='.$obj->internal_uri().'&ref='.$ref;
		else
			return '/_bors/admin/delete/?object='.$obj->internal_uri().'&ref='.$ref;
	}

	function parent_delete_url()
	{

		$obj = $this->real_object();
		$obj_admin_url = $this->url();

		$request_url = bors()->request()->url();
		// Хак для edit-smart: http://admin.aviaport.ru/_bors/admin/edit-smart/?object=aviaport_directory_airline_xref_plane__3
		if($x = $this->get('object'))
		{
			$obj = $x;
			$obj_admin_url = $this->admin()->url();
			$request_url = $x->get('url');
		}

		// Если ссылка главного объекта не равна ссылке на удаляемый файл
		// то родительской страницей (реферером для возврата) является главная страница
		// Тестировать на: http://ipotek-bank.wrk.ru/admin/dbpages/10
//		var_dump($request_url, $obj->get('url'), $obj_admin_url);
		if(!blib_urls::in_array($request_url, array(
			$obj->get('url'),
			$obj_admin_url,
		)))
			return bors()->request()->url();

		// Если у объекта есть метод admin_parent_url, то берём оттуда:
		if($url = $obj->get('admin_parent_url'))
			return $url;

		// Иначе берём первого родителя из ссылки (если есть)
		if($obj_admin_url)
		{
			if($this->url() != $obj_admin_url)
				$obj = bors_load_uri($obj_admin_url);
			else
				$obj = $this;

			$ps1 = object_property($this, 'parents');
			if($p1 = $ps1[0])
				return $p1;
		}

//WTF?
//		$ps2 = object_property(bors_load_uri($p1), 'parents');
//		if($p2 = $ps2[0])
//			return $p2;

		// Иначе смотрим на реферер
		// Если он не равен текущей странице, то он нам и нужен
		if($ref = bors()->request()->referer())
		{
			if(!blib_urls::in_array($ref, array(
				$obj->url(),
				$obj->admin()->url(),
			)))
				return $ref;
		}

		return NULL;
	}

	function append_child_url()
	{
		if(method_exists($obj = $this->real_object(), 'admin_append_child_url'))
			return $obj->admin_append_child_url();

		return '/_bors/admin/append/child?object='.urlencode($obj->internal_uri());
	}

	function property_url()
	{
		if(method_exists($obj = $this->real_object(), 'manage_url'))
			return $obj->property_url();

		return '/_bors/admin/property?object='.urlencode($obj->internal_uri());
	}

	function imaged_titled_link($title = NULL)
	{
		$obj = $this->real_object();

		if(is_null($title))
			$title = $obj->title();

		if(!$title)
			$title = ec('[без имени]');

		if(object_property($obj->access(), 'can_edit'))
			$res = "<a rel=\"nofollow\" href=\"{$obj->admin()->url()}\">{$title}</a>";
		else
			$res = "{$title}<!-- ".object_property($obj->access(), 'debug_title', 'access is NULL')." of {$obj->debug_title()} -->";

		try
		{
			if($obj->url())
				$res .= "&nbsp;<a rel=\"nofollow\" href=\"{$obj->url()}\" target=\"_blank\"><img src=\"/_bors/i/look-16.gif\" width=\"16\" height=\"16\" alt=\"View\" title=\"".ec('Посмотреть на сайте')."\" style=\"vertical-align:middle\" /></a>";
		}
		catch(Exception $e) { }

		return $res;
	}

	function imaged_titled_link_ex($params=[])
	{
		if(is_array($params))
			$mode = popval($params, 'mode');
		else
		{
			$mode = $params;
			$params = array();
		}

		$obj = $this->real_object();

		if(array_key_exists('title', $params))
		{
			$title = defval($params, 'title');
		}
		else
		{
			$title = $obj->title();

			if(!$title)
				$title = ec('[без имени]');
		}

		if($title)
		{
			if($obj->access()->can_edit())
				$html = "<a rel=\"nofollow\" href=\"{$obj->admin()->url()}\">{$title}</a>";
			else
				$html = "{$title}";
		}

		try
		{
			if(stripos($mode, 'v') !== false && $obj->url())
				$html .= "&nbsp;<a rel=\"nofollow\" href=\"{$obj->url()}\" target=\"_blank\"><img src=\"/_bors/i/look-16.gif\" width=\"16\" height=\"16\" alt=\"View\" title=\"".ec('Посмотреть на сайте')."\" style=\"vertical-align:middle\" /></a>";
		}
		catch(Exception $e) { }

		if(stripos($mode, 'e') !== false && $obj->access()->can_delete())
			$html .= '&nbsp;' . $this->imaged_edit_link('');

		if(stripos($mode, 'd') !== false && $obj->access()->can_delete())
			$html .= '&nbsp;' . $this->imaged_delete_link('');

		if(stripos($mode, 'u') !== false && $obj->access()->can_action(NULL,NULL))
			$html .= '&nbsp;' . $this->imaged_action_link('unlink', $params);

		if(!empty($params['actions']))
		{
			require_once('inc/images.php');
			foreach($params['actions'] as $x)
				$html .= '&nbsp;' . bors_icon($x['image'], array('url' => $x['url'], 'title' => $x['title']));
		}

		return $html;
	}

	function imaged_direct_titled_link($params=[])
	{
		if(!is_array($params))
		{
			$title = $params;
			$params = [];
		}
		else
			$title = popval($params, 'title');

		$obj = $this->real_object();
		if(is_null($title))
			$title = $obj->title();

		if(!$title)
			$title = ec('[без имени]');

		$res = "<a rel=\"nofollow\" href=\"{$obj->url()}\">{$title}</a>";

		$popup = config('titles.imaged_direct_titled_link.popup', ec('Изменить'));

		try
		{
			//FIXME: подключить проверку доступа
			if($obj->url() && $obj->access()->can_edit())
				$res .= "&nbsp;<a rel=\"nofollow\" href=\"{$obj->admin()->edit_url()}\"><img src=\"/_bors/i16/edit.png\" width=\"16\" height=\"16\" alt=\"View\" title=\"".htmlspecialchars($popup)."\" style=\"vertical-align:middle\" /></a>";
		}
		catch(Exception $e) { }

		return $res;
	}

	function imaged_link($type, $image, $title=NULL)
	{
		require_once('inc/images.php');
		$url = $this->object()->urls($type);
		return bors_icon($image, array('url' => $url, 'title' => $title));
	}

	function imaged_action_link($action, $params=[])
	{
		require_once('inc/images.php');

		$url = defval($params, 'url');
		if(!$url)
			$url = $this->admin_object()->url().'?act='.htmlspecialchars($action);

		return bors_icon(defval($params, 'image', $action), ['url' => $url, 'title' => defval($params, 'title')]);
	}

	function titled_link($title = NULL)
	{
		$obj = $this->real_object();
		if(is_null($title))
			$title = $obj->title();
		return "<a rel=\"nofollow\" href=\"{$obj->admin()->url()}\">{$title}</a>\n";
	}

	function imaged_nav_named_link()
	{
		$obj = $this->real_object();
		return "<a rel=\"nofollow\" href=\"{$obj->admin()->url()}\">{$obj->nav_name()}</a>&nbsp;<a rel=\"nofollow\" href=\"{$obj->url()}\" target=\"_blank\"><img src=\"/_bors/i/look-16.gif\" width=\"16\" height=\"16\" alt=\"View\" title=\"".ec('Посмотреть на сайте')."\" style=\"vertical-align:middle\" /></a>";
	}

	function imaged_edit_link($title = NULL, $popup = NULL, $unlink_in_admin = true)
	{
		$obj = $this->real_object();

		if(is_null($title))
			$title = ec('Редактировать ')
				.bors_lower($obj->class_title_vp())
				.ec(' «').$obj->title().ec('»');

		$x = $title ? '&nbsp;' : '';
		$url = $this->edit_url();

		if(is_null($popup))
			$popup = $title;

		if(!bors()->main_object() || $unlink_in_admin && preg_match('!'.preg_quote($obj->admin()->admin_url(), '!').'!', bors()->main_object()->url()))
			$url = '';

		if($url)
			return "<a rel=\"nofollow\" href=\"{$url}\" style=\"text-decoration: none\"><img src=\"/_bors/i/edit-16.png\" width=\"16\" height=\"16\" alt=\"edit\" title=\"$popup\" style=\"vertical-align:middle\"/></a>{$x}".($title?"<a rel=\"nofollow\" href=\"{$url}\" title=\"$popup\">{$title}</a>":'');
		else
			return "<img src=\"/_bors/i/edit-16.png\" width=\"16\" height=\"16\" alt=\"edit\" title=\"$popup\" style=\"vertical-align:middle\"/>{$x}{$title}";
	}

	function imaged_append_child_link($title = NULL, $popup = NULL, $unlink_in_admin = true)
	{
		$obj = $this->real_object();

		if(is_null($title))
			$title = ec('Добавить дочерний объект');

		$x = $title ? '&nbsp;' : '';
		$url = $this->append_child_url();

		if(is_null($popup))
			$popup = $title;

		if(!bors()->main_object() || $unlink_in_admin && preg_match('!'.preg_quote($obj->admin()->append_child_url(), '!').'!', bors()->main_object()->url()))
			$url = '';

		if($url)
			return "<a href=\"{$url}\" style=\"text-decoration: none\"><img src=\"/_bors/i/new-16.png\" width=\"16\" height=\"16\" alt=\"edit\" title=\"$popup\" style=\"vertical-align:middle\"/></a>{$x}".($title?"<a rel=\"nofollow\" href=\"{$url}\" title=\"$popup\">{$title}</a>":'');
		else
			return "<img src=\"/_bors/i/new-16.png\" width=\"16\" height=\"16\" alt=\"edit\" title=\"$popup\" style=\"vertical-align:middle\"/>{$x}{$title}";
	}

	function imaged_property_link($title = NULL, $popup = NULL, $unlink_in_admin = true)
	{
		$obj = $this->real_object();

		if(is_null($title))
			$title = ec('Свойства ')
				.bors_lower($obj->class_title_rp())
				.' '
				.$obj->title();

		$x = $title ? '&nbsp;' : '';
		$url = $this->property_url();

		if(is_null($popup))
			$popup = $title;

		if(!bors()->main_object() || $unlink_in_admin && preg_match('!'.preg_quote($obj->admin()->property_url(), '!').'!', bors()->main_object()->url()))
			$url = '';

		if($url)
			return "<a rel=\"nofollow\" href=\"{$url}\" style=\"text-decoration: none\"><img src=\"/_bors/i/property-16.png\" width=\"16\" height=\"16\" alt=\"prop\" title=\"$popup\" style=\"vertical-align:middle\"/></a>{$x}".($title?"<a rel=\"nofollow\" href=\"{$url}\" title=\"$popup\">{$title}</a>":'');
		else
			return "<img src=\"/_bors/i/property-16.png\" width=\"16\" height=\"16\" alt=\"prop\" title=\"$popup\" style=\"vertical-align:middle\"/>{$x}{$title}";
	}

	function hide_url()
	{
		if(method_exists($obj = $this->object(), 'hide_url'))
			return $obj->hide_url();

		return '/_bors/admin/visibility?act=' . ( $obj->is_hidden() ? 'show' : 'hide') . '&object='.urlencode($obj->internal_uri());
	}
	function imaged_hide_link($title = NULL, $popup = NULL, $unlink_in_admin = true)
	{
		$obj = $this->object();

		$full_title = ($obj->is_hidden() ? ec('Показать ') : ec('Скрыть '))
			.bors_lower($obj->class_title())
			.' '
			.$obj->title();

		if(is_null($title))
			$title = $full_title;

		$x = $title ? '&nbsp;' : '';
		$url = $this->hide_url();

		if(is_null($popup))
			$popup = $full_title;

		if(!bors()->main_object() || $unlink_in_admin && preg_match('!'.preg_quote($obj->admin()->hide_url(), '!').'!', bors()->main_object()->url()))
			$url = '';

		$img = $obj->is_hidden() ? 'visible' : 'hidden';
		$alt = $obj->is_hidden() ? 'show' : 'hide';

		if($url)
			return "<a rel=\"nofollow\" href=\"{$url}\" style=\"text-decoration: none\"><img src=\"/_bors/i/$img-16.gif\" width=\"16\" height=\"16\" alt=\"$alt\" title=\"$popup\" style=\"vertical-align:middle\"/></a>{$x}".($title?"<a rel=\"nofollow\" href=\"{$url}\" title=\"$popup\">{$title}</a>":'');
		else
			return "<img src=\"/_bors/i/$img-16.gif\" width=\"16\" height=\"16\" alt=\"$alt\" title=\"$popup\" style=\"vertical-align:middle\"/>{$x}{$title}";
	}

	function imaged_delete_link($title = NULL, $popup = NULL, $unlink_in_admin = true)
	{
		$obj = $this->real_object();
		if(!$obj->access()->get('can_delete'))
			return '';

		// http://admin.aviaport.ru/digest/origins/3516/ — внизу страницы
		$delete_text = ec('Удаление ')
			.bors_lower($obj->class_title_rp())
			.ec(' «').$obj->get('title').ec('»');

		// Справа в http://admin.aviaport.ru/_bors/admin/edit/synonyms/?real_object=aviaport_directory_aviafirms_firm__1519&object=aviaport_admin_directory_aviafirms_firm__1519&edit_class=http://admin.aviaport.ru/directory/aviafirms/1519/
		if($title===false)
			$title = '';
		// Внизу http://admin.aviaport.ru/digest/origins/3516/
		elseif(is_null($title))
			$title = $delete_text;

		$x = $title ? '&nbsp;' : '';
		$url = $this->delete_url();

		if(is_null($popup))
			$popup = $delete_text;

		if(!bors()->main_object() || 
			($unlink_in_admin 
				&& preg_match('!'.preg_quote($obj->admin()->delete_url(), '!').'!', 
					bors()->main_object()->url()))
		)
			$url = '';

		if($url)
			return "<a rel=\"nofollow\" href=\"{$url}\" style=\"text-decoration: none\"><img src=\"/_bors/i16/delete.png\" width=\"16\" height=\"16\" alt=\"prop\" title=\"$popup\" style=\"vertical-align:middle\"/></a>{$x}".($title?"<a rel=\"nofollow\" href=\"{$url}\" title=\"$popup\">{$title}</a>":'');
		else
			return "<img src=\"/_bors/i16/delete.png\" width=\"16\" height=\"16\" alt=\"prop\" title=\"$popup\" style=\"vertical-align:middle\"/>{$x}{$title}";
	}

	function login_url() { return $this->real_object()->url().'?login'; }
	function imaged_login_link($title = NULL, $popup = NULL, $unlink_in_admin = true)
	{
		$obj = $this->real_object();

		if(is_null($title))
			$title = ec('Вход в систему');

		$x = $title ? '&nbsp;' : '';
		$url = $this->login_url();

		if(is_null($popup))
			$popup = $title;

		if(!bors()->main_object() || preg_match('!\?login$!', bors()->main_object()->url()))
			$url = '';

		if($url)
			return "<a rel=\"nofollow\" href=\"{$url}\" style=\"text-decoration: none\"><img src=\"/_bors/i/login-16.gif\" width=\"16\" height=\"16\" alt=\"prop\" title=\"$popup\" style=\"vertical-align:middle\"/></a>{$x}".($title?"<a rel=\"nofollow\" href=\"{$url}\" title=\"$popup\">{$title}</a>":'');
		else
			return "<img src=\"/_bors/i/login-16.gif\" width=\"16\" height=\"16\" alt=\"prop\" title=\"$popup\" style=\"vertical-align:middle\"/>{$x}{$title}";
	}

	function logout_url() { return $this->real_object()->url().'?logout'; }
	function imaged_logout_link($title = NULL, $popup = NULL, $unlink_in_admin = true)
	{
		if(is_null($title))
			$title = ec('Выход из системы');

		$x = $title ? '&nbsp;' : '';
		$url = $this->logout_url();

		if(is_null($popup))
			$popup = $title;

		return "<a rel=\"nofollow\" href=\"{$url}\" style=\"text-decoration: none\"><img src=\"/_bors/i/logout-16.png\" width=\"16\" height=\"16\" alt=\"prop\" title=\"$popup\" style=\"vertical-align:middle\"/></a>{$x}".($title?"<a rel=\"nofollow\" href=\"{$url}\" title=\"$popup\">{$title}</a>":'');
	}

	function imaged_set_default_link($item, $title = NULL, $popup = NULL)
	{
		if(is_null($title))
			$title = ec('Сделать изображением по умолчанию');
		if(is_null($popup))
			$popup = ec('Сделать изображением по умолчанию');

		if($title)
			$title = "&nbsp;$title";

		return "<a rel=\"nofollow\" href=\"".$this->object()->setdefaultfor_url($item)."\"><img src=\"/_bors/i/set-default-16.gif\" width=\"16\" height=\"16\" alt=\"def\" title=\"$popup\"/>{$title}</a>";
	}

	function edit_links()
	{
		return "/admin/edit/crosslinks/?object={$this->real_object()->internal_uri()}&edit_class={$this->real_object()->admin()->url()}";
	}

	function urls($type = NULL)
	{
		$object = $this->object();

		if(method_exists($object, 'urls') && ($object_url = $object->urls($type)) && !is_object($object_url))
			return $object_url;

		switch($type)
		{
			case 'links':
				return config('admin_host_url')."/_bors/admin/edit/crosslinks/?real_object={$this->real_object()->internal_uri_ascii()}&object={$this->object()->internal_uri_ascii()}&edit_class={$this->real_object()->admin()->url()}";
			case 'synonyms':
				return config('admin_host_url')."/_bors/admin/edit/synonyms/?real_object={$this->real_object()->internal_uri_ascii()}&object={$this->object()->internal_uri_ascii()}&edit_class={$this->real_object()->admin()->url()}";
			case 'new':
				return config('admin_host_url')."/{$object->section_name()}/new/";
		}

		return $this->object()->admin()->url().$type.'/';
	}
}
Back to Top