Integrations and APIs for Service

Get Involved. Join the Conversation.

Topic

    Younes Nachar
    How to expose ContactType ctype_Id in contacts?
    Topic posted September 21, 2011 by Younes NacharRed Ribbon: 250+ Points, last edited October 29, 2011 
    1620 Views, 24 Comments
    Title:
    How to expose ContactType ctype_Id in contacts?
    Content:

    I need to expose ctype_id contact type for the customer portal pages. Any idea? 

    Comment

    • Ryan Seltzer

      There are a few different ways you could do this.

       

      You could extend the /rightnow/models/standard/contact_model.php into your own custom version  and put it in /developement/models/custom.  You would want to basically go in and modify the sql in the get function to add the ctype_id to the query that pulls back the data for that object.  Then you would update your other code which normally loads the standard model and instead use the custom one.  Then you when you 'get' the contact it will have the ctype_id as an accessible attribute.  This is not the approach I would take however.

      I would recommend using a hook in CP however as it will be less maintance and upgrade better.

      You could setup a hook by modifying the config/hooks.php file and adding a 'post_login' hook that basically took the c_id and looked up the ctype_id based on that, and then set that as a session variable.


      $rnHooks['post_login'] = array(
      'class' => 'CustomContactModel',
      'function' => 'set_ctype',
      'filepath' => ''
      );

       

      This hook would then call your model named /models/custom/CustomContactModel and execute the set_ctype function where you can put your code to retrieve the ctype_id for the contact who just logged in and set it in a session variable for usage on any page during that session.

    • Younes Nachar

       "Then you would update your other code which normally loads the standard model and instead use the custom one".

      Where  does the standard model get called? What file?

      I am trying to modify existing contact model call that populate the contact object from standard model to custom one.

    • Younes Nachar

       I copied the standard contact_model and pasted it in the custom model folder and made my changes as suggested by Ryan. I changed the get method so that it returns ctype_id. I also made a hook call to the get method like follow.

      $rnHooks['post_login'] = array(
         'class' => 'contact_model',
         'function' => 'get',
         'filepath' => ''
       );

      After the hook is added to the hooks file I cannot login anymore!!!

      Here is how I am trying to get the ccontact type Id from the page: <rn:field name="contacts.ctype_id" /> knowing that I called the contact type id as ctype_id in the get method.

      Will you please advise?

      Thanks,

    • Younes Nachar

      I also tries to hard code an id in the method that retrieved the contact type but with no results. Here is the methos that set that id:

      Method:

      function set_ctype()

           $_SESSION['ctype_id'] = sql_get_int('SELECT ctype_id from contacts WHERE c_id = ' . intval(6551));
      }

      Hook:

      $rnHooks['pre_page_set_selection'] = array(
         'class' => 'contact_model_custom',
         'function' => 'set_ctype',
         'filepath' => ''
       );

      Still nothing shows up for the contact type id!!!

    • Younes Nachar

       I wanted to know also how to pass in the parameters values to the methods as methods in the hooks only have method name?

      Thanks

    • Ryan Seltzer

      It sounds like you are mixing up two different approaches here.  If you are using the hook then you do not want to use the modified 'get' call or anything based on the standard model, you simply want to create a very basic function call in your own completely custom model that just basically looks up the ctype_id and sets that in a session variable.   I would call the function something specific like 'set_ctype' and have it just do that.

       

      If you look at the actual source code that calls the post_login hook you will get a better idea of what parameters are passed into the hook.  Looking at /euf/src/rightnow/application/controllers/pta.php for example you can see it here

       

      $postHookData = array('returnValue'=>$apiProfile, 'data'=>array('source'=>'PTA'));
      RightNowHooks::callHook('post_login', $postHookData);

       

      So you can basically access the 'returnValue' and 'data' in your function call from that hook.  That is where you could pull out the users information and do a lookup for the ctype_id.

       

       

    • Ryan Seltzer

      Younes - Regarding your post of trying pre_page_set_selection... why are you using that hook?  To test it with hardcoded values and such use the pre_page_render hook, which will get called on each page load of CP.  My guess is you were never triggering the pre_page_set_selection hook.

    • Younes Nachar

       Ryan,

      So changed the hook type and used the same set_ctype as you see above without any result!

      $rnHooks['pre_page_set_selection'] = array(
         'class' => 'contact_model_custom',
         'function' => 'set_ctype',
         'filepath' => ''
       );

      function set_ctype()

           $_SESSION['ctype_id'] = sql_get_int('SELECT ctype_id from contacts WHERE c_id = ' . intval(6551));
      }

      and called it from the page using: <?$_SESSION['ctype_id']?>

      Do you see anything wrong in that?

      Thanks,

    • Allan Schrum

      Just to make sure you know of this resource, Connect for PHP exposes ctype_id as Contact->ContactType

      use RightNow\Connect\v1 as RNCPHP; // or v1_1 if your are on 11.5+

      $con = RNCPHP\Contact::fetch( 123 ); // the ID of the contact

      echo $con->ContactType; // This is the numeric value of the ContactType

      echo $con->ContactType->ID; // This is also the numeric value of the ContactType

      echo $con->ContactType->LookupName; // This is the text name equivalent of the ID of the ContactType numeric value

      Regards,

      -Allan

    • Younes Nachar

       

      Ryan,

      By the way, I do not want to use hardcoded values but instead I want to pass in c_id to the method. I do not know how to do that. If you can please point me to how to achieve that.

      Thanks,

    • Ryan Seltzer

      It still looks like you were not trying the pre_page_render hook.  also, to actually print out the PHP variable value you would want to use

       

      <?=$_SESSION['ctype_id']?>   (note the equals sign).

       

      Also please see Allans thread regarding using Connect for PHP to get to the contact type.  The only thing with that solution is that you would have to have it on each page where you need that access, and it would require hitting the database to fetch the data each time rather then doing it once and storing it in the session variable.  Still a viable solution however and something to try out.

    • Ryan Seltzer

      You basically pass your method a reference to the array that gets passed into the hook. 

       

      function set_ctype(&$arr)
      {
         $arr['data'] = ......

         //do stuff here and check out what all is in $arr by dumping it out.

      }

    • Younes Nachar

      Ryan,

      So I changed the hook to what you pointed out and added the equal sign at the retrieval of the session but still cannot retrieve the value.

      I do not want to use Allan way as it is too expensive (calling the db everytime I need the variable.).

      $rnHooks['pre_page_render'] = array(
         'class' => 'contact_model_custom',
         'function' => 'set_ctype',
         'filepath' => ''
       );

       <?=$_SESSION['ctype_id']?>

      function set_ctype()

           $_SESSION['ctype_id'] = sql_get_int('SELECT ctype_id from contacts WHERE c_id = ' . intval(6551));
      }

       

       

    • Mark Rhoads

      The function set_ctype() could use ConnectPHP instead of direct SQL and be far more easily upgradable to future releases/versions.

    • Ryan Seltzer

      Have you confirmed that you are actually executing the hook?  Put some simple debug stuff in there and make sure it is executing the code where you think you are setting the session variable.