Source of file GenealogyPage.php
Size: 17,347 Bytes - Last Modified: 2021-12-24T05:15:56+00:00
/var/www/docs.ssmods.com/process/src/code/pages/GenealogyPage.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509 | <?php /* * MIT License * * Copyright (c) 2016 Hudhaifa Shatnawi * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ /** * * @author Hudhaifa Shatnawi <hudhaifa.shatnawi@gmail.com> * @version 1.0, Nov 2, 2016 - 2:45:38 PM */ class GenealogyPage extends Page { private static $group_code = 'genealogists'; private static $group_title = 'Genealogists'; private static $group_permission = 'CMS_ACCESS_CMSMain'; private static $co_group_code = 'co-genealogists'; private static $co_group_title = 'Co-Genealogists'; private static $icon = "genealogist/images/icn-genealogy.png"; public function canCreate($member = false) { if (!$member || !(is_a($member, 'Member')) || is_numeric($member)) { $member = Member::currentUserID(); } return (DataObject::get($this->owner->class)->count() > 0) ? false : true; } protected function onBeforeWrite() { parent::onBeforeWrite(); $this->getUserGroup(); $this->getCoUserGroup(); } /** * Returns/Creates the genealogists group to assign CMS access. * * @return Group Librarians group */ protected function getUserGroup() { $code = $this->config()->group_code; $group = Group::get()->filter('Code', $code)->first(); if (!$group) { $group = new Group(); $group->Title = $this->config()->group_title; $group->Code = $code; $group->write(); $permission = new Permission(); $permission->Code = $this->config()->group_permission; $group->Permissions()->add($permission); } return $group; } /** * Returns/Creates the genealogists group to assign CMS access. * * @return Group Librarians group */ protected function getCoUserGroup() { $code = $this->config()->co_group_code; $group = Group::get()->filter('Code', $code)->first(); if (!$group) { $group = new Group(); $group->Title = $this->config()->co_group_title; $group->Code = $code; $group->write(); } return $group; } } class GenealogyPage_Controller extends Page_Controller { private static $allowed_actions = array( 'info', 'suggest', 'Form_Suggest', 'doSuggest', 'Form_Kinship', ); private static $url_handlers = array( 'person-info/$ID' => 'info', 'suggest/$ID' => 'suggest', 'Form_Suggest' => 'Form_Suggest', // list all forms before the index in the handlers array 'Form_Kinship' => 'Form_Kinship', // list all forms before the index in the handlers array '$ID/$Other' => 'index', // any action redirects to the index MUST be added at the end of the array ); public function init() { parent::init(); Requirements::css("genealogist/css/vendors/bootstrap-slider.css"); Requirements::css("genealogist/css/vendors/introjs.css"); Requirements::css("genealogist/css/vendors/jquery.modal.css"); Requirements::css("genealogist/css/genealogy.css"); if ($this->isRTL()) { Requirements::css("genealogist/css/vendors/introjs-rtl.css"); Requirements::css("genealogist/css/genealogy-rtl.css"); } Requirements::javascript("genealogist/js/vendors/jquery.dragscroll.js"); Requirements::javascript("genealogist/js/vendors/jquery.fullscreen.js"); Requirements::javascript("genealogist/js/vendors/jquery.panzoom.js"); Requirements::javascript("genealogist/js/vendors/jquery-scrollto.js"); Requirements::javascript("genealogist/js/vendors/jquery.modal.js"); Requirements::javascript("genealogist/js/vendors/URI.js"); Requirements::javascript("genealogist/js/vendors/html2canvas.js"); Requirements::javascript("genealogist/js/vendors/bootstrap-slider.js"); Requirements::javascript("genealogist/js/vendors/intro.js"); Requirements::javascript("genealogist/js/jquery.jOrgChart.js"); Requirements::javascript("genealogist/js/genealogy.timeline.js"); Requirements::javascript("genealogist/js/genealogy.controls.js"); Requirements::javascript("genealogist/js/genealogy.ajax.js"); Requirements::javascript("genealogist/js/genealogy.js"); } /// Actions /// public function index(SS_HTTPRequest $request) { $id = $this->getRequest()->param('ID'); $other = $this->getRequest()->param('Other'); if (!$id) { return array( 'LandingPage' => true, 'RandBranch' => $this->getBranches()->sort('rand()')->first(), 'RandFigure' => $this->getFigures()->sort('rand()')->first() ); } if ($request->isAjax()) { $data = $other ? $this->kinship($id, $other) : $this->tree($id); return $this ->customise($data) ->renderWith('TheTree'); } $title = $this->getSubTitle($id, $other); return array( 'Tree' => false, 'Title' => $title ); } public function info() { $id = $this->getRequest()->param('ID'); $person = GenealogistHelper::get_gender($id); if (!$person) { return $this->httpError(404, 'That person could not be found!'); } else { return $person->renderWith("TheTree_InfoCard"); } } public function suggest() { $id = $this->getRequest()->param('ID'); $person = null; if ($id) { $person = DataObject::get_by_id('Gender', (int) $id); } if ($person) { return $this ->customise(array( 'Gender' => $person, 'Title' => $person->FullName )) ->renderWith(array('GenealogyPage_Suggest', 'Page')); } else { return $this ->customise(array()) ->renderWith(array('GenealogyPage_SuggestAny', 'Page')); // return $this->httpError(404, 'That person could not be found!'); } } /// Sub Pages /// private function tree($id) { $person = $id ? DataObject::get_by_id('Gender', (int) $id) : $this->getMainClansAndBranches()->last(); if (!$person) { return $this->httpError(404, 'That person could not be found!'); } $isAncestral = $this->getRequest()->getVar('ancestral') ? 1 : 0; $config = SiteConfig::current_site_config(); $title = $config->Title . ' - '; $title .= $this->getSubTitle($person); return array( 'Tree' => $person->getDescendants(), 'MultiRoot' => false, 'ShowTimeline' => !$isAncestral, 'Collapsible' => !$isAncestral, 'PageTitle' => $title ); } private function kinship($id, $other) { if ($id) { $p1 = DataObject::get_by_id('Gender', (int) $id); } if ($other) { $p2 = DataObject::get_by_id('Gender', (int) $other); } if (!$p1 || !$p2) { return $this->httpError(404, 'That person could not be found!'); } $kinships = GenealogistHelper::get_kinships($p1, $p2); $roots = array(); foreach ($kinships as $kinship) { $roots[] = $this->getKinshipLeaves($kinship); } $config = SiteConfig::current_site_config(); $title = $config->Title . ' - '; $title .= $this->getSubTitle($p1, $p2); return array( 'Tree' => $this->virtualRoot($roots), 'MultiRoot' => true, 'ShowTimeline' => false, 'Collapsible' => false, 'PageTitle' => $title//$p1->getShortName() . ' : ' . $p2->getShortName() ); } private function getSubTitle($p1, $p2 = null) { if (is_numeric($p1)) { $p1 = $p1 ? DataObject::get_by_id('Gender', (int) $p1) : $this->getMainClansAndBranches()->last(); } if (is_numeric($p2)) { $p2 = DataObject::get_by_id('Gender', (int) $p2); } if (!$p1 && !$p2) { return $this->httpError(404, 'That person could not be found!'); } if ($p2) { $title = _t('Genealogist.KINSHIP_OF', // "Kinships Between {value1} & {value2}", // array( 'value1' => $p1->getShortName(), 'value2' => $p2->getShortName(), ) ); return $title; } else { $isAncestral = $this->getRequest()->getVar('ancestral') ? 1 : 0; $title = $isAncestral ? _t('Genealogist.ANCESTORS_OF', // "Family Ancestors of {value}", // array( 'value' => $p1->getShortName() ) ) : _t('Genealogist.TREE_OF', // "Family Tree of {value}", // array( 'value' => $p1->getShortName() ) ); return $title; } } private function virtualRoot($trees) { $leaves = ''; foreach ($trees as $tree) { $leaves .= $tree; } return <<<HTML <li> <a href="#" class="info-item">Root</a> <ul> {$leaves} </ul> </li> HTML; } private function getKinshipLeaves($kinships = array()) { $root = $kinships[0]; $html = <<<HTML <li class="{$root->CSSClasses()}"> <a href="#" title="{$root->getFullName()}" data-url="{$root->InfoLink()}" class="info-item">{$root->getPersonName()}</a> <ul> {$this->appendLeaf($kinships[1])} {$this->appendLeaf($kinships[2])} </ul> </li> HTML; return $this->appendParents($root, $html); } private function appendParents($person, $html3) { if (!$person || $person->isClan() || !$person->Father()->exists()) { return $html3; } $father = $person->Father(); $html4 = <<<HTML <li class="{$father->CSSClasses()}"> <a href="#" title="{$father->getFullName()}" data-url="{$father->InfoLink()}" class="info-item">{$father->getPersonName()}</a> <ul> {$html3} </ul> </li> HTML; return $this->appendParents($father, $html4); } private function appendLeaf($kinship = array(), $index = 0) { if ($index > count($kinship) || $kinship[$index] == false) { return ''; } $person = $kinship[$index]; $index++; $html = <<<HTML <li class="{$person->CSSClasses()}"> <a href="#" title="{$person->getFullName()}" data-url="{$person->InfoLink()}" class="info-item">{$person->getPersonName()}</a> <ul> {$this->appendLeaf($kinship, $index)} </ul> </li> HTML; return $html; } /// Forms /// public function Form_Kinship($personID = null) { // Create fields $source = $this->isAdmin() ? 'Gender' : 'Male'; $link = $this->AbsoluteLink(); $fields = new FieldList( AutoPersonField::create( 'Person1', // _t('Genealogist.FIRST_PERSON', 'First Person'), // '', // null, // null, // $source, // array('IndexedName', 'Name', 'NickName') // ) ->setSourceSort('CHAR_LENGTH(IndexedName) ASC') ->setLimit(20), // AutoPersonField::create( 'Person2', // _t('Genealogist.SECOND_PERSON', 'Second Person'), // '', // null, // null, // $source, // array('IndexedName', 'Name', 'NickName') // ) ->setSourceSort('CHAR_LENGTH(IndexedName) ASC') ->setLimit(20) ); // Create action $actions = new FieldList( $button = new FormAction('findKinship', _t('Genealogist.FIND', 'Find')) ); $button->addExtraClass('kinship-btn'); $button->setAttribute('data-url', $this->Link()); // Create Validators $validator = new RequiredFields('Person1', 'Person2'); return new Form($this, 'Form_Kinship', $fields, $actions, $validator); } public function Form_Suggest($personID = null) { $subjects = array( 'General' => _t('Genealogist.SUBJECT', 'Subject'), 'Name' => _t('Genealogist.NAME', 'Name'), 'Father' => _t('Genealogist.FATHER', 'Father'), 'Mother' => _t('Genealogist.MOTHER', 'Mother'), 'Spouse' => _t('Genealogist.SPOUSE', 'Spouse'), 'Sons' => _t('Genealogist.SONS', 'Sons'), 'Daughters' => _t('Genealogist.DAUGHTERS', 'Daughters'), 'BirthDate' => _t('Genealogist.BIRTHDATE', 'Birth Date'), 'DeathDate' => _t('Genealogist.DEATHDATE', 'Death Date'), ); // Create fields $fields = new FieldList( HiddenField::create('PersonID', 'PersonID', $personID), // TextField::create('Name', _t('Genealogist.YOUR_NAME', 'Your Name')), // EmailField::create('Email', _t('Genealogist.EMAIL', 'Email')), // TextField::create('Phone', _t('Genealogist.PHONE', 'Phone')), // DropdownField::create('Subject', _t('Genealogist.SUBJECT', 'Subject'), $subjects), // TextareaField::create('Message', _t('Genealogist.MESSAGE', 'Message')) ); // Create action $actions = new FieldList( new FormAction('doSuggest', _t('Genealogist.SEND', 'Send')) ); // Create Validators $validator = new RequiredFields('Name', 'Message'); return new Form($this, 'Form_Suggest', $fields, $actions, $validator); } public function doSuggest($data, $form) { $personID = $data['PersonID']; $name = $data['Name']; $email = $data['Email']; $phone = $data['Phone']; $subject = $data['Subject']; $message = $data['Message']; GenealogistHelper::suggest_change($name, $email, $phone, $personID, $subject, $message); return $this->owner->redirectBack(); } /// Utils /// public function getDBVersion() { return DB::get_conn()->getVersion(); } public function getPerson($id) { return GenealogistHelper::get_gender($id); } public function getFigures() { return DataObject::get('Gender') ->filterAny(array( 'ClassName:StartsWith' => 'Branch', 'ClassName:StartsWith' => 'Clan', )); } public function getAllClansAndBranches() { return GenealogistHelper::get_all_clans_and_branches(); } public function getMainClansAndBranches() { // return GenealogistHelper::get_main_clans_and_branches(); return GenealogistHelper::get_main_clans_and_branches(); } /** * Checks if the user is an authorized member * @return boolean true if the user is an authorized member */ public function isAdmin() { return GenealogistHelper::is_genealogists(); } } |