Add social sharing

parent 8ae3ec80
......@@ -18,6 +18,10 @@ class LessonController extends BaseController
public function actionView($id)
{
$model = Lessons::findOne($id);
\Yii::$app->view->registerMetaTag([
'property' => 'og:title',
'content' => $model->title
]);
return $this->render('view', ['model' => $model]);
}
}
<?php
namespace common\modules\school\controllers;
use common\components\BaseController;
use common\modules\school\models\UserStatistics;
class StatisticsController extends BaseController
{
public static function actionsTitles()
{
return [
'Shares' => 'Публикации в соцсетях',
];
}
public function actionShares()
{
if (!\Yii::$app->user->isGuest){
$nameSoc = $_POST['name'];
$lessonId = $_POST['lesson'] ? $_POST['lesson'] : UserStatistics::NOT_LESSON;
$userId = \Yii::$app->user->id;
$shareUser = UserStatistics::findOne([$nameSoc => UserStatistics::SELECTED_SOC_NETWORK, 'user_id' => $userId, 'lesson_id' => $lessonId]);
if (!$shareUser){
$shareUser = new UserStatistics();
$shareUser->{$nameSoc} = UserStatistics::SELECTED_SOC_NETWORK;
$shareUser->user_id = $userId;
$shareUser->lesson_id = $lessonId;
$shareUser->save();
}
}
}
}
......@@ -55,7 +55,7 @@ class Courses extends \common\components\ActiveRecordModel
{
return [
[['title', 'description', 'type'], 'required'],
[['type', 'spec_proposition'], 'integer', 'max' => 11],
[['type', 'spec_proposition'], 'integer'],
[['title'], 'string', 'max' => 150],
[['image'], 'string', 'max' => 100],
[['description', 'description_for_course'], 'safe'],
......
......@@ -39,7 +39,7 @@ class Lessons extends \common\components\ActiveRecordModel
[['title', 'video_id', 'course_id', 'number'], 'required'],
[['title'], 'string', 'max' => 150],
[['video_id'], 'string', 'max' => 100],
[['course_id', 'number'], 'integer', 'max' => 11],
[['course_id', 'number'], 'integer'],
[['text'], 'safe'],
];
}
......
<?php
namespace common\modules\school\models;
class UserStatistics extends \common\components\ActiveRecordModel
{
const NOT_LESSON = 0;
const SELECTED_SOC_NETWORK = 1;
public static function tableName()
{
return 'user_statistics';
}
public function name()
{
return 'Публикации в соцсетях';
}
public function attributeLabels()
{
return [
'lesson_id' => 'Урок',
'user_id' => 'Пользователь',
'facebook' => 'Facebook',
'vkontakte' => 'Вконтакте',
'gplus' => 'Google+',
'twitter' => 'Twitter',
'tumblr' => 'Tumblr',
];
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['user_id'], 'required'],
[['lesson_id', 'user_id', 'facebook', 'vkontakte', 'gplus', 'twitter', 'tumblr'], 'integer'],
];
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
];
}
}
......@@ -3,6 +3,7 @@
//use kartik\social\FacebookPlugin;
//echo FacebookPlugin::widget(['appId'=>'FACEBOOK_APP_ID']);
use common\modules\school\assets\WidgetAssetBundle;
use yii\web\View;
WidgetAssetBundle::register($this);
......@@ -26,7 +27,25 @@ WidgetAssetBundle::register($this);
<div class="sh_social">
<script type="text/javascript" src="//yastatic.net/es5-shims/0.0.2/es5-shims.min.js" charset="utf-8"></script>
<script type="text/javascript" src="//yastatic.net/share2/share.js" charset="utf-8"></script>
<div class="ya-share2" data-services="vkontakte,facebook,gplus,twitter,tumblr" data-title="Школа аналитики"></div>
<div id="my-share"></div>
<?php $this->registerJs("Ya.share2('#my-share', {
hooks: {
onshare: function (name) {
$.ajax({
type: 'POST',
url: '/school/statistics/shares',
data: {'name': name},
success: function(data){}
});
}
},
theme: {
services: 'vkontakte,facebook,gplus,twitter,tumblr'
},
content: {
title: 'Школа аналитики',
}
});", View::POS_END, 'my-options');?>
</div>
</div>
</div>
......
<?php
use common\modules\users\widgets\UserBoxWidget;
use common\modules\school\assets\WidgetAssetBundle;
use yii\web\View;
WidgetAssetBundle::register($this);
?>
......@@ -27,15 +28,30 @@ WidgetAssetBundle::register($this);
<h1 class="vc-title">Урок <?php echo $model->number;?>. <?php echo $model->title;?></h1>
</div>
<div class="col-md-3 col-md-offset-1 col-xs-4 col-sm-12">
<ul class="sh_social">
<li>
<a href="#"><img src="/images/icon/sh_social_vk.png" height="30" width="30" alt=""></a>
<a href="#"><img src="/images/icon/sh_social_fb.png" height="30" width="30" alt=""></a>
<a href="#"><img src="/images/icon/sh_social_tw.png" height="30" width="30" alt=""></a>
<a href="#"><img src="/images/icon/sh_social_gp.png" height="30" width="30" alt=""></a>
<a href="#"><img src="/images/icon/sh_social_t.png" height="30" width="30" alt=""></a>
</li>
</ul>
<div class="sh_social">
<script type="text/javascript" src="//yastatic.net/es5-shims/0.0.2/es5-shims.min.js" charset="utf-8"></script>
<script type="text/javascript" src="//yastatic.net/share2/share.js" charset="utf-8"></script>
<div id="my-share"></div>
<?php $this->registerJs("Ya.share2('#my-share', {
hooks: {
onshare: function (name) {
lesson = ".$model->id.";
$.ajax({
type: 'POST',
url: '/school/statistics/shares',
data: {'name': name, 'lesson': lesson},
success: function(data){}
});
}
},
theme: {
services: 'vkontakte,facebook,gplus,twitter,tumblr'
},
content: {
title: 'Школа аналитики',
}
});", View::POS_END, 'my-options');?>
</div>
</div>
</div>
<div class="row">
......
<?php
use yii\db\Schema;
use yii\db\Migration;
class m160215_210504_user_statistics extends Migration
{
public function safeUp()
{
$tableOptions = null;
if ($this->db->driverName === 'mysql')
{
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB';
}
// Структура таблицы `user_statistics`
$this->createTable('user_statistics', [
'id' => Schema::TYPE_PK,
'lesson_id' => Schema::TYPE_INTEGER . '(11)',
'user_id' => Schema::TYPE_INTEGER . '(11) NOT NULL',
'facebook' => Schema::TYPE_INTEGER . '(11)',
'vkontakte' => Schema::TYPE_INTEGER . '(11)',
'gplus' => Schema::TYPE_INTEGER . '(11)',
'twitter' => Schema::TYPE_INTEGER . '(11)',
'tumblr' => Schema::TYPE_INTEGER . '(11)',
], $tableOptions);
$this->createIndex('FK_user_statistics_users', 'user_statistics', 'user_id');
$this->createIndex('FK_user_statistics_lessons', 'user_statistics', 'lesson_id');
}
public function safeDown()
{
$this->dropTable('user_statistics');
}
}
......@@ -388,7 +388,6 @@ $(document).ready(function() {
$(".menu").removeClass("menu_active");
$(".toggle-mnu").removeClass("on");
});
});
$(window).load(function() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment