Forum Moderators: coopster
<?php
class SettingsModel extends Model {
public $fields = array(
'avatar_location',
'avatar_type',
'facebook',
'age',
'twitter',
'youtube',
'website',
'description',
'name',
'gender',
'location',
);
public $integer_fields = array(
'mainmenu_noicons'
);
public function doUpdate($args, $mid) {
$data = array();
//this takes $_POST variables and compares them with the
// gdu_membersettings fields, compiling them into an array
foreach ($args as $key => $value) {
$field = str_replace('field_', '', $key);
if (in_array($field, $this->fields)) {
$data[$field] = $value;
} elseif (in_array($field, $this->integer_fields)) {
$value = (int) $value;
$data[$field] = $value;
}
}
//Update member settings
$query = new Query("UPDATE");
$query->table("gdu_membersettings");
foreach ($data as $key => $value) {
$query->set($key . " = ?", $value);
}
$query->where("settings_mid = ?", $mid);
$query->limit(1);
$stmt = $query->prepare();
$stmt->execute();
}
}
?> <?php
namespace gdu\models;
use hydrogen\model\Model;
use hydrogen\database\Query;
require_once(ROOT_PATH . "/lib/gdu/sqlbeans/PfieldsBean.php");
use gdu\sqlbeans\PfieldsBean;
class SettingsModel extends Model {
protected static $modelID = "settingsmodel";
/**
*
* @var array This array contains table names and their corresponing
* column names, which are matched based the name of a form elememnt
* EG gfb is the form field where the user enters their facebook ID
*/
public $fields = array('ibf_pfields_content' => array(
'grn' => array('gdu_real_name', 0),
'gfb' => array('gdu_facebook', 1),
'gtw' => array('gdu_twitter', 0),
'gyt' => array('gdu_youtube', 0),
'gwb' => array('gdu_website', 0)
));
/**
* Returns an SQLBean: ibf_pfields_content
*
* @param int $member_id
* @return object
*/
public function getPfields($member_id) {
$query = new Query("SELECT");
$query->where("member_id = ?", $member_id);
$pfields = PfieldsBean::select($query, true);
//if member_id not found
if (count($pfields) == 0) {
return false;
} else {
$pfields = $pfields[0];
return $pfields;
}
}
/**
* This function will use the form elements name to see if there is a
* match in the $fields array. A match will return a table and a column
* name. It will then update those columns using the correct SQLBean by
* calling the doUpdate() function.
*
* @param array $args this is usually $_POST
* @param object SQLBean for ibf_members
* @param object SQLBean for ibf_profile_portal
* @param object SQLBean for ibf_pfields_content
*/
public function prepareUpdate($args, $ibf_member, $ibf_profileportal, $ibf_pfields) {
$args_ibf_member = array();
$args_ibf_profileportal = array();
$args_ibf_pfields = array();
$count = 0;
//start the sorting
foreach ($_POST as $key => $p) {
//catch all data for ibf_pfields
if (array_key_exists($key, $this->fields['ibf_pfields_content'])) {
$args_ibf_pfields[$count]['column'] = $this->fields['ibf_pfields_content'][$key][0];
$args_ibf_pfields[$count]['value'] = $p;
$args_ibf_pfields[$count]['int'] = $this->fields['ibf_pfields_content'][$key][1];
}
$count++;
}
//run the function to update if matches were found
if (count($args_ibf_pfields) > 0) {
$this->doUpdate($ibf_pfields, $args_ibf_pfields);
}
}
private function doUpdate($bean, $args) {
foreach ($args as $arg) {
if ($arg['int'] == 0) {
$bean->$arg['column'] = $arg['value'];
} else {
//if int column, add the int thing
$bean->$arg['column'] = (int) $arg['value'];
}
}
$bean->update();
}
}
//end class
?>