[dev] [commit] r957 - phplib/models templates/admin wwwbase/admin
automailer at dexonline.ro
automailer at dexonline.ro
Mon Aug 26 17:17:10 EEST 2013
Author: cata
Date: Mon Aug 26 17:17:10 2013
New Revision: 957
Log:
Better lexem cloning: copy meanings, meaning tags, meaning sources, synonyms,
and most Lexem fields.
Modified:
phplib/models/BaseObject.php
phplib/models/Lexem.php
phplib/models/Meaning.php
templates/admin/lexemEdit.ihtml
wwwbase/admin/lexemEdit.php
Modified: phplib/models/BaseObject.php
==============================================================================
--- phplib/models/BaseObject.php Mon Aug 26 15:24:31 2013 (r956)
+++ phplib/models/BaseObject.php Mon Aug 26 17:17:10 2013 (r957)
@@ -72,6 +72,20 @@
}
}
+
+ /**
+ * Copies the values of all fields except id. Works better than PHP's clone operator.
+ **/
+ function parisClone() {
+ $clone = Model::factory(get_called_class())->create();
+ $fields = $this->as_array();
+ foreach ($fields as $key => $value) {
+ if ($key != 'id') {
+ $clone->$key = $value;
+ }
+ }
+ return $clone;
+ }
}
?>
Modified: phplib/models/Lexem.php
==============================================================================
--- phplib/models/Lexem.php Mon Aug 26 15:24:31 2013 (r956)
+++ phplib/models/Lexem.php Mon Aug 26 17:17:10 2013 (r957)
@@ -422,6 +422,42 @@
public function __toString() {
return $this->description ? "{$this->formNoAccent} ({$this->description})" : $this->formNoAccent;
}
+
+ public function cloneLexem() {
+ $clone = $this->parisClone();
+ $clone->description = ($this->description) ? "CLONĂ {$this->description}" : "CLONĂ";
+ $clone->modelType = 'T';
+ $clone->modelNumber = 1;
+ $clone->restriction = '';
+ $clone->isLoc = false;
+ $clone->verifSp = false;
+ $clone->structSealed = false;
+ $clone->save();
+
+ // Clone the definition list
+ $ldms = LexemDefinitionMap::get_all_by_lexemId($this->id);
+ foreach ($ldms as $ldm) {
+ LexemDefinitionMap::associate($clone->id, $ldm->definitionId);
+ }
+
+ // Clone the root meanings
+ $meanings = Model::factory('Meaning')->where('lexemId', $this->id)->where('parentId', 0)->find_many();
+ foreach ($meanings as $m) {
+ $m->cloneMeaning($clone->id, 0);
+ }
+
+ // Clone the sources
+ $lss = LexemSource::get_all_by_lexemId($this->id);
+ foreach ($lss as $ls) {
+ $lsClone = $ls->parisClone();
+ $lsClone->lexemId = $clone->id;
+ $lsClone->save();
+ }
+
+ $clone->regenerateParadigm();
+ return $clone;
+ }
+
}
?>
Modified: phplib/models/Meaning.php
==============================================================================
--- phplib/models/Meaning.php Mon Aug 26 15:24:31 2013 (r956)
+++ phplib/models/Meaning.php Mon Aug 26 17:17:10 2013 (r957)
@@ -142,6 +142,46 @@
parent::delete();
}
+ /**
+ * Different from __clone(). We save the object to the database to assign it an ID. We also clone its descendants,
+ * synonyms/antonyms, sources and tags.
+ **/
+ public function cloneMeaning($newLexemId, $newParentId) {
+ $clone = $this->parisClone();
+ $clone->lexemId = $newLexemId;
+ $clone->parentId = $newParentId;
+ $clone->save();
+
+ // Clone its tags
+ $mtms = MeaningTagMap::get_all_by_meaningId($this->id);
+ foreach ($mtms as $mtm) {
+ $mtmClone = $mtm->parisClone();
+ $mtmClone->meaningId = $clone->id;
+ $mtmClone->save();
+ }
+
+ // Clone its sources
+ $mss = MeaningSource::get_all_by_meaningId($this->id);
+ foreach ($mss as $ms) {
+ $msClone = $ms->parisClone();
+ $msClone->meaningId = $clone->id;
+ $msClone->save();
+ }
+
+ // Clone its synonyms / antonyms
+ $synonyms = Synonym::get_all_by_meaningId($this->id);
+ foreach ($synonyms as $synonym) {
+ $synonymClone = $synonym->parisClone();
+ $synonymClone->meaningId = $clone->id;
+ $synonymClone->save();
+ }
+
+ // Clone its children
+ $children = Meaning::get_all_by_parentId($this->id);
+ foreach ($children as $child) {
+ $child->cloneMeaning($newLexemId, $clone->id);
+ }
+ }
}
?>
Modified: templates/admin/lexemEdit.ihtml
==============================================================================
--- templates/admin/lexemEdit.ihtml Mon Aug 26 15:24:31 2013 (r956)
+++ templates/admin/lexemEdit.ihtml Mon Aug 26 17:17:10 2013 (r957)
@@ -120,14 +120,13 @@
{if $homonyms}
<tr>
<td>omonime:</td>
- <td>
+ <td colspan="3">
{foreach from=$homonyms item=h key=i}
{if $i}|{/if}
<a href="lexemEdit.php?lexemId={$h->id}">{include file="common/bits/lexemName.ihtml" lexem=$h}</a>
[{$h->modelType}{$h->modelNumber}{$h->restriction}]
{/foreach}
</td>
- <td class="rightColumn"></td><td></td>
</tr>
{/if}
</table>
Modified: wwwbase/admin/lexemEdit.php
==============================================================================
--- wwwbase/admin/lexemEdit.php Mon Aug 26 15:24:31 2013 (r956)
+++ wwwbase/admin/lexemEdit.php Mon Aug 26 17:17:10 2013 (r957)
@@ -241,23 +241,6 @@
return $result;
}
-function _cloneLexem($lexem) {
- $clone = Lexem::create($lexem->form, 'T', 1, '');
- $clone->comment = $lexem->comment;
- $clone->description = ($lexem->description) ? "CLONĂ {$lexem->description}" : "CLONĂ";
- $clone->noAccent = $lexem->noAccent;
- $clone->save();
-
- // Clone the definition list
- $ldms = LexemDefinitionMap::get_all_by_lexemId($lexem->id);
- foreach ($ldms as $ldm) {
- LexemDefinitionMap::associate($clone->id, $ldm->definitionId);
- }
-
- $clone->regenerateParadigm();
- return $clone;
-}
-
/* This page handles a lot of actions. Move the minor ones here so they don't clutter the preview/save actions,
which are hairy enough by themselves. */
function handleLexemActions() {
@@ -309,7 +292,7 @@
$cloneLexem = util_getRequestParameter('cloneLexem');
if ($cloneLexem) {
- $newLexem = _cloneLexem($lexem);
+ $newLexem = $lexem->cloneLexem();
log_userLog("Cloned lexem {$lexem->id} ({$lexem->form}), new id is {$newLexem->id}");
util_redirect("lexemEdit.php?lexemId={$newLexem->id}");
}
More information about the Dev
mailing list