Esempio n. 1
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. 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
  * @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. 3
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;
 }
Esempio n. 4
0
 /**
  * Delete/Remove File
  * @param object|array $file
  *
  * return boolean
  */
 public function postFileDelete($file)
 {
     if (method_exists($this, 'fileDelete')) {
         return $this->fileDelete($file);
     }
     try {
         $file = (object) $file;
         if (file_exists($this->postFileUploadFolder() . $file->filename)) {
             zbase_db_transaction_start();
             $files = $this->postFiles();
             $i = 0;
             foreach ($files as $f) {
                 if ($f['filename'] == $file->filename) {
                     unset($files[$i]);
                     unlink($this->postFileUploadFolder() . $file->filename);
                     break;
                 }
                 $i++;
             }
             $this->postSetOption('_files', $files);
             $this->save();
             zbase()->json()->addVariable('post_file_deleted', 1);
             zbase_db_transaction_commit();
             return true;
         }
     } catch (\Zbase\Exceptions\RuntimeException $e) {
         zbase_db_transaction_rollback();
         zbase_exception_throw($e);
         return false;
     }
 }
Esempio n. 5
0
 /**
  * Third/Final step in updating email address
  *
  */
 public function updateEmailAddress()
 {
     $newEmail = $this->getDataOption('email_new', null);
     if (!is_null($newEmail)) {
         zbase_db_transaction_start();
         try {
             $oldEmail = $this->email();
             $oldEmails = $this->getDataOption('email_old', []);
             $oldEmails[] = ['old' => $this->email(), 'date' => zbase_date_now(), 'ip' => zbase_ip(), 'new' => $newEmail];
             //$this->setDataOption('email_old', $oldEmails);
             $emailVerificationEnabled = zbase_config_get('auth.emailverify.enable', true);
             $this->email = $newEmail;
             $this->email_verified = $emailVerificationEnabled ? 0 : 1;
             $this->email_verified_at = null;
             if (!empty($emailVerificationEnabled)) {
                 $code = zbase_generate_code();
                 $this->setDataOption('email_verification_code', $code);
                 zbase_alert('info', _zt('Successfully updated your email address. We sent an email to <strong>%email%</strong> to verify your new email address.', ['%email%' => $newEmail]));
                 zbase_messenger_email($this->email(), 'account-noreply', _zt('Email address verification code'), zbase_view_file_contents('email.account.newEmailAddressVerification'), ['entity' => $this, 'code' => $code, 'newEmailAddress' => $newEmail]);
             } else {
                 zbase_alert('info', _zt('Successfully updated your email address to <strong>' . $newEmail . '</strong>', ['%email%' => $newEmail]));
             }
             /**
              * Remove options on updating email address
              */
             $this->unsetDataOption('email_new');
             $this->unsetDataOption('email_new_request_date');
             $this->save();
             $this->log('user::updateEmailAddress', null, ['old_email' => $oldEmail]);
             zbase_db_transaction_commit();
             return true;
         } catch (\Zbase\Exceptions\RuntimeException $e) {
             zbase_db_transaction_rollback();
             return false;
         }
     }
     return false;
 }