Skip to content

Commit

Permalink
Merge pull request #170 from JarvusInnovations/develop
Browse files Browse the repository at this point in the history
Release: emergence-skeleton v1.3.16
  • Loading branch information
themightychris authored Oct 29, 2018
2 parents 9b2d929 + 71d9416 commit a92074d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
53 changes: 49 additions & 4 deletions php-classes/Emergence/Comments/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Emergence\Comments;

use ActiveRecord;
use HandleBehavior;


class Comment extends \VersionedRecord
{
// support subclassing
Expand Down Expand Up @@ -43,10 +45,14 @@ class Comment extends \VersionedRecord
)
);

public static $validations = array(
public static $validators = array(
'Context' => array(
'validator' => 'require-relationship',
'required' => true
),
'Message' => array(
'validator' => 'string_multiline'
,'errorMessage' => 'You must provide a message.'
'validator' => 'string_multiline',
'errorMessage' => 'You must provide a message.'
)
);

Expand Down Expand Up @@ -77,4 +83,43 @@ public function save($deep = true)

parent::save();
}
}

/**
* Differentially apply a complete array of new comments data to a given context
*/
public static function applyCommentsData(ActiveRecord $Context, array $commentsData)
{
// index existing comment records by ID
$existingComments = [];

foreach ($Context->Comments as $Comment) {
$existingComments[$Comment->ID] = $Comment;
}


// create new and update existing comment
$comments = [];
foreach ($commentsData as $commentData) {
if (empty($commentData['Message'])) {
throw new Exception('Comment data must have Message set');
}

if (
!empty($commentData['ID'])
&& ($Comment = $existingComments[$commentData['ID']])
) {
$Comment->Message = $commentData['Message'];
} else {
$Comment = static::create([
'Message' => $commentData['Message']
]);
}

$comments[] = $Comment;
}


// write new list to relationship
$Context->Comments = array_merge($existingComments, $comments);
}
}
2 changes: 1 addition & 1 deletion php-classes/Emergence/People/Groups/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class Group extends ActiveRecord
)
);

public static $validations = array(
public static $validators = array(
'Name' => array(
'errorMessage' => 'A name is required'
)
Expand Down

0 comments on commit a92074d

Please sign in to comment.