* @access public
*/
public $migcsaID = false;
+ public $dbh = false;
/**
* Constructor
'FM' => 11,
);
- $dbh = new PDO(
+ $this->dbh = new PDO(
'pgsql: host=ds4.gaslightmedia.com dbname=migcsa2_update user=nobody',
null,
null,
)
);
- $dbh->setAttribute(
+ $this->dbh->setAttribute(
PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION
);
switch( $option ) {
case 'customFields':
- $view = 'customFields';
+ $view = 'customFields';
$response = $this->importCustomFields();
break;
case 'import':
- $view = 'import';
+ $view = 'import';
$response = $this->importNewMembers();
break;
+ case 'employees':
+ $view = 'employees';
+ $response = $this->importEmployees();
+ break;
case 'list':
$view = 'index';
break;
return $response;
}
+ public function importEmployees()
+ {
+ $sql = "
+ SELECT *
+ FROM member_employees";
+ $stmt = $this->dbh->query( $sql );
+ $employees = $stmt->fetchAll();
+
+ // We have the data
+ // Need to get the account id's for member_id and employee based on the old_member_id from member table
+ // So first find the member_id's
+ foreach ( $employees as &$employee ) {
+ // Find the old_member_id for member_id
+ $member_account_id = $this->wpdb->get_var(
+ $this->wpdb->prepare(
+ "SELECT id
+ FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "accounts
+ WHERE ref_dest IN (SELECT id
+ FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "members
+ WHERE old_member_id = %d)",
+ $employee['member_id']
+ )
+ );
+ $employee['member_account_id'] = $member_account_id;
+ // Find the old_member_id for employee
+ $employee_account_id = $this->wpdb->get_var(
+ $this->wpdb->prepare(
+ "SELECT id
+ FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "accounts
+ WHERE ref_dest IN (SELECT id
+ FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "members
+ WHERE old_member_id = %d)",
+ $employee['employee']
+ )
+ );
+ $employee['employee_account_id'] = $employee_account_id;
+ // If we have both of these then add employee (if not already there)
+ $this->addEmployee( $member_account_id, $employee_account_id );
+ }
+
+ return '<pre>$employees: ' . print_r( $employees, true ) . '</pre>';
+ }
+
+ public function addEmployee( $account, $employee )
+ {
+ // Double check that it already doesn't exists
+ $emp_id = $this->wpdb->get_var(
+ $this->wpdb->prepare(
+ "SELECT id
+ FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "employees
+ WHERE account = %d
+ AND employee = %d",
+ $account,
+ $employee
+ )
+ );
+ if ( !$emp_id ) {
+ $this->wpdb->insert(
+ GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'employees',
+ array(
+ 'account' => $account,
+ 'employee' => $employee
+ ),
+ array(
+ '%d',
+ '%d'
+ )
+ );
+ }
+ // Flag the main account as boss
+ $this->wpdb->update(
+ GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'accounts',
+ array( 'boss' => 1 ),
+ array( 'id' => $account ),
+ array( '%d' )
+ );
+ }
+
public function importNewMembers()
{
$response = '';
FROM member M LEFT OUTER JOIN state S ON (S.state_id = M.mailing_state_id)
WHERE M.member_id NOT IN (" . implode( ',', $old_members ) . ")
ORDER BY M.member_id";
- $stmt = $dbh->query( $sql );
+ $stmt = $this->dbh->query( $sql );
$members = $stmt->fetchAll();
$response .= '<pre>$members: ' . print_r( $members, true ) . '</pre>';
SELECT C.name
FROM category C LEFT OUTER JOIN member_category MC ON (C.category_id = MC.category_id)
WHERE MC.member_id = :member_id";
- $getMemberCategories = $dbh->prepare( $categorySql );
+ $getMemberCategories = $this->dbh->prepare( $categorySql );
foreach ( $members as $member ) {
// Initialize $insert_data array.