/** * Move to trash. Validation will be performed * before trashing with `trash` (TrashInterface::ScenarioTrash) scenario. * * * @see TrashInterface::ScenarioTrash * @param boolean $runValidation whether to perform validation before saving the record. * If the validation fails, the record will not be saved to database. * * @return boolean * @Ignored */ public function trash($runValidation = true) { ScenarioManager::setScenario($this, TrashInterface::ScenarioTrash); $validator = new Validator($this); if (!$runValidation || $validator->validate()) { $eventBefore = new TrashEvent($this); if (Event::hasHandler($this, TrashInterface::EventBeforeTrash)) { if (!Event::valid($this, TrashInterface::EventBeforeTrash, $eventBefore)) { return false; } } $meta = ManganMeta::create($this); $trash = $eventBefore->getTrash(); if (empty($trash)) { $trash = new Trash(); } $trash->name = (string) $this; $trash->data = $this; $trash->type = isset($meta->type()->label) ? $meta->type()->label : get_class($this); if (!$trash->save()) { return false; } $eventAfter = new TrashEvent($this); $eventAfter->setTrash($trash); if (!Event::valid($this, TrashInterface::EventAfterTrash, $eventAfter)) { return false; } // Use deleteOne, to avoid beforeDelete event, // which should be raised only when really removing document: // when emtying trash $em = new EntityManager($this); return $em->deleteOne(PkManager::prepareFromModel($this)); } return false; }
public function __construct($scenario = 'insert', $lang = '') { parent::__construct($scenario, $lang); static $once = false; // Also check if has handler because of EventDestroyer if (!$once || !Event::hasHandler($this, TrashInterface::EventAfterTrash)) { (new ParentChildTrashHandlers())->registerParent($this, ChildDocument::class); $once = true; } }
public static function ensureClass($model, $name, &$dbValue) { if (!is_array($dbValue) || !array_key_exists('_class', $dbValue) || empty($dbValue['_class'])) { $class = static::getClassName($model, $name); } else { $class = $dbValue['_class']; } if (!ClassChecker::exists($class)) { $event = new ClassNotFound($model); $event->notFound = $class; if (Event::hasHandler($model, NotFoundResolver::EventClassNotFound) && Event::handled($model, NotFoundResolver::EventClassNotFound, $event)) { $class = $event->replacement; } else { throw new ManganException(sprintf("Embedded model class `%s` not found in model `%s` field `%s`", $class, get_class($model), $name)); } } $dbValue['_class'] = $class; }
/** * Trigger before exists event * @return boolean */ private function _beforeExists() { if (!Event::hasHandler($this->model, FinderInterface::EventBeforeExists)) { return true; } return Event::handled($this->model, FinderInterface::EventBeforeExists); }