Last active
April 8, 2018 02:24
-
-
Save croxton/13c05d48bc7c8c841028 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 if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
if( ! defined('PATH_THIRD')) { define('PATH_THIRD', APPPATH . 'third_party'); }; | |
/** | |
* Zenbu Transcribe support extension | |
* ========================================= | |
* Enables display of Transcribe language for the entry in Zenbu | |
* @version 1.0.0 | |
* @author Koen Veestraeten (@StudioKong) | |
* Based on the examples Zenbu MX Cloner and Zenbu Tag Formatting provided by Nicolas Bottari | |
* Zenbu MX Cloner: https://github.com/nicolasbottari/zenbu_mx_cloner.zenbu_addon.ee2_addon | |
* Zenbu Tag Formatting: https://github.com/nicolasbottari/zenbu_tag_formatting.zenbu_addon.ee2_addon | |
* ----------------------------------------- | |
* | |
* *** IMPORTANT NOTES *** | |
* I (Koen Veestraeten) am not responsible for any | |
* damage, data loss, etc caused directly or indirectly by the use of this extension. | |
* | |
* REQUIRES | |
* Zenbu module: | |
* @link http://nicolasbottari.com/expressionengine_cms/zenbu/ | |
* Hooks to extend Zenbu: | |
* @see http://nicolasbottari.com/expressionengine_cms/dev/zenbu | |
* | |
* Transcribe module: | |
* @link http://eeharbor.com/transcribe | |
* | |
*/ | |
class Transcribe_zenbu_ext { | |
var $name = 'Zenbu Transcribe support extension'; | |
var $addon_short_name = 'transcribe_zenbu'; | |
var $version = '1.0.0'; | |
var $description = 'Enables display of the entry language in Zenbu'; | |
var $settings_exist = 'n'; | |
var $docs_url = ''; | |
var $settings = array(); | |
/** | |
* Constructor | |
* | |
* @param mixed Settings array or empty string if none exist. | |
*/ | |
function __construct($settings='') | |
{ | |
$this->EE =& get_instance(); | |
$this->settings = $settings; | |
$this->EE->lang->loadfile('transcribe_zenbu'); | |
} | |
/** | |
* =============================== | |
* function hook_zenbu_add_column | |
* =============================== | |
* Adds a row in Zenbu's Display settings section | |
* @return array $output An array of data used by Zenbu | |
* The $output array must have the following keys: | |
* column: Computer-readable used as identifier for settings. Keep it unique! | |
* label: Human-readable label used in the Display settings row. | |
*/ | |
function hook_zenbu_add_column() | |
{ | |
// Get whatever was passed through this hook from previous add-ons | |
$field = $this->EE->extensions->last_call; | |
// Add to this array with this add-on's data | |
$field[] = array( | |
'column' => 'show_language', // Computer/Cylon-readable | |
'label' => $this->EE->lang->line('show_language'), // Human-readable | |
); | |
return $field; | |
} | |
/** | |
* ====================================== | |
* function hook_zenbu_entry_cell_data | |
* ====================================== | |
* Adds data to a Zenbu entry row | |
* @param int $entry_id The current Entry ID | |
* @param array $entry_array An array of all entries found by Zenbu | |
* @param int $channel_id The current channel ID for the entry | |
* | |
* @return array $output An array of data used by Zenbu. | |
* The key must match the computer-readable identifier, minus the 'show_' part. | |
*/ | |
function hook_zenbu_entry_cell_data($entry_id, $entry_array, $channel_id) | |
{ | |
// Get whatever was passed through this hook from previous add-ons | |
$output = $this->EE->extensions->last_call; | |
if (isset($entry_id)) { | |
// Get the language id of the entry | |
$this->EE->db->select('language_id, entry_id'); | |
$this->EE->db->from('transcribe_entries_languages'); | |
$this->EE->db->where('entry_id = "' . $entry_id . '" '); | |
$query = $this->EE->db->get(); | |
if ($query->num_rows() > 0) | |
{ | |
$language_id = $query->row('language_id'); | |
} | |
$query->free_result(); | |
// Get the language name and abbreviation that corresponds with the language id found | |
$this->EE->db->select('name, abbreviation'); | |
$this->EE->db->from('transcribe_languages'); | |
$this->EE->db->where('id = "' . $language_id . '" '); | |
$query = $this->EE->db->get(); | |
if ($query->num_rows() > 0) | |
{ | |
$row = $query->row(); | |
$language_name = $row->name; | |
$language_abbreviation = $row->abbreviation; | |
} | |
$query->free_result(); | |
// Output values for Zenbu table | |
$language_icon = '<img src="'. $this->EE->config->item('theme_folder_url').'third_party/'.$this->addon_short_name.'/flags/'. $language_abbreviation. '.png" title="'.$language_name.'">'; | |
// Build the link to the entry | |
$filter_array = array(); | |
$filter_array["return_to_zenbu"] = "y"; | |
$filter_array = base64_encode(serialize($filter_array)); | |
$output['language'] = anchor(BASE.AMP."C=content_publish".AMP."M=entry_form".AMP."channel_id=".$channel_id.AMP."entry_id=".$entry_id.AMP."filter=".$filter_array, $language_icon); | |
return $output; | |
} | |
} | |
/** | |
* =========================================== | |
* function hook_zenbu_custom_order_sort | |
* =========================================== | |
* Adds custom entry ordering/sorting | |
* Build on top of main Active Record to retrieve Zenbu results | |
* @param string $sort The sort order (asc/desc) | |
* | |
* @return void | |
*/ | |
function hook_zenbu_custom_order_sort($sort) | |
{ | |
// join the transcribe entries xref table and sort on the language column | |
$this->EE->db->join('transcribe_entries_languages', 'channel_titles.entry_id = transcribe_entries_languages.entry_id', 'left'); | |
$this->EE->db->order_by('language_id', $sort); | |
} | |
/** | |
* =========================================== | |
* function hook_zenbu_modify_title_display | |
* =========================================== | |
* Modifies the display of the "title" column in the entry listing | |
* @param string $output The output string to be displayed in the Zenbu column | |
* @param array $entry_array An array containing all the entry_ids of the entry listing results | |
* @param int $row An array of row data for the current entry | |
* @return string $output The final output to be displayed in the Zenbu column | |
* | |
* @return void | |
*/ | |
function hook_zenbu_modify_title_display($output, $entry_array, $row) | |
{ | |
$r = $output; | |
// get the language ID of this entry | |
$this->EE->db->where('entry_id', $row['entry_id']); | |
$entry_language = $this->EE->db->get('transcribe_entries_languages', 1); | |
$entry_language = $entry_language->row(); | |
if( ! empty($entry_language) ) | |
{ | |
$language_id = $entry_language->language_id; | |
$relationship_id = $entry_language->relationship_id; | |
$link = array(); | |
$link[] = BASE; | |
$link[] = 'C=content_publish'; | |
$link[] = 'M=entry_form'; | |
$link[] = 'channel_id=' . $row['channel_id']; | |
$link[] = 'entry_id=' . $row['entry_id']; | |
$link[] = 'relationship=' . $relationship_id; | |
$link[] = 'language=' . $language_id; | |
$link = implode(AMP, $link); | |
$r = '<a href="' . $link . '" class="zenbu_entry_form_link">' . $row['title'] . '</a>'; | |
// get all languages, cache | |
if ( ! isset($this->EE->session->cache[__CLASS__]['languages'])) | |
{ | |
$languages = $this->EE->db->get('transcribe_languages'); | |
$this->EE->session->cache[__CLASS__]['languages'] = $languages->result(); | |
} | |
$languages = $this->EE->session->cache[__CLASS__]['languages']; | |
// get currently defined relationships | |
$relationships = $this->EE->db->get_where('transcribe_entries_languages', array('relationship_id' => $relationship_id)); | |
$relationships = $relationships->result(); | |
$existing_translations = array(); | |
foreach( $relationships as $key => $relationship ) | |
$existing_translations[$key] = $relationship->language_id; | |
if (count($existing_translations) > 1) | |
{ | |
$html_output = array(); | |
foreach( $languages as $language ) | |
{ | |
// determine entry_id, if any | |
$key = array_search($language->id, $existing_translations); | |
$entry_id = ($key === FALSE) ? '' : $relationships[$key]->entry_id; | |
// generate edit/create link | |
$link = array(); | |
$link[] = BASE.AMP.'C=content_publish'.AMP.'M=entry_form'.AMP.'channel_id='.$row['channel_id']; | |
$link[] = (empty($entry_id) ? '' : AMP.'entry_id='.$entry_id); | |
$link[] = 'relationship='.$relationship_id; | |
$link[] = 'language='.$language->id; | |
$link = implode(AMP, $link); | |
if ( ! empty($entry_id)) | |
{ | |
$html_output[] = ' <a href="'.$link.'" title="'.$language->name.'">'.$language->abbreviation.'</a> '; | |
} | |
} | |
$r = $r . ' <span style="color:#8195A0">(</span>'. implode('|', $html_output) . '<span style="color:#8195A0">)</span>'; | |
} | |
} | |
return $r; | |
} | |
/** | |
* Settings Form | |
* | |
* @param Array Settings | |
* @return void | |
*/ | |
function settings_form() | |
{ | |
return ""; | |
} | |
/** | |
* Save Settings | |
* | |
* This function provides a little extra processing and validation | |
* than the generic settings form. | |
* | |
* @return void | |
*/ | |
function save_settings() | |
{ | |
if (empty($_POST)) | |
{ | |
show_error($this->EE->lang->line('unauthorized_access')); | |
} | |
unset($_POST['submit']); | |
$settings = $_POST; | |
$this->EE->db->where('class', __CLASS__); | |
$this->EE->db->update('extensions', array('settings' => serialize($settings))); | |
$this->EE->session->set_flashdata( | |
'message_success', | |
$this->EE->lang->line('preferences_updated') | |
); | |
} | |
function activate_extension() { | |
$data[] = array( | |
'class' => __CLASS__, | |
'hook' => "zenbu_add_column", | |
'method' => "hook_zenbu_add_column", | |
'settings' => serialize(array()), | |
'priority' => 110, | |
'version' => $this->version, | |
'enabled' => "y" | |
); | |
$data[] = array( | |
'class' => __CLASS__, | |
'hook' => "zenbu_entry_cell_data", | |
'method' => "hook_zenbu_entry_cell_data", | |
'settings' => serialize(array()), | |
'priority' => 110, | |
'version' => $this->version, | |
'enabled' => "y" | |
); | |
$data[] = array( | |
'class' => __CLASS__, | |
'hook' => "zenbu_custom_order_sort", | |
'method' => "hook_zenbu_custom_order_sort", | |
'settings' => serialize(array()), | |
'priority' => 110, | |
'version' => $this->version, | |
'enabled' => "y" | |
); | |
$data[] = array( | |
'class' => __CLASS__, | |
'hook' => "zenbu_modify_title_display", | |
'method' => "hook_zenbu_modify_title_display", | |
'settings' => serialize(array()), | |
'priority' => 110, | |
'version' => $this->version, | |
'enabled' => "y" | |
); | |
// insert in database | |
foreach($data as $key => $data) | |
{ | |
$this->EE->db->insert('exp_extensions', $data); | |
} | |
} | |
function disable_extension() { | |
$this->EE->db->where('class', __CLASS__); | |
$this->EE->db->delete('exp_extensions'); | |
} | |
/** | |
* Update Extension | |
* | |
* This function performs any necessary db updates when the extension | |
* page is visited | |
* | |
* @return mixed void on update / false if none | |
*/ | |
function update_extension($current = '') | |
{ | |
if ($current == '' OR $current == $this->version) | |
{ | |
return FALSE; | |
} | |
if ($current < $this->version) | |
{ | |
// Update to version 1.0 | |
} | |
$this->EE->db->where('class', __CLASS__); | |
$this->EE->db->update( | |
'extensions', | |
array('version' => $this->version) | |
); | |
} | |
} | |
// END CLASS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment