/modules/newsletter/user_create.php

https://github.com/heliopsis/cjw_newsletter · PHP · 217 lines · 147 code · 41 blank · 29 comment · 22 complexity · ecba09c2a81f9a9fb0cdbdceec20a5f3 MD5 · raw file

  1. <?php
  2. /**
  3. * File user_create.php
  4. *
  5. * create a new newsletter user - if email exists redirect to user_edit of existing newsletter user
  6. *
  7. * @copyright Copyright (C) 2007-2010 CJW Network - Coolscreen.de, JAC Systeme GmbH, Webmanufaktur. All rights reserved.
  8. * @license http://ez.no/licenses/gnu_gpl GNU GPL v2
  9. * @version //autogentag//
  10. * @package cjw_newsletter
  11. * @subpackage modules
  12. * @filesource
  13. */
  14. /**
  15. * newsletter/user_create?Email=abc@examplcom
  16. * PostParameter for email
  17. */
  18. // linked from
  19. // - newsletter/user_list ?RedirectUrl=
  20. // - newsletter/subscription_list ?RedirectUrl=
  21. require_once( 'kernel/common/i18n.php' );
  22. include_once( 'kernel/common/template.php' );
  23. $module = $Params['Module'];
  24. $http = eZHTTPTool::instance();
  25. $tpl = templateInit();
  26. $templateFile = 'design:newsletter/user_create.tpl';
  27. $contextCreateNewsletterUser = false;
  28. $subscriptionDataArr = array( 'first_name' => '' ,
  29. 'last_name' => '',
  30. 'email' => '',
  31. 'salutation' => '',
  32. 'note' => '',
  33. 'id_array' => array(),
  34. 'list_array' => array(),
  35. 'list_output_format_array' => array()
  36. );
  37. $warningArr = array();
  38. $oldPostArray = array();
  39. if( $http->hasPostVariable( 'OldPostVarSerialized' ) )
  40. {
  41. $oldPostArray = unserialize( base64_decode( $http->postVariable( 'OldPostVarSerialized' ) ) );
  42. }
  43. else
  44. {
  45. $oldPostArray = $_POST;
  46. }
  47. $redirectUrlCancel = $redirectUrlStore = 'newsletter/user_list';
  48. if ( $http->hasVariable( 'RedirectUrlActionCancel' ) )
  49. {
  50. $redirectUrlCancel = $http->variable( 'RedirectUrlActionCancel' );
  51. }
  52. elseif ( $http->hasVariable( 'RedirectUrl' ) )
  53. {
  54. $redirectUrlCancel = $http->variable( 'RedirectUrl' );
  55. }
  56. if ( $http->hasVariable( 'RedirectUrlActionStore' ) )
  57. {
  58. $redirectUrlStore = $http->variable( 'RedirectUrlActionStore' );
  59. }
  60. elseif ( $http->hasVariable( 'RedirectUrl' ) )
  61. {
  62. $redirectUrlStore = $http->variable( 'RedirectUrl' );
  63. }
  64. // set data from POST for new and existing users
  65. if ( $http->hasPostVariable( 'Subscription_Email' ) )
  66. {
  67. $subscriptionDataArr['email'] = trim( $http->postVariable( 'Subscription_Email' ) );
  68. }
  69. if ( $http->hasPostVariable( 'Subscription_FirstName' ) )
  70. {
  71. $subscriptionDataArr['first_name'] = trim( $http->postVariable( 'Subscription_FirstName' ) );
  72. }
  73. if ( $http->hasPostVariable( 'Subscription_LastName' ) )
  74. {
  75. $subscriptionDataArr['last_name'] = trim( $http->postVariable( 'Subscription_LastName' ) );
  76. }
  77. if ( $http->hasPostVariable( 'Subscription_Salutation' ) )
  78. {
  79. $subscriptionDataArr['salutation'] = trim( $http->postVariable( 'Subscription_Salutation' ) );
  80. }
  81. if ( $http->hasPostVariable( 'Subscription_Note' ) )
  82. {
  83. $subscriptionDataArr['note'] = trim( $http->postVariable( 'Subscription_Note' ) );
  84. }
  85. if ( $http->hasPostVariable( 'Subscription_IdArray' ) )
  86. $subscriptionDataArr['id_array'] = $http->postVariable( 'Subscription_IdArray' );
  87. if ( $http->hasPostVariable( 'Subscription_ListArray' ) )
  88. $subscriptionDataArr['list_array'] = $http->postVariable( 'Subscription_ListArray' );
  89. // $subscriptionDataArr['list_output_format_array'] = array();
  90. foreach ( $subscriptionDataArr['id_array'] as $listId )
  91. {
  92. if ( $http->hasPostVariable( "Subscription_OutputFormatArray_$listId" ) )
  93. {
  94. $subscriptionDataArr['list_output_format_array'][ $listId ] = $http->postVariable( "Subscription_OutputFormatArray_$listId" );
  95. }
  96. else
  97. {
  98. $defaultOutputFormatId = 0;
  99. $subscriptionDataArr['list_output_format_array'][ $listId ] = array( $defaultOutputFormatId );
  100. }
  101. }
  102. $viewParameters = array();
  103. if( is_array( $Params['UserParameters'] ) )
  104. {
  105. $viewParameters = array_merge( $viewParameters, $Params['UserParameters'] );
  106. }
  107. $tpl->setVariable( 'view_parameters', $viewParameters );
  108. // validate data if new user will be created
  109. if ( $module->isCurrentAction( 'CreateEdit' ) )
  110. {
  111. $newsletterUserId = -1;
  112. $msg = 'edit_new';
  113. $requiredSubscriptionFields = array( 'email' );
  114. foreach ( $requiredSubscriptionFields as $fieldName )
  115. {
  116. switch ( $fieldName )
  117. {
  118. case 'email':
  119. {
  120. if ( !eZMail::validate( $subscriptionDataArr['email'] ) || $subscriptionDataArr['email'] == '' )
  121. {
  122. $warningArr['email'] = array( 'field_key' => ezi18n( 'cjw_newsletter/subscription', 'Email'),
  123. 'message' => ezi18n( 'cjw_newsletter/subscription', 'You must provide a valid email address.' ) );
  124. }
  125. else
  126. {
  127. // check if email already exists
  128. $existingNewsletterUserObject = CjwNewsletterUser::fetchByEmail( $subscriptionDataArr['email'] );
  129. if( is_object( $existingNewsletterUserObject ) )
  130. {
  131. // If email exists redirect to user_edit
  132. $newsletterUserId = $existingNewsletterUserObject->attribute( 'id' );
  133. $msg = 'edit_existing';
  134. /* $warningArr['email'] = array( 'field_key' => ezi18n( 'cjw_newsletter/subscription', 'Email' ),
  135. 'message' => ezi18n( 'cjw_newsletter/subscription', 'Email is already used by an other newsletter user.' ) );
  136. */
  137. }
  138. }
  139. } break;
  140. default:
  141. }
  142. }
  143. // only store changes if all is ok
  144. if( $module->isCurrentAction( 'CreateEdit' ) && count( $warningArr ) == 0 )
  145. {
  146. // rerun with all postData
  147. $rerunUrl = 'newsletter/user_edit/'. $newsletterUserId;
  148. $newPostArray = array_merge( $oldPostArray, $_POST );
  149. if ( isset( $newPostArray['OldPostVarSerialized'] ) )
  150. unset( $newPostArray['OldPostVarSerialized'] );
  151. $_POST = array();
  152. $_POST = $newPostArray;
  153. $_POST['UserCreateMsg'] = $msg;
  154. $_POST['StoreDraftButton'] = 'storedraft';
  155. $Result['rerun_uri'] = $rerunUrl;
  156. return $module->setExitStatus( eZModule::STATUS_RERUN );
  157. }
  158. }
  159. elseif ( $module->isCurrentAction( 'Cancel' ) )
  160. {
  161. $module->redirectTo( $redirectUrlCancel );
  162. }
  163. $tpl->setVariable( 'old_post_var_serialized', base64_encode( serialize( $oldPostArray ) ) );
  164. $tpl->setVariable( 'subscription_data_array', $subscriptionDataArr );
  165. $tpl->setVariable( 'warning_array', $warningArr );
  166. $tpl->setVariable( 'redirect_url_action_cancel', $redirectUrlCancel );
  167. $tpl->setVariable( 'redirect_url_action_store', $redirectUrlStore );
  168. $Result = array();
  169. $Result['content'] = $tpl->fetch( $templateFile );
  170. $Result['path'] = array( array( 'url' => 'newsletter/index',
  171. 'text' => ezi18n( 'cjw_newsletter/path', 'Newsletter' ) ),
  172. array( 'url' => 'newsletter/user_list',
  173. 'text' => ezi18n( 'cjw_newsletter/user_list', 'Users' ) ),
  174. array( 'url' => false,
  175. 'text' => ezi18n( 'cjw_newsletter/user_create', 'Create' ) ) );
  176. ?>