'Size Min', 'property_size_max'=>'Size Max'); function BroadcastEmail($id = null) { $this->DBObj($id); $this->tblIndividual = $this->db->tblMember; } function _initFile() { if (!is_object($this->file)) { require_once('class/DB/ImageDB.php'); $this->file = new ImageDB(FILE_DB_TS_FS); } return true; } function initFromForm(&$frm) { $files = array(); $files['attachment'] = $frm->getElementValue('attachment'); $data = $frm->exportValues(); $this->arrayParseUrl2Tag($data); $this->initMarketsFromArray($data); $this->fixMoneyFields($data); if ($data['id_specialty'] == 5) { $data['subject_radio3'] = 'sale'; } $data['id_individual'] = $this->memberData['id']; if ($data['use_range'] == 'n') { $data['property_size_max'] = $data['property_size_min']; } if ($data['property_size_min'] == '') { $data['property_size_min'] = null; } if ($data['property_size_max'] == '') { $data['property_size_max'] = null; } if ($data['id_type'] == 3) { $data['property_size_min'] = null; $data['property_size_max'] = null; } $this->initFromArray($data, $files); return true; } function initFromDB() { $this->_data = $this->getFromDB($this->id); return true; } function initMarketsFromArray(&$data) { $idsArea = $this->initCheckBoxListFromForm($data['id_area']); $data['id_area'] = array(); if ($data['market_group'] == 'out_of_metro') { $data['id_area'] = array(4=>$idsArea[4]); } elseif ($data['market_group'] == 'twin_cities') { switch ((int)$data['id_specialty']) { case 1: $data['id_area'] = array(1=>$idsArea[1]); break; case 2: $data['id_area'] = array(2=>$idsArea[2]); break; case 3: case 5: switch ((int)$data['market_subgroup']){ case 1: $data['id_area'] = array(1=>$idsArea[1]); break; case 2: $data['id_area'] = array(2=>$idsArea[2]); break; case 4: $data['id_area'] = array(3=>$idsArea[3]); break; } break; case 4: $data['id_area'] = array(3=>$idsArea[3]); break; } } } function isAccess4TemplateManagment($id_individual) { $sql = " SELECT IF(Location2Individual.id_individual IS NULL, '0', '1') allow_manage_template FROM {$this->tblIndividual} as Individual LEFT JOIN {$this->db->tblLocation2Individual} as Location2Individual ON Location2Individual.id_parent = Individual.id_location AND Location2Individual.id_individual = Individual.id WHERE Individual.id = ".$this->db->quote($id_individual, 'integer')." "; $res = $this->db->queryOne($sql); $this->_sqlError($res); return (bool)$res; } function getFromDB($id, $field = false) { $id = intval($id); $sql = "SELECT {$this->tableName}.*, CONCAT(Individual.first_name, ' ', Individual.middle_initial, IF (Individual.middle_initial = '' , '', '. '), Individual.last_name) as full_name, Individual.email as individual_email, investment AS listing_investment, investment AS requirements_investment FROM {$this->tableNameDB} AS {$this->tableName} JOIN {$this->tblIndividual} as Individual ON Individual.id = {$this->tableName}.id_individual WHERE {$this->tableName}.id = $id"; $res = $this->db->queryRow($sql); if (null === $res) { return $res; } if ($field === false || $field == 'group_type') { $sql = "SELECT group_id FROM {$this->db->tblEmail2GroupType} WHERE email_id = {$id}"; $groupResult = $this->db->queryCol($sql); foreach ($groupResult as $groupId) { $res['group_type'][$groupId] = 1; } } if ($field === false || $field == 'listing_type' || $field == 'requirements_type') { $sql = "SELECT subtype_id FROM {$this->db->tblEmail2SubType} WHERE email_id = {$id}"; $typeResult = $this->db->queryCol($sql); foreach ($typeResult as $typeId) { $res['requirements_type'][$typeId] = 1; } $res['listing_type'] = $typeResult[0]; } $this->arrayParseTag2Url(&$res); return $this->getSubElem($res, $field); } function getEmailAreaIdsFromDB($tableName, $id_parent) { require_once('app/Member/Member.php'); return Member::getEmailAreaIdsFromDB($tableName, $id_parent); } function getIdsFromDB($tableName, $DTableName, $field_name, $id_parent) { $sql = " SELECT tblLink.{$field_name} as id, tblLink.{$field_name} as title FROM {$tableName} as tblLink JOIN {$DTableName} as Dictionary ON tblLink.{$field_name} = Dictionary.id WHERE tblLink.id_parent = ".$id_parent; $res = $this->db->queryAll($sql, 0, nnull, true); $this->_sqlError($res); return $res; } function initCheckBoxListFromForm($list) { if (is_numeric($list)) { return array($list=>$list); } require_once('app/Member/Member.php'); $ret = Member::initCheckBoxListFromForm($list); return $ret; } function getDAreaByType() { require_once('app/Member/Member.php'); return Member::getDAreaByType(); } function insert($data = null, $files = null) { if (!is_null($data)) { $this->initFromArray($data, $files); } $this->_initTable(); $data = $this->_data; unset($data['id']); if ($data['id_type'] == 1) { $data['investment'] = $data['listing_investment']; } elseif ($data['id_type'] == 1) { $data['investment'] = $data['requirements_investment']; } $fields = $this->getSimpleFieldList(); if (isset($fields['sorter'])) { $data['sorter'] = $this->getSorter(); } $this->id = $this->table->insert($data); $this->updateDependencies(); //$this->updateEmailAreaIds($this->_data['id_area']); $this->handleFiles(); return $this->id; } function update($data = null, $files = null) { if (!is_null($data)) { $this->initFromArray($data, $files); } if ($this->_data['id_type'] == 1) { $this->_data['investment'] = $this->_data['listing_investment']; } elseif ($data['id_type'] == 1) { $this->_data['investment'] = $this->_data['requirements_investment']; } $this->_initTable(); $this->table->update($this->_data, 'id = '. $this->db->quote($this->id, 'integer') ); $this->updateDependencies(); //$this->updateEmailAreaIds($this->_data['id_area']); $this->handleFiles(); return true; } function updateEmailAreaIds($data) { $tableName = $this->tableNameDB.'2Area'; $this->deleteIds($tableName); if (!is_array($data) || empty($data)) { return $data; } $sql = ''; foreach ($data as $id_area_type => $ids_area) { foreach ($ids_area as $id_area) { $sql .= "\n(".intval($this->id).', '.intval($id_area_type).', '.intval($id_area).'),'; } } if (empty($sql)) { return false; } $sql = "INSERT INTO {$tableName} (`id_parent`, `id_area_type`, `id_area`) VALUES".rtrim($sql, ',').';'; $res = $this->db->query($sql); $this->_sqlError($res); } function updateDependencies() { if ($this->id) { require_once('class/DB/DBTable.php'); $tableGroup = new DBTable('Email2GroupType'); $tableSubType = new DBTable('Email2SubType'); $tableGroup->delete('email_id = ' . (int)$this->id); $tableSubType->delete('email_id = ' . (int)$this->id); foreach (array_keys($this->_data['group_type']) as $group_id) { $tableGroup->insert(array( 'email_id' => (int)$this->id, 'group_id' => (int)$group_id )); } if ($this->_data['id_type'] == 1) { $tableSubType->insert(array( 'email_id' => (int)$this->id, 'subtype_id' => (int)$this->_data['listing_type'] )); } elseif ($this->_data['id_type'] == 2) { foreach (array_keys($this->_data['requirements_type']) as $subtype_id) { $tableSubType->insert(array( 'email_id' => (int)$this->id, 'subtype_id' => (int)$subtype_id )); } } } } function updateLocationTemplate($data, $id) { $this->_initLocationTemplateFromForm($data); require_once('class/DB/DBTable.php'); $LocationTemplate = new DBTable('LocationTemplate'); $LocationTemplate->update($data, 'id='.intval($id).' AND id_parent = '.intval($data['id_parent'])); $ret = array( 'id'=>$id, 'templateList'=>$this->getLocationTemplate4Select('title', 'id_parent='.intval($data['id_parent'])), ); return $ret; } protected function _initLocationTemplateFromForm(&$data) { $data['body'] = $this->baseUrl2Tag($data['body']); } protected function _initLocationTemplateFromDb(&$data) { $data['body'] = $this->tag2BaseUrl($data['body']); } function insertLocationTemplate($data) { $this->_initLocationTemplateFromForm($data); require_once('class/DB/DBTable.php'); $LocationTemplate = new DBTable('LocationTemplate'); $id = $LocationTemplate->insert($data); $ret = array( 'id'=>$id, 'templateList'=>$this->getLocationTemplate4Select('title', 'id_parent='.intval($data['id_parent'])), ); return $ret; } public function getLocationTemplate($id_location_template) { $data = $this->getLocationTemplateById($id_location_template); $this->_initLocationTemplateFromDb($data); return $data; } function deleteLocationTemplate($id, $id_parent) { require_once('class/DB/DBTable.php'); $LocationTemplate = new DBTable('LocationTemplate'); $LocationTemplate->delete('id='.intval($id). ' AND id_parent = '.intval($id_parent)); $ret = array( 'templateList'=>$this->getLocationTemplate4Select('title', 'id_parent='.intval($id_parent)), ); return $ret; } function prepareEmail($data, &$Doc) { require_once('lib/htmlMimeMail/htmlMimeMail.php'); $mail = new htmlMimeMail(); $data['body'] = str_replace(BASE_URL.'/broadcastEmailFiles/', '', $data['body']); $Doc->smartyInit(); $Doc->Smarty->assign('item', $data); $html = $Doc->Smarty->fetch('BroadcastEmail/email.tpl'); $mail->setHtml($html); $mail->_findHtmlImages(WWW_PATH.'/broadcastEmailFiles/'); $mail->setFrom('"'.$data['full_name'].'" <'.$data['individual_email'].'>'); $mail->setSubject($data['subject']); $mail->setHeader('X-Mailer', 'HTML Mime mail class'); return $mail; } function sendMail($email, $data, &$Doc) { $mail = $this->prepareEmail($data, &$Doc); $result = $mail->send(array($email)); $err = array(); if (!$result) { $err[] = array('email'=>$email, 'errors'=>$mail->errors); } return $err; } function getRecipients($by = 'individual', $checked = null) { $this->initFromDB(); $data = $this->getData(); $sql = " SELECT Individual.id, Individual.email, CONCAT(Individual.last_name, IF(Individual.last_name = '','', ', '), Individual.first_name, IF (Individual.middle_initial = '' , '', ' '), Individual.middle_initial, IF (Individual.middle_initial = '' , '', '.')) as full_name, IF (Email2Individual.id_individual IS NULL, 'n', 'y') as checked, Email2Individual.error FROM {$this->tblIndividual} as Individual ".(($checked !== true)?'LEFT':'')." JOIN {$this->tableNameDB}2Individual as Email2Individual ON Email2Individual.id_parent = ".$this->db->quote($this->id, 'integer')." AND Email2Individual.id_individual = Individual.id JOIN {$this->tblIndividual}2EmailType as Individual2EmailType ON Individual2EmailType.id_parent = Individual.id AND Individual2EmailType.id_email_type = ".$this->db->quote($data['id_type'], 'integer') . ' '; if (count($data['group_type'])) { $sql .=" JOIN {$this->tblIndividual}2EmailGroup as m2eg ON m2eg.member_id = Individual.id AND m2eg.group_id IN (" . implode(',',array_keys($data['group_type'])) . ") JOIN {$this->db->tblDEmailGroupType} as egt ON egt.id = m2eg.group_id "; } $sql .= 'LEFT JOIN `' . $this->db->tblMember2BlockedMember . '` AS `Member2BlockedMember` ' . 'ON `Member2BlockedMember`.`id_blocked_member` = ' . $this->db->quote($this->memberData['id'], 'integer') . ' ' . 'AND `Member2BlockedMember`.`id_parent` = `Individual`.`id` '; $sql .=" WHERE 1 AND `Member2BlockedMember`.`id_parent` IS NULL AND ( Individual.id_mncar_status IN (1, 3) OR Individual.id_mncar_l_status IN (1, 3) OR Individual.id_misc_status IN (1, 3) ) AND Individual.allow_mcpe_access = 'y' "; $sql .= 'ORDER BY Individual.last_name, Individual.first_name '; $sql = str_replace("\t\t\t", "", $sql); $list = $this->db->queryAll($sql, 0, null, true); $this->isDBError($list); //vdie($sql, $list); return $list; } function getRecipientsBySpecialty() { $list = $this->getRecipients('market'); if (!is_array($list) || empty($list)) return array(); $resList = array(); foreach($list as $k=>$v) { if (!isset($resList[$v['id_area']])) { $resList[$v['id_area']] = array( 'title'=>$v['area_title'], 'items'=>array() ); } $resList[$v['id_area']]['items'][$v['id']] = $v; } //vdie($resList); return $resList; } function getRecipientsByMatches() { $_list = $this->getRecipients('matches'); $list = array( 'one' => array(), 'one_or_more' => array(), ); foreach ($_list as $id => $data) { if ($data['cnt_markets'] == 1) { $list['one'][$id] = $data; } else { $list['one_or_more'][$id] = $data; } } return $list; } function saveEmailSignature($id_individual, $email_signature) { $sql = " UPDATE {$this->tblIndividual} SET email_signature = ".$this->db->quote($email_signature, 'text')." WHERE id = ".$this->db->quote($id_individual, 'integer')." "; $res = $this->db->query($sql); $this->_sqlError($res); } function saveRecipients($ids) { $this->db->query("DELETE FROM {$this->tableNameDB}2Individual WHERE id_parent = " . $this->db->quote($this->id, 'integer')); if (is_array($ids) && !empty($ids)) { $sql = "INSERT INTO {$this->tableNameDB}2Individual (`id_parent`, `id_individual`) VALUES "; $id_parent = intval($this->id); foreach ($ids as $id) { $sql .= "\n ({$id_parent}, " . intval($id) . "),"; } $sql = rtrim($sql, ',').';'; $res = $this->db->query($sql); $this->_sqlError($res); } // $id_email = intval($this->id); // $this->db->query("SET @sorter_{$id_email} := 0 "); // $sql = "UPDATE {$this->tableNameDB}2Individual as Email2Individual " // . "JOINT {$this->db->tblMember} as Individual ON Individual.id = Email2Individual.id_individual"; // $this->db->query($sql); } function getWaitingRecipientsCount() { $sql = " SELECT COUNT(*) FROM {$this->tblIndividual} as Individual JOIN {$this->tableNameDB}2Individual as Email2Individual ON Email2Individual.id_parent = ".$this->db->quote($this->id, 'integer')." AND Email2Individual.id_individual = Individual.id WHERE 1 AND Email2Individual.error IS NULL "; return (int)$this->db->queryOne($sql); } function getRecipientEmails($limit = null) { $sql = " SELECT Individual.id, Individual.email FROM {$this->tblIndividual} as Individual JOIN {$this->tableNameDB}2Individual as Email2Individual ON Email2Individual.id_parent = ".$this->db->quote($this->id, 'integer')." AND Email2Individual.id_individual = Individual.id WHERE 1 AND Email2Individual.error IS NULL ORDER BY Individual.last_name, Individual.first_name "; if ($limit = intval($limit)) { $sql .= ' LIMIT ' . $limit; } return $this->db->queryAll($sql, 0, null, true); } function getReport($sorter = 0) { $sorter = intval($sorter); $sql = " SELECT Email2Individual.id_individual as id, Email2Individual.error, Email2Individual.sorter FROM {$this->tableNameDB}2Individual as Email2Individual WHERE 1 AND Email2Individual.sorter > {$sorter} AND Email2Individual.id_parent = " . $this->db->quote($this->id, 'integer') . " AND Email2Individual.error IS NOT NULL ORDER BY Email2Individual.sorter "; $_list = (array)$this->db->queryAll($sql); $data = array('list' => array()); foreach ($_list as $row) { $data['list'][$row['id']] = $row['error']; } if (empty($_list)) { $data['sorter'] = $sorter; } else { $row = end($_list); $data['sorter'] = (int)$row['sorter']; } $data['waitingCount'] = $this->getWaitingRecipientsCount(); return $data; } function sendBroadcastEmail(&$Doc) { return false; $data = $this->getData(); $mail = $this->prepareEmail($data, &$Doc); $emails = $this->getRecipientEmails(); if (is_array($emails) && !empty($emails)) { foreach ($emails as $id_individual => $email) { $ret = $mail->send(array($email)); if (false === $ret) { $this->logError($this->id, $id_individual); } } } $this->saveSendReport($ret); } function logError($id_email, $id_individual, $sorter) { $this->_log($id_email, $id_individual, 'y', $sorter); } function logSuccess($id_email, $id_individual, $sorter) { $this->_log($id_email, $id_individual, 'n', $sorter); } function _log($id_email, $id_individual, $value, $sorter) { $sql = " UPDATE {$this->tableNameDB}2Individual SET `error` = '{$value}', `sorter` = " . intval($sorter) . " WHERE id_parent = " . intval($id_email) . " AND id_individual = " . intval($id_individual) . " "; $res = $this->db->query($sql); } function saveSendReport($list) { } function delete() { $this->_files = array( 'attachment' => array('del' => 1), ); $this->_data['group_type'] = array(); $this->_data['id_type'] = 3; $this->updateDependencies(); $this->deleteIds($this->tableNameDB.'2Individual'); parent::delete(); return true; } function markCompleted($id = null) { if ($id === null) { $id = $this->id; } $this->_initTable(); $this->table->update(array('completed'=>'y'), 'id = '.$this->db->quote($id, 'integer')); } function isEmailOwner() { if (!isset($this->_data['id_individual'])) { return false; } return ((int)$this->_data['id_individual'] == (int)$this->memberData['id']); } function isCompleted() { if (!isset($this->_data['completed'])) { return false; } return ($this->_data['completed'] == "y"); } function getIndividualFckFilesPath() { $path = 'broadcastEmailFiles/individual/'.intval($this->memberData['id']); $dirName = WWW_PATH.'/'.$path; if (!file_exists($dirName)) { umask(0); if (false === mkdir($dirName, 0777, true)) { sendDeveloperEmail('Can not create directory: '.$dirName); } } return $path; } function getEmailFckFilesPath($id) { $path = 'broadcastEmailFiles/email/'.intval($id); $dirName = WWW_PATH.'/'.$path; if (!file_exists($dirName)) { umask(0); if (false === mkdir($dirName, 0777, true)) { sendDeveloperEmail('Can not create directory: '.$dirName); } } return $path; } function copyEmailFiles() { $individualWebPath = $this->getIndividualFckFilesPath(); $emailWebPath = $this->getEmailFckFilesPath($this->id); $emailBody = $this->getFromDB($this->id, 'body'); $extensions = array('gif', 'jpg', 'jpeg', 'jpe', 'bmp', 'png', 'tif', 'tiff'); preg_match_all('/(?:"|\')([^"\']+\.('.implode('|', $extensions).'))(?:"|\')/Ui', $emailBody, $images); $imageCnt = count($images[1]); $htmlImages = array(); for ( $i = 0; $i < $imageCnt; $i++ ) { $imgFileName = str_replace(BASE_URL.'/', '', urldecode($images[1][$i])); $imgFullName = WWW_PATH.'/'.$imgFileName; if (file_exists($imgFullName)) { $htmlImages[] = $imgFullName; } } if (!is_array($htmlImages) || empty($htmlImages)) { return false; } foreach ($htmlImages as $file) { $destinationFile = str_replace($individualWebPath, $emailWebPath, $file); if (!$this->createDirecroty(dirname($destinationFile))) { continue; } if (!file_exists($destinationFile)) { if (false === copy($file, $destinationFile)) { sendDeveloperEmail("File copy error.\nSource File: ".$file."\nDestination File: ".$destinationFile); continue; } } else { // sendDeveloperEmail("Destination file already exists: ".$destinationFile); } } $emailBody = str_replace($individualWebPath, $emailWebPath, $emailBody); $this->_initTable(); $data = array('body'=>$emailBody); $this->arrayParseUrl2Tag($data); $this->table->update($data, 'id='.$this->db->quote($this->id, 'integer')); } function createDirecroty($dirName) { if (file_exists($dirName) && is_dir($dirName)) { return true; } umask(0); if (false === mkdir($dirName, 0777, true)) { sendDeveloperEmail('Can not create directory: '.$dirName); return false; } return true; } function deleteNotCompletedEmails() { $sql = " SELECT id FROM {$this->tableNameDB} as Email WHERE Email.id_individual = ".$this->db->quote($this->memberData['id'], 'integer')." AND Email.completed = 'n' "; $ids_email = $this->db->queryCol($sql); foreach ($ids_email as $id_email) { $id_email = intval($id_email); $sql = " DELETE FROM {$this->tableNameDB}2Individual WHERE id_parent = {$id_email} "; $res = $this->db->query($sql); $sql = " DELETE FROM {$this->tableNameDB}2Area WHERE id_parent = {$id_email} "; $res = $this->db->query($sql); $sql = " DELETE FROM {$this->tableNameDB} WHERE id_individual = ".$this->db->quote($this->memberData['id'], 'integer')." AND completed = 'n' "; $res = $this->db->query($sql); } } function fixMoneyFields(&$data) { foreach ($this->moneyFields as $name=>$title) { if (!empty($data[$name])) { $data[$name] = str_replace(array(',', ' '), '', $data[$name]); } } } function getRecipientsSorter() { $sql = " SELECT MAX(sorter) FROM {$this->tableNameDB}2Individual WHERE id_parent = " . $this->db->quote($this->id, 'integer'). " "; return (int)$this->db->queryOne($sql) + 1; } function getEmailId4Send() { $sql = " SELECT e.id FROM {$this->tableNameDB}2Individual AS e2i JOIN {$this->db->tblEmail} AS e ON e.id = e2i.id_parent WHERE e2i.error IS NULL GROUP BY e2i.id_parent ORDER BY e2i.id_parent "; $list = $this->db->queryCol($sql); return $list; } }