Commit 78b7518e authored by o.tarasiuk@internetdevels.com's avatar o.tarasiuk@internetdevels.com
Browse files

firststep complate

parent 2256ced4
<?php
use \Drupal\Core\Entity\EntityInterface;
use \Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Field\TypedData\FieldItemDataDefinition;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Database\Connection;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Database\Database;
use Drupal\node\Entity\Node;
/**
* Implements hook_entity_view().
*/
function dummy_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
if ($display->getMode() == 'full') {
if (isset($entity->field_node_title)) {
$nid = $entity->id();
$paragraph_id = $entity->field_node_title->target_id;
$p = Paragraph::load("$paragraph_id");
if (isset($p->field_reviews)) {
$counter = $p->field_reviews->value;
$counter++;
// drupal_set_message(t("Переглянули ноду $counter "));
// $p->field_reviews->setValue("$counter");
}
if (isset($p->field_users)) {
$uid = \Drupal::currentUser()->id();
$users = $p->field_users->getValue();
$boolval = TRUE;
foreach ($users as $v) {
if ($v['target_id'] == $uid) {
$boolval = FALSE;
}
}
if ($boolval) {
// $p->field_users->appendItem($uid);
$users_db = getUsersDB($nid, $uid);
}
else {
$users_db = FALSE;
}
}
$counter = getCountDB($nid, $counter);
// $users_db = getUsersDB($nid, $uid);
saveDatabase($nid, $users_db, $counter);
// $try = setValueInParagraph();
// $p->save();
}
}
}
/**
* Implements hook_entity_update().
*/
function dummy_node_update(Drupal\Core\Entity\EntityInterface $entity) {
if (isset($entity->field_node_title)) {
$paragraph_id = $entity->field_node_title->target_id;
$p = Paragraph::load("$paragraph_id");
if (isset($p->field_change)) {
$counter = $p->field_change->value;
$counter++;
drupal_set_message(t("Обновили ноду - $counter "));
$p->field_change->setValue("$counter");
$p->save();
}
}
}
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function dummy_node_insert(Drupal\Core\Entity\EntityInterface $entity) {
if ($entity->bundle() == 'basic_analytics') {
$p = Paragraph::create([
'type' => 'node_title',
]);
$entity->field_node_title = $p;
$entity->save();
}
}
function saveDatabase($nid, $uids, $counter) {
$select = \Drupal::database()->merge('dummy_counter');
$select->key('nid', $nid);
if ($uids && is_string($uids)) {
$select->fields([
'totalcount' => $counter,
'uid' => $uids,
]);
}
else {
$select -> fields([
'totalcount' => $counter,
]);
}
$select->execute();
}
function getCountDB ($nid, $count) {
$select = \Drupal::database()->select('dummy_counter', 'nc');
$select->fields('nc', ['totalcount']);
$select->condition('nc.nid', $nid);
$counterDB = $select->execute()->fetchField();
if (!$counterDB) {
return $count;
}
else {
$counterDB++;
return $counterDB;
}
}
function getUsersDB($nid, $uid_current) {
$select = \Drupal::database()->select('dummy_counter', 'nc');
$select->fields('nc', ['uid']);
$select->condition('nc.nid', $nid);
$users = $select->execute()->fetchField();
$users_array = explode(',', $users);
if ($users == '') {
return $uid_current;
}
foreach ($users_array as $value) {
if ($uid_current == $value) {
return FALSE;
}
}
$users .= ",$uid_current";
return $users;
}
function setValueInParagraph () {
$select = \Drupal::database()->select('dummy_counter', 'nc');
$select->fields('nc', ['nid', 'uid', 'totalcount']);
$select->orderBy('nc.nid', 'ASC');
$getData = $select->execute()->fetchAll();
foreach ($getData as $value) {
$output[] = get_object_vars($value);
}
foreach ($output as $k => $v) {
$node = Node::Load($v['nid']);
if (isset($node->field_node_title)) {
$paragraph_id = $node->field_node_title->target_id;
$p = Paragraph::load("$paragraph_id");
if (isset($p->field_reviews)) {
$counter = $p->field_reviews->value;
if ($v['totalcount'] > $counter) {
drupal_set_message(t("Переглянули ноду $counter "));
$p->field_reviews->setValue($v['totalcount']);
}
}
if (isset($p->field_users)) {
$users_db = explode(',', $v['uid']);
// $users = $p->field_users->getValue();
// foreach ($users_db as $value) {
// $try[] = ['target_id' => $value];
// }
// $users = array_merge($try, $users);
// $users = array_map("unserialize", array_unique(array_map("serialize", $users)));
foreach ($users_db as $value) {
// $try = TRUE;
// foreach ($users as $value2) {
// if ($value2['target_id'] == $value) {
// $try = FALSE;
// break;
// }
// }
// if ($try) {
$p->field_users->appendItem($value);
// }
}
}
$p->save();
}
$delete= \Drupal::database()->delete('dummy_counter');
$delete->condition('nid', $v['nid']);
$delete->execute();
}
return TRUE;
}
/**
* Implements hook_cron().
*/
function dummy_cron() {
$try = setValueInParagraph();
if ($try) {
drupal_set_message(t('клас'));
}
}
\ No newline at end of file
......@@ -23,32 +23,31 @@ function dummy_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $ent
if (isset($p->field_reviews)) {
$counter = $p->field_reviews->value;
$counter++;
// drupal_set_message(t("Переглянули ноду $counter "));
// $p->field_reviews->setValue("$counter");
// drupal_set_message(t("Переглянули ноду $counter "));
// $p->field_reviews->setValue("$counter");
}
if (isset($p->field_users)) {
$uid = \Drupal::currentUser()->id();
$users = $p->field_users->getValue();
$boolval = TRUE;
foreach ($users as $v) {
foreach ($v as $value) {
if ($value == $uid) {
$boolval = FALSE;
}
if ($v['target_id'] == $uid) {
$boolval = FALSE;
}
}
if ($boolval) {
// $p->field_users->appendItem($uid);
// $p->field_users->appendItem($uid);
$users_db = getUsersDB($nid, $uid);
}
else {
$users_db = FALSE;
}
}
// $users_db = getUsersDB($nid, $uid);
$counter = getCountDB($nid, $counter);
// $users_db = getUsersDB($nid, $uid);
saveDatabase($nid, $users_db, $counter);
$try = setValueInParagraph();
$p->save();
// $try = setValueInParagraph();
// $p->save();
}
}
}
......@@ -101,6 +100,19 @@ function saveDatabase($nid, $uids, $counter) {
$select->execute();
}
function getCountDB ($nid, $count) {
$select = \Drupal::database()->select('dummy_counter', 'nc');
$select->fields('nc', ['totalcount']);
$select->condition('nc.nid', $nid);
$counterDB = $select->execute()->fetchField();
if (!$counterDB) {
return $count;
}
else {
$counterDB++;
return $counterDB;
}
}
function getUsersDB($nid, $uid_current) {
$select = \Drupal::database()->select('dummy_counter', 'nc');
......@@ -143,28 +155,42 @@ function setValueInParagraph () {
if (isset($p->field_users)) {
$users_db = explode(',', $v['uid']);
$users = $p->field_users->getValue();
// $users = $p->field_users->getValue();
// foreach ($users_db as $value) {
// $try[] = ['target_id' => $value];
// }
// $users = array_merge($try, $users);
// $users = array_map("unserialize", array_unique(array_map("serialize", $users)));
// $users = array_merge($try, $users);
// $users = array_map("unserialize", array_unique(array_map("serialize", $users)));
foreach ($users_db as $value) {
$try = TRUE;
foreach ($users as $value2) {
if ($value2['target_id'] == $value) {
$try = FALSE;
break;
}
}
if ($try) {
$p->field_users->appendItem($value);
}
// $try = TRUE;
// foreach ($users as $value2) {
// if ($value2['target_id'] == $value) {
// $try = FALSE;
// break;
// }
// }
// if ($try) {
$p->field_users->appendItem($value);
// }
}
$p -> save();
}
$p->save();
}
$delete= \Drupal::database()->delete('dummy_counter');
$delete->condition('nid', $v['nid']);
$delete->execute();
}
return TRUE;
}
}
/**
* Implements hook_cron().
*/
function dummy_cron() {
$try = setValueInParagraph();
if ($try) {
drupal_set_message(t('клас'));
}
}
\ No newline at end of file
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