Created
February 11, 2016 23:18
-
-
Save 3D-I/f50685ed334b62342e0f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
} | |
public function update_data() | |
{ | |
return array( | |
array('custom', array(array($this, 'install_notebbcodes'))), | |
); | |
} | |
public function revert_data() | |
{ | |
return array( | |
array('custom', array(array(&$this, 'remove_notebbcodes'))), | |
); | |
} | |
/** | |
* Remove BBCodes, used by migrations to perform removes | |
* | |
* @param array $bbcode_tags Array of BBCode tags to remove | |
* @return null | |
* @access public | |
*/ | |
public function remove_notebbcodes($bbcode_tags) | |
{ | |
global $cache; | |
/** | |
* @var array An array of bbcodes tags to remove | |
*/ | |
$bbcode_tags = array('note', 'note='); | |
// remove existing bbcodes | |
$sql = 'DELETE FROM ' . BBCODES_TABLE . ' | |
WHERE ' . $this->db->sql_in_set('bbcode_tag', $bbcode_tags) . ' | |
OR bbcode_id < 0'; | |
$this->db->sql_query($sql); | |
// purge the bbcodes' cache | |
$cache->destroy('sql', BBCODES_TABLE); | |
} | |
/** | |
* 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_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>', | |
'bbcode_helpline' => '[note]note text[/note]', | |
'display_on_posting'=> 1, | |
), | |
'note=' => array( | |
'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>', | |
'bbcode_helpline' => '[note=text-to-note]note text[/note]', | |
'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['bbcode_helpline']); | |
$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; | |
$bbcode_array['display_on_posting'] = ((int) $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