Esempio n. 1
0
 /**
  * Return the Wrapper Attributes
  * @return array
  */
 public function wrapperAttributes()
 {
     $attr = parent::wrapperAttributes();
     $attr['class'][] = 'zbase-widget-wrapper';
     $attr['id'] = 'zbase-widget-wrapper-' . $this->id();
     return $attr;
 }
Esempio n. 2
0
 /**
  * Widget entity interface.
  * 	Data should be validated first before passing it here
  * @param string $method post|get
  * @param string $action the controller action
  * @param array $data validated; assoc array
  * @param Zbase\Widgets\Widget $widget
  */
 public function widgetController($method, $action, $data, \Zbase\Widgets\Widget $widget)
 {
     if (preg_match('/-update/', $action)) {
         $action = 'update';
     }
     if (preg_match('/-create/', $action)) {
         $action = 'create';
     }
     if (preg_match('/-delete/', $action)) {
         $action = 'delete';
     }
     if ($action == 'update' && strtolower($method) == 'post' || $action == 'create' && strtolower($method) == 'post') {
         $this->nodeAttributes($data);
     }
     zbase_db_transaction_start();
     try {
         $parent = !empty($data['category']) ? $data['category'] : null;
         $parentNodes = [];
         if (!empty($parent)) {
             $currentParent = $this->ancestors()->first();
             if (is_array($parent)) {
                 foreach ($parent as $p) {
                     if ($parent instanceof Interfaces\EntityInterface) {
                         $parentCategoryNode = $p;
                     } else {
                         $parentCategoryNode = $this->repository()->byAlphaId($p);
                     }
                     if ($parentCategoryNode instanceof Interfaces\EntityInterface) {
                         if ($currentParent->id() != $parentCategoryNode->id()) {
                             $parentNodes[] = $parentCategoryNode;
                         }
                     } else {
                         zbase_alert('error', _zt('There was a problem performing your request.'));
                         return false;
                     }
                 }
             }
         }
         if (empty($parentNodes)) {
             $parentNodes[] = self::root();
         }
         if ($action == 'create' && strtolower($method) == 'post') {
             $this->save();
             $this->_setParentNodes($parentNodes);
             $this->uploadNodeFile();
             $this->log($action);
             zbase_db_transaction_commit();
             zbase_cache_flush([$this->getTable()]);
             zbase_alert('success', _zt('Created "%title%"!', ['%title%' => $this->title, '%id%' => $this->id()]));
             return true;
         }
         if ($action == 'update' && strtolower($method) == 'post') {
             $this->save();
             $this->_setParentNodes($parentNodes);
             $this->uploadNodeFile();
             $this->log($action);
             zbase_db_transaction_commit();
             zbase_cache_flush([$this->getTable()]);
             zbase_alert('success', _zt('Saved "%title%"!', ['%title%' => $this->title, '%id%' => $this->id()]));
             return true;
         }
         if ($action == 'delete' && strtolower($method) == 'post') {
             $this->delete();
             $this->log($action);
             zbase_db_transaction_commit();
             zbase_cache_flush([$this->getTable()]);
             $undoText = '';
             if (!empty($this->hasSoftDelete())) {
                 $undoText = '<a href="' . $widget->getModule()->url(zbase_section(), ['action' => 'restore', 'id' => $this->id()]) . '" title="Undo Delete" class="undodelete">Undo</a>.';
                 $undoText .= ' | <a href="' . $widget->getModule()->url(zbase_section(), ['action' => 'ddelete', 'id' => $this->id()]) . '" title="Delete Forever " class="ddeleteforever">Delete Forever</a>';
             }
             zbase_alert('success', _zt('Deleted "%title%"! %undo%', ['%title%' => $this->title, '%id%' => $this->id(), '%undo%' => $undoText]));
             return true;
         }
     } catch (\Zbase\Exceptions\RuntimeException $e) {
         zbase_db_transaction_rollback();
     }
     if ($action == 'index') {
         return;
     }
     if ($action == 'update') {
         if ($this->hasSoftDelete() && $this->trashed()) {
             $undoText = '<a href="' . $widget->getModule()->url(zbase_section(), ['action' => 'restore', 'id' => $this->id()]) . '" title="Restore" class="undodelete">Restore</a>';
             $undoText .= ' | <a href="' . $widget->getModule()->url(zbase_section(), ['action' => 'ddelete', 'id' => $this->id()]) . '" title="Delete Forever " class="ddeleteforever">Delete Forever</a>';
             zbase_alert('warning', _zt('Row "%title%" was trashed! %undo%', ['%title%' => $this->title, '%id%' => $this->id(), '%undo%' => $undoText]));
             return false;
         }
     }
     if ($action == 'delete') {
         if ($this->hasSoftDelete() && $this->trashed()) {
             $undoText = '<a href="' . $widget->getModule()->url(zbase_section(), ['action' => 'restore', 'id' => $this->id()]) . '" title="Restore" class="undodelete">Restore</a>';
             $undoText .= ' | <a href="' . $widget->getModule()->url(zbase_section(), ['action' => 'ddelete', 'id' => $this->id()]) . '" title="Delete Forever " class="ddeleteforever">Delete Forever</a>';
             zbase_alert('warning', _zt('Row "%title%" was trashed! %undo%', ['%title%' => $this->title, '%id%' => $this->id(), '%undo%' => $undoText]));
             return false;
         }
     }
     try {
         if ($action == 'move') {
         }
         if ($action == 'restore') {
             if ($this->trashed()) {
                 $this->restore();
                 $this->log($action);
                 zbase_db_transaction_commit();
                 zbase_cache_flush([$this->getTable()]);
                 $this->_actionMessages[$action]['success'][] = _zt('Row "%title%" was restored!', ['%title%' => $this->title, '%id%' => $this->id()]);
                 return true;
             }
             zbase_alert('error', _zt('Error restoring "%title%". Row was not trashed.!', ['%title%' => $this->title, '%id%' => $this->id()]));
             return false;
         }
         if ($action == 'ddelete') {
             if ($this->trashed()) {
                 $this->forceDelete();
                 $this->log($action);
                 zbase_db_transaction_commit();
                 zbase_cache_flush([$this->getTable()]);
                 zbase_alert('success', _zt('Row "%title%" was removed from database!', ['%title%' => $this->title, '%id%' => $this->id()]));
                 return true;
             }
             zbase_alert('error', _zt('Error restoring "%title%". Row was not trashed.!', ['%title%' => $this->title, '%id%' => $this->id()]));
             return false;
         }
     } catch (\Zbase\Exceptions\RuntimeException $e) {
         zbase_alert('error', _zt('There was a problem performing the request for "%title%".', ['%title%' => $this->title, '%id%' => $this->id()]));
         zbase_db_transaction_rollback();
     }
     return false;
 }
