Skip to content

Commit

Permalink
Added ability to manually trigger a refresh of the VMG Chosen Member …
Browse files Browse the repository at this point in the history
…data table based on pipe-delimited data already in field
  • Loading branch information
lukewilkins committed Dec 16, 2015
1 parent 313c399 commit bccbd13
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 76 deletions.
4 changes: 2 additions & 2 deletions ee2/third_party/vmg_chosen_member/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
*
* @package VMG Chosen Member
* @author Luke Wilkins <[email protected]>
* @copyright Copyright (c) 2011-2014 Vector Media Group, Inc.
* @copyright Copyright (c) 2011-2015 Vector Media Group, Inc.
*/

if ( ! defined('VMG_CM_VERSION'))
{
define('VMG_CM_VERSION', '2.1');
define('VMG_CM_VERSION', '2.2');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion ee2/third_party/vmg_chosen_member/ft.vmg_chosen_member.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*
* @package VMG Chosen Member
* @author Luke Wilkins <[email protected]>
* @copyright Copyright (c) 2011-2014 Vector Media Group, Inc.
* @copyright Copyright (c) 2011-2015 Vector Media Group, Inc.
*/
class Vmg_chosen_member_ft extends EE_Fieldtype
{
Expand Down
169 changes: 168 additions & 1 deletion ee2/third_party/vmg_chosen_member/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*
* @package VMG Chosen Member
* @author Luke Wilkins <[email protected]>
* @copyright Copyright (c) 2011-2014 Vector Media Group, Inc.
* @copyright Copyright (c) 2011-2015 Vector Media Group, Inc.
*/
class ChosenHelper
{
Expand All @@ -33,6 +33,15 @@ public function __construct()

/**
* Gather associated member data info
* @param int $entry_id
* @param int $field_id
* @param int $col_id
* @param int $row_id
* @param int $var_id
* @param array $settings
* @param array $select_fields
* @param string $group_by
* @return array
*/
public function memberAssociations($entry_id = null, $field_id = null, $col_id = null, $row_id = null, $var_id = null, $settings = null, $select_fields = null, $group_by = 'vcm.member_id')
{
Expand Down Expand Up @@ -143,6 +152,8 @@ public function memberAssociations($entry_id = null, $field_id = null, $col_id =

/**
* Ensure required data is available to save this record
* @param array $record
* @return boolean
*/
public function validRecord($record)
{
Expand All @@ -159,6 +170,9 @@ public function validRecord($record)

/**
* Return list of valid Member IDs from selected list
* @param array $selections
* @param array $settings
* @return array
*/
public function validateSelections($selections, $settings)
{
Expand Down Expand Up @@ -195,6 +209,9 @@ public function validateSelections($selections, $settings)

/**
* Clear selections from database that are no longer selected
* @param array $selections
* @param array $settings
* @return int
*/
public function clearOldSelections($selections, $settings)
{
Expand All @@ -219,6 +236,8 @@ public function clearOldSelections($selections, $settings)

/**
* Make draft data live
* @param array $settings
* @return boolean
*/
public function publishDraft($settings)
{
Expand All @@ -233,10 +252,14 @@ public function publishDraft($settings)
->update('vmg_chosen_member', array(
'is_draft' => 0,
));

return true;
}

/**
* Remove all draft entries
* @param array $settings
* @return int
*/
public function discardDraft($settings)
{
Expand All @@ -249,6 +272,7 @@ public function discardDraft($settings)

/**
* Remove records for fields/columns/variables that no longer exist
* @return boolean
*/
public function cleanUp()
{
Expand Down Expand Up @@ -320,10 +344,15 @@ public function cleanUp()
ee()->db->where_in('var_id', $bad_var_rows)
->delete('vmg_chosen_member');
}

return true;
}

/**
* Save current selections to database
* @param array $selections
* @param array $settings
* @return boolean
*/
public function saveSelections($selections, $settings)
{
Expand All @@ -348,10 +377,13 @@ public function saveSelections($selections, $settings)
));

}

return true;
}

/**
* Get all available member groups
* @return boolean
*/
public function getMemberGroups()
{
Expand All @@ -364,6 +396,7 @@ public function getMemberGroups()

/**
* Get all custom member fields
* @return array
*/
public function getCustomMemberFields()
{
Expand All @@ -376,6 +409,9 @@ public function getCustomMemberFields()

/**
* Get action_id for a specific method
* @param string $method
* @param boolean $full_path
* @return string
*/
public function actionId($method, $full_path = false)
{
Expand Down Expand Up @@ -404,6 +440,7 @@ public function actionId($method, $full_path = false)

/**
* Include the required CSS and JS
* @return boolean
*/
public function includeAssets()
{
Expand All @@ -425,6 +462,7 @@ public function includeAssets()

/**
* Build CSS files
* @return array
*/
public function buildCss()
{
Expand All @@ -436,6 +474,7 @@ public function buildCss()

/**
* Build JS files
* @return array
*/
public function buildJs()
{
Expand All @@ -447,6 +486,8 @@ public function buildJs()

/**
* Prefix array for custom output
* @param array $array
* @param string $prefix
*/
public function setPrefix($array, $prefix = '')
{
Expand Down Expand Up @@ -476,6 +517,9 @@ public function setPrefix($array, $prefix = '')

/**
* Remove X number of characters from end of string
* @param string $string
* @param int $backspace
* @return string
*/
public function backspace($string, $backspace)
{
Expand All @@ -488,6 +532,8 @@ public function backspace($string, $backspace)

/**
* Build base fieldtype data array
* @param stdClass &$obj
* @return array
*/
public function initData(&$obj)
{
Expand Down Expand Up @@ -517,6 +563,11 @@ public function initData(&$obj)

/**
* Return settings value by auto handling fallbacks
* @param stdClass &$obj
* @param string $name
* @param string $fallback
* @param boolean $literal_fallback
* @return mixed
*/
public function getSetting(&$obj, $name, $fallback, $literal_fallback = false)
{
Expand All @@ -543,6 +594,10 @@ public function getSetting(&$obj, $name, $fallback, $literal_fallback = false)

/**
* Returns simple preview of a string with formatting removed
* @param string $text
* @param string $search
* @param integer $max_length
* @return string
*/
public function cleanFieldPreview($text, $search, $max_length = 25)
{
Expand All @@ -559,6 +614,10 @@ public function cleanFieldPreview($text, $search, $max_length = 25)

/**
* Get field settings array
* @param int $field_id
* @param int $col_id
* @param int $var_id
* @return array/boolean
*/
public function fieldSettings($field_id, $col_id = 0, $var_id = 0)
{
Expand Down Expand Up @@ -610,6 +669,7 @@ public function fieldSettings($field_id, $col_id = 0, $var_id = 0)

/**
* Get all custom member fields
* @return array
*/
public function customMemberFields()
{
Expand All @@ -622,6 +682,10 @@ public function customMemberFields()

/**
* Member autocomplete results
* @param array $settings
* @param array $search_fields
* @param array $search_fields_where
* @return array
*/
public function memberAutoComplete($settings, $search_fields, $search_fields_where)
{
Expand All @@ -637,6 +701,9 @@ public function memberAutoComplete($settings, $search_fields, $search_fields_whe

/**
* Convert field/column name to ID
* @param string $field_name
* @param string $column_name
* @return array
*/
public function convertFieldName($field_name, $column_name = false)
{
Expand All @@ -662,6 +729,10 @@ public function convertFieldName($field_name, $column_name = false)

/**
* Get Channel Entries containing member
* @param int $field_id
* @param int $col_id
* @param array $member_ids
* @return array
*/
public function associatedChannelEntries($field_id, $col_id, $member_ids)
{
Expand Down Expand Up @@ -721,4 +792,100 @@ public function associatedChannelEntries($field_id, $col_id, $member_ids)
return $results;
}

/**
* Convert data from standard fields in to vmg_chosen_member table
* @return boolean
*/
public function convertStandardFieldData()
{
$fields = ee()->db->select('field_id')
->from('channel_fields')
->where('field_type', 'vmg_chosen_member')
->get()
->result_array();

foreach ($fields AS $field) {
$entries = ee()->db->select("entry_id, '".$field['field_id']."' AS field_id, field_id_".$field['field_id']." AS member_ids", false)
->from('channel_data')
->where('field_id_' . $field['field_id'] . ' !=', '')
->get()
->result_array();

foreach ($entries AS $entry) {
$this->saveAssociation($entry);
}
}

// Convert data from matrix fields
if (ee()->db->table_exists('matrix_cols')) {
$fields = ee()->db->select('col_id, field_id, var_id')
->from('matrix_cols')
->where('col_type', 'vmg_chosen_member')
->get()
->result_array();

foreach ($fields AS $field) {
$entries = ee()->db->select("entry_id, '".$field['field_id']."' AS field_id, '".$field['col_id']."' AS col_id, '".$field['var_id']."' AS var_id, row_id, col_id_".$field['col_id']." AS member_ids", false)
->from('matrix_data')
->where('col_id_' . $field['col_id'] . ' !=', '')
->get()
->result_array();

foreach ($entries AS $entry) {
$this->saveAssociation($entry);
}
}
}

// Convert data from low variable fields
if (ee()->db->table_exists('low_variables')) {
$entries = ee()->db->select("lv.variable_id AS var_id, gv.variable_data AS member_ids", false)
->from('low_variables AS lv')
->join('global_variables AS gv', 'gv.variable_id = lv.variable_id', 'inner')
->where('lv.variable_type', 'vmg_chosen_member')
->where('gv.variable_data !=', '')
->get()
->result_array();

foreach ($entries AS $entry) {
$this->saveAssociation($entry);
}
}

return true;
}

/**
* Save field associations from convertStandardFieldData()
* @param array $entry
* @return boolean
*/
private function saveAssociation($entry)
{
$member_ids = explode('|', $entry['member_ids']);
unset($entry['member_ids']);

$valid_fields = array('entry_id', 'field_id', 'col_id', 'row_id', 'var_id', );
$fields = $field_values = array();
foreach ($entry as $key => $value) {
if (in_array($key, $valid_fields)) {
$fields[] = '`'.$key.'`';
$field_values[] = (int) $value;
}
}

$table = ee()->db->dbprefix.'vmg_chosen_member';

$order = 0;
foreach ($member_ids AS $member_id) {
$member_id = (int) $member_id;
$keys = array_merge(array('`member_id`', '`order`'), $fields);
$values = array_merge(array($member_id, $order++), $field_values);

ee()->db->query("INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).") ON DUPLICATE KEY UPDATE member_id = ".$member_id);
}

return true;
}

}
Loading

0 comments on commit bccbd13

Please sign in to comment.