Как написать простую Социальную сеть
Posted by Vladimir Shovin with 0 comments
$response = array(
"success" => true,
"complete" => true,
"data" => $data
);
echo json_encode($response);
Приведу немного образцов кода для осуществления CRUD на примере отправки сообщений:
function sendProfileMsg(text) {
var data = {
userid: anketaUserId,
msg: text
};
data["token"] = token;
$.post(sendProfileMsgUrl,
data,
function(data) {
if (data.complete) {
getAllProfileMsgs();
}
},
"json"
);
}
public function sendmsgAction() {
header('Access-Control-Allow-Origin: http://svlaboratory.org:8080');
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
$userId = $this->_getParam('userid');
$msg = $this->_getParam('msg');
$authHelper = new Ext_Controller_Action_Helper_Auth();
$token = $this->_getParam('token', null);
$ip = $_SERVER['REMOTE_ADDR'];
$myUserId = $authHelper->getUserId($token, $ip);
$response = array(
"success" => true,
"complete" => true
);
$modelChat = new Model_Chat();
$data = array(
"message" => $msg,
"user_id_from" => $myUserId,
"user_id_to" => $userId,
);
$modelChat->save($data);
$this->notificateUser($userId, $myUserId);
echo json_encode($response);
}
class Ext_Model
{
protected $_dbAdapter;
/**
*
* @var array
*/
protected $_fields = array();
protected $_primaryKey = array();
/**
*
* @var Zend_Db_Table_Abstract
*/
protected $_dbTable = null;
/**
*
* @var string
*/
protected $_dbTableClass = null;
protected $_id=null;
public function getId()
{
return $this->_id;
}
public function setId($id)
{
$this->_id=$id;
}
public function find($id)
{
$id = (int) $id;
$select = $this->getDBTable()->select();
//$this->_beforeFetch($select); //ERROR delete comment with Join User becouse next id must be c.id
$select->where("id = ?", $id);
$row = $this->getDBTable()->fetchRow($select);
if (!$row) {
return array();
}
return $row->toArray();
}
public function fetchRowManyWhere($conditions = array())
{
$select = $this->getDBTable()->select();
$this->_beforeFetch($select);
foreach ($conditions as $field => $value) {
$select->where("`$field` = ?", $value);
}
$row = $this->getDBTable()->fetchRow($select);
if (!$row) {
return array();
}
return $row->toArray();
}
public function fetchAllManyWhere($conditions = array(), $limit = null, $offset = null, $order = null)
{
$select = $this->getDBTable()->select();
$this->_beforeFetch($select);
foreach ($conditions as $field => $value) {
$select->where("`$field` = ?", $value);
}
if ($order !== null) {
$select->order($order);
}
if ($limit !== null || $offset !== null) {
$select->limit($limit, $offset);
}
$rows = $this->getDBTable()->fetchAll($select);
if (!$rows) {
return array();
}
return $rows->toArray();
}
public function save($data)
{
$insertId = null;
$data = $this->_beforeSave($data);
// операции для сохранения записи
$cleanData = array();
$pk = array();
foreach ($this->_fields as $field) {
if (array_key_exists($field, $data)) {
$cleanData[$field] = $data[$field];
}
}
foreach ($this->_primaryKey as $field => $value) {
if (isset($cleanData[$value])) {
$pk[$value] = $cleanData[$value];
}
}
if (!empty($cleanData)) {
if (empty($pk)) {
$insertId = $this->insert($cleanData);
$data["id"] = $insertId;
} else {
$this->update($pk, $cleanData);
}
}
$this->_afterSave($data);
return $insertId;
}
public function update($id, $data)
{
$data = $this->_beforeUpdate($data);
$where = "";
foreach ($this->_primaryKey as $key => $values) {
if ($key > 0 and $key <= count($this->_primaryKey)) {
$where .= ' and ';
}
$where .= "`$values`" . " = " . "$id[$values]";
unset($data[$values]);
}
$this->getDBTable()->update($data, $where);
$data=$id+$data;
$this->_afterUpdate($data);
}
public function insert($data)
{
$data = $this->_beforeInsert($data);
$this->getDBTable()->insert($data);
$this->setId($this->getDBTable()->getAdapter()->lastInsertId());
$this->_afterInsert($data);
return $this->getDBTable()->getAdapter()->lastInsertId();
}
public function delete($id)
{
$id=$this->_beforeDelete($id);
$this->getDBTable()->delete("id = " . (int) $id);
$this->_afterDelete($id);
}
public function deleteByAttrib($att, $value)
{
$select = $this->getDBTable()->select();
$where = $this->getDBTable()->getAdapter()->quoteInto("`$att` = ?", $value);
$this->getDBTable()->delete($where);
}
public function getDBTable()
{
if (!$this->_dbTable) {
if (!$this->_dbTableClass) {
throw new Ext_Model_Exception('DB table class is not set');
}
$this->_dbTable = new $this->_dbTableClass;
}
return $this->_dbTable;
}
public function setDBTable(Zend_Db_Table_Abstract $table)
{
$this->_dbTable = $table;
return $this;
}
public function lastInsertId()
{
return $this->getDBTable()->getAdapter()->lastInsertId();
}
protected function _beforeFetch($select)
{
}
protected function _beforeSave($data)
{
return $data;
}
protected function _afterSave($data)
{
return $data;
}
protected function _beforeUpdate($data)
{
return $data;
}
protected function _afterUpdate($data)
{
return $data;
}
protected function _beforeInsert($data)
{
return $data;
}
protected function _afterInsert($data)
{
return $data;
}
protected function _beforeDelete($id)
{
return $id;
}
protected function _afterDelete($id)
{
return $id;
}
}
class Model_DbTable_Chat extends Zend_Db_Table_Abstract
{
protected $_name = 'chat';
}
class Model_Chat extends Ext_Model
{
protected $_dbTableClass = 'Model_DbTable_Chat';
protected $_fields = array(
'id',
'message',
'description',
'user_id_from',
'user_id_to',
'date',
);
protected $_primaryKey = array('id');
protected function _beforeInsert($data)
{
$data['date'] = date("Y-m-d H:i:s", time());
return $data;
}
public function fetchAllMsg($iser_id1, $iser_id2)
{
$select = $this->getDBTable()->select();
$this->_beforeFetch($select);
$sql = "SELECT * FROM chatbotchat WHERE (user_id_from = :userid1 AND user_id_to = :userid2) OR (user_id_from = :userid2 AND user_id_to = :userid1) Order by date ASC";
$rows = $this->getDBTable()->getAdapter()->fetchAll($sql, array("userid1" => $iser_id1, "userid2" => $iser_id2));
if (!$rows) {
return array();
}
return $rows;
}
public function fetchReadAllMsg($iser_id1, $iser_id2)
{
$select = $this->getDBTable()->select();
$this->_beforeFetch($select);
$sql = "UPDATE chatbotchat SET is_read = 1 WHERE (user_id_from = :userid2 AND user_id_to = :userid1)";
$this->getDBTable()->getAdapter()->query($sql, array("userid1" => $iser_id1, "userid2" => $iser_id2));
}
public function fetchProfilesMsg($user_id)
{
$select = $this->getDBTable()->select();
$this->_beforeFetch($select);
$sql = "SELECT * FROM chatbotchat WHERE (user_id_from = :userid OR user_id_to = :userid) Order by date DESC";
$rows = $this->getDBTable()->getAdapter()->fetchAll($sql, array("userid" => $user_id));
if (!$rows) {
return array();
}
return $rows;
}
}
-- -----------------------------------------------------
-- Table `svlab`.`user`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `svlab`.`user` (
`id` INT AUTO_INCREMENT ,
`first_name` VARCHAR(45) DEFAULT NULL,
`last_name` VARCHAR(200) DEFAULT NULL,
`nickname` VARCHAR(200) DEFAULT NULL,
`gender` VARCHAR(200) DEFAULT NULL,
`birth_date` DATETIME DEFAULT NULL,
`avatar_url` VARCHAR(200) DEFAULT NULL,
`min_avatar_url` VARCHAR(200) DEFAULT NULL,
`email` VARCHAR(45) NOT NULL,
`password` VARCHAR(45) DEFAULT "12345",
`password_recover` VARCHAR(15) DEFAULT NULL,
`country` VARCHAR(200) DEFAULT NULL,
`region` VARCHAR(200) DEFAULT NULL,
`city` VARCHAR(200) DEFAULT NULL,
`postal_code` VARCHAR(200) DEFAULT NULL,
`street` VARCHAR(200) DEFAULT NULL,
`house_number` VARCHAR(200) DEFAULT NULL,
`add_address_info` VARCHAR(200) DEFAULT NULL,
`site` VARCHAR(200) DEFAULT NULL,
`about_me_info` TEXT DEFAULT NULL,
`registration_date` DATETIME NOT NULL,
`comment_rating` FLOAT DEFAULT 0,
`comment_rating_pos` FLOAT DEFAULT 0,
`comment_rating_neg` FLOAT DEFAULT 0,
`white_ips` TEXT,
`is_active` TINYINT(1) DEFAULT false,
PRIMARY KEY (`id`) ,
UNIQUE INDEX `user_id_unique` (`id` ASC) ,
UNIQUE INDEX `user_email_unique` (`email` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `svlab`.`chat`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `svlab`.`chat` (
`id` INT NOT NULL AUTO_INCREMENT,
`message` TEXT,
`user_id_from` INT NOT NULL,
`user_id_to` INT NOT NULL,
`date` DATETIME,
`is_read` TINYINT(1) DEFAULT false,
PRIMARY KEY (`id`),
UNIQUE INDEX `chatbotchat_id_unique` (`id` ASC))
ENGINE=MyISAM DEFAULT CHARSET=utf8;
Описание Социальной сети и мобильное приложение тут:
http://svlaboratory.org/blog/blog-single/articleid/59
Один из возможных минимальных
Список литературы (для изучения с нуля)
Читать можно в том же порядке
1. А. Кириленко Самоучитель HTML. - Питер - 2005 - 272 с.
2. Д. Макфарланд JavaScript и jQuery - Эксмо. - 2017 - 880 с.
3. Э.А. Майер CSS Карманный справочник - Вильямс. - 2018 - 288 с.
4. Д. Флэнаган JavaScript: карманный справочник - Вильямс. - 2018 - 320 с.
5. Н.А. Прохороренок jQuery. Новый стиль программирования на JavaScript - Вильямс. - 2010 -272 с.
6. М. В. Кузнецов, И.В. Симдянов Самоучитель PHP 7. - БХВ-Петербург - 2018 - 448 с.
7. В. Васвани Zend Framework: Разработка веб-приложений на PHP - Питер, 2012 - 432 с.
8. А. Бьюли Изучаем SQL - Символ-Плюс. - 2007 - 312 с.
9. В.А. Дронов HTML, JavaScript, PHP и MySQL. Джентльменский набор Web-мастера - BHV-СПб - 2018 -
Количество просмотров: 660