Esempio n. 3
0
 /**
  * Return the Wrapper Attributes
  * @return array
  */
 public function wrapperAttributes()
 {
     $attr = parent::wrapperAttributes();
     if (!empty($this->_entity)) {
         if ($this->_action == 'delete' && strtolower(zbase_request_method()) != 'post' || $this->isNode() && $this->_entity->hasSoftDelete() && empty($this->_entityIsDefault) && $this->_entity->trashed()) {
             $attr['class'][] = 'action-delete';
             $attr['style'][] = 'border:2px solid red; padding:20px;';
         }
     }
     return $attr;
 }
Esempio n. 4
0
 /**
  * Widget entity interface.
  * 	Data should be validated first before passing it here
  * @param string $method post|get
  * @param string $action the controller action
  * @param array $data validated; assoc array
  * @param Zbase\Widgets\Widget $widget
  * @return boolean
  */
 public function nodeWidgetController($method, $action, $data, \Zbase\Widgets\Widget $widget)
 {
     zbase_db_transaction_start();
     try {
         if ($action == 'create' && strtolower($method) == 'post') {
             $this->createNode($data);
             $this->log($action);
             zbase_db_transaction_commit();
             zbase_cache_flush([$this->getTable()]);
             $this->_actionMessages[$action]['success'][] = _zt('Created "%title%"!', ['%title%' => $this->title, '%id%' => $this->id()]);
             return true;
         }
         if ($action == 'update' && strtolower($method) == 'post') {
             $this->updateNode($data);
             $this->log($action);
             zbase_db_transaction_commit();
             zbase_cache_flush([$this->getTable()]);
             $this->_actionMessages[$action]['success'][] = _zt('Saved "%title%"!', ['%title%' => $this->title, '%id%' => $this->id()]);
             return true;
         }
     } catch (\Zbase\Exceptions\RuntimeException $e) {
         zbase_db_transaction_rollback();
     }
     if ($action == 'index') {
         return;
     }
     if ($action == 'update') {
         if ($this->hasSoftDelete() && $this->trashed()) {
             $undoText = '<a href="' . $widget->getModule()->url(zbase_section(), ['action' => 'restore', 'id' => $this->alphaId()]) . '" title="Restore" class="undodelete">Restore</a>';
             $undoText .= ' | <a href="' . $widget->getModule()->url(zbase_section(), ['action' => 'ddelete', 'id' => $this->alphaId()]) . '" title="Delete Forever " class="ddeleteforever">Delete Forever</a>';
             $this->_actionMessages[$action]['warning'][] = _zt('Row "%title%" was trashed! %undo%', ['%title%' => $this->title, '%id%' => $this->id(), '%undo%' => $undoText]);
             return false;
         }
     }
     if ($action == 'delete') {
         if ($this->hasSoftDelete() && $this->trashed()) {
             $undoText = '<a href="' . $widget->getModule()->url(zbase_section(), ['action' => 'restore', 'id' => $this->alphaId()]) . '" title="Restore" class="undodelete">Restore</a>';
             $undoText .= ' | <a href="' . $widget->getModule()->url(zbase_section(), ['action' => 'ddelete', 'id' => $this->alphaId()]) . '" title="Delete Forever " class="ddeleteforever">Delete Forever</a>';
             $this->_actionMessages[$action]['warning'][] = _zt('Row "%title%" was trashed! %undo%', ['%title%' => $this->title, '%id%' => $this->id(), '%undo%' => $undoText]);
             return false;
         }
     }
     try {
         if (strtolower($method) == 'post') {
             if ($action == 'delete') {
                 if ($this->hasSoftDelete()) {
                     $this->deleted_at = zbase_date_now();
                     $this->save();
                 } else {
                     $this->delete();
                 }
                 $this->log($action);
                 zbase_db_transaction_commit();
                 zbase_cache_flush([$this->getTable()]);
                 $undoText = '';
                 if (!empty($this->hasSoftDelete())) {
                     $undoText = '<a href="' . $widget->getModule()->url(zbase_section(), ['action' => 'restore', 'id' => $this->alphaId()]) . '" title="Undo Delete" class="undodelete">Undo</a>';
                     $undoText .= ' | <a href="' . $widget->getModule()->url(zbase_section(), ['action' => 'ddelete', 'id' => $this->alphaId()]) . '" title="Delete Forever " class="ddeleteforever">Delete Forever</a>';
                 }
                 $this->_actionMessages[$action]['success'][] = _zt('Deleted "%title%"! %undo%', ['%title%' => $this->title, '%id%' => $this->id(), '%undo%' => $undoText]);
                 return true;
             }
         }
         if ($action == 'restore') {
             if ($this->trashed()) {
                 $this->restore();
                 $this->log($action);
                 zbase_db_transaction_commit();
                 zbase_cache_flush([$this->getTable()]);
                 $this->_actionMessages[$action]['success'][] = _zt('Row "%title%" was restored!', ['%title%' => $this->title, '%id%' => $this->id()]);
                 return true;
             }
             $this->_actionMessages[$action]['error'][] = _zt('Error restoring "%title%". Row was not trashed.!', ['%title%' => $this->title, '%id%' => $this->id()]);
             return false;
         }
         if ($action == 'ddelete') {
             if ($this->trashed()) {
                 $this->forceDelete();
                 $this->log($action);
                 zbase_db_transaction_commit();
                 zbase_cache_flush([$this->getTable()]);
                 $this->_actionMessages[$action]['success'][] = _zt('Row "%title%" forever deleted!', ['%title%' => $this->title, '%id%' => $this->id()]);
                 return true;
             }
             $this->_actionMessages[$action]['error'][] = _zt('Error restoring "%title%". Row was not trashed.!', ['%title%' => $this->title, '%id%' => $this->id()]);
             return false;
         }
     } catch (\Zbase\Exceptions\RuntimeException $e) {
         $this->_actionMessages[$action]['error'][] = _zt('There was a problem performing the request for "%title%".', ['%title%' => $this->title, '%id%' => $this->id()]);
         zbase_db_transaction_rollback();
     }
     return false;
 }
Esempio n. 5
0
 /**
  * Widget entity interface.
  * 	Data should be validated first before passing it here
  * @param string $method post|get
  * @param string $action the controller action
  * @param array $data validated; assoc array
  * @param Zbase\Widgets\Widget $widget
  * @return boolean
  */
 public function nodeWidgetController($method, $action, $data, \Zbase\Widgets\Widget $widget)
 {
     zbase_db_transaction_start();
     try {
         if (strtolower($method) == 'post') {
             if ($action == 'file-update') {
                 $action = 'update';
             }
             if ($action == 'file-delete') {
                 $action = 'delete';
             }
         }
         if (strtolower($method) == 'post' && zbase_request_is_upload()) {
             $parentObject = $widget->parentEntityObject();
             if (empty($parentObject)) {
                 return false;
             }
             $this->receiveFile($parentObject);
             $action = 'create';
         }
         if (strtolower($method) == 'post') {
             if (!empty($data)) {
                 $newData = $data;
                 $data = [];
                 foreach ($newData as $dK => $dV) {
                     $data[str_replace('nodefile', '', $dK)] = $dV;
                 }
             }
         }
         if ($action == 'create' && strtolower($method) == 'post') {
             if (isset($data['title'])) {
                 $this->title = $data['title'];
             }
             if (isset($data['excerpt'])) {
                 $this->excerpt = $data['excerpt'];
             }
             $this->save();
             $this->log($action);
             zbase_db_transaction_commit();
             zbase_cache_flush([$this->getTable()]);
             $this->_actionMessages[$action]['success'][] = _zt('File Uploaded!', ['%title%' => $this->title, '%id%' => $this->id()]);
             return true;
         }
         if ($action == 'update' && strtolower($method) == 'post') {
             foreach ($data as $k => $v) {
                 unset($data[$k]);
                 $data[str_replace('nodefile', '', $k)] = $v;
             }
             if (!empty($data['status'])) {
                 $this->status = 2;
                 unset($data['status']);
             } else {
                 $this->status = 0;
             }
             if (isset($data['title'])) {
                 $this->title = $data['title'];
             }
             if (isset($data['excerpt'])) {
                 $this->excerpt = $data['excerpt'];
             }
             if (isset($data['primary']) && !empty($data['primary'])) {
                 $this->updatePrimary($data['primary']);
                 $this->parentObject()->image = $this->alphaId();
             } else {
                 $this->parentObject()->image = null;
             }
             $this->parentObject()->save();
             $this->save();
             $this->log($action);
             zbase_db_transaction_commit();
             zbase_cache_flush([$this->getTable()]);
             $this->_actionMessages[$action]['success'][] = _zt('Saved', ['%title%' => $this->title, '%id%' => $this->id()]);
             return true;
         }
         if ($action == 'primary' && strtolower($method) == 'post') {
             $this->log($action);
             zbase_db_transaction_commit();
             zbase_cache_flush([$this->getTable()]);
             $this->_actionMessages[$action]['success'][] = _zt('Saved', ['%title%' => $this->title, '%id%' => $this->id()]);
             return true;
         }
     } catch (\Zbase\Exceptions\RuntimeException $e) {
         zbase_db_transaction_rollback();
     }
     if ($action == 'index') {
         return;
     }
     if ($action == 'update') {
         if ($this->hasSoftDelete() && $this->trashed()) {
             $undoText = '<a href="' . $widget->getModule()->url(zbase_section(), ['action' => 'restore', 'id' => $this->id()]) . '" title="Restore" class="undodelete">Restore</a>';
             $undoText .= ' | <a href="' . $widget->getModule()->url(zbase_section(), ['action' => 'ddelete', 'id' => $this->id()]) . '" title="Delete Forever " class="ddeleteforever">Delete Forever</a>';
             $this->_actionMessages[$action]['warning'][] = _zt('Row "%title%" was trashed! %undo%', ['%title%' => $this->title, '%id%' => $this->id(), '%undo%' => $undoText]);
             return false;
         }
     }
     if ($action == 'delete') {
         if ($this->hasSoftDelete() && $this->trashed()) {
             $undoText = '<a href="' . $widget->getModule()->url(zbase_section(), ['action' => 'restore', 'id' => $this->id()]) . '" title="Restore" class="undodelete">Restore</a>';
             $undoText .= ' | <a href="' . $widget->getModule()->url(zbase_section(), ['action' => 'ddelete', 'id' => $this->id()]) . '" title="Delete Forever " class="ddeleteforever">Delete Forever</a>';
             $this->_actionMessages[$action]['warning'][] = _zt('Row "%title%" was trashed! %undo%', ['%title%' => $this->title, '%id%' => $this->id(), '%undo%' => $undoText]);
             return false;
         }
     }
     try {
         if ($action == 'delete') {
             if ($this->hasSoftDelete()) {
                 $this->deleted_at = zbase_date_now();
                 $this->save();
             } else {
                 $this->_deleteFile();
                 $this->delete();
             }
             $this->log($action);
             zbase_db_transaction_commit();
             zbase_cache_flush([$this->getTable()]);
             $undoText = '';
             if (!empty($this->hasSoftDelete())) {
                 $undoText = '<a href="' . $widget->getModule()->url(zbase_section(), ['action' => 'restore', 'id' => $this->id()]) . '" title="Undo Delete" class="undodelete">Undo</a>.';
                 $undoText .= ' | <a href="' . $widget->getModule()->url(zbase_section(), ['action' => 'ddelete', 'id' => $this->id()]) . '" title="Delete Forever " class="ddeleteforever">Delete Forever</a>';
             }
             $this->_actionMessages[$action]['success'][] = _zt('File Deleted! %undo%', ['%title%' => $this->title, '%id%' => $this->id(), '%undo%' => $undoText]);
             return true;
         }
         if ($action == 'restore') {
             if ($this->trashed()) {
                 $this->restore();
                 $this->log($action);
                 zbase_db_transaction_commit();
                 zbase_cache_flush([$this->getTable()]);
                 $this->_actionMessages[$action]['success'][] = _zt('Row "%title%" was restored!', ['%title%' => $this->title, '%id%' => $this->id()]);
                 return true;
             }
             $this->_actionMessages[$action]['error'][] = _zt('Error restoring "%title%". Row was not trashed.!', ['%title%' => $this->title, '%id%' => $this->id()]);
             return false;
         }
         if ($action == 'ddelete') {
             if ($this->trashed()) {
                 $this->forceDelete();
                 $this->_deleteFile();
                 $this->log($action);
                 zbase_db_transaction_commit();
                 zbase_cache_flush([$this->getTable()]);
                 $this->_actionMessages[$action]['success'][] = _zt('Row "%title%" was removed from database!', ['%title%' => $this->title, '%id%' => $this->id()]);
                 return true;
             }
             $this->_actionMessages[$action]['error'][] = _zt('Error restoring "%title%". Row was not trashed.!', ['%title%' => $this->title, '%id%' => $this->id()]);
             return false;
         }
     } catch (\Zbase\Exceptions\RuntimeException $e) {
         $this->_actionMessages[$action]['error'][] = _zt('There was a problem performing the request for "%title%".', ['%title%' => $this->title, '%id%' => $this->id()]);
         zbase_db_transaction_rollback();
     }
     return false;
 }