tableName = DB_PREFIX_SERVICE.'FormMailReq'; $this->_filterFields = array($this->tableName.'FldVal.value'); $this->id_fm = intval($id_fm); parent::DBObj($id); } function insert($lang) { require_once 'class/DB/Lang/DLang.php'; $DLang = new DLang(); $doc = null; $FormMail_ShowView = SiteMap::getObj('FormMail/Show/ShowView.php'); $form = $FormMail_ShowView->getForm($doc, $this->id_fm, $DLang->getDefault()); $data = $this->_data; $data['id_fm'] = $this->id_fm; $data['lang'] = $lang; parent::insert($data); $sql = "UPDATE {$this->tableNameDB} SET ip = INET_ATON(".$this->db->quote($_SERVER['REMOTE_ADDR']).") WHERE id = ".$this->db->quote($this->id); $this->db->query($sql); foreach ($form->getElements() as $sorter => $element) { if ($element->getLabel()){ $data = array('id_fmr' => $this->id, 'id_fmfld' => intval(str_replace(array(']', 'fild['), '', $element->getName())), 'sorter' => $sorter, 'type' => $element->getType(), 'label' => $element->getLabel(), ); $id_fmrf = $this->fld->insert($data); foreach ($this->_getValues($element) as $value) { $data = array('id_fmrf' => $id_fmrf, 'value' => $value ); $this->fldVal->insert($data); } } } return $this->id; } function _getValues($element) { $values = (array)$this->getSubElem($this->_data, $element->getName()); switch ($element->getType()) { case 'group': $elements = $element->getElements(); if (count($elements)){ if ('checkbox' == $elements[0]->getType()) { $values = array_keys($values); } } break; case 'file': $val = $element->getValue(); $this->_initFile(); $values = array( (string)$this->file->handleUpload($element->getValue()) ) ; break; } return $values; } function _initTable() { if (!is_object($this->table)) { require_once('class/DB/DBTable.php'); $this->table = new DBTable($this->tableName); $this->fld = new DBTable($this->tableName.'Fld'); $this->fldVal = new DBTable($this->tableName.'FldVal'); } return true; } function save($lang) { $this->insert($lang); $this->send(); } function send() { $res = $this->getFromDB($this->id); if ($res['fm']['email_to']){ $mime = $this->_getMailmime($res); if ($res['fm']['email_from']){ $mime->setFrom($res['fm']['email_from']); } $mime->setSubject($res['fm']['subject']); foreach (explode(',', $res['fm']['email_to']) AS $email){ $email = trim($email); if (!empty($email)){ $mime->send(array($email)); } } return true; } return false; } function _getMailmime($res) { include('lib/htmlMimeMail/htmlMimeMail.php'); $mime =& new htmlMimeMail(); require_once('class/Doc/Doc.php'); $Doc = new Doc( array('tpl' => SiteMap::getPath('FormMail/tpl/body.tpl')) ); $Doc->addContent( array('tpl' => SiteMap::getPath('FormMail/tpl/mail.tpl'), 'mail' => $res) ); $mime->setText($Doc->fetch()); $this->_initFile(); foreach ($res['fields'] AS $field){ if ('file' == $field['type']){ $fileArr = $this->file->getArr($field['value'][0]); $filePath = $this->file->saveFS('tmp/', $fileArr['name']); $mime->addAttachment($mime->getFile($filePath), $fileArr['name'], $fileArr['type']); unlink($filePath); } } return $mime; } function getFromDB($id, $field = false) { $res = parent::getFromDB($id); $sql = "SELECT * FROM {$this->tableNameDB}Fld WHERE id_fmr = ".$this->db->quote($id); $res['fields'] = $this->db->queryAll($sql); foreach ($res['fields'] as $key => $fld) { $sql = "SELECT value FROM {$this->tableNameDB}FldVal WHERE id_fmrf = ".$this->db->quote($fld['id']); $res['fields'][$key]['value'] = $this->db->queryCol($sql); } $FormMail = SiteMap::getObj('FormMail/FormMail.php'); $res['fm'] = $FormMail->getFromDB($res['id_fm']); return $this->getSubElem($res, $field); } function _getWhere4Grid($opt) { return parent::_getWhere4Grid($opt)." AND {$this->tableName}.id_fm = ".$this->db->quote($this->id_fm).' '; } function _getWhat4Grid() { return "{$this->tableName}.id, {$this->tableName}.added, INET_NTOA({$this->tableName}.ip) AS ip "; } function _getGroup4Grid() { return "GROUP BY {$this->tableName}.id"; } function _getJoin4Grid() { return " INNER JOIN {$this->tableNameDB}Fld AS {$this->tableName}Fld ON {$this->tableName}Fld.id_fmr = {$this->tableName}.id INNER JOIN {$this->tableNameDB}FldVal AS {$this->tableName}FldVal ON {$this->tableName}Fld.id = {$this->tableName}FldVal.id_fmrf "; } function getList4Grid($opt = array() ) { $start = 0; $ipp = 0; $ipp = 1024 * 1024 * 1024; extract($opt); $list = array(); $this->db->setLimit($ipp, $start); $sql = "SELECT ".$this->_getWhat4Grid($opt)." FROM {$this->tableNameDB} AS {$this->tableName} ". $this->_getJoin4Grid($opt).' '. $this->_getWhere4Grid($opt).' '. $this->_getGroup4Grid($opt).' '. $this->_getOrder4Grid($opt); $list['list'] = $this->db->queryAll($sql); if (PEAR::isError($list['list'])) { dump($list['list']->getUserInfo(), 'getList4Grid error'); } $sql = "SELECT COUNT(DISTINCT {$this->tableName}.id) FROM {$this->tableNameDB} AS {$this->tableName} ". $this->_getJoin4Grid($opt).' '. $this->_getWhere4Grid($opt).' '; $list['num_rows'] = $this->db->queryOne($sql); $list['num_pages'] = 1 + floor(($list['num_rows'] - 1) / $ipp); foreach ($list['list'] as $key => $itm){ $sql = "SELECT {$this->tableNameDB}Fld.id_fmfld, {$this->tableNameDB}FldVal.value FROM {$this->tableNameDB}Fld INNER JOIN {$this->tableNameDB}FldVal ON {$this->tableNameDB}FldVal.id_fmrf = {$this->tableNameDB}Fld.id WHERE {$this->tableNameDB}Fld.id_fmr = ".$this->db->quote($itm['id']); $vals = $this->db->queryAll($sql, null, MDB2_FETCHMODE_ASSOC, true, false, true); $row = array(); foreach ($vals as $keyFld => $vals){ $row[$keyFld] = implode('; ', $vals); }; $list['list'][$key] += $row; }; return $list; } /* function getList4Grid($opt = array() ) { $start = 0; $ipp = 0; $ipp = 1024 * 1024; extract($opt); $Field = SiteMap::getObj('FormMail/Field/Field.php', $this->id_fm); $fields = $Field->getList4Grid(); $sql = "SELECT {$this->tableName}.* FROM "; foreach ($fields['list'] AS $fld){ vdie($fld); } // $Field->getList4Grid(); vdie( $Field->getList4Grid() ); if ('' == $this->_getGroup4Grid($opt)){ $this->db->setLimit($ipp, $start); $res = $this->_getRes4Grid($this->_getWhat4Grid($opt), $opt); $all = $res->fetchAll(); $res->free(); $num_rows_res = $this->_getRes4Grid('COUNT(*)', $opt); $num_rows = $num_rows_res->fetchOne(); $num_rows_res->free(); }else{ $res = $this->_getRes4Grid($this->_getWhat4Grid($opt), $opt); $res->seek($start); $all = array(); while ( ($row = $res->fetchRow(MDB2_FETCHMODE_ASSOC)) && count($all) < $ipp) { $all[] = $row; }; $num_rows = $res->numRows(); $res->free(); }; // $this->db->query("SELECT CO"); $list = array( 'num_rows' => $num_rows, 'num_pages' => 1 + floor(($num_rows - 1) / $ipp), 'list' => $all, ); return $list; } */ } ?>