Created
February 24, 2012 05:32
-
-
Save pnomolos/1898030 to your computer and use it in GitHub Desktop.
Better Expression Engine - the start of a class to make your life simpler when dealing with EE programmatically.
This file contains hidden or 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 (!class_exists('Better_EE')) { | |
class Better_EE { | |
public static $field_defaults = array( | |
'site_id' => 1, | |
'field_type' => 'text', | |
'field_fmt' => 'none', | |
'field_show_fmt' => 'n', | |
'field_maxl' => 500, | |
'field_label' => '', | |
'field_settings' => array( | |
'field_content_type' => 'all', | |
'field_show_smileys' => 'n', | |
'field_show_glossary' => 'n', | |
'field_show_spellcheck' => 'n', | |
'field_show_formatting_btns' => 'n', | |
'field_show_file_selector' => 'n', | |
'field_show_writemode' => 'n' | |
) | |
); | |
public static $field_formatting_defaults = array('none','br','xhtml'); | |
public $ee = null; | |
public $db = null; | |
public function __construct($ee) { | |
if (!$ee) { | |
throw new EE_Exception('Instance of EE class required'); | |
} | |
$this->ee = $ee; | |
$this->db = $ee->db; | |
} | |
public function add_field($data = array()) { | |
$errors = array(); | |
$field_formatting = self::$field_formatting_defaults; | |
if (isset($data['field_formatting'])) { | |
$field_formatting = (array)$data['field_formatting']; | |
unset($data['field_formatting']); | |
} | |
$data = array_merge_recursive(self::$field_defaults, $data); | |
// Seriously? Base64 encoded _and_ serialized? | |
$data['field_settings'] = base64_encode(serialize($data['field_settings'])); | |
foreach (array('field_name', 'group_id') as $field) { | |
if (!isset($data[$field])) | |
$errors[] = $field; | |
} | |
if (count($errors)) { | |
throw new EE_Exception(join(', ', $errors) . ' not defined in $data'); | |
} | |
if (!$data['field_label']) $data['field_label'] = $data['field_name']; | |
// Check for field existence in exp_channel_fields and add if necessary | |
$field_exists = $this->db->query( | |
"SELECT field_id FROM exp_channel_fields WHERE | |
site_id = '{$data['site_id']}' AND group_id = '{$data['group_id']}' AND field_name = '{$data['field_name']}'" | |
); | |
$field_id = $field_exists->row('field_id'); | |
if (!$field_id) { | |
if (!$this->db->query($this->db->insert_string('exp_channel_fields', $data))) { | |
throw new EE_Exception('Unable to add field ' . $data['field_name'] . ' to exp_channel_fields'); | |
} | |
$field_id = $this->db->insert_id(); | |
} | |
// Check for field existence in exp_channel_data and add if necessary | |
$found_field = false; | |
$describe = $this->db->query( | |
"DESCRIBE exp_channel_data" | |
); | |
if ($describe->num_rows() > 0) { | |
foreach ($describe->result_array() as $row) { | |
if (('field_id_' . $field_id) == $row['Field']) { | |
$found_field = true; break; | |
} | |
} | |
} | |
if (!$found_field) { | |
if (!$this->db->query("ALTER TABLE exp_channel_data ADD COLUMN field_id_{$field_id} TEXT, ADD COLUMN field_ft_{$field_id} TINYTEXT")) { | |
throw new EE_Exception('Unable to modify exp_channel_data to add field ' . $data['field_name'] . ' with field_id of ' . $field_id); | |
} | |
} | |
$field_exists = $this->db->query( | |
"SELECT field_id FROM exp_field_formatting WHERE field_id = {$field_id}" | |
); | |
if (!$field_exists->row('field_id')) { | |
foreach ($field_formatting as $format) { | |
$format_data = array('field_id' => $field_id, 'field_fmt' => $format); | |
$this->db->query($this->db->insert_string('exp_field_formatting', $format_data)); | |
} | |
} | |
} // add_field | |
public function delete_field($field_id) { | |
if (!is_numeric($field_id)) | |
return false; | |
$this->db->query("DELETE FROM exp_channel_fields WHERE field_id = '{$field_id}'"); | |
$this->db->query("DELETE FROM exp_field_formatting WHERE field_id = '{$field_id}'"); | |
$this->db->query("ALTER TABLE exp_channel_data DROP COLUMN field_id_{$field_id}, DROP COLUMN field_ft_{$field_id}"); | |
return true; | |
} // delete_field | |
} | |
class EE_Exception extends Exception {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment