Skip to content

Instantly share code, notes, and snippets.

@3D-I
Created February 11, 2016 02:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 3D-I/d58ee343394fe4f3b361 to your computer and use it in GitHub Desktop.
Save 3D-I/d58ee343394fe4f3b361 to your computer and use it in GitHub Desktop.
<?php
/**
*
* notebbcode (Prime Note Bbcode) - extension
*
* @copyright (c) 2016 3Di (based on the hard work of Matt Friedman/VSE)
*
* @license GNU General Public License, version 2 (GPL-2.0)
*
*/
namespace threedi\notebbcode\migrations;
class notebbcode_1 extends \phpbb\db\migration\migration
{
static public function depends_on()
{
return array('\phpbb\db\migration\data\v310\dev');
}
/**
* {@inheritdoc}
*/
public function update_data()
{
return array(
array('custom', array(array($this, 'install_notebbcodes'))),
);
}
/**
* Installs BBCodes, used by migrations to perform add/updates
*
* @param array $bbcode_data Array of BBCode data to install
* @return null
* @access public
*/
public function install_notebbcodes($bbcode_data)
{
// Load the acp_bbcode class
if (!class_exists('acp_bbcodes'))
{
include($this->phpbb_root_path . 'includes/acp/acp_bbcodes.' . $this->php_ext);
}
$bbcode_tool = new \acp_bbcodes();
/**
* @var array An array of bbcodes data to install
*/
$bbcode_data = array(
'note=' => array(
'bbcode_helpline' => '[note]note text[/note]',
'bbcode_match' => '[note]{TEXT}[/note]',
'bbcode_tpl' => '<span class="prime_bbcode_note_spur" onmouseover="show_note(this);" onmouseout="hide_note(this);" onclick="lock_note(this);"></span><span class="prime_bbcode_note">{TEXT}</span>',
'display_on_posting'=> 1,
),
'note=' => array(
'bbcode_helpline' => '[note=text-to-note]note text[/note]',
'bbcode_match' => '[note={TEXT1}]{TEXT2}[/note]',
'bbcode_tpl' => '<span class="prime_bbcode_note_text" onmouseover="show_note(this);" onmouseout="hide_note(this);" onclick="lock_note(this);">{TEXT1}</span><span class="prime_bbcode_note_spur" onmouseover="show_note(this);" onmouseout="hide_note(this);" onclick="lock_note(this);"></span><span class="prime_bbcode_note">{TEXT2}</span>',
'display_on_posting'=> 0,
),
);
foreach ($bbcode_data as $bbcode_name => $bbcode_array)
{
// Build the BBCodes
$data = $bbcode_tool->build_regexp($bbcode_array['bbcode_match'], $bbcode_array['bbcode_tpl']);
$bbcode_array += array(
'bbcode_tag' => $data['bbcode_tag'],
'first_pass_match' => $data['first_pass_match'],
'first_pass_replace' => $data['first_pass_replace'],
'second_pass_match' => $data['second_pass_match'],
'second_pass_replace' => $data['second_pass_replace']
);
$sql = 'SELECT bbcode_id
FROM ' . BBCODES_TABLE . "
WHERE LOWER(bbcode_tag) = '" . strtolower($bbcode_name) . "'
OR LOWER(bbcode_tag) = '" . strtolower($bbcode_array['bbcode_tag']) . "'";
$result = $this->db->sql_query($sql);
$row_exists = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
if ($row_exists)
{
// Update existing BBCode
$bbcode_id = $row_exists['bbcode_id'];
$sql = 'UPDATE ' . BBCODES_TABLE . '
SET ' . $this->db->sql_build_array('UPDATE', $bbcode_array) . '
WHERE bbcode_id = ' . $bbcode_id;
$this->db->sql_query($sql);
}
else
{
// Create new BBCode
$sql = 'SELECT MAX(bbcode_id) AS max_bbcode_id
FROM ' . BBCODES_TABLE;
$result = $this->db->sql_query($sql);
$max_bbcode_id = $this->db->sql_fetchfield('max_bbcode_id');
$this->db->sql_freeresult($result);
if ($max_bbcode_id)
{
$bbcode_id = $max_bbcode_id + 1;
// Make sure it is greater than the core BBCode ids...
if ($bbcode_id <= NUM_CORE_BBCODES)
{
$bbcode_id = NUM_CORE_BBCODES + 1;
}
}
else
{
$bbcode_id = NUM_CORE_BBCODES + 1;
}
if ($bbcode_id <= BBCODE_LIMIT)
{
$bbcode_array['bbcode_id'] = (int) $bbcode_id;
// set display_on_posting to 1 by default, so if 0 is desired, set it in our data array
$bbcode_array['display_on_posting'] = (int) !isset($bbcode_array['display_on_posting']);
$this->db->sql_query('INSERT INTO ' . BBCODES_TABLE . ' ' . $this->db->sql_build_array('INSERT', $bbcode_array));
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment