Commit 2256ced4 authored by o.tarasiuk@internetdevels.com's avatar o.tarasiuk@internetdevels.com
Browse files

database function complate(todo: delate comments)

parent 93fd207a
......@@ -9,6 +9,7 @@
use Drupal\Core\Database\Connection;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Database\Database;
use Drupal\node\Entity\Node;
/**
* Implements hook_entity_view().
......@@ -16,30 +17,18 @@
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)) {
// $paragraph_id = $entity->field_node_title->getValue();
// foreach ($paragraph_id as $element) {
// $p = Paragraph::load($element['target_id']);
// if (isset($p->field_reviews)) {
// $counter = $p->field_reviews->value;
// $counter++;
// drupal_set_message(t("$counter"));
// $p->field_reviews->setValue("$counter");
// $p->save();
// }
// }
// drupal_set_message(t("$counter"));
$nid = $entity->id();
$nid = $entity->id();
$paragraph_id = $entity->field_node_title->target_id;
$p = Paragraph::load("$paragraph_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");
// drupal_set_message(t("Переглянули ноду $counter "));
// $p->field_reviews->setValue("$counter");
}
if (isset($p->field_users)) {
$uid = \Drupal::currentUser()->id();
$users = $p->field_users->getValue();
$uid = \Drupal::currentUser()->id();
$users = $p->field_users->getValue();
$boolval = TRUE;
foreach ($users as $v) {
foreach ($v as $value) {
......@@ -49,11 +38,16 @@ function dummy_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $ent
}
}
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);
// $users_db = getUsersDB($nid, $uid);
saveDatabase($nid, $users_db, $counter);
$try = setValueInParagraph();
$p->save();
}
}
......@@ -65,7 +59,7 @@ function dummy_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $ent
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");
$p = Paragraph::load("$paragraph_id");
if (isset($p->field_change)) {
$counter = $p->field_change->value;
$counter++;
......@@ -81,7 +75,7 @@ function dummy_node_update(Drupal\Core\Entity\EntityInterface $entity) {
*/
function dummy_node_insert(Drupal\Core\Entity\EntityInterface $entity) {
if ($entity->bundle() == 'basic_analytics') {
$p = Paragraph::create([
$p = Paragraph::create([
'type' => 'node_title',
]);
$entity->field_node_title = $p;
......@@ -93,8 +87,7 @@ function dummy_node_insert(Drupal\Core\Entity\EntityInterface $entity) {
function saveDatabase($nid, $uids, $counter) {
$select = \Drupal::database()->merge('dummy_counter');
$select->key('nid', $nid);
if (is_string($uids)) {
if ($uids && is_string($uids)) {
$select->fields([
'totalcount' => $counter,
'uid' => $uids,
......@@ -113,8 +106,11 @@ 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 = $select->execute()->fetchField();
$users_array = explode(',', $users);
if ($users == '') {
return $uid_current;
}
foreach ($users_array as $value) {
if ($uid_current == $value) {
return FALSE;
......@@ -124,3 +120,51 @@ function getUsersDB($nid, $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();
}
}
}
}
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