_db->select(); $select->from($this->pair, array('id', 'unsubscribed')); $select->where('`email` = ' . $this->_db->quote($email)); $select->limit(1); return $this->_db->fetchRow($select); } protected function _isUserInGroup($id, $idGroup) { $select = $this->_db->select(); $select->from(array('p2g' => $this->_getTableName('ContactPerson2Group'))); $select->where('`idPerson` = ' . intval($id)); $select->where('`idGroup` = ' . intval($idGroup)); $select->limit(1); $res = $this->_db->fetchRow($select); return (empty($res)) ? false : true; } /** * Function returns subscription status code for user specified by email * 0 - subscribtion not exists * 1 - subscribtion exists * 2 - user is unsubscribed * * @param string $email * @return int */ public function getSubscriptionStatus($email) { if (empty($this->_newsGroupId)) { throw new Exception('"$_newsGroupId" must be defined in App_Association_AbstractObj'); } $user = $this->_getUserByEmail($email); if (empty($user)) { return App_Association_Subscribe_Obj::SUBSCRIPTION_NOT_EXISTS; } elseif ('y' == $user['unsubscribed']) { return App_Association_Subscribe_Obj::USER_UNSUBSCRIBED; } else { return ($this->_isUserInGroup($user['id'], $this->_newsGroupId)) ? App_Association_Subscribe_Obj::SUBSCRIPTION_EXISTS : App_Association_Subscribe_Obj::SUBSCRIPTION_NOT_EXISTS; } } public function subscribeUser($data) { $user = $this->_getUserByEmail($data['email']); if (empty($user)) { $this->_primaryKey = $this->table->insert($data); $this->_getTable('ContactPerson2Group')->insert( array('idPerson' => $this->_primaryKey, 'idGroup' => $this->_newsGroupId) ); } else { $this->_primaryKey = intval($user['id']); if ('n' == $user['unsubscribed'] && !$this->_isUserInGroup($user['id'], $this->_newsGroupId) ) { $this->_getTable('ContactPerson2Group')->insert( array('idPerson' => $this->_primaryKey, 'idGroup' => $this->_newsGroupId) ); } } return $this->_primaryKey; } public function insert($data = null) { if (null === $data) { $data = $this->_data; } else { $this->_data = $data; } unset($data['id']); $metaData = $this->table->getMetaData(); if (isset($metaData['sorter'])) { $data['sorter'] = $this->_getSorter(); } $this->_db->beginTransaction(); try { $this->subscribeUser($data); $this->_db->commit(); } catch (Exception $e) { Qs_Debug::log($e->getMessage(), 3); $this->_db->rollBack(); return $e; } return $this->_primaryKey; } }