isDirty()) { $this->setFieldValue('modified_date', $sqlNow); } return $this->updateFromStoredData($lang, false, false, $this->getErrorKey(), $errors); } public function create($lang, array &$errors) { //BrxDebug::debugPointByClass(__CLASS__, '', __METHOD__, __LINE__, 'aaa_0415'); $sqlNow = BrxDate::sqlNow(); $this->setFieldValue('creation_date', $sqlNow); $this->setFieldValue('modified_date', $sqlNow); $r = $this->updateFromStoredData($lang, true, false, $this->getErrorKey(), $errors); //$this->setId($this->getFieldValue('id')); return $r; } abstract function getErrorKey(); } class Draft extends BaseDraft { public function __construct($id) { parent::__construct('research.drafts', $id); } public function getErrorKey() { return 'class_research_draft'; } public static function getOrCreate($lang, $idToCreate) { //BrxDebug::debugPointByClass(__CLASS__, '', __METHOD__, __LINE__, 'aaa_0415'); $id = BrxData::Dlookup('id', 'research.drafts', "id=$idToCreate"); $draft = new Draft($id); if ($id != $idToCreate) { //BrxDebug::debugPointByClass(__CLASS__, '', __METHOD__, __LINE__, 'aaa_0415'); $draft->setFieldValue('id', $idToCreate); $errors = array(); $draft->create($lang, $errors); } return $draft; } public function getDocument($userLanguage, $languageId, $docType, array &$errors) { //BrxDebug::debugPointByClass(__CLASS__, '', __METHOD__, __LINE__, 'aaa_0415'); $docTypeId = -1; if ($docType === 'bio') { $docTypeId = 1; } else if ($docType === 'biblio') { $docTypeId = 2; } else if ($docType == '1') { $docTypeId = 1; } else if ($docType == '2') { $docTypeId = 2; } $doc = null; if ($docTypeId > 0) { //BrxDebug::debugPointByClass(__CLASS__, '', __METHOD__, __LINE__, 'aaa_0415'); $draftId = $this->id(); $docId = BrxData::Dlookup('id', "research.draft_documents", "draft_id=$draftId and document_type_id=$docTypeId and language_id=$languageId"); if ($docId && $docId > 0) { //BrxDebug::debugPointByClass(__CLASS__, '', __METHOD__, __LINE__, 'aaa_0415'); $doc = new DraftDocument($docId); } else { //BrxDebug::debugPointByClass(__CLASS__, '', __METHOD__, __LINE__, 'aaa_0415'); $doc = new DraftDocument('new'); $doc->setBasicInfo($this->id(), $docTypeId, $languageId); $doc->create($userLanguage, $errors); } } //BrxDebug::debugPointByClass(__CLASS__, '', __METHOD__, __LINE__, 'aaa_0415'); return $doc; } } class DraftDocumentSplit extends DraftDocument { private $paragraphs; private $width; public function __construct($id, $width) { parent::__construct($id); //BrxDebug::debugPointByClass(__CLASS__, '', __METHOD__, __LINE__, 'aaa_0420'); $this->width = $width; $this->paragraphs = array(); } public function split() { //BrxDebug::debugPointByClass(__CLASS__, '', __METHOD__, __LINE__, 'aaa_0420'); $me = $this; $paragraphs = array(); $f = function($paragraph) use($me) { /* @var $paragraph DraftParagraph */ $p = new DraftParagraphSplit($paragraph->id()); $newtext = $p->splitAsWords(); $me->paragraphs[] = array('paragraph'=>$paragraph, 'paragraph_text'=>$newtext); }; $this->eachParagraph($f); } public function getParagraphs() { //BrxDebug::debugPointByClass(__CLASS__, '', __METHOD__, __LINE__, 'aaa_0420'); return $this->paragraphs; } } class DraftDocument extends BaseDraft { public function __construct($id) { parent::__construct('research.draft_documents', $id); } public function setBasicInfo($draftId, $docTypeId, $languageId) { //BrxDebug::debugPointByClass(__CLASS__, '', __METHOD__, __LINE__, 'aaa_0415'); $this->setFieldValue('draft_id', $draftId); $this->setFieldValue('document_type_id', $docTypeId); $this->setFieldValue('language_id', $languageId); } public function addParagraph($lang, $ndx, $text, $splitIntoWords, array &$errors) { if ($this->getParagraphCount() == 0) { //BrxDebug::debugPointByClass(__CLASS__, '', __METHOD__, __LINE__, 'aaa_0415'); $spec = trim(strip_tags($text)); $this->setFieldValue('draft_spec', $spec); $this->update($lang, $errors); } //BrxDebug::debugPointByClass(__CLASS__, '', __METHOD__, __LINE__, 'aaa_0415'); $p = new DraftParagraph('new'); $p->setBasicInfo($this->getFieldValue('id'), $ndx, $text); $p->create($lang, $errors); if ($splitIntoWords) { $p->splitIntoWords($lang, $errors); } return $p; } public function getErrorKey() { return 'err_draft_document'; } public function getParagraphCount() { $id = $this->id(); if ($id && $id > 0) { return BrxData::DCount('*', 'research.draft_paragraphs', "draft_document_id=" . $id); } return 0; } public function clearParagraphs($errors) { BrxData::each('select id from research.draft_paragraphs where draft_document_id=' . $this->id(), function($row) { BrxData::execute('delete from research.draft_words where draft_paragraph_id=' . $row[0]); BrxData::execute('delete from research.draft_paragraphs where id=' . $row[0]); }); } public function splitWords($print = true, array &$data) { $function = function($p) use ($print) { /* @var $p DraftParagraph */ $p->splitIntoWords($print); }; $this->eachParagraph($function); } public function eachParagraph($paragraphFunctor) { $id = $this->id(); $functor = function($array) use ($paragraphFunctor) { $paragraphId = $array[0]; $p = new DraftParagraph($paragraphId); $paragraphFunctor($p); }; BrxData::each("select id from research.draft_paragraphs where draft_document_id = $id order by draft_document_id, paragraph_order", $functor); } } class DraftParagraph extends BaseDraft { public function __construct($id) { parent::__construct('research.draft_paragraphs', $id); } public function setBasicInfo($docId, $ndx, $text) { //BrxDebug::debugPointByClass(__CLASS__, '', __METHOD__, __LINE__, 'aaa_0415'); $this->setFieldValue('draft_document_id', $docId); $this->setFieldValue('paragraph_order', $ndx); $this->setFieldValue('paragraph_text', trim($text)); } public function getErrorKey() { return 'err_draft_paragraph'; } public function eachWord($eachFunctor) { $sql = 'select * from research.draft_words where draft_paragraph_id=' . $this->id() . ' order by draft_paragraph_id, word_order'; BrxData::eachRow($sql, $eachFunctor); } public function splitIntoWords($lang, $errors) { //BrxDebug::debugPointByClass(__CLASS__, '', __METHOD__, __LINE__, 'aaa_0415'); $text = $this->getFieldValue('paragraph_text'); $d2 = new ParseSplitResearchWords(); $sqlNow = BrxDate::sqlNow(); $values = array('id' => '', 'draft_paragraph_id' => $this->getFieldValue('id'), 'word_order' => 0, 'word_text' => '', 'creation_date' => $sqlNow, 'modified_date' => $sqlNow); $t2 = BrxHtmlProcessor::processText($d2, $text); //print "$t2"; $words = preg_split('/[|]/', $t2); //BrxDebug::debugPointByClass(__CLASS__, '', __METHOD__, __LINE__, 'aaa_0415'); for ($i = 0; $i < count($words); $i++) { if ($words[$i]) { $values['id'] = ''; $values['word_text'] = $words[$i]; $values['word_order'] = $i * 10 + 10; $this->addWord($lang, $errors, $values); } } } public function addWord($lang, $errors, $values) { BrxData::insert($values, 'research.draft_words'); } public function getParagraphOrder() { return $this->getFieldValue('paragraph_order'); } public function getParagraphText() { return $this->getFieldValue('paragraph_text'); } } class DraftParagraphSplit extends DraftParagraph { public function __construct($id) { parent::__construct($id); } public function renderAsWords($class='rendered-word') { $data = array(); $this->splitAsWords(false, $data); } public function splitAsWords(array &$data, $functionExtraFormatById = null, $class = "rendered-word") { $formats = array(); $me = $this; $functionRender = function($record) use($me, &$data, $functionExtraFormatById, $class, &$formats) { $wordText = $record['word_text']; if (!$wordText) { return; } $last = 0; $activeFormats = array(); $tmpdata = array(); while (true) { $newlast = $me->splitFormats($wordText, $last, $tmpdata, $activeFormats); if ($newlast === false) { break; } $last = $newlast; } if ($last < strlen($wordText)) { $final .= substr($wordText, $last, strlen($wordText) - $last); } if ($final !== $wordText) { BrxDebug::debugPointByClass(__CLASS__, '', __METHOD__, __LINE__, 'aaa_0415'); } return false; }; $this->eachWord($functionRender); } public function splitFormats($wordText, $last, array &$data, array &$activeFormats) { $matches = array(); $found = preg_match('/<[^>]*>/', $wordText, $matches, PREG_OFFSET_CAPTURE, $last); if ($found) { $foundtext = $matches[0][0]; $position = $matches[0][1]; $this->processFormat($foundtext, $activeFormats); if ($last < $position) { $text = substr($wordText, $last, $position - $last); $this->addFormattedWord($text, $data, $activeFormats); } return $position + strlen($foundtext); } else { return false; } } public function processFormat($foundtext, array &$activeFormats) { if (substr($foundtext, 1, 1) === '/') { $popped = array_pop($activeFormats); BrxDebug::debugPoint('processFormat::' . $popped . ', ' . $foundtext); } else { $activeFormats[] = $foundtext; } } public function addFormattedWord($text, array $data, array $activeFormats) { $data[] = array($text, $activeFormats); } } class DraftWords extends SimpleDataClass { public function __construct($id) { parent::__construct('research.draft_words', $id); } } class ParseSplitResearchParagraph extends DataHandlerSimple { private $spacing; const COUNTING = 1; private $width; private $mode; private $lines; private $plain; public function __construct($width, $spacing = 2) { //BrxDebug::debugPointByClass(__CLASS__, '', __METHOD__, __LINE__, 'aaa_0411'); $this->width = $width; $this->mode = ParseSplitResearchParagraph::COUNTING; $this->lines = array(); $this->plain = ''; $this->spacing = $spacing; } function openHandler(&$parser, $name, $attrs, $close = false) { //BrxDebug::debugPointByClass(__CLASS__, '', __METHOD__, __LINE__, 'aaa_0411'); parent::openHandler($parser, $name, $attrs, $close); } // Closing tag handler function closeHandler(&$parser, $name) { //BrxDebug::debugPointByClass(__CLASS__, '', __METHOD__, __LINE__, 'aaa_0411'); parent::closeHandler($parser, $name); } function dataHandler(&$parser, $data) { //BrxDebug::debugPointByClass(__CLASS__, '', __METHOD__, __LINE__, 'aaa_0411'); // TODO: decode before plain $cb0 = mb_strlen($this->plain); $cb1 = mb_strlen($data); if ($cb0 + $cb1 >= $this->width) { //BrxDebug::debugPointByClass(__CLASS__, '', __METHOD__, __LINE__, 'aaa_0411'); $this->splitLine($data); } else { //BrxDebug::debugPointByClass(__CLASS__, '', __METHOD__, __LINE__, 'aaa_0411'); $this->plain .= $data; parent::dataHandler($parser, $data); } } function escapeHandler(& $parser, $data) { BrxDebug::debugPointByClass(__CLASS__, '', __METHOD__, __LINE__, 'aaa_0411'); parent::escapeHandler($parser, $data); } public function postProcess() { //BrxDebug::debugPointByClass(__CLASS__, '', __METHOD__, __LINE__, 'aaa_0411'); if ($this->dest) { //BrxDebug::debugPointByClass(__CLASS__, '', __METHOD__, __LINE__, 'aaa_0411'); $this->lines[] = $this->dest; $this->dest = ''; } } public function printLines() { print "\n\n"; print '
';
BrxDebug::debugPointByClass(__CLASS__, '', __METHOD__, __LINE__, 'aaa_0411');
foreach ($this->lines as $line) {
print $line;
for ($i = 0; $i < $this->spacing; $i++) {
print "
\n";
}
}
print '