+class ProfilePhones implements ProfileSetting
+{
+ private $tel;
+ private $pub;
+ protected $link_type;
+ protected $link_id;
+
+ public function __construct($type, $link_id)
+ {
+ $this->tel = new ProfileTel();
+ $this->pub = new ProfilePub();
+ $this->link_type = $type;
+ $this->link_id = $link_id;
+ }
+
+ public function value(ProfilePage &$page, $field, $value, &$success)
+ {
+ $success = true;
+ if (is_null($value)) {
+ $value = array();
+ $res = XDB::iterator('SELECT display_tel AS tel, tel_type AS type, pub, comment
+ FROM profile_phones
+ WHERE pid = {?} AND link_type = {?}
+ ORDER BY tel_id',
+ $page->pid(), $this->link_type);
+ if ($res->numRows() > 0) {
+ $value = $res->fetchAllAssoc();
+ } else {
+ $value = array(
+ 0 => array(
+ 'type' => 'fixed',
+ 'tel' => '',
+ 'pub' => 'private',
+ 'comment' => '',
+ )
+ );
+ }
+ }
+ foreach ($value as $key=>&$phone) {
+ if (isset($phone['removed']) && $phone['removed']) {
+ unset($value[$key]);
+ } else {
+ unset($phone['removed']);
+ $phone['pub'] = $this->pub->value($page, 'pub', $phone['pub'], $s);
+ $phone['tel'] = $this->tel->value($page, 'tel', $phone['tel'], $s);
+ if(!isset($phone['type']) || ($phone['type'] != 'fixed' && $phone['type'] != 'mobile' && $phone['type'] != 'fax')) {
+ $phone['type'] = 'fixed';
+ $s = false;
+ }
+ if (!$s) {
+ $phone['error'] = true;
+ $success = false;
+ }
+ if (!isset($phone['comment'])) {
+ $phone['comment'] = '';
+ }
+ }
+ }
+ return $value;
+ }
+
+ private function saveTel($pid, $telid, array &$phone)
+ {
+ if ($phone['tel'] != '') {
+ XDB::execute("INSERT INTO profile_phones (pid, link_type, link_id, tel_id, tel_type,
+ search_tel, display_tel, pub, comment)
+ VALUES ({?}, {?}, {?}, {?}, {?},
+ {?}, {?}, {?}, {?})",
+ $pid, $this->link_type, $this->link_id, $telid, $phone['type'],
+ format_phone_number($phone['tel']), $phone['tel'], $phone['pub'], $phone['comment']);
+ }
+ }
+
+ public function save(ProfilePage &$page, $field, $value)
+ {
+ XDB::execute("DELETE FROM profile_phones
+ WHERE pid = {?} AND link_type = {?} AND link_id = {?}",
+ $page->pid(), $this->link_type, $this->link_id);
+ $this->saveTels($page->pid(), $field, $value);
+ }
+
+ //Only saves phones without a delete operation
+ public function saveTels($pid, $field, $value)
+ {
+ foreach ($value as $telid=>&$phone) {
+ $this->saveTel($pid, $telid, $phone);
+ }
+ }
+}
+