@@ -79,27 +79,16 @@ function dosomething_campaign_load($node, $public = FALSE) {
7979 $ campaign ->status = dosomething_campaign_get_campaign_status ($ node );
8080 $ campaign ->type = dosomething_campaign_get_campaign_type ($ node );
8181
82- // Plain text fields.
83- $ fields_plain_text = array (
84- 'call_to_action ' ,
85- 'starter_statement_header ' ,
86- );
87- foreach ($ fields_plain_text as $ property ) {
82+ // Plain text properties.
83+ $ plain_text = dosomething_campaign_get_property_names_plain_text ();
84+ foreach ($ plain_text as $ property ) {
8885 $ field_name = 'field_ ' . $ property ;
8986 $ campaign ->{$ property } = $ wrapper ->{$ field_name }->value ();
9087 }
9188
92- // Filtered text fields.
93- $ fields_filtered_text = array (
94- 'items_needed ' ,
95- 'promoting_tips ' ,
96- 'solution_copy ' ,
97- 'solution_support ' ,
98- 'starter_statement ' ,
99- 'time_and_place ' ,
100- 'vips ' ,
101- );
102- foreach ($ fields_filtered_text as $ property ) {
89+ // Filtered text properties.
90+ $ filtered_text = dosomething_campaign_get_property_names_filtered_text ();
91+ foreach ($ filtered_text as $ property ) {
10392 $ field_name = 'field_ ' . $ property ;
10493 if ($ value = $ wrapper ->{$ field_name }->value ()) {
10594 // Store filtered text.
@@ -209,6 +198,59 @@ function dosomething_campaign_load($node, $public = FALSE) {
209198 return $ campaign ;
210199}
211200
201+ /**
202+ * Creates and returns a campaign node from given JSON string.
203+ */
204+ function dosomething_campaign_create_node_from_json ($ string ) {
205+ $ data = json_decode ($ string );
206+ $ node = new stdClass ();
207+ $ node ->type = 'campaign ' ;
208+ $ node ->title = $ data ->title ;
209+ // Set all plain text properties:
210+ $ plain_text = dosomething_campaign_get_property_names_plain_text ();
211+ foreach ($ plain_text as $ text_field ) {
212+ if (isset ($ data ->{$ text_field })) {
213+ $ field_name = 'field_ ' . $ text_field ;
214+ $ node ->{$ field_name }[LANGUAGE_NONE ][0 ]['value ' ] = $ data ->{$ text_field };
215+ }
216+ }
217+ // Set all filtered text properties:
218+ $ filtered_text = dosomething_campaign_get_property_names_filtered_text ();
219+ foreach ($ filtered_text as $ text_field ) {
220+ if (isset ($ data ->{$ text_field })) {
221+ $ field_name = 'field_ ' . $ text_field ;
222+ $ node ->{$ field_name }[LANGUAGE_NONE ][0 ]['value ' ] = $ data ->{$ text_field };
223+ $ node ->{$ field_name }[LANGUAGE_NONE ][0 ]['format ' ] = 'markdown ' ;
224+ }
225+ }
226+ node_save ($ node );
227+ return $ node ;
228+ }
229+
230+ /**
231+ * Returns array of campaign property names which contain plain text.
232+ */
233+ function dosomething_campaign_get_property_names_plain_text () {
234+ return array (
235+ 'call_to_action ' ,
236+ 'starter_statement_header ' ,
237+ );
238+ }
239+
240+ /**
241+ * Returns array of campaign property names which contain filtered text.
242+ */
243+ function dosomething_campaign_get_property_names_filtered_text () {
244+ return array (
245+ 'items_needed ' ,
246+ 'promoting_tips ' ,
247+ 'solution_copy ' ,
248+ 'solution_support ' ,
249+ 'starter_statement ' ,
250+ 'time_and_place ' ,
251+ 'vips ' ,
252+ );
253+ }
212254/**
213255 * Returns a loaded campaign $node's field_campaign_type value.
214256 *
0 commit comments