Example #1
0
  public function edit($args = array()) {
    
    $model = null;

    $defaults = array(
      "init_model" => true,
      "view" => $this->view_key(), 
      "model" => $this->model_key(),
      "manage" => "manage",
      "title_args" => array(),
      "cap_verified" => false
    );

    $r = wp_parse_args($args, $defaults);

    if (!$r["cap_verified"] && !MasterPress::current_user_can($this->cap("edit"))) {
      wp_die(__("Sorry, you do not have the required capability to edit this item.", MASTERPRESS_DOMAIN));
    }


    MasterPress::$model_class = MasterPress::model_class($r["model"]);
    MasterPress::$view_class = MasterPress::view_class($r["view"]);

    $view_class = MasterPress::$view_class;
    $model_class = MasterPress::$model_class;

    MPV::incl($r["view"]);

    $manage = $r["manage"];
    
    if ($this->is_postback()) {
      
      $key = $this->key();
        
      if (isset($_REQUEST["return"])) {
        $key = $_REQUEST["return"];

        if ($key == "masterpress") {
          $key = "";
        } else {
          $key = str_replace("masterpress-", "", $key);
        }
      
      }
          
      if (apply_filters("mp_mpc_edit", $do = true)) {

        $model = $this->submit();
        MasterPress::$model = $model; 
      
        $valid = false;
      
        if ($model && is_object($model)) {
          $valid = $model->is_valid();
        } else {
          $valid = $model;
        } 
      
        if ( $valid ) {
          // hallelulah! we can now do proper redirects, rather than a fake action.
          // this prevents issues with refreshing the manage page after a save showing the form with "this item already exists"

          $qs = array("from" => MasterPress::$action);
        
          self::fill_redirect_parent($qs);

          if (is_object($model)) {
            $id = $model->id;
        
            if (isset($id)) {
              $qs["id"] = $id;
            }
          } else {
            $qs["id"] = MasterPress::$id;
          }
      
          if (isset($_REQUEST["mp_redirect"])) {
            wp_redirect( $_REQUEST["mp_redirect"] . "&def_updated=1" );
            exit;
          }
          
          wp_redirect( MasterPress::admin_url( $key, $manage, $qs, false ) );

          exit();
          return true;
        } else {
          if (is_object($model)) {
            $this->log_model_errors($model);
          }
        }
      
      } else {

        $qs = array( "id" => $_POST["id"], "from" => MasterPress::$action );

        self::fill_redirect_parent($qs);
        
        // submit disabled - simply redirect back to the manage screen
        wp_redirect( MasterPress::admin_url( $key, $manage, $qs, false ) );
        exit;
        
      } // apply_filters
      
    } else {
      
      if ($r["init_model"] && class_exists($model_class)) {
        $model = call_user_func_array( array($model_class, "find_by_id"), array(MasterPress::$id) );
        MasterPress::$model = $model;
      }
    }
      
    // set view variables

    $this->setup_view( 
      array(
        "view" => $r["view"],
        "title_args" => wp_parse_args($r["title_args"], array( "text" => MPV::__edit( call_user_func( array( $view_class, "__s" ) ) ), "actions" => "update", "info_panel" => false, "controller" => $this->key() )),
        "form" => true,
        "method" => "form",
        "method_args" => array( "type" => "edit" ),
      )
    );

    
    
  }