JSztuczki

101
JSztuczki Tomasz Dziuda – JoomlaDay 2015

Transcript of JSztuczki

JSztuczkiTomasz Dziuda – JoomlaDay 2015

15

Sztuczki podstawoweDla początkujących

1/5 Nadpisywanie modułówi elementów stronyBy nie modyfikować core Joomla!

Widoki komponentów

Widoki komponentów

Wiadomości systemowe

Widoki komponentów

Widoki modułów

Wiadomości systemowe

Widoki komponentów

Widoki modułów

Wiadomości systemowe

Style modułów

Widoki komponentów

Widoki modułów

Wiadomości systemowe

Style modułów

Stronicowanie

2/5 Dodatkowe widokiFormatowanie odpowiedzi serwera

https://mydomain.com/index.php?tmpl=special

https://mydomain.com/index.php?tmpl=special

special.phpw katalogu szablonu

https://mydomain.com/index.php?tmpl=special

special.phpw katalogu szablonu

Uwaga! Niektóre rozszerzenia, takie jakAdmin Tools mogą blokować niestandardowewywołania parametru tmpl. Należy wtedy takąwartość dodać do listy dozwolonych wywołań.

3/5 Ograniczone wyszukiwanie

Gdy potrzebujemy większej elastyki wyszukiwarki

com_finder i filtry wyszukiwania

com_finder i filtry wyszukiwania

com_finder i filtry wyszukiwania

com_finder i filtry wyszukiwania

com_finder i filtry wyszukiwania

4/5 Nadpisywanie frazjęzykowychBez dotykania plików

1

1

2

1

1

2

1

12

12

3

5/5 Złożona składnia tytułówwpisów i modułówBo czasem nie wystarczy zwykły tekst…

Podział na linie i wyróżnianie słów

Lorem[br]ipsum[br]dolor

Lorem __ipsum__ dolor

Lorem **ipsum** dolor

$title = str_replace('[br]', '<br />', $module->title);

Podział na linie i wyróżnianie słów

$title = str_replace('[br]', '<br />', $module->title);

$title = preg_replace('/__(.*?)__/i', ’<em>${1}</em>', $title);

Podział na linie i wyróżnianie słów

Sztuczki średnio-złożoneDla biegłych użytkowników Joomla!

1/5 JLESSParser LESS-a jest w core Joomla!

$app = JFactory::getApplication();$path = JPATH_THEMES . '/' . $this->template;

$app = JFactory::getApplication();$path = JPATH_THEMES . '/' . $this->template;$less = new JLess;$less->setFormatter(new JLessFormatterJoomla);

$app = JFactory::getApplication();$path = JPATH_THEMES . '/' . $this->template;$less = new JLess;$less->setFormatter(new JLessFormatterJoomla);

try {$less->compileFile($path.'/main.less', $path.'/main.css');

}

$app = JFactory::getApplication();$path = JPATH_THEMES . '/' . $this->template;$less = new JLess;$less->setFormatter(new JLessFormatterJoomla);

try {$less->compileFile($path.'/main.less', $path.'/main.css');

} catch (Exception $e) {$app->enqueueMessage($e->getMessage(), 'error');

}

2/5 JLayoutsPo co się powtarzać?

$layout = new JLayoutFile(‘path.param.x’);

echo $layout->render($data);

<?php $msgList = $displayData['msgList']; ?><div id="system-message-container">

<?php if (is_array($msgList) && !empty($msgList)) : ?><div id="system-message">

<?php foreach ($msgList as $type => $msgs) : ?><div class="alert alert-<?php echo $type; ?>">

<a class="close" data-dismiss="alert">×</a><?php if (!empty($msgs)) : ?><h4 class="alert-heading"><?php echo JText::_($type); ?></h4>

<div><?php foreach ($msgs as $msg) : ?><p class="alert-message"><?php echo $msg; ?></p><?php endforeach; ?>

</div><?php endif; ?>

</div><?php endforeach; ?>

</div><?php endif; ?>

</div>

Plik: layouts/joomla/system/message.php

$layout = new JLayoutFile('joomla.content.tags', null, array('debug' => true)

);

echo $layout->render(null);

$layout = new JLayoutFile('joomla.content.tags', null, array('debug' => true)

);

echo $layout->render(null);

Layout: joomla.content.tagsInclude Paths: Array (

[0] => /www/test/templates/gk_technews/html/layouts/com_content[1] => /www/test/components/com_content/layouts [2] => /www/test/templates/gk_technews/html/layouts [3] => /www/test/layouts

) Searching layout for: joomla/content/tags.phpFound layout: /www/test/layouts/joomla/content/tags.php

$layout = new JLayoutFile('joomla.content.tags', JPATH_SITE . '/components/com_custom/layouts', array('debug' => true)

);

$layout = new JLayoutFile('joomla.content.tags', JPATH_SITE . '/components/com_custom/layouts', array('debug' => true)

);

Layout: joomla.content.tagsInclude Paths: Array (

[0] => /www/test/components/com_custom/layouts[1] => /www/test/templates/gk_technews/html/layouts/com_content[2] => /www/test/components/com_content/layouts [3] => /www/test/templates/gk_technews/html/layouts [4] => /www/test/jqi/layouts

) Searching layout for: joomla/content/tags.phpFound layout: /www/test/layouts/joomla/content/tags.php

$layout = new JLayoutFile('joomla.content.tags', null,array(

'debug' => true,'component' => ‘com_tags','client' => 1,'suffixes' => array('rtl', 'j34')

));

$layout = new JLayoutFile('joomla.content.tags', null,array(

'debug' => true,'component' => ‘com_tags','client' => 1,'suffixes' => array('rtl', 'j34')

));

Layout: joomla.content.tagsInclude Paths: Array (

[0] => /www/test/templates/gk_technews/html/layouts/com_tags[1] => /www/test/administrator/components/com_tags/layouts[2] => /www/test/templates/gk_technews/html/layouts [3] => /www/test/layouts

) Suffixes: Array (

[0] => rtl[1] => j34

)Searching layout for: joomla/content/tags.rtl.phpSearching layout for: joomla/content/tags.j34.php Searching layout for: joomla/content/tags.phpFound layout: /www/test/layouts/joomla/content/tags.php

3/5 Dodatkowe polaw edytorachBy nie udziwniać składni wpisów

class plgSystemPlg_Custom_Params extends JPlugin {var $_pluginPath;

}

class plgSystemPlg_Custom_Params extends JPlugin {var $_pluginPath;

function __construct($subject) {parent::__construct($subject);$this->_plugin = JPluginHelper::getPlugin('system', 'plg_custom_params');$this->_pluginPath = JPATH_PLUGINS."/system/plg_custom_params/";

}

}

class plgSystemPlg_Custom_Params extends JPlugin {var $_pluginPath;

function __construct($subject) {parent::__construct($subject);$this->_plugin = JPluginHelper::getPlugin('system', 'plg_custom_params');$this->_pluginPath = JPATH_PLUGINS."/system/plg_custom_params/";

}

function onContentPrepareForm($form, $data) {if ($form->getName()=='com_content.article') {

JForm::addFormPath($this->_pluginPath);$form->loadFile('parameters', false);return true;

}}

}

class plgSystemPlg_Custom_Params extends JPlugin {var $_pluginPath;

function __construct($subject) {parent::__construct($subject);$this->_plugin = JPluginHelper::getPlugin('system', 'plg_custom_params');$this->_pluginPath = JPATH_PLUGINS."/system/plg_custom_params/";

}

function onContentPrepareForm($form, $data) {if ($form->getName()=='com_content.article') {

JForm::addFormPath($this->_pluginPath);$form->loadFile('parameters', false);return true;

}}

}

<?xml version="1.0" encoding="utf-8"?><form>

<fields name="attribs"><fieldset name="basic" label="Test">

<field name="custom_field1" type="text" default="" label="Field 1" /><field name="custom_field2" type="text" default="" label="Field 2" />

</fieldset></fields>

</form>

<?xml version="1.0" encoding="utf-8"?><form>

<fields name="attribs"><fieldset name=“test" label="Test">

<field name="custom_field1" type="text" default="" label="Field 1" /><field name="custom_field2" type="text" default="" label="Field 2" />

</fieldset></fields>

</form>

$article_attribs = json_decode($this->item->attribs);

echo $article_attribs[’custom_field1'];

4/5 Avatary we wpisachBo ludzi pamięta się z twarzy…

https://pl.gravatar.com/

$author_obj = JFactory::getUser($this->item->created_by);$author_email = $author_obj->email;

$author_obj = JFactory::getUser($this->item->created_by);$author_email = $author_obj->email;

$avatar_hash = md5(strtolower(trim($author_email)));$avatar_size = 120;

$author_obj = JFactory::getUser($this->item->created_by);$author_email = $author_obj->email;

$avatar_hash = md5(strtolower(trim($author_email)));$avatar_size = 120;

$avatar_url = ‘//www.gravatar.com/avatar/’.$avatar_hash .‘.jpg’;$avatar_url .= ‘?s=‘ . $avatar_size;

5/5 Zmiana szablonu dlawybranych podstronBez generowania złożonych struktur w menu…

public function onAfterRoute() {$app = JFactory::getApplication();

if ($app instanceof JApplicationSite) {$jinput = $app->input;$option = $jinput->getCmd('option', '');$view = $jinput->getCmd('view', '');$params = new JRegistry();$params->def('templateColor', '#f00');$params->def('templateBackgroundColor', '#000');

if($option === 'com_content' && $view === 'article’) {

$app->setTemplate('protostar', $params);}

}}

public function onAfterRoute() {$app = JFactory::getApplication();

if ($app instanceof JApplicationSite) {$jinput = $app->input;$option = $jinput->getCmd('option', '');$view = $jinput->getCmd('view', '');$params = new JRegistry();$params->def('templateColor', '#f00');$params->def('templateBackgroundColor', '#000');

if($option === 'com_content' && $view === 'article’) {

$app->setTemplate('protostar', $params);}

}}

public function onAfterRoute() {$app = JFactory::getApplication();

if ($app instanceof JApplicationSite) {$jinput = $app->input;$option = $jinput->getCmd('option', '');$view = $jinput->getCmd('view', '');$params = new JRegistry();$params->def('templateColor', '#f00');$params->def('templateBackgroundColor', '#000');

if($option === 'com_content' && $view === 'article’) {$app->setTemplate('protostar', $params);

}}

}

public function onAfterRoute() {$app = JFactory::getApplication();

if ($app instanceof JApplicationSite) {$jinput = $app->input;$option = $jinput->getCmd('option', '');$view = $jinput->getCmd('view', '');$params = new JRegistry();$params->def('templateColor', '#f00');$params->def('templateBackgroundColor', '#000');

if($option === 'com_content' && $view === 'article’) {$app->setTemplate('protostar', $params);

}}

}

Sztuczki zaawansowaneCoś dla koneserów

1/5 Wtyczki bez wtyczekPo co instalować dodatkowe paczki?

$dispatcher = JEventDispatcher::getInstance();

$dispatcher = JEventDispatcher::getInstance();

$dispatcher->register('onAfterRender', 'CustomParser');

$dispatcher = JEventDispatcher::getInstance();

$dispatcher->register('onAfterRender', 'CustomParser');

function CustomParser(){$body = JResponse::getBody();

JResponse::setBody($body);}

$dispatcher = JEventDispatcher::getInstance();

$dispatcher->register('onAfterRender', 'CustomParser');

function CustomParser(){$body = JResponse::getBody();$body = str_replace('ABC', 'XYZ', $body);JResponse::setBody($body);

}

2/5 JImageOperacje na grafikach

$img = new JImage(__DIR__ . '/images/logo.png');

$img = new JImage(__DIR__ . '/images/logo.png');

$img->resize(320, 320, false, JImage::SCALE_FILL);

$img = new JImage(__DIR__ . '/images/logo.png');

$img->resize(320, 320, false, JImage::SCALE_FILL);

$img->crop(160, 160, 80, 80, false);

$img = new JImage(__DIR__ . '/images/logo.png');

$img->resize(320, 320, false, JImage::SCALE_FILL);

$img->crop(160, 160, 80, 80, false);

$img->rotate(45, 0x000000, false);

$img = new JImage(__DIR__ . '/images/logo.png');

$img->resize(320, 320, false, JImage::SCALE_FILL);

$img->crop(160, 160, 80, 80, false);

$img->rotate(45, 0x000000, false);

$img->filter('negate');

$img = new JImage(__DIR__ . '/images/logo.png');

$img->resize(320, 320, false, JImage::SCALE_FILL);

$img->crop(160, 160, 80, 80, false);

$img->rotate(45, 0x000000, false);

$img->filter('negate');

$img->toFile(__DIR__ . '/images/new.jpg', IMAGETYPE_JPEG);

3/5 Wykrywanieadministratora postronie front-enduMoże się zdarzyć, że musisz o tym wiedzieć

Front-end

Back-end

Wartość przechowywanaw plikach cookie

0 = front-end1 = back-end

0 = zalogowany1 = niezalogowany

Warto sprawdzić czy sesja nie wygasła

$config = \JFactory::getConfig(); $session_lifetime = $config->get('lifetime’);

Dodatkowo można porównaćwartość ciągu user-agent

Dane użytkownika

4/5 Złożone kontrolkiBo czasem pole tekstowe i color picker to za mało…

<input type=“hidden”>

<input type=“hidden”>

Zmiana ustawień

json_encode($config)

<input type=“hidden”>

Zmiana ustawień

json_encode($config)

Wczytanie ustawień

json_decode($config)

5/5 Własne sposobyautoryzacjiJak pragnę OAuth…

function onUserAuthenticate($credentials, $options, &$response) {

// kod obsługujący autoryzację

return $response;}

$credentials[’username’] // Login użytkownika

$credentials['password’] // Hasło użytkownika

$user = JUser::getInstance($result->id);

$user = JUser::getInstance($result->id);

$response->email = $user->email; $response->username = $user->username; $response->fullname = $user->name;

$user = JUser::getInstance($result->id);

$response->email = $user->email; $response->username = $user->username; $response->fullname = $user->name;

if(JFactory::getApplication()->isAdmin()) { $response->language = $user->getParam('admin_language');

} else { $response->language = $user->getParam('language');

}

// Poprawne zalogowanie$response->status = JAuthentication::STATUS_SUCCESS;

// Błędne zalogowanie$response->status = JAuthenticaton::STATUS_FAILURE;

Pytania